1 / 28

Chapter LINKED LIST

Chapter LINKED LIST. What is Linked List?. Linked List is a common Data structure used to store similar data in memory. Elements are not stored in continuous memory location. Elements are bound to each other by explicit links. Basic Types of Linked List. Contd….

ian-fulton
Télécharger la présentation

Chapter 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. ChapterLINKEDLIST

  2. What is Linked List? • Linked List is a common Data structure used to store similar data in memory. • Elements are not stored in continuous memory location. • Elements are bound to each other by explicit links

  3. Basic Types of Linked List

  4. Contd…

  5. 1. Arrays use sequential allocation 2. No pointer used in Array. 3. Accessing kth element is cheaper in arrays. 4. Insertion and Deletion of elements is a difficult operation in Arrays. 5. Nodes are statically attached. 6. Merging & Breaking Lists is difficult in case of Arrays. 7. Arrays are goes mostly for static data structures. 1. Linked list uses linked allocation. 2. Linked list uses some extra memory i.e. link pointer. 3. Accessing kth element is costly in Linked list. 4. Insertion and Deletion of elements is a easy operation in Linked lists 5. Since nodes in Linked list are dynamically allocated, it has no limitations on growth. 6. Merging & Breaking Lists is easier in case of Linked lists. 7. Linked list is a better data structure in most cases. Difference between Arrays & Linked list

  6. Question ? Write a program to create Singly linked list which will store data of employees. Every Employee has following information • Employee Number • Salary

  7. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct emp { int empno; float sal; struct emp *next; }; struct emp *first, *newrec, *old;

  8. void insert() { newrec = (struct emp*) malloc (sizeof(struct emp)); printf("enter emp no and salary"); scanf("%d%f",&newrec->empno, &newrec->sal ); newrec->next = NULL; if(first==NULL) first= newrec; else old->next = newrec; old=newrec; }

  9. void display() { newrec = first; while(newrec!=NULL) { printf("%d\t%f \n",newrec->empno,newrec->sal); newrec=newrec->next; } }

  10. void main() { clrscr(); first = NULL; insert(); insert(); insert(); display(); getch(); }

  11. Insertion Steps Initial Step: three pointers and no memory allocated

  12. First Step : Memory allocated using one Structure created for emp 1

  13. Second Step :

  14. Third Step:

  15. Forth Step :

  16. Fifth Step :

  17. And So on…..

  18. Doubly Linked List Each structure in the list will contain some data and two pointers ‘prew’ & ‘next’ which will store the address of previous and next structures. ‘Prew’ pointer of first structure and ‘next’ pointer of last structure are null. Two additional pointers are also used namely first & last which will point to first and last structure.

  19. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct emp { float sal; int empno; struct emp *prev, *next; }; struct emp *first, *newrec, *last;

  20. void insert() { float d; newrec=(struct emp*) malloc (sizeof(struct emp)); printf("enter the salary and employee no:"); scanf("%d%f",&newrec->empno,&d); newrec->sal=d; newrec->next=NULL; if(first==NULL) { newrec->prev =NULL; first=newrec; } else { newrec->prev = last; last->next=newrec; } last = newrec; }

  21. void display() { newrec = last; while(newrec!=NULL) { printf("%d\t%f\n",newrec->empno,newrec->sal); newrec=newrec->prev; } }

  22. void main() { first= NULL; insert(); insert(); insert(); display(); getch(); }

  23. Insertion sort Algorithm:- Step-1: Compare the current element in the UNSORTED list with each element in the SORTED list. If you find an element in the SORTED list to have just higher value than our current element then go to step-2 or else go to step-3

  24. Step-2: Make the element with that just higher value as your TARGET position. Insert the current element at a position just before the TARGET. Consider the next element, if any and go to step-1 or else quit Step-3: If you find the current element under consideration in the sorted list as the LAST element then just insert your current element from the unsorted list at the end of the Sorted list or else just move to the next element in the sorted list. Consider the next element, if any and go to step-1 or else quit 

  25. Insert node at any Place Void add () { struct node *a, *b; int p ; newrec = ( struct emp*) malloc ( sizeof (struct emp)); printf (“Enter the data to be added”); scanf(“%d”, & newrec ->data); printf (“Enter position :”); scanf (“%d”, &p);

  26. if (p == 1) { newrec -> next = first; first = newrec; } Else { a = b = first; for (i=1; i<= p-1; i++) { b = a; a = a-> next; } b -> next = newrec; newrec -> next = a; } / * end else*/ } / * end add */

  27. Delete node from any Place Void delete() { int p ; struct node *a , * b; printf (“Enter position :”); scanf (“%d”, &p); a = b = first;

  28. for (i=1; i<= p-1; i++) { b = a; a = a-> next; } b -> next = a -> next; if (p == 1) { first = a -> next; } free (a); } / * end delete */

More Related