1 / 67

Linked List

Linked List. Introduction to Linked List. It is a data Structure which consists if group of nodes that forms a sequence. It is very common data structure that is used to create tree, graph and other abstract data types.

christelb
Télécharger la présentation

Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked List Prof. Himanshu Patel

  2. Introduction to Linked List • It is a data Structure which consists if group of nodes that forms a sequence. • It is very common data structure that is used to create tree, graph and other abstract data types. • Linked list comprise of group or list of nodes in which each node have link to next node to form a chain

  3. Linked List definition • Linked List is Series of Nodes • Each node Consist of two Parts  Data Part & Pointer Part • Pointer Part stores the address of the next node

  4. What is linked list Node ? Node A has two part one data part which consists of the 5 as data and the second part which contain the address of the next node (i.e it contain the address of the next node)

  5. Linked list Blocks

  6. Linked list Blocks We can represent linked list in real life using train in which all the buggies are nodes and two coaches are connected using the connectors.

  7. Linked list Blocks • In case of railway we have peoples seating arrangement inside the coaches is called as data part of lined list while connection between two buggies is address filed of linked list. • Like linked list, trains also have last coach which is not further connected to any of the buggie. • Engine can be called as first node of linked list

  8. linked list // C Structure to represent a node struct node { int info; struct node *link; };

  9. Operations on linked list • Insert • Insert at first position • Insert at last position • Insert into ordered list • Delete • Traverse list (Print list) • Copy linked list

  10. Algorithms for Singly linked lis

  11. Algorithm to insert new node at beginning of the linked list

  12. Algorithm to insert new node at beginning of the linked list • INSERTBEG (VAL,FIRST) • This function inserts a new element VAL at the beginning of the linked list. • FIRST is a pointer which contains address of first node in the list. • [Check for availability stack underflow] • If AVAIL = NULL then • Write “Availability stack underflow” • Return • [Obtain address of next free node] • NEWAVAIL • [Remove free node from availability stack] • AVAILLINK (AVAIL) • [Initialize node to the linked list] • INFO (NEW) VAL • LINK (NEW) <- First • [Assign the address of the Temporary node to the First Node ] • FIRSTNEW • [Finished] • Return (FIRST)

  13. Algorithm to insert new node at end of the linked list

  14. Algorithm to insert new node at end of the linked list • [If list is empty?] • If FIRST = NULL then • FIRSTNEW • [initialize search for last node] • SAVEFIRST • [Search end of the list] • Repeat while LINK (SAVE) ≠ NULL • SAVELINK (SAVE) • [Set LINK field of last node to NEW ] • LINK (SAVE) NEW • [Finished] • Return (FIRST) • INSERTEND (VAL,FIRST) • This function inserts a new element VAL • at the end of the linked list. • FIRST is a pointer which contains address • of first node in the list. • [Check for availability stack underflow] • If AVAIL = NULL then • Write “Availability stack underflow” • Return • [Obtain address of next free node] • NEWAVAIL • [Remove free node from availability • stack] • AVAILLINK (AVAIL) • [initialize field of new node] • INFO (NEW) VAL • LINK (NEW) NULL

  15. Algorithm to insert new node at specific location • [If list is empty?] • If FIRST = NULL then • LINK (NEW) NULL • FIRSTNEW • [Search the list until desired address found] • SAVEFIRST • Repeat while LINK (SAVE) ≠ NULL and SAVE ≠ N • PREDSAVE • SAVELINK (SAVE) • [Node found] • If(LINK(SAVE)==NULL) { • LINK (PRED) NEW }else • { • LINK (PRED) NEW • NEWSAVE } • [Finished] • Return (FIRST) • INSPOS (VAL, FIRST, N) • This function inserts a new element VAL • into the linked list specified by address N. • FIRST is a pointer which contains address of first node in the list. • [Check for availability stack underflow] • If AVAIL = NULL then • Write “Availability stack underflow” • Return • [Obtain address of next free node] • NEWAVAIL • [Remove free node from availability stack] • AVAILLINK (AVAIL) • [initialize field of new node] • INFO (NEW) VAL

  16. Algorithm to delete first node of linked list • DELFIRST(FIRST) • This function deletes a first node from the list. • FIRST is a pointer which contains address of first node in the list. • [Check for empty list] • If FIRST = NULL then • Write “List is empty” • Return • [Check for the element in the list and delete it] • If LINK (FIRST) = NULL then • YINFO (FIRST) • FIRSTNULL • Else • TEMPFIRST • YINFO (TEMP) • FIRSTLINK (TEMP) • [Finished] • Return (FIRST)

  17. Algorithm to delete last node of linked list • [Check for the element in the list and delete it] • If LINK (FIRST) = NULL then • YINFO (FIRST) • FIRSTNULL • Else • (Assign the address pointed by FIRST pointer to TEMP pointer) • TEMPFIRST • Repeat while LINK (TEMP) ≠ NULL • PREDTEMP • TEMPLINK (TEMP) • [Delete Last Node] • YINFO (TEMP) • LINK (PRED) NULL • [Finished] • Return (FIRST) • DELLAST(FIRST) • This function deletes a last node from the list. • FIRST is a pointer which contains address of first node in the list. • [Check for empty list] • If FIRST = NULL then • Write “List is empty” • Return

  18. Algorithm to delete specific node from linked list • DELPOS(FIRST,N) • This function deletes a node from specific location from the list. • FIRST is a pointer which contains address of first node in the list. • [Check for empty list] • If FIRST = NULL then • Write “List is empty” • Return • [If there is only one node?] • If LINK (FIRST) = NULL then • YINFO (FIRST) • FIRSTNULL • [If list contains more than one node?] • TEMPFIRST • Repeat while LINK (TEMP) ≠ NULL and TEMP ≠ N • PREDTEMP • TEMPLINK (TEMP) • [Node found?] • If TEMP ≠ N then • Write “Node not found” • Else • YINFO (TEMP) • LINK (PRED) LINK (TEMP) • [Finished] • Return (FIRST)

  19. Circular Linked list

  20. Circular Linked list • A list in which last node contains a link or pointer to the first node in the list is known as circular linked list. • Representation of circular linked list is shown below:

  21. Advantage of circular linked list over singly linked linear list: • In singly linked list the last node contains NULL address. So if we are at middle of the list and want to access the first node we can not go backward in the list. Thus every node in the list is not accessible from given node. While in Circular linked list last node contains an address of the first node so every node is in the list is accessible from given node. • In singly linked list if we want to delete node at location X, first we have to find out the predecessor of the node. To find out the predecessor of the node we have to search entire list from starting from FIRST node in the list. So in order to delete the node at Location X we also have to give address of the FIRST node in the list. While in Circular linked list every node is accessible from given node so there is no need to give address of the first node in the list. • Concatenation and splitting operations are also efficient in circular linked list as compared to the singly linked list.

  22. Disadvantage: Without some care in processing it is possible to get into the infinite loop. So we must able to detect end of the list. To detect the end of the list in circular linked list we use one special node called the HEAD node. Such a circular linked list with HEAD node is shown below:

  23. struct node { int info; struct node *link; }; struct node *first;

  24. void create ( ) { struct node *ptr, *cpt; char ch; ptr = (struct node *) malloc (sizeof (struct node)); printf ("Input first node"); scanf ("%d",&ptr->info); first = ptr; do { cpt = (struct node *) malloc (sizeof (struct node)); printf ("Input next node"); scanf ("%d", &cpt -> info); ptr -> link = cpt; ptr = cpt;

  25. printf ("Press <Y/N> for more node"); ch = getch ( ); } while (ch =='Y'); ptr ->link = first; }

  26. void traverse ( ) { struct node *ptr; printf ("Traversing of link list:\n"); ptr = first; while (ptr->link != first) { printf ("%d\n",ptr-> info); ptr = ptr->link; } }

  27. void insert_beg ( ) { struct node *ptr,*cpt; ptr = (struct node *) malloc (sizeof (struct node)); if (ptr ==NULL) { printf ("overflow\n"); return; } printf ("Input new node"); scanf ("%d", &ptr -> info); cpt = first; while (cpt ->link != first) { cpt = cpt ->link; } ptr -> link = first; first = ptr; cpt -> link = first; }

  28. void insert_end ( ) { struct node *ptr, *cpt; ptr = (struct node *) malloc (sizeof (struct node)); if (ptr ==NULL) { printf ("overflow\n"); return; } printf ("Input new node information"); scanf ("%d", &ptr -> info); cpt = first; while (cpt -> link != first); cpt = cpt -> link; cpt -> link = ptr; ptr -> link = first; }

  29. void delete_beg( ) { struct node *ptr, *cpt; if (first == NULL) { printf ("underflow\n"); return; } cpt = first; while (cpt -> link != first) cpt = cpt -> link; ptr = first; first = ptr -> link; cpt -> link = first; free (ptr); }

  30. void delete_end( ) { struct node *ptr, *cpt; if (first == NULL) { printf ("underflow\n"); return; } cpt = first; while (cpt -> link != first) { ptr = cpt; cpt = cpt -> link; } ptr -> link = first; free (cpt); }

  31. CIRCULAR_LINK_INSERT_FIRST (X, FIRST, LAST) FIRST and LAST are pointers to the first and last element of a circular linked linear list ..

  32. CIR_LINK_INSERT_END (X, FIRST, LAST)

  33. CIR_LINK_DELETE (X, FIRST, LAST)

  34. Doubly link list

  35. Doubly link list • In Singly linked list we are able to traverse the list in only one direction. However in some cases it is necessary to traverse the list in both directions. • This property of linked list implies that each node must contain two link fields instead of one link field. • One link is used to denote the predecessor of the node and another link is used to denote the successor of the node.

  36. Doubly link list Thus each node in the list consist of three fields: Information LPTR RPTR

  37. Doubly link list Thus each node in the list consist of three fields: Information LPTR RPTR

  38. Doubly link list • In singly linked list we can traverse only in one direction while in doubly linked list we can traverse the list in both directions. • Deletion operation is faster in doubly linked list as compared to the singly linked list

  39. Advantage of Doubly link list • A list in which each node contains two links one to the predecessor of the node and one to the successor of the node is known as doubly linked linear list or two way chain. • Representation of doubly linked linear list is shown below • L is a pointer denotes the left most node in the list. • R is a pointer denotes the right most node in the list. • The left link of the left most node and right link of right most node are set to NULL to indicate end of the list in each direction.

  40. struct node { int info; struct node *lpt; struct node *rpt; }; struct node *first;

  41. void create ( ) { struct node *ptr, *cpt; char ch; ptr = (struct node *) malloc (sizeof (struct node)); printf ("\n Input first node information"); scanf ("%d", & ptr -> info); ptr -> lpt = NULL; first = ptr; do{ cpt = (struct node *) malloc (sizeof (struct node)); printf ("\nInput next node information \n"); scanf ("%d", & cpt -> info); ptr -> rpt = cpt; cpt -> lpt = ptr; ptr = cpt; printf ("Press <Y/N> for more node \n"); ch = getche ( ); } while (ch== 'Y'); ptr -> rpt = NULL; }

  42. void ftraverse ( ) { struct node *ptr; printf ("forward Traversing :\n"); ptr = first; while (ptr != NULL) { printf ("%d \n", ptr -> info); ptr = ptr -> rpt; } }

  43. void btraverse ( ) { struct node *ptr; printf (“Backward Traversing :\n"); ptr = first; while (ptr -> rpt != NULL) ptr = ptr -> rpt; While ( ptr != NULL ) { printf(“%d\n”, ptr -> info); Ptr = ptr -> lpt; } }

  44. void insert_beg ( ) { struct node *ptr; ptr = (struct node *) malloc (sizeof (struct node)); if (ptr == NULL) {printf ("OVERFLOW"); return; } printf ("Input new node"); scanf ("%d", & ptr -> info); ptr -> rpt = first; first -> lpt = ptr; first = ptr; printf ("New node is insert\n"); }

  45. void insert_end ( ) { struct node *ptr, *cpt; ptr = (struct node *) malloc (sizeof (struct node)); if(ptr == NULL) { printf ("OVERFLOW"); return; } printf ("Input new node information"); scanf ("%d", & ptr -> info); cpt = first; while (cpt -> rpt!= NULL) cpt = cpt -> rpt; cpt -> rpt = ptr; ptr -> lpt = cpt; ptr -> rpt = NULL; printf ("Insertion is done\n"); }

  46. void insert_given_node ( ) { struct node *ptr, *cpt, *tpt; int m; ptr = (struct node *) malloc (sizeof (struct node)); if (ptr == NULL) { printf ("OVERFLOW"); return; } printf ("Input new node information"); scanf ("%d", &ptr -> info); printf ("\nInput node information after which insertion"); scanf ("%d", &m); cpt = first;

  47. while (cpt -> info!= m) cpt = cpt -> rpt; tpt = cpt -> rpt; cpt -> rpt = ptr; ptr -> lpt = cpt; ptr -> rpt = tpt; tpt -> lpt = ptr; printf ("Insertion is done\n"); }

  48. void delete_beg ( ) { struct node *ptr; if(first == NULL) { printf ("Underflow\n"); return; } ptr=first; first = ptr -> rpt; first -> lpt = NULL; free(ptr); }

  49. void delete_end ( ) { struct node *ptr, *cpt; if (first == NULL) { printf ("Underflow\n"); return; } ptr = first; while (ptr -> rpt!= NULL) { cpt = ptr; ptr = ptr -> rpt; } cpt -> rpt = NULL; free (ptr); printf ("Deletion is done\n"); }

  50. void delete_given_node ( ) { struct node *ptr, *cpt, *tpt; int m; if(first == NULL) { printf ("Underflow"); return; } printf ("\n Node to be deleted is"); scanf ("%d", &m); ptr = first; while (ptr -> info != m) ptr = ptr -> rpt; cpt = ptr -> lpt; tpt = ptr -> rpt; cpt -> rpt = tpt; tpt -> lpt = cpt; free (ptr); printf ("Deletion is done\n"); }

More Related