Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Info field
Linked List Types Singly Linked list Circular singly linked list Doubly linked lists Circular doubly linked lists
Graphical Representation
Singly Linked List
First Last Node
1000
10
15
1000
2000
20
NULL 2000
4000
Graphical Representation
Circular Singly Linked List Last node contains the address
First 10 Last
1000
15
1000
2000
20
4000 2000
4000
NULL
10
2000
1000
15
3000
2000
20
NULL
1000
2000
3000
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
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
x;
Later on you can use x as a data type x a, b; Means a and b are variables of the type integer
Graphical Representation
Operations on Singly Linked List
First Last Node
1000
10
15
1000
2000
20
NULL 2000
4000
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
30
NULL
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
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
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
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
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
Graphical Representation
Operations on Singly Linked List
First 15
1000 2000 20
Last Node
NULL 2000
30
NULL
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
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
Graphical Representation
Operations on Singly Linked List
Cur/ First Last Node 15
1000 2000 20 3000 2000 30
NUL L
10
1000
3000
Graphical Representation
Operations on Singly Linked List
prev cur
1000
Last Node
2000 20 3000 2000 30
NUL L
10
15
1000
3000
Graphical Representation
Operations on Singly Linked List
prev cur
20 3000 2000
Last Node
30
NUL L
10
1000
15
1000
2000
3000
Graphical Representation
Operations on Singly Linked List
prev cur
3000 2000 30
NUL L
10
1000
15
1000
2000
20
3000
Graphical Representation
Operations on Singly Linked List
First prev 15
1000 20 3000 2000
10
1000
2000
4000
3000
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
Push item 30
S L I T N K E A D
C 3000 30 Null
top
L I S T
Push item 20
S L I T N
top
A 2000 20 3000
K E D L I
3000 K
30
Null
S T
Push item 10
top
S 1000 10 L 2000 I N K 2000 20 3000 E D L I 3000 30 Null S T
POP
10 L I
T 2000 20 3000
top
N K E D L I
C 3000 30 Null
S T
POP
20 L I N K E D
top
C 3000 30 Null K
L I S T
POP
30 L I N K
STACK EMPTY
E D L I S T
Inserting an Element from the rear and deleting An element from the front is nothing but Q
Inserting 20 temp
20
NULL
Front=null Front=rear=temp
Inserting 20 front/rear
20
NULL
20
NULL
30
null 2000
rear->link=temp
20
2000
30
null 2000
20
2000
30
null
40
Null
2000
20
2000
30
3000
40
Null 3000
2000
Delete Operation
Deleting 20 front rear
20
2000
30
3000
40
Null 3000
2000
30
3000
40
Null 3000
2000
Deleting 40
front
rear
40
Null
3000
Q Empty