Vous êtes sur la page 1sur 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

Suppose START is the first position in linked list. Let DATA be the element to be inserted in the new node i.e. temp. POS is the position where the new node is to be inserted. P is a temporary pointer to hold the node address Algorithms for insertion: Insert a Node at the beginning 1. Input DATA to be inserted 2. Create a new node i.e. temp 3. temp item = DATA 4. If (SATRT equal to NULL) (a) temp next = NULL 5. Else (a) temp next = START 6. START = temp 7. Exit Insert a Node at the end 1. Input DATA to be inserted 2. Create a new node i.e. temp 3. temp item = DATA 4. temp next = NULL 8. If (SATRT equal to NULL) (a) START = temp 9. else (a) P = START (b) While ( P Next not equal to NULL) (i) P = P Next 10. P Next = temp 11. Exit Insert a Node at any specified position 1. Input DATA and POS to be inserted 2. intialise P = START; and k = 0 3. 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. temp 5. temp item = DATA 6. temp next = P Next 7. P next = temp 8. Exit

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. Let DATA be the element to be deleted. TEMP, P is a temporary pointer to hold the node address. 1. Input the DATA to be deleted 2. if ((START DATA) is equal to DATA) (a) TEMP = START (b) START = START Next (c) Set free the node TEMP, which is deleted (d) Exit 3. P = START 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, which is deleted (iv) Exit (b) P = P Next 5. if ((P next DATA) == DATA) (a) TEMP = P Next (b) Set free the node TEMP, which is deleted (c) P Next = NULL (d) Exit 6. Disply DATA not found 7. 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. After searching, if the DATA is found, POS will contain the corresponding position in the list. 1. Input the DATA to be searched 2. Initialize P = START; POS =1; 3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL) 4. If (P DATA is equal to DATA) (a) Display The data is found at POS (b) Exit 5. P = P Next

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

printf("\nLinked List is Empty: return;

");

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

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

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

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

/* Write a program create two linked lists with n node and m nodes respectively and concatenate them as single list */ #include<stdio.h> #include<conio.h> struct node

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

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

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

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

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

Algorithm for insert an element into 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. DATA is an element to be inserted. 1. Input the DATA element to be insered 2. Create a New Node i.e.temp 3. temp item = DATA 4. temp Next = NULL 5. If(REAR not equal to NULL) (a) REAR next = temp; 6. REAR = REAR next; 7. 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. DATA is an element deleted from the queue. 1. If (FRONT is equal to NULL) (a) Display The Queue is empty 2. 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; 3. Exit /* Write a program to perform operations on Queue using linked list */ #include<stdio.h> #include<conio.h> struct node { int item;

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

void del() { struct node *temp; if(empty()) { printf("\n NO element in queue to delete:"); return; } temp=front; front=front->next; free(temp); count--; } void main() { int choice,ch; front=rear=NULL; clrscr(); do{ printf("\n1.insert\n2.delete\n3.show\nenter choice: "); scanf("%d",&choice); switch(choice) { case 1: insert();break; case 2: del();break; case 3: show(); break; default: printf("\n Wrong Choice: "); } printf("\n Do You Want To Continue: (1/0) : "); scanf("%d",&ch); } while(ch==1); getch(); } 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. Every node in the doubly linked list has three fields: Left Pointer, Right Pointer and DATA. 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. RPoint will point to the node in the right side.
START

-----------------NODE------------------------Left Pointer INFORMATION Right Pointer

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

item

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

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

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

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

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

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

printf(\nNO elements to delete in the list:); break; case 8: show();break; default: printf("\n Wrong choice: "); } printf("\nContinue: (1/0)"); scanf("%d",&ch); } while(ch==1); getch(); } Circular Linked List: A circular linked list is one, which has no beginning and no end. 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.

A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner as shown in the Fig. 5.34. Implementation of circular doubly linked list is left to the readers.

/* PROGRAM TO IMPLEMENT CIRCULAR SINGLY LINKED LIST */

#include<stdio.h> #include<conio.h> int count; void insPos(int); void delPos(int); struct node { int item; struct node *next; } *start; void create()

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

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

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

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

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

} getch(); }

while(ch==1);

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. Start -----------------NODE------------------------Contains address of Contains address prev node i.e start INFORMATION of first 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.h> #include<conio.h> int count; struct node { struct node *prev; int item; struct node *next; } *start; void create() { int x; struct node *temp,*p;

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

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

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

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

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

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

Vous aimerez peut-être aussi