Linked List
Linked List
is a Collection of nodes, where each node contains the address of next node. List are also called as linked list
11
List
Each node in a list contains two parts
Data part :
Link part :
List
data
data
data NULL
22
List
Types of List
Linear list
2.
Non-Linear list
33
e.g:
2. Simple to implement
44
Can become Full as Size of the array is fixed Allocating "extra" space not possible Array items are stored contiguously. not enough contiguous space, will be problem. Can be wasteful, space may never be used
2.
3.
4. 5.
Insert and delete in middle of elements is tedious. Difficult to maintain sorted order Need to shift the elements
55
Non-Homogeneous List
Example:
char name[20];
int SEM; struct Listnode *next;
};
typedef Listnode Node;
6 6
simple.
77
88
data
data
data
NULL
data
data
data
10 10
Start
Start
data right
left data
12 12
13 13
14
15 15
4. Traversing a list
Write code for Display Linked list
start
100
30
200 100
20
300
30
400
300
40
NULL 400
200
16 16
void Display ( node *first ) { node *temp; if (first == NULL) { printf (Empty List ); return; } temp = first; while (temp != NULL) { printf ( %d , temp data); temp = temp next; } } /* end of Display */
17
1. Insert a node
a) Insert at front
18 18
a)
Insert at front
node *temp;
temp = getnode ( );
tempdata = item ; tempnext = start; start = temp;
/* end of insert_front */
19 19
b) Insert at end
start
200
last
400
20 300
200
30
400 300
40
NULL
400
30
NULL
500
Logic ..
1. Create new node 2. search last node
500
temp
20 20
temp -> data = item ; // add data if (start == NULL) { start = temp ; return ; } while ( lastnext != NULL ) //search last node
last = lastnext ; lastnext = temp; } /* end of insert_front */
Prof. Vishwanath, 9844340356
// create link
21
22 22
a) remove at front
start 100 200
30
100
200
20
300
30
400 300
40
NULL
400
200
temp
Logic ..
1. Copy start to temp 2. Move start to next node
? 100
3. free (temp )
Prof. Vishwanath, 9844340356
23 23
void remove_front ( ) { node *temp; if (start == NULL) { printf ( ..List is Empty..); return start; }
temp = start;
start = start next;
// display temp
24
b) remove at end
start prev last
200
400
? 500
20
300
30
400 300
400 40 NULL
400
50
NULL
200
500
Logic ..
1. No node in list No deletion 2. Only one node in list List becomes empty
Prof. Vishwanath, 9844340356
3. More nodes
void remove_end ( )
return ;
while (lastnext != NULL) /* search last node */ { prev = last; last = lastnext ; } prevnext = NULL;
} /* end of insert_front */
Prof. Vishwanath, 9844340356
free (last);
26 26
200
prev
300
20
200
300
30
600 400
40
400
500
30
NULL 500
300
pos = 3
30
400 ?
600
600
temp
27 27
node insert_pos ( int pos, int item ) { node *temp, *prev ; int i; temp = getnode (item);
if ( pos==1 )
insert_front ( );
prev = start;
for ( i = 1; i < pos-1 ; i++ ) // Search position { if (prev == NULL) break; prev = prev -> next; } if ( prev == NULL ) { printf("\n***Invalid Position **\n"); free (temp); return ; } temp next = prev next ;
prev next = temp;
/* END OF FUNCTION */
28 28
200
prev
300
cur ? 400
20
300
30 400
300
40
500
400
50
NULL
500
200
pos = 3
1. No node in list No deletion 2. Deletion at 1st position Change start to next node
Prof. Vishwanath, 9844340356
3. More nodes
30 30
prev = start; for (i = 1; i < pos-1 ; i++ ) { // Search prev Node for node to be deleted
}
{
free (cur);
31 31
Stack implementation
Implementation :
Push() - Insert At Front
Pop() - Remove At Front
32 32
Queue implementation
Insert At Rear Remove At Front
33 33
Problems..
Write C-routine to concatenate two singly Linked lists..
List1
200
last
300
List2 ? 400
20
200
220
30
300
220
30
400 NULL
300
40
500
400
50
NULL 500
Function call:
34 34
// List = List1
35 35