51
PAPER PRESENTATION ON LINK LIST

Linked list ppt

Embed Size (px)

DESCRIPTION

Linked List ppt - Data Structures

Citation preview

  • 1.DATASTRUCTURE DATRUCTURE IS THE WAY OF ORGANIZING ALL DATA ITEMS IN ORDER THAT NOT ONLY ELEMENTS TO BE STORED BUT ALSO THE RELATION BETWEEN THE ELEMENTS

2. DATASTRUCTURE LINEAR LINKLIST ARRAY STACK QUEUE NONLINEAR TREES& GRAPH 3. Linked List What are the problems with Arrays - Size is fixed -Array Items are stored contiguously -Insertions and deletion at particular position is complex Why Linked list ? -Size is not fixed -Data can be stored at any place -Insertions and deletions are simple and faster 4. What is Linked List? A linked list is a collection of nodes with various fields It contains data field and Address field or Link field Info field Link Field/ Address Field Pointer to the first node 5. Linked List Types Singly Linked list Circular singly linked list Doubly linked lists Circular doubly linked lists 6. Graphical Representation 10 1000 1000 2000 200015 NULL20 4000 Singly Linked List First 7. Graphical Representation 10 1000 1000 2000 200015 400020 4000 Circular Singly Linked List Last node contains the address of the first node First 8. Doubly Linked list Contains the address of previous node and next node NULL 2000 30001000 10 15 202000 1000 2000 NULL3000 First 9. Circular Doubly Linked list Contains the address of first node and last node 3000 2000 30001000 10 15 202000 1000 2000 10003000 First 10. Representation of Singly Linked list struct node { int info; struct node *link }; typedef struct node *NODE; Meaning of this: A node is of the type struct and contains info field and link filed 11. What is typedef ? typedef is a keyword in the C Language used to give the new name to data types The intent is to make it easier for programmers Example typedef int x; Later on you can use x as a data type x a, b; Means a and b are variables of the type integer 12. Graphical Representation 10 1000 1000 2000 200015 NULL20 4000 Operations on Singly Linked List First Insert Delete Display the contents 13. Allocation of memory to newnode newnode =( NODE ) malloc (sizeof(struct node)); It Returns address of the location with piece of memory of the size struct. That is for information field and address field 14. Algorithm to Insert an Element from the front 1 temp=allocate memory 2 temp(info)=item; 3 link(temp)=first; 4 call temp as first 5 temp link=first, 16. 10 1000 1000 2000 200015 NULL20 4000 Operations on Singly Linked List Insert from the front First first=temp 17. Algorithm to Display the content // Algorithm Display if (first==NULL) write list empty return; Write Contents of the List temp=first While(temp!=NULL) { write (info(temp)) temp=link(temp) } 18. C Function to Insert an Element from the front Insertfront(NODE first int item) { NODE temp; temp=(NODE) malloc(sizeof(struct node)); temp->info=item; temp->link=NULL ; temp->link=first; first=temp; return(temp); } 19. Algorithm to Delete from the front 1 if (first==NULL) 2 Write list empty and return first 3 temp=first 4 first=link(first) 5 free(temp) 6 return(first) 20. C Function to Delete the element from the front NODE deletefront(NODE first) { if (first==NULL) { printf(List is empty..n); return first; } temp=first; first=first->link; printf( The ietm deleted is %d,temp->info); free(temp); return(first); } 21. 10 1000 1000 2000 200015 NULL20 4000 Operations on Singly Linked List Delete from the front Temp/ First temp=first 22. 10 1000 1000 2000 200015 NULL20 4000 Operations on Singly Linked List Delete from the front First temp temp=first, first=first->link 23. 1000 2000 200015 NULL20 Operations on Singly Linked List Delete from the front First 24. Stack Using Linked List CONCEPT : LIFO Inserting an element from front and deleting an element from front is nothing but STACK 25. 30 Null top 3000 Push item 30 S T A C K L I N K E D L I S T 26. 30 Null top 20 3000 2000 3000 Push item 20 27. 30 Null top 20 3000 2000 3000 10 20001000 Push item 10 28. 30 Null top 20 3000 2000 3000 POP 10 29. 30 Null top 3000 POP 20 30. POP 30 STACK EMPTY 31. Inserting an Element from the rear and deleting An element from the front is nothing but Q Implementation of Q using Linked List 32. 20 temp Front=null Front=rear=temp NULL Insert 20 33. 20 Inserting 20 NULL Front/ Rear 34. 20 front/rear Inserting 30 NULL rear->link=temp temp 30 null 2000 35. 20 front Inserting 30 2000 rear->link=temp rear=temp rear 30 null 20001000 36. 20 front Inserting 40 2000 rear->link=temp rear=temp rear 30 null 20001000 40 Null 37. 20 front Inserting 40 2000 rear->link=temp rear=temp rear 30 3000 20001000 40 Null 3000 38. 20 front Deleting 20 2000 temp=front front=front->link free(temp) rear 30 3000 20001000 40 Null 3000 Delete Operation 39. front Deleting 30 rear 30 3000 2000 40 Null 3000 40. front Deleting 40 rear 40 Null 3000 41. Q Empty 42. 10 1000 1000 2000 200015 NULL20 4000 Searching an Item in a Singly Linked List first Cur=first Pos=1; while(cur!=NULL && item!=cur->info) cur=cur->link; pos++; key=20 cur Cur->info=10 43. 10 1000 1000 2000 200015 NULL20 4000 Searching an Item in a Singly Linked List key =30 First Cur=first Pos=1; while(cur!=NULL && item!=cur->info) cur=cur->link; pos++; Cur Cur->info = 15 Key=30 44. 10 1000 1000 2000 200015 NULL20 4000 Searching an Item in a Singly Linked List First Cur=first Pos=1; while(cur!=NULL && item!=cur->info) cur=cur->link; pos++; Cur Now Item = 20 , key found at position =3 45. C Function to search an Item in the list NODE search(NODE first, int key) { NODE cur; int pos; if(first==NULL) { printf( List Empty Search Not Possible..n); return; } Cur=first; pos=1; while( cur != NULL && key!=cur->info) { cur=cur->link; 46. If(cur==NULL) printf( Item not foundn); else printf( Item found at position .. %d,pos); } 47. Inserting an Element at any Position 10 20 2000 2000 NULL 48. 21 ANIL 03 2000 2000 22 sunil 3 NULL first Each Student Record in a Node 1000 49. Representation of the Record using Linked List struct student { int id; char name[20]; int sem; struct student link; }; typedef struct student *NODE;