Chapter LINKED LIST

28
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 Presentation

Transcript of Chapter LINKED LIST

Page 1: Chapter LINKED LIST

Chapter

LINKED LIST

Page 2: 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

Page 3: Chapter LINKED LIST

Basic Types of Linked List

Page 4: Chapter LINKED LIST

Contd…

Page 5: Chapter LINKED LIST

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

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

#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

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

void display()

{

newrec = first;

while(newrec!=NULL)

{

printf("%d\t%f \n",newrec->empno,newrec->sal);

newrec=newrec->next;

}

}

Page 10: Chapter LINKED LIST

void main(){ clrscr(); first = NULL; insert(); insert(); insert();

display(); getch();}

Page 11: Chapter LINKED LIST

Initial Step: three pointers and no

memory allocated

Insertion Steps

Page 12: Chapter LINKED LIST

• First Step : Memory allocated using one

Structure created for emp 1

Page 13: Chapter LINKED LIST

Second Step :

Page 14: Chapter LINKED LIST

Third Step:

Page 15: Chapter LINKED LIST

• Forth Step :

Page 16: Chapter LINKED LIST

Fifth Step :

Page 17: Chapter LINKED LIST

And So on…..

Page 18: Chapter LINKED LIST

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

#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

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

void display() {

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

}

Page 22: Chapter LINKED LIST

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

Page 23: Chapter LINKED LIST

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

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

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

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

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

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 */