chapter linked list
DESCRIPTION
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…. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/1.jpg)
Chapter
LINKED LIST
![Page 2: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/2.jpg)
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
![Page 3: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/3.jpg)
Basic Types of Linked List
![Page 4: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/4.jpg)
Contd…
![Page 5: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/5.jpg)
Difference between Arrays & Linked list
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.
![Page 6: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/6.jpg)
Question ?
Write a program to create Singly linked list which will store data of employees. Every Employee has following information
• Employee Number
• Salary
![Page 7: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/7.jpg)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp
{
int empno;
float sal;
struct emp *next;
};
struct emp *first, *newrec, *old;
![Page 8: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/8.jpg)
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; }
![Page 9: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/9.jpg)
void display()
{
newrec = first;
while(newrec!=NULL)
{
printf("%d\t%f \n",newrec->empno,newrec->sal);
newrec=newrec->next;
}
}
![Page 10: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/10.jpg)
void main(){ clrscr(); first = NULL; insert(); insert(); insert();
display(); getch();}
![Page 11: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/11.jpg)
Initial Step: three pointers and no
memory allocated
Insertion Steps
![Page 12: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/12.jpg)
• First Step : Memory allocated using one
Structure created for emp 1
![Page 13: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/13.jpg)
Second Step :
![Page 14: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/14.jpg)
Third Step:
![Page 15: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/15.jpg)
• Forth Step :
![Page 16: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/16.jpg)
Fifth Step :
![Page 17: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/17.jpg)
And So on…..
![Page 18: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/18.jpg)
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.
![Page 19: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/19.jpg)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp
{
float sal;
int empno;
struct emp *prev, *next;
};
struct emp *first, *newrec, *last;
![Page 20: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/20.jpg)
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; }
![Page 21: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/21.jpg)
void display() {
newrec = last; while(newrec!=NULL) { printf("%d\t%f\n",newrec->empno,newrec->sal); newrec=newrec->prev; }
}
![Page 22: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/22.jpg)
void main() { first= NULL; insert(); insert(); insert(); display(); getch(); }
![Page 23: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/23.jpg)
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
![Page 24: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/24.jpg)
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
![Page 25: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/25.jpg)
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);
![Page 26: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/26.jpg)
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 */
![Page 27: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/27.jpg)
Delete node from any Place
Void delete(){ int p ; struct node *a , * b; printf (“Enter position :”); scanf (“%d”, &p); a = b = first;
![Page 28: Chapter LINKED LIST](https://reader035.vdocument.in/reader035/viewer/2022070400/5681355c550346895d9cc3e4/html5/thumbnails/28.jpg)
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 */