Sie sind auf Seite 1von 52

Linked List

Compiled By JVGorabal Asst Prof CSE Dept

Linked List
What is the problem 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
Compiled By JVGorabal Asst Prof CSE Dept

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
Pointer to the first node

Info field

Link Field/ Address Field

Compiled By JVGorabal Asst Prof CSE Dept

Linked List Types Singly Linked list Circular singly linked list Doubly linked lists Circular doubly linked lists

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Singly Linked List
First Last Node
1000

10

15
1000

2000

20

NULL 2000

4000

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Circular Singly Linked List Last node contains the address
First 10 Last
1000

15
1000

2000

20

4000 2000

4000

Compiled By JVGorabal Asst Prof CSE Dept

Doubly Linked list


Contains the address of previous node and next node
First Last

NULL

10

2000

1000

15

3000

2000

20

NULL

1000

2000

3000

Compiled By JVGorabal Asst Prof CSE Dept

Circular Doubly Linked list Contains the address of first node and last node
First Last

3000

10

2000

1000

15

3000

2000

20

1000

1000

2000

3000

Compiled By JVGorabal Asst Prof CSE Dept

Representation of Singly Linked list


strut 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
Compiled By JVGorabal Asst Prof CSE Dept

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

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First Last Node
1000

10

15
1000

2000

20

NULL 2000

4000

Insert Delete Display the contents


Compiled By JVGorabal Asst Prof CSE Dept

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

Compiled By JVGorabal Asst Prof CSE Dept

Algorithm to Insert an Element from the front


1 Temp <- newnode 2 temp=allocate memory 3 temp(info)=item; 4 Link(temp)=first; 2 Call temp as first 3 Return first;

temp NULL NULL

30

NULL

Temp node with item Compiled By JVGorabal Asst Prof


CSE Dept

Graphical Representation
Operations on Singly Linked List temp 10
1000

First

Last Node
2000 20 NULL 2000

15
1000

4000

temp->link=first,
Insert from the front

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First Last Node 15
1000 2000 20 NULL 2000

10

1000

4000

first=temp
Insert from the front

Compiled By JVGorabal Asst Prof CSE Dept

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) }
Compiled By JVGorabal Asst Prof CSE Dept

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=first; first=temp; return(temp); }

Compiled By JVGorabal Asst Prof CSE Dept

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)

Compiled By JVGorabal Asst Prof CSE Dept

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); }
Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
Temp/ First Last Node 15
1000 2000 20 NULL 2000

10

1000

4000

temp=first
Delete from the front

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First Last Node 15
1000 2000 20 NULL 2000

10

1000

temp

4000

temp=first, first=first->link
Delete from the front

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First 15
1000 2000 20

Last Node
NULL 2000

Delete from the front

Compiled By JVGorabal Asst Prof CSE Dept

Algorithm to Insert an Element from the Rear


1 temp < - newnode 2 temp = allocate memory 3 temp(info)=item; 4 link(last)=temp; 2 Call temp as last 3 Return last;

temp NULL NULL

30

NULL

Temp node with item Compiled By JVGorabal Asst Prof


CSE Dept

C Function to Insert from the rear


NODE insertrear(int item , NODE first) { NODE temp; temp=(NODE*)malloc(sizeof (strcut node)); If (first==NULL) first=last=temp; return first; last->link=temp; last=temp; return last; }
Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First Last temp

10

1000

15
1000

2000

20

NULL 2000

30

NUL L

4000

3000

first=temp
Insert from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First Last Node 15
1000 2000 20 3000 2000 30
NUL L

10

1000

4000

3000

first=temp
Insert from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Algorithm to Delete the Element form the rear


1 if first ==NULL // no nodes write list empty and return first 2 if (link(first)) is empty // Only one node free first return first 3 4 5 6 traverse up to last node // More than one node prev=NULL & cur = first while(link(cur)!=NULL) prev=cur;

7 cur=link(cur) 8 Link(prev)=NULL; 9 free(cur);


Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
Cur/ First Last Node 15
1000 2000 20 3000 2000 30
NUL L

10

1000

4000 prev =NULL Cur=first; while(cur->link!=NULL) { prev=cur; cur=cur->link; }

3000

Delete from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
prev cur
1000

Last Node
2000 20 3000 2000 30
NUL L

10

15
1000

4000 prev =NULL Cur=first; While(cur->link!=NULL) { prev=cur; cur=cur->link; }

3000

Delete from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
prev cur
20 3000 2000

Last Node
30
NUL L

10

1000

15
1000

2000

4000 prev =NULL Cur=first; While(cur->link!=NULL) { prev=cur; cur=cur->link; }

3000

Delete from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
prev cur
3000 2000 30
NUL L

10

1000

15
1000

2000

20

4000 prev =NULL Cur=first; While(cur->link!=NULL) { prev=cur; cur=cur->link; }

3000

Delete from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First prev 15
1000 20 3000 2000

cur Last Node


30
NUL L

10

1000

2000

4000

3000

Delete from the Rear

Compiled By JVGorabal Asst Prof CSE Dept

Graphical Representation
Operations on Singly Linked List
First prev 15
1000 2000 20 NULL 2000

10

1000

4000

prev->link=NULL
Delete from the Rear
Compiled By JVGorabal Asst Prof CSE Dept

Stack Using Linked List CONCEPT : LIFO Inserting an element from front and deleting an element from front is nothing but STACK

Compiled By JVGorabal Asst Prof CSE Dept

Push item 30
S L I T N K E A D

C 3000 30 Null

top

L I S T

Compiled By JVGorabal Asst Prof CSE Dept

Push item 20
S L I T N

top
A 2000 20 3000

K E D L I

3000 K

30

Null

S T

Compiled By JVGorabal Asst Prof CSE Dept

Push item 10

top
S 1000 10 L 2000 I N K 2000 20 3000 E D L I 3000 30 Null S T

Compiled By JVGorabal Asst Prof CSE Dept

POP

10 L I

T 2000 20 3000

top

N K E D L I

C 3000 30 Null

S T

Compiled By JVGorabal Asst Prof CSE Dept

POP

20 L I N K E D

top
C 3000 30 Null K

L I S T

Compiled By JVGorabal Asst Prof CSE Dept

POP

30 L I N K

STACK EMPTY

E D L I S T

Compiled By JVGorabal Asst Prof CSE Dept

Implementation Q using linked Lits

Inserting an Element from the rear and deleting An element from the front is nothing but Q

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 20 temp

20

NULL

Front=null Front=rear=temp

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 20 front/rear

20

NULL

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 30 front/rear temp

20

NULL

30

null 2000

rear->link=temp

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 30 front rear

20

2000

30

null 2000

1000 rear->link=temp rear=temp

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 40 front rear

20

2000

30

null

40

Null

1000 rear->link=temp rear=temp

2000

Compiled By JVGorabal Asst Prof CSE Dept

Inserting 40 front rear

20

2000

30

3000

40

Null 3000

1000 rear->link=temp rear=temp

2000

Compiled By JVGorabal Asst Prof CSE Dept

Compiled By JVGorabal Asst Prof CSE Dept

Delete Operation
Deleting 20 front rear

20

2000

30

3000

40

Null 3000

1000 temp=front front=front->link free(temp)

2000

Compiled By JVGorabal Asst Prof CSE Dept

Deleting 30 front rear

30

3000

40

Null 3000

2000

Compiled By JVGorabal Asst Prof CSE Dept

Deleting 40

front

rear

40

Null

3000

Compiled By JVGorabal Asst Prof CSE Dept

Q Empty

Compiled By JVGorabal Asst Prof CSE Dept

Das könnte Ihnen auch gefallen