Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINKED LIST.

Similar presentations


Presentation on theme: "LINKED LIST."— Presentation transcript:

1 LINKED LIST

2 1 2 Insert node at the beginning struct node { int data;
struct node *next; }*head,*var,*trav; head=NULL; head Insert node at the beginning 1 var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else var->next=head; 10 var 1000 10 head 1000 2 NULL

3 5 3 4 Insert node at the beginning
10 head 1000 20 head 2000 5 NULL 1000 10 1000 NULL Insert node at the beginning 3 var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else var->next=head; 20 var 2000 4 20 var 2000 1000

4 6 1 2 3 4 4 5 Insert node at the End 20 head 2000 1000 10 1000 NULL 20
3000 30 3000 NULL 6 20 temp 2000 Insert node at the End 1 1000 struct node *temp; temp=head; var=(struct node *)malloc(sizeof (struct node)); var->data=value; if(head==NULL) { head=var; head->next=NULL; } else while(temp->next!=NULL) temp=temp->next; var->next=NULL; temp->next=var; 30 var 3000 2 temp 3 10 1000 NULL 30 var 3000 4 NULL 4 temp 5 10 1000 3000

5 1 2 3 5 6 Insert node at the Middle 4 head 20 2000 1000 10 1000 3000
NULL Insert node at the Middle var 1 struct node *var2,*temp; var=(struct node *)malloc(sizeof (struct node)); var->data=value; temp=head; if(head==NULL) { head=var; head->next=NULL; } else while(temp->data!=loc) temp=temp->next; var2=temp->next; (3000) temp->next=var; var->next=var2; 40 4000 temp 2 20 2000 1000 10 temp 3 10 1000 3000 temp 4 var 10 1000 4000 5 40 4000 3000 6

6 Delete node from end head 20 2000 1000 10 1000 4000 40 4000 3000 30
NULL Delete node from end struct node *temp; temp=head; while(temp->next != NULL) { var=temp; //2000, 1000,4000(FINAL VALUE) temp=temp->next;//finally 3000 } if(temp ==head) head=temp->next; free(temp); return 0; printf("data deleted from list is %d",temp->data); var->next=NULL; free(temp); // free(3000) 40 4000 NULL

7 Delete node from Middle
head 20 2000 1000 10 1000 4000 40 4000 NULL Delete node from Middle struct node *temp,*var; temp=head; while(temp!=NULL) { if(temp->data == value) if(temp==head) head=temp->next; free(temp); return 0; } else var->next=temp->next; else { var=temp; temp=temp->next; } }// END OF WHILE printf("data deleted from list is %d",value);

8 Display the content of the linked list
head 20 2000 1000 10 1000 4000 40 4000 NULL Display the content of the linked list trav=head; if(trav==NULL) { printf("\nList is Empty"); } else printf("\nElements in the List: "); while(trav!=NULL) printf(" -> %d ",trav->data); trav=trav->next; printf("\n");

9 Doubly Linked List Each node of the list contain two Links – one to the previous node and other to the next node. The previous link of the first node and the next link of the last node points to NULL.  We don’t need to keep track of the previous node for traversal or no need of traversing the whole list for finding the previous node. More pointers needs to be handled and more links need to updated.

10 Implementation of Doubly Linked List in C
prev n next node struct node { struct node *prev; int n; struct node *next; } *h, *temp, *temp1 h = NULL To create an empty node node NULL n NULL

11 Insert at beginning temp temp 10 20 1000 2000 h temp h temp1 10 20 10
if (h == NULL) { int data;   temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); (10) temp->n = data; h = temp; temp1 = h; } else { int data;   temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; temp->next = h; h->prev = temp; h = temp; } temp temp NULL 10 NULL NULL 20 NULL 1000 2000 h temp h temp1 NULL 10 NULL NULL 20 1000 2000 10 NULL NULL 10 NULL 1000 2000 1000 1000

12 Insert at the end h h = temp; 20 10 2000 1000 temp temp1 temp1 temp 30
NULL 20 1000 2000 10 NULL Insert at the end 2000 1000 if (h == NULL) { int data; temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; h = temp; temp1 = h; } else { int data; temp =(struct node *)malloc(1*sizeof(struct node)); temp->prev = NULL; temp->next = NULL; printf("\n Enter value to node : "); scanf("%d", &data); temp->n = data; temp1->next = temp; temp->prev = temp1; temp1 = temp; } temp temp1 temp1 temp NULL 30 NULL 2000 10 NULL 2000 10 3000 1000 30 NULL 3000 1000 1000 3000 temp1 = h; (first node [10])

13 After inserting the node at the end
2000 10 3000 1000 30 NULL 20

14 Deleting the node in the Doubly Linked List
int i = 1, pos; printf("\n Enter position to be deleted : "); scanf("%d", &pos); temp2 = h; if (h == NULL) { printf("\n Error : Empty list no elements to delete"); return; } else while (i < pos) temp2 = temp2->next; i++; if (i == 1) if (temp2->next == NULL) printf("Node deleted from list"); free(temp2); temp2 = h = NULL; if (temp2->next == NULL) { temp2->prev->next = NULL; free(temp2); printf("Node deleted from list"); return; } temp2->next->prev = temp2->prev; if (i != 1) temp2->prev->next = temp2->next; if (i == 1) h = temp2->next; printf("\n Node deleted"); 2000 10 3000 1000 30 NULL 20

15 Display from beginning To search an item
temp2 = h; if (temp2 == NULL) { printf("List empty to display \n"); return; } printf("\n Linked list elements from begining : "); while (temp2->next != NULL) printf(" %d ", temp2->n); temp2 = temp2->next; int data, count = 0; temp2 = h; if (temp2 == NULL) { printf("\n Error : List empty to search for data"); return; } printf("\n Enter value to search : "); scanf("%d", &data); while (temp2 != NULL) if (temp2->n == data) printf("\n Data found in %d position",count + 1); else temp2 = temp2->next; count++; printf("\n Error : %d not found in list", data);

16 Circularly Linked List
More complicated linked data structure. Elements can be placed anywhere The last node points back to the first node

17 Insert at the Beginning x y
struct node { int data; struct node *link; }; struct node *head = NULL, *x, *y, *z; Creating node head x = (struct node*)malloc(sizeof(struct node)); printf("\n Enter the data:"); scanf("%d", &x->data); //10 x->link = x; head = x; 10 1000 1000 Insert at the Beginning x y x = head; y = (struct node*)malloc(sizeof(struct node)); printf("\n Enter the data:"); scanf("%d", &y->data); while (x->link != head) { x = x->link; } x->link = y; y->link = head; head = y; 10 1000 20 1000 2000 x y 10 2000 20 1000 1000 2000

18 Delete node at the start
x head = y; head 10 2000 20 1000 2000 1000 Delete node at the start if (head == NULL) printf("\n List is empty"); else { x = head; y = head; while (x->link != head) x = x->link; } head = y->link; x->link = head; free(y); X=2000 Y=2000 While(1000 !=2000) { X=1000; } Ist iteration While(2000 !=2000) { } 2nd iteration x head=1000 x->link = 1000 free(2000) 10 1000 1000

19 head Display node details 10 2000 20 1000 2000 1000 if (head == NULL)
printf("\n List is empty"); else { x = head; while (x->link != head) printf("%d->", x->data); x = x->link; } printf("%d", x->data);


Download ppt "LINKED LIST."

Similar presentations


Ads by Google