You are on page 1of 39

Unit II

Linked List
Syllabus :
Linked Lists : Linked List: Operations – Implementation of Stacks, Queues in C. Types of Linked Lists: Singly Linked List, Doubly Linked List, Circular Linked List. Applications of Linked Lists. Linked List: Definition: It is a linear collection of data elements called nodes, and node allocate space for each element separately, it contains two fields. The first part contains the information of the element; second part contains the address of next node in the linked list. --------------NODE------------INFORMATION ITEM NULL / ADDRESS ADDRESS OF NEXT ITEM

Representation of Linked List using structure: struct node { int item; struct node *next; }*start; Here start is a node type pointer variable. It can hold the address of another pointer variable. It occupies 2 bytes of memory to point the address of 1st node. Allocate required memory dynamically for the node sruct node *temp temp=(struct node*)malloc(sizeof(struct node)); temp

INFORMATION

NULL / ADDRESS

----------2 bytes----------- -----------2bytes-----------ITEM ADDRESS OF NEXT temp is a node type pointer variable it can points the dynamically allocated memory i.e. 4 bytes can be accessed by using the following notation. temp -> item = data; temp -> next = NULL/ ADDRESS OF NEXT NODE.

Fig: E.g. for liked list:

Linked list have many advantages and some of them are: 1. Linked list are dynamic data structure. That is, they can grow or shrink during the execution of a program. 2. Efficient memory utilization: In linked list (or dynamic) representation, memory is not pre-allocated. Memory is allocated whenever it is required. And it is deallocated (or removed) when it is not needed. 3. Insertion and deletion are easier and efficient. Linked list provides flexibility in inserting a data item at a specified position and deletion of a data item from the given position. 4. Many complex applications can be easily carried out with linked list. Linked list has following disadvantages 1. More memory: to store an integer number, a node with integer data and address field is allocated. That is more memory space is needed. 2. Access to an arbitrary data item is little bit cumbersome and also time consuming. The primitive operations performed on the linked list are as follows 1. Creation 2. Insertion 3. Deletion 4. Traversing 5. Searching 6. Concatenation 7. Reverse the linked list 8. Sort the linked list Creation operation is used to create a linked list. Once a linked list is created with one node, insertion operation can be used to add more elements in a node. Insertion operation is used to insert a new node at any specified location in the linked list. A new node may be inserted.

(a) At the beginning of the linked list (b) At the end of the linked list (c) At any specified position in between in a linked list Deletion operation is used to delete an item (or node) from the linked list. A node may be deleted from the (a) Beginning of a linked list (b) End of a linked list (c) Specified location of the linked list Traversing is the process of going through all the nodes from one end to another end of a linked list. In a singly linked list we can visit from left to right, forward traversing, nodes only. But in doubly linked list forward and backward traversing is possible. Concatenation is the process of appending the second list to the end of the first list. Consider a list A having n nodes and B with m nodes. Then the operation concatenation will place the 1st node of B in the (n+1)th node in A. After concatenation A will contain (n+m) nodes Reverse order of the elements present in the linked list is left to right. Just change this order from right to left will give the all nodes in reverse of the linked list. Sort – using any one of the sorting method we arranged the all elements present in the linked list are in ascending order. Types of Linded Lists: Basically we can divide the linked list into the following three types 1. Singly linked list 2. Doubly linked list 3. Circular linked list Singly linked list All the nodes in a singly linked list are arranged sequentially by linking with a pointer. A singly linked list can grow or shrink, because it is a dynamic data structure. Following figure explains the different operations on a singly linked list. Fig: E.g. for liked list:

Operations on Linked List: Create a node: struct node *temp; Temp= (struct node *) malloc (sizeof (struct node)); Temp->data=x (30); temp->next=NULL;

Fig: illustrate insertion operation on Linked List: Fig: Insert new node at given position: If the position =3 .

