Linked Lists
Linked Lists
Queue
A queue can be implemented as a linked list
A new member joins at the end
Member at the front gets serviced
Q- front
NULL
NULL
enqueue
A new member joins the queue
Joining is always at the end of the queue
Linked list: (Insert a node at the rear of list)
front
NULL
rear
dequeue
A member gets serviced and leaves the queue
It is always the member at the front of the queue
Linked list: ( Delete a node at front of the list )
front
NULL
gone
Queue operations
Insert node at rear of the list
Delete a node from front of the list
NULL
NULL
11
r
NULL
12
q=r;
// start from head node
while (qnext != NULL)
q = qnext; // find the last element
qnext =p;
// attach the new node to last node
return r;
}
Contd.11
4
NULL
Add a node
containing 15 at
the end of the list
11
r
15
NULL
11
15
q
r
15
NULL
NULL
Output
List = {10, }
List = {10, 11, }
List = {10, 11, 12, }
16
Given list
let p point to first node
let r point to second node
p
NULL
r
19
r
NULL
20
Stack of cartons
stack of mattresses
stack operations
PUSH
insert an node at front of list
POP
delete a node at front of list
Creating a stack
void display (struct node *);
struct node * PUSH(struct node * , int);
int main()
{ struct node *top;
top = NULL;
top = PUSH(top, 10);
display(top);
top = PUSH(top, 11);
display(top);
top = PUSH(top, 12);
display(top);
return 0;
25
}
Output
List = {10, }
List = {11, 10, }
List = {12, 11, 10, }
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *link; }
NODE;
void Push( int );
int pop( );
void Display( );
NODE *top = NULL; /* Global Declarations */
int main( ) { . . . . .
......
.......
int POP( ) {
int info;
NODE *t;
if( top == NULL) {
printf(" stack empty!!!");
return 1; }
else {
t=top;
info=top->data;
top=top->link;
t->link=NULL;
free(t);
return(info);
}
}
head
NULL
NULL
prev
32
cur
head
NULL
prev
cur
33
8. Function returns r.
head
NULL
prev
35
cur
head
NULL
prev
37
cur
head
NULL
NULL
prev
38
cur
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *link; }
NODE;
void Push( int );
int pop( );
void Display( );
NODE *top = NULL; /* Global Declarations */
int main( ) { . . . . .
......
given list
P
10
prev
30
20
40
cur
40
41
NULL
reverse list
Q
40
30
prev
cur
20
42
10
NULL
cur = P;
Q = NULL;
while( cur != NULL) {
q = //s.n.malloc
qdata = curdata;
qnext = Q;
Q = q;
cur = curnext;
}
reverse list
10
45
NULL
reverse list
10
46
NULL
reverse list
20
q
47
10
NULL
reverse list
20
Q
48
10
NULL
Given an array
[ 7 9 12 15 22 38 46 50]
Insert 20. This would require all elements >20 to be shifted right.
[ 7 9 12 15 20 22 38 46 50]
prev
13
15
new
ptr
18
22
--
52
prev
13
15
new
ptr
18
22
prev
new
ptr
8
--
54
12
prev
new
ptr
8
55
12
prev
new
16
__
56
12
prev
new
16
__
57
12
Get data from the user and insert nodes to form a list in
sorted order
main() {
struct node *start;
int i,n,value;
start = NULL;
scanf("%d",&n);
for(i=0; i<n; i++) {
printf( Give Data: " );
scanf("%d , &value);
start = insert_m (start, value);
}
display(start);
return 0;
}
13
15
18
22
qnext
pnext
62
12
q
3
12
qnext
16
__
pnext
65
Delete node 8
first
prev
ptr
12
Example of Deletion
first
prev
ptr
12
delete(r,4)
Deleting an element
11
4
q
11
11
q
68
15
15
4
q
15
p NULL
Deleting an element
struct node * delete(struct node * r, int value)
{ struct node *p, *q;
p =r;
11
q = p;
q
while(p!=NULL) {
if (p->data == value){
if (p==r) r = p->next;
else q->next = p->next;
p->next = NULL;
11
free(p);
q
return r;
}
else { q = p;
p = p->next;
11
}
q
}
return r;
70
}
delete(r,4)
15
15
15
4
p NULL
Exercises
Print a list backwards (try a recursive print)
Count the number of elements in a list (both
using and not using recursion)
Concatenate two lists
Reverse a list
71