Вы находитесь на странице: 1из 6

Data Structure with C

Date:- 14/02/2012

Instructor: Amit Kumar Gupta Topic: Linked List


-

Header Node: It contains all the information about the other nodes of the linked list. It is an
extra node put at the front of the list.

A node in a linked list occupies 4 bytes of memory space.


Link list are of three types:a) Linear Link List. b) Doubly Link list. c) Circular Link List.

C -implementation of declare a Linear Linked List:struct node { int info; struct node *next; }; typedef struct node *nodeptr; C -implementation of create a Node:nodeptr getnode()
{ nodeptr p; p=(nodeptr)malloc(sizeof(struct node)); return p; }

C -implementation Free a Node:void freenode(nodeptr p) { free(p); }

Data Structure with C


Date:- 16/02/2012

Linear Link List Operation:Insert first:-

At first initialize node type. Then we take the data input from the user and store in the node info variable. Create a temporary node node *temp and allocate space for it.Then place info to temp->data. So the first field of the node *temp is filled. Now temp->next must become a part of the remaining linked list (although now linked list is empty but imagine that we have a 2 node linked list and head is pointed at the front) So temp->next must copy the address of the *head (Because we want insert at first) and we also want that *head will always point at front. So *head must copy the address of the node *temp.

Delete first:-

Delete a node from linked list is relatively easy. First, we create node *temp. Transfer the address of *head to *temp. So *temp is pointed at the front of the linked list. We want to delete the first node. So transfer the address of temp->next to head so that it now pointed to the second node. Now free the space allocated for first node.

Data Structure with C


Insert after specified number of nodes:Insert data in the linked list after specified number of node is a little bit complicated. But the idea is simple. Suppose, we want to add a node after 2nd position. So, the new node must be in 3rd position. The first step is to go the specified number of node. Let, node *temp1 is pointed to the 2nd node now.

Now, Create a temporary node node *temp and allocate space for it. Then place info to temp->next , so the first field of the node node *temp is filled. To establish the connection between new node and the existing linked list, new nodes next must pointed to the 2nd nodes (temp1) next. The 2nd nodes (temp1) next must pointed to the new node (temp).

Delete specified number of node:To delete a specified node in the linked list, we also require to find the specified node and previous node of the specified node. Create temporary node * temp1, *old_temp and allocate space for it. Take the input from user to know the number of the node.Now node *temp1 is now pointed at the specified node and *old_temp is pointed at the previous node of the specified node. The previous node of the specified node must connect to the rest of the linked list so we transfer the address of temp1->next to old_temp->next. Now free the space allocated for the specified node.

Data Structure with C


Date:- 17/02/2012

Circular Linked List: Circular Linked List is a special type of linked list in which all the nodes are linked in continuous circle. Circular list can be Singly or doubly linked list. Note that, there are no Nulls in Circular Linked Lists. In these type of lists, elements can be added to the back of the list and removed from the front in constant time. Both types of circularly-linked lists benefit from the ability to traverse the full list beginning at any given node. This avoids the necessity of storing first Node and last node, but we need a special representation for the empty list, such as a last node variable which points to some node in the list or is null if its empty. This representation significantly simplifies adding and removing nodes with a non-empty list, but empty lists are then a special case. Circular linked lists are most useful for describing naturally circular structures, and have the advantage of being able to traverse the list starting at any point. They also allow quick access to the first and last records through a single pointer (the address of the last element).

info next

info next

info next null

DELETE A NODE from circular linked list:Void delafter (nodeptr p,int y) { Nodeptr q; If((p==NULL)||(p==p->next)) { Printf(void deletion); Exit(0); } q=p->next; Y=q->next=q->next; Free (q); }

Data Structure with C


CONCATENATING 2-List:
Plist1

info

next

info

next

info

next

Plist2

info

next

info

next

info

next null

Nodeptr *concat(nodeptr*plist,nodeptr*plist2) { nodeptr p; if(*plist2==NULL) return(&plist); if(*plist1==NULL) { *plist1=*plist2; return(&plist1); } p=(*plist-1)->next; while(p!=NULL) p=p->next; p->next=*plist2; return (&plist1); }

Data Structure with C


Doubly Linked List:Doubly-linked list(DLL) is a more sophisticated kind of linked list. In DLL, each node has two links: one points to previous node and one points to next node. The previous link of first node in the list points to a Null and the next link of last node points to Null.

C -implementation of declare a doubly Linked List:Struct node { Int info; Struct node *rightnext; Struct node *leftnext; }

Вам также может понравиться