temp → next = P → Next 7. Exit . temp → item = DATA 4. P → next = temp 8. If (SATRT equal to NULL) (a) START = temp 9.e. Create a new node i. temp 3. Let DATA be the element to be inserted in the new node i. temp 3. else (a) P = START (b) While ( P → Next not equal to NULL) (i) P = P → Next 10. intialise P = START. temp → item = DATA 4. Input DATA to be inserted 2. P is a temporary pointer to hold the node address Algorithms for insertion: Insert a Node at the beginning 1.Suppose START is the first position in linked list. Create a new node i. and k = 0 3. Input DATA to be inserted 2. temp. Exit Insert a Node at the end 1. temp → next = NULL 8. temp → item = DATA 6. temp 5. Else (a) temp → next = START 6. Exit Insert a Node at any specified position 1. Input DATA and POS to be inserted 2. START = temp 7. P → Next = temp 11.e. If (SATRT equal to NULL) (a) temp → next = NULL 5.e. Repeat the step 3 while( k is less than POS) (a) P = P -> Next (b) If (P is equal to NULL) (i) Display “Node in the list less than the position” (ii) Exit (c) k = k + 1 4. Create a New Node i.e. POS is the position where the new node is to be inserted.

Fig: Delete a node in specified position Algorithm to delete a node from the linked list: Suppose START is the first position in linked list. which is deleted (iv) Exit (b) P = P → Next 5. TEMP. if the DATA is found. If (P → DATA is equal to DATA) (a) Display “The data is found at POS” (b) Exit 5. 5 and 6 until (TEMP is equal to NULL) 4. POS will contain the corresponding position in the list. Exit Algorithm for searching for a node: Suppose START is the address of the first node in the linked list and DATA is the information to be searched. P is a temporary pointer to hold the node address. which is deleted (c) P → Next = NULL (d) Exit 6. if ((P → next → DATA) == DATA) (a) TEMP = P → Next (b) Set free the node TEMP. P = START 4. if ((START → DATA) is equal to DATA) (a) TEMP = START (b) START = START → Next (c) Set free the node TEMP. 1. POS =1. After searching. Input the DATA to be searched 2. 3. Disply “DATA not found” 7. 1. P = P → Next . Input the DATA to be deleted 2. Initialize P = START. Repeat the step 4. while ((P → Next → Next) not equal to NULL)) (a) if ((P → NEXT → DATA) equal to DATA) (i) TEMP = P → Next (ii) P → Next = TEMP → Next (iii) Set free the node TEMP. Let DATA be the element to be deleted. which is deleted (d) Exit 3.

temp->item=x. if(start==NULL) { . } void show() { int i. printf("\n Enter the element: ").&x). 1. struct node { int item. Following algorithm will visit all nodes from the START node to the end.6.h> int count. Display “P → DATA” 5. POS = POS+1 7. If (START is equal to NULL) (a) Display “The list is Empty” (b) Exit 2. } count++. else { p=start. If (P is equal to NULL) (a) Display “The data is not found in the list” 8.*p. if(start==NULL) start=temp.h> #include<conio. P = P → Next 6. temp->next=NULL. struct node *p. Exit Algorithm for display all nodes: Suppose START is the address of the first node in the linked list. while(p->next!=NULL) p=p->next. struct node *temp. void create() { int x. } *start. Initialize P = START 3. temp=(struct node*)malloc(sizeof (struct node)). Repeat the step 4 and 5 until (P == NULL ) 4. struct node *next. Exit /* Write a Program to illustrate all the operations on Singly Linked list using C */ #include<stdio. scanf("%d". p->next=temp.

} .*p. struct node *temp. count++. p->next=temp.p->item). count++. temp->item=x. } } void insPos(int pos) { int x. } else if(pos==count+1) { p=start. start=temp. temp=(struct node*)malloc(sizeof(struct node)).printf("\nLinked List is Empty: return.i++) p=p->next. p=p->next. for(i=1. printf("\nEnter the element to add into linked list: "). scanf("%d". } p=start. p->next=temp. } void insEnd() { insPos(count+1). } p=start. temp->next=NULL.i<pos-1. temp->next=p->next. } } void insBeg() { insPos(1). count++.count+1).count. return. if(pos==1) { temp->next=start.i. "). } else { if(pos>=count+1) { printf("\nWe have %d nodes only you can insert at <=%d locaton:". while(p->next!=NULL) p=p->next. while(p!=NULL) { printf("\n %d". printf("\n Elements present in the list are: ").&x).

struct node *p.*temp.i=1. free(temp). p->next=NULL.void delPos(int pos) { int i.count).p=p->next) { if(p->item==key) { pos=i+1.i++. p->next=p->next->next. } else { if(pos>count) { printf("\nYou entered positon not there: onley %d nodes".pos. . } else if(pos==count) { p=start. } } void delBeg() { delPos(1). delPos(pos). temp=p->next.p==NULL||i<count.count--.count--. for(i=0. return. if(pos==1) { temp=start. } void delEle(int key) { int i=0. struct node *p. break.i<pos-1.key.i++) p=p->next.count--. free(temp). } void delEnd() { delPos(count). start=start->next. while(p->next->next!=NULL) p=p->next. } if(i>count) { printf("\nYou Entered element is not in the list: "). temp=p->next. free(temp). } for(p=start. return.p=start.

p1=start. p2->item = x. } else { p1=start. } } void main() { int key. p1->next=NULL.n.ch.*p2. if(start->next==NULL) { printf("\nOnly one node: \n").j. p2=p1->next. struct node *p1. } start=p2.i<count-1. for(i=0. p2=p3. } } } } void rev() { struct node *p1. p3=p2->next.*p2.pos. show(). p1->item = p2->item.i++. while(p3!=NULL) { p1=p2.*p3.p2 = p1->next. do { .} } } void sort() { int x.choice.p1=p1->next) { for(j=0. j<count-1-i.i. p2 = p2->next) { if(p1->item > p2->item) { x = p1->item. p2->next=p1. p2->next=p1. clrscr().i. start=NULL. p3=p2->next.j++.

&key). insPos(pos). case 3: printf("\nEnter the position to insert: ").&choice).DelEle"). break.i++) create(). scanf("%d". } /* Write a program create two linked lists with n node and m nodes respectively and concatenate them as single list */ #include<stdio. break.break.sort\n 10. case 9: sort(). printf("\n8.break.InsBeg\n2. case 10: show(). break.D elPos\n7. scanf("%d".create\n1.reverse\n9. scanf("%d". case 4: delBeg(). case 2: insEnd().DelBeg\n5. break. break.&n). scanf("%d".printf("0. getch().InsPos\n4. delEle(key).&pos). } else printf(“\nNO elements in the list to delete:”). scanf("%d". break.h> struct node .of nodes to create Linked List: "). case 7: if(start!=NULL) { printf("\n Enter the existing element to delete: ").break. } else printf(“\n NO elements in list to delete: ”). case 5: delEnd().&pos). scanf("%d".&ch).InsEnd\n3. switch(choice) { case 0:printf("\nEnter the no.break.show\nEnter the choice: ").break. case 6: if(start!=NULL) { printf("\nEnter the position to delete: ").h> #include<conio.i<n. case 8: rev(). } printf("\nContinue: (1/0)"). for(i=0. case 1: insBeg(). default: printf("\n Wrong choice: "). delPos(pos). } while(ch==1).DelEnd\n6.

temp->item=x. temp->next=NULL. } } return start. struct node *next. } else { p=start.&x). temp=(struct node*)malloc(sizeof (struct node)). p->next=temp. } printf("\n Elements present in the list are: "). while(p!=NULL) { printf("\n %d".i. while(p->next!=NULL) p=p->next.n.i<n.*p. scanf("%d".i++) { printf("\n Enter the element: "). p=p->next. struct node *start=NULL. return. . }*A.*temp.*B. if(start==NULL) { start=temp.p->item). } } struct node* create() { int x. void show(struct node *p) { int i. scanf("%d". if(p==NULL) { printf("\nLinked List is Empty: "). for(i=0.&n).{ int item.

show(A). show(A). printf("\nEnter the no. clrscr().of nodes to create Linked List A: "). else { while(p->next!=NULL) p=p->next. show(B). printf("\nEnter the no. } Stack using linked list: Implementation issues of the stack (Last In First Out .} void concat() { struct node *p=A. B=create().i. . A=NULL. getch(). concat().m. if(p==NULL) A=B. B=NULL.LIFO) using linked list is illustrated in following figures.of nodes to create Linked List B: "). p->next=B. } } void main() { int n. A=create().

1. TOP = temp 6. int count. temp → Next = TOP 5. Input the DATA to be pushed 2. which is pointing towards the topmost element of the stack. TEMP is pointer variable to hold any nodes address. temp 3. Creat a New Node i. if (TOP is equal to NULL) (a) Display “The stack is empty” 2. TOP is NULL when the stack is empty.h> struct node { int item. 1. TOP is NULL when the stack is empty. struct node *next. Exit Algorithm for POP operation Suppose TOP is a pointer. Exit /* Write a program to perform operations on stack using linked list */ #include<stdio. DATA is the data item to be pushed. temp → item = DATA 4. which is pointing towards the topmost element of the stack.h> #include<conio. Else (a) TEMP = TOP (b) Display “The popped element TOP → item” (c) TOP = TOP → Next (d) TEMP → Next = NULL (e) Free the TEMP node 3. }*top.e. DATA is the information on the node which is just deleted. int empty() { .Algorithm for PUSH operation: Suppose TOP is a pointer.

if(empty()) { printf("\n NO element in stack to delete:").count). p=top. printf("\nEnter the element to push into stack: "). struct node *p. temp->next=NULL. . return.&x). } printf("\nTotal Elements present in the stack %d :". scanf("%d". temp->item=x.if(top==NULL) return 1. temp=(struct node*)malloc(sizeof(struct node)). struct node *temp. } } void push() { int x. else { temp->next=top. top=temp.i. else return 0. } void pop() { struct node *temp. while(p!=NULL) { printf("\n%d". } void show() { int i. } count++.p->item). if(empty()) { printf("\nNo elements in the stack:"). if(top==NULL) top=temp. p=p->next.

top=top->next. break. } printf("\n Do You Want To Continue: (1/0) : ").return.push\n2.LIFO) using linked list is illustrated in the following figures.pop\n3. } void main() { int choice.break.break.&ch). } Queue using Linked List: Implementation issues of the Queue (First in First Out . scanf("%d". switch(choice) { case 1: push(). case 2: pop(). count--.&choice). free(temp). . } while(ch==1). } temp=top.show\nenter choice: "). default: printf("\n Wrong Choice: "). getch(). scanf("%d". case 3: show(). do { printf("\n1.ch.

temp → Next = NULL 5.which is pointing to the queue where the elements are popped.temp 3. If (FRONT is equal to NULL) (a) Display “The Queue is empty” 2.e. . FRONT is a pointer. 1.h> #include<conio. REAR = REAR → next. Exit Algorithm for delete an element from a QUEUE REAR is a pointer in queue where the new elements are added. FRONT is a pointer. which is pointing to the queue where the elements are popped. 6. 1. Create a New Node i. DATA is an element deleted from the queue. Input the DATA element to be insered 2. Exit /* Write a program to perform operations on Queue using linked list */ #include<stdio. If(REAR not equal to NULL) (a) REAR → next = temp. 7.Algorithm for insert an element into QUEUE REAR is a pointer in queue where the new elements are added. 3.h> struct node { int item. DATA is an element to be inserted. temp → item = DATA 4. Else (a) Display “The popped element is FRONT → item” (b) If(FRONT is not equal to REAR) (i) FRONT = FRONT → Next (c) Else (d) FRONT = NULL.

rear=rear->next. scanf("%d".count).p->item). temp->item=x. else return 0. if(empty()) { printf("\nNo elements in the queue:"). if(rear==NULL) { rear=temp.*rear. struct node *p. int empty() { if(front==NULL) return 1. temp->next=NULL. } } void insert() { int x. } count++. } else { rear->next=temp. return.i. temp=(struct node*)malloc(sizeof(struct node)). }*front. } printf("\nTotal Elements present in the queue %d :". printf("\nEnter the element to insert into queue: "). int count. } .&x). p=front. while(p!=NULL) { printf("\n%d". p=p->next. front=temp.struct node *next. struct node *temp. } void show() { int i.

if(empty()) { printf("\n NO element in queue to delete:"). The following figure shows a typical doubly linked list. LPoint will point to the node in the left side (or previous node) that is LPoint will hold the address of the previous node.&ch). scanf("%d". } Doubly linked list: A doubly linked list is one in which all nodes are linked together by multiple links which help in accessing both the successor (next) and predecessor (previous) node for any arbitrary node within the list.break. scanf("%d".break. clrscr(). do{ printf("\n1.ch. front=front->next. case 3: show(). RPoint will point to the node in the right side.&choice). Right Pointer and DATA. count--. } printf("\n Do You Want To Continue: (1/0) : "). START -----------------NODE------------------------Left Pointer INFORMATION Right Pointer . getch(). } while(ch==1). case 2: del(). front=rear=NULL.insert\n2. break. switch(choice) { case 1: insert(). return. Every node in the doubly linked list has three fields: Left Pointer.show\nenter choice: ").delete\n3.void del() { struct node *temp. free(temp). default: printf("\n Wrong Choice: "). } void main() { int choice. } temp=front.

Prev next Representation: struct node { struct node *prev. int item. Fig: Representation of double linked list item . struct node *next. }*start.

Else (a) Display “Position NOT found” 7. Input the DATA and POS 2. Repeat the step 4 if (i less than POS) and (P is not equal to NULL) 4. Exit Algorithm for deleting a Element form Double Linked List: Suppose START is the address of the first node in the linked list. i = 0 3.e. P is a temporary pointer to hold the node address. Input the POS 2. DATA will contain the information on the deleted node. P. Else . POS is the position where the NewNode i.e temp (b) temp → item = DATA (c) temp → next = P → next (d) temp → prev = P (e) (P → next) → prev = temp (f ) P → next = temp 6. If (P not equal to NULL) and (i equal to POS) (a) Create a New Node i. P = P → Next. temp are temporary pointers to hold the address of the node. Initialize P = START. i = 0 3.Algorithm for Insert a node in the double linked list: Suppose START is the first position in linked list. After deletion. P = P → next. temp is to be inserted. Repeat the step 4 if (i less than POS) and (P is not equal to NULL) 4. Let POS is the position of the node to be deleted. Initialize P = START. Let item be the element to be inserted in the new node. 1. If (P not equal to NULL) and (i equal to POS) (a) Create a New Node i. i = i +1 5. 1. temp (b) temp → item = DATA (c) temp → next = P → next (d) temp → prev = P (e) (P → next) → prev = temp (f ) P → next = temp 6. i = i +1 5.e.

void delPos(int). struct node *temp. printf("\n Enter the element: ").*p. struct node { struct node *prev. if(start==NULL) { printf("\nLinked List is Empty: ").h> int count. int item. else { p=start. if(start==NULL) start=temp.&x). } count++. while(p->next!=NULL) p=p->next. temp->next=NULL. void create() { int x. } void show() { int i. void insPos(int).h> #include<conio. p->next=temp. . Exit */ Write a Program to illustrate all operations on Double Linked List using C*/ #include<stdio. struct node *next. temp->prev=NULL. } *start. temp=(struct node*)malloc(sizeof (struct node)). struct node *p. temp->prev=p->next. temp->item=x. scanf("%d".(a) Display “Position NOT found” 7.

return. } void insEnd() { insPos(count+1). scanf("%d".count+1).*p. } void insPos(int pos) { int x. struct node *temp. } count++. if(pos>count+1) { printf("\nWe have %d nodes only you can insert at <=%d locaton:". temp->next=start.&x). . temp=(struct node*)malloc(sizeof(struct node)). } } void insBeg() { insPos(1).i. p=p->next. temp->prev=NULL. if(pos==1) { if(start==NULL) start=temp. while(p!=NULL) { printf("\n %d". start=temp. } printf("\nEnter the element to add into linked list: "). else { start->prev=temp. return. printf("\n Elements present in the list are: "). } p=start. temp->item=x.count. temp->next=NULL.p->item).

for(i=1. count++. } else { p=start.} else if(pos==count+1) { p=start.i++.p=start.p=p->next) { if(p->item==key) { pos=i+1. } . temp->next=p->next. for(i=0. count++. temp->prev=p. p->next=temp.i<pos-1.p==NULL||i<count. } } void delBeg() { delPos(1).i++) p=p->next. p->next=temp. } void delEle(int key) { int i=0. } if(i>count) { printf("\nYou Entered element is not in the list: "). break. } void delEnd() { delPos(count). struct node *p. temp->prev=p. return. while(p->next!=NULL) p=p->next.pos. p->next->prev=temp. delPos(pos).

key. while(p->next->next!=NULL) p=p->next. free(temp).count). } } else if(pos==count) { p=start. struct node *p.*temp. temp->next->prev=p.} } void delPos(int pos) { int i. p->next=NULL. } for(p=start. } else { temp=start. return. start->next->prev=NULL.i=1. start=start->next.count--.i++) p=p->next. start=NULL. if(pos==1) { if(count==1) { temp=start.i<pos-1. .count--. temp=p->next. free(temp). free(temp). free(temp). p->next=p->next->next. } else { if(pos>count) { printf("\nYou entered positon not there: only %d nodes available". temp=p->next.count--.

insPos(pos). start=NULL. case 4: delBeg(). } else . break. break. scanf("%d". scanf("%d".n.choice.of nodes to create Linked List: ").} } void main() { int key. for(i=0.break. printf("\nEnter the Choice:"). do { printf("1.i.DelPos\n7.pos.InsEnd\n3. case 7: if(start!=NULL) { printf("\n Enter the existing element to delete: ").i++) create().break. delEle(key).DelBeg\n5. case 5: delEnd(). } else printf(“\nNO elements to delete in the list:”). clrscr(). break.&n). scanf("%d". scanf("%d". delPos(pos).DelEnd\n6. printf("\nEnter the no.&pos). case 6: if(start!=NULL) { printf("\nEnter the position to delete: ").&choice).Sho w"). switch(choice) { case 1: insBeg(). case 2: insEnd(). break. case 3: printf("\nEnter the position to insert: ").i<n.InsPos\n4.InsBeg\n2.ch. scanf("%d".&pos).DelEle\n8.&key).

h> int count. break. /* PROGRAM TO IMPLEMENT CIRCULAR SINGLY LINKED LIST */ #include<stdio. void create() . } Circular Linked List: A circular linked list is one.34. A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner as shown in the Fig. case 8: show(). } printf("\nContinue: (1/0)"). default: printf("\n Wrong choice: "). getch(). A singly linked list can be made a circular linked list by simply storing the address of the very first node in the linked field of the last node. A circular linked list is shown in the following figure. struct node *next. Implementation of circular doubly linked list is left to the readers.printf(“\nNO elements to delete in the list:”).h> #include<conio. 5.&ch). } while(ch==1). scanf("%d". struct node { int item. void insPos(int). void delPos(int). which has no beginning and no end. } *start.break.

printf("\n Enter the element: "). struct node *temp.&x). struct node *p. printf("\n Elements present in the list are: ").*p. while(p->next!=start) p=p->next. struct node *temp. while(p->next!=start) { printf("\n %d". } void show() { int i. if(start==NULL) { printf("\nLinked List is Empty: "). return. } p=start. temp->item=x. p->next=temp.{ int x. temp->next=start. } printf("\n %d". } else { p=start. } count++. p=p->next.*p. . } void insPos(int pos) { int x. if(start==NULL) { start=temp.p->item). scanf("%d".p->item). temp=(struct node*)malloc(sizeof (struct node)). temp->next=start.i.

scanf("%d". temp->next=p->next.&x). } count++.if(pos>count+1) { printf("\nWe have %d nodes only you can insert at <=%d locaton:". } else if(pos==count+1) { p=start. p->next=temp. p->next=temp. temp=(struct node*)malloc(sizeof(struct node)). while(p->next!=start) p=p->next. while(p->next!=start) p=p->next. start=temp. p->next=temp. temp->next=NULL.count. return.i++) p=p->next. } } . } else { p=start.count+1). } printf("\nEnter the element to add into linked list: "). for(i=1. temp->item=x. count++. if(pos==1) { if(start==NULL) { temp->next=start. temp->next=start. temp->next=start. start=temp.i<pos-1. } else { p=start. count++.

count--. p->next=start. } else { if(pos>count) { printf("\nYou entered positon not there: only %d nodes available".void insBeg() { insPos(1). if(pos==1) { if(count==1) { temp=start. } else if(pos==count) { p=start. while(p->next!=start) p=p->next. free(temp).count). p->next=start. count--. temp=p->next. start=NULL. . struct node *p.key. while(p->next->next!=start) p=p->next. } void delPos(int pos) { int i. } free(temp). } else { p=temp=start.*temp. } void insEnd() { insPos(count+1). start=start->next.

count--. } } void delBeg() { delPos(1). . return.pos.i<pos-1. } } } void main() { int key. if(start==NULL) { printf("\nNo elements in the list to delete:"). free(temp).i.ch.n.choice. temp=p->next. return.i=1. } void delEnd() { delPos(count).p==NULL||i<count.pos. } for(i=0. } if(i>count) { printf("\nYou Entered element is not in the list: "). } for(p=start. break.p=start. delPos(pos).i++. struct node *p. p->next=p->next->next.return. } void delEle(int key) { int i=0.p=p->next) { if(p->item==key) { pos=i+1.i++) p=p->next.

&key). scanf("%d".&choice). delPos(pos). scanf("%d".&pos). do { printf("1.DelEle\n8.InsEnd\n3.DelBeg\n5. clrscr(). insPos(pos). case 2: insEnd(). default: printf("\n Wrong choice: ").i<n.&ch). scanf("%d". . case 6: if(start!=NULL) { printf("\nEnter the position to delete: "). break.DelPos\n7.&n). break.start=NULL. break. } else printf("\nNO elements in list to delete"). break. break. for(i=0.break.&pos). case 3: printf("\nEnter the position to insert: ").of nodes to create Linked List: ").i++) create(). scanf("%d". } else printf("\n No elements in list to delete").InsPos\n4.DelEnd\n6. printf("\nEnter the Choice:").InsBeg\n2. switch(choice) { case 1: insBeg(). case 4: delBeg(). } printf("\nContinue: (1/0)").break.Sho w"). case 7: if(start!=NULL) { printf("\n Enter the existing element to delete: "). scanf("%d". scanf("%d". case 8: show(). case 5: delEnd(). printf("\nEnter the no.break. delEle(key).

void create() { int x. } while(ch==1).e start INFORMATION of first node i. } *start. struct node *temp. Start -----------------NODE------------------------Contains address of Contains address prev node i.e start Prev Example for circular double Linked List: item next /* Write a program to implement all operations on Circular Double Linked List */ #include<stdio. Cicular Double Linked List: A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner as shown in the below Implementation of circular doubly linked list is left to the readers. struct node *next. int item.} getch().h> #include<conio.h> int count.*p. struct node { struct node *prev. .

temp->prev=p->next. } count++.p->item).&x). while(p->next!=start) { printf("\n %d". temp->prev=NULL. scanf("%d". } printf("\n %d". } void insPos(int pos) { . } else { p=start. if(start==NULL) { start=temp. temp->item=x. return. } void show() { int i. start->prev=temp. p=p->next. printf("\n Elements present in the list are: "). temp->next=NULL. start->prev=temp. p->next=temp. if(start==NULL) { printf("\nLinked List is Empty: "). } p=start. start->next=temp.printf("\n Enter the element: "). temp->next=start.p->item). temp=(struct node*)malloc(sizeof (struct node)). struct node *p. while(p->next!=start) p=p->next.

temp->prev=NULL.int x. p->next=temp.&x). start=temp. } count++. temp->next=NULL. } else if(pos==count+1) { start->prev->next=temp. start->prev=temp. temp->prev=start->prev. scanf("%d". } else { p=start.i. temp=(struct node*)malloc(sizeof(struct node)).count. count++. return. start->prev->next=temp. start->prev=temp. start->next=temp. if(pos==1) { if(start==NULL) { start=temp.i++) p=p->next. . temp->item=x. } printf("\nEnter the element to add into linked list: ").i<pos-1. temp->prev=p. temp->next=start. temp->next=p->next. if(pos>count+1) { printf("\nWe have %d nodes only you can insert at <=%d locaton:".*p. temp->next=start. start->prev=temp. struct node *temp. for(i=1. } else { temp->prev=start->prev.count+1).

start->prev->next=start->next.key. } } else if(pos==count) { p=start.*temp. while(p->next->next!=start) p=p->next. count--. } else { temp=start. free(temp). start->next->prev=start->prev. } void delPos(int pos) { int i. } } void insBeg() { insPos(1). temp=p->next.p->next->prev=temp. p->next=start.count--. } else . free(temp). start=NULL. struct node *p. start=start->next. free(temp). } void insEnd() { insPos(count+1). start->prev=p. count++. if(pos==1) { if(count==1) { temp=start.

temp->next->prev=p.p=p->next) { if(p->item==key) { pos=i+1.p->next!=start||i<count. } } . return. free(temp). } for(i=0.p=start. return.pos. if(start==NULL) { printf("\nNo elements in the list to delete:"). return.i<pos-1.i++.count). break. temp=p->next.i=1. } void delEle(int key) { int i=0. struct node *p. } void delEnd() { delPos(count).count--. } for(p=start.{ if(pos>count) { printf("\nYou entered positon not there: only %d nodes available". } if(i>count) { printf("\nYou Entered element is not in the list: "). p->next=p->next->next. delPos(pos).i++) p=p->next. } } void delBeg() { delPos(1).

delPos(pos).DelPos\n7. case 3: printf("\nEnter the position to insert: "). scanf("%d".Sho w"). printf("\nEnter the no. } else printf("\n List Empty:").&choice).i++) create(). do { printf("1. case 7: if(start!=NULL) { printf("\n Enter the existing element to delete: ").&key).pos. break.DelEnd\n6. case 6: if(start!=NULL) { printf("\nEnter the position to delete: ").ch. case 2: insEnd(). break. switch(choice) { case 1: insBeg(). break.n.i. case 5: delEnd(). delEle(key). for(i=0. scanf("%d".&pos). clrscr(). start=NULL.&n).break. scanf("%d".i<n. insPos(pos). } else . printf("\nEnter the Choice:").of nodes to create Linked List: ").DelBeg\n5. case 4: delBeg().} void main() { int key. scanf("%d".break.&pos).choice.DelEle\n8.InsEnd\n3.InsPos\n4.InsBeg\n2. scanf("%d". break.

break. } while(ch==1). } printf("\nContinue: (1/0)"). break.printf("\nList Empty: "). } .&ch). case 8: show(). getch(). scanf("%d". default: printf("\n Wrong choice: ").