Data Structure With C - Part 2
Data Structure With C - Part 2
Date:- 14/02/2012
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.
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; }
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.
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.
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
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); }
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); }
C -implementation of declare a doubly Linked List:Struct node { Int info; Struct node *rightnext; Struct node *leftnext; }