You are on page 1of 36

Data Structures and Algorithms

(21003)
Semester I, 2014-2015
IIT odh!ur
"in#ed "ists
2
"in#ed list

A lin#ed list consists o$%

A se&uence o$ nodes
3
a ' c d

(ach node contains a )alue

and a lin# (!ointer or re$erence) to some other node

The last node contains a null lin#

The list ma* (or ma* not) ha)e a header


head
T*!es o$ lin#ed lists

singl*-lin#ed list

dou'l*-lin#ed list

circular-lin#ed list
4
+ore terminolog*

A node,s successor is the ne-t node in the


se&uence

The last node has no successor

A node,s !redecessor is the !re)ious node in


the se&uence

The $irst node has no !redecessor

A list,s length is the num'er o$ elements in it

A list ma* 'e em!t* (contain no elements)


5
.reating lin#ed list
typedef struct nodeType {
int value;
struct nodeType *next;
} node;
node *head;
6
44 97 23 17
head:
/!erations on single lin#ed list

.reating an em!t* list

Tra)erse a list

Searching an element

Insert

Delete

7

in-order tra)erse

re)erse order tra)erse


.reating an em!t* lin#ed list
void create!pty"ist#node **head$
{
*head%&'"";

}
8
Tra)ersing lin#ed list in order
void Traverse(n)rder#node *head$
{
*hile#head+%&'""$
{
cout,,-head./value01n;
head%head./next;

}
9
Tra)ersing lin#ed list in re)erse order
void Traverse2everse)rder#node *head$
{
(f #head./next +%&'""$
Traverse2everse)rder#head./next$;
cout,,-head./value01n;
}
10
Searching in lin#ed list

node *3earch'nsorted#node *head4 int ite!$
{
5hile ##head +%&'""$ 66 #head./value +%ite!$$
head%head./next;
return head;
}

node *3earch3orte#node *head4 int ite!$
{
5hile#head +%&'""$
{
(f#head./value%%ite!$
return head;
lse(f#ite!,head./value$
return &'"";
lse
head%head./next;
}
}

11
"ist is unsorted
"ist is sorted
Inserting in lin#ed list

Inserting an element


12

at the 'eginning o$ the list

at the end o$ the list

a$ter a gi)en element


Inserting at the 'eginning
void (nsert7e8in#node **head4 int ite!$
{
node *ptr;
ptr%#node *$ !alloc#si9eof#node$$;
ptr./value%ite!;
(f#*head%%&'""$
ptr./next%&'"";
lse
ptr./next%*head;
*head%ptr;
}
A
X
C B
new node
Inserting at the end
void (nsert7e8in#node **head4 int ite!$
{
node *ptr4*loc;
ptr%#node *$ !alloc#si9eof#node$$;
ptr./value%ite!;
ptr./next%&'"";
(f#*head%%&'""$
*head%ptr;
lse
{
loc%*head;
5hile#loc./next +%&'""$
loc%loc./next;
loc./next%ptr;
}
A
X
C B
new node
loc
Inserting a$ter a gi)en element
void (nsert:fter#node **head4 int ite!4 int after$
{
node *ptr4*loc;
loc%search#head4after$;
(f #loc%%#node *$ &'""$;*ele!ent -after0 not
found*;
return;
ptr%ne*node;
ptr./value%ite!;
ptr./next%loc./next;
loc./next%ptr;
}
A
X
C B
new node
loc
Deletion in lin#ed list

Deleting an element


16

at the 'eginning o$ the list

at the end o$ the list

a$ter a gi)en element


Deletion $rom the 'eginning
void <elete7e8in#node **head4 int ite!$
{
node *ptr;
(f#*head%%&'""$
return;
lse
{
ptr%*head;
*head%#*head$.//next;
free#ptr$;
}
}
A C B
ptr
Deletion $rom the end
void <eletend#node **head$
{
node *ptr4*loc;
(f#*head%%&'""$
return;
lse(f##*head$./next%%#node*$ &'""$
{ ptr%*head;
*head%&'"";
free#ptr$;
}
lse
{loc%*head;
ptr%#*head$./next;
5hile#ptr./next +%&'""$
{ loc%ptr;
ptr%ptr./next;
}
loc./next%&'"";
free#ptr$;
}
}
A C B
ptr
loc
ptr
Deletion a$ter a gi)en element
void <elete:fter#node *head4 int ite!4 int after$
{
node *ptr4*loc;
loc%search#head4after$;
(f #loc%%#node *$ &'""$;*ele!ent -after0 not
found*;
return;
ptr%loc./next;
loc./next%ptr./next;
free#ptr$;
}
A C B
ptr
loc
Dou'l* lin#ed list
(ach node is di)ided into 3 !arts

0irst !art122contains the address o$


the !receding element in the list

Second !art12contains the


in$ormation o$ the element

Third !art1contains the address o$


the succeeding element in the list
head:
a = c
tail:
.reating dou'l* lin#ed list
typedef struct nodeType
{
struct nodeType *prev;
int value;
struct nodeType *next;
} node;
node *head4 *tail;
/!erations on dou'l* lin#ed list

.reating an em!t* list

Tra)erse a list

Searching an element

Insert

Delete

22

in-order tra)erse

re)erse order tra)erse


.reating an em!t* dou'l* lin#ed list
void create!pty<"ist#node **head4 node **tail$
{
*head%*tail%&'"";

}
23
Tra)ersing lin#ed list in order
void <Traverse(n)rder#node *head$
{
*hile#head+%&'""$
{
cout,,-head./value01n;
head%head./next;
}
24
Tra)ersing lin#ed list in re)erse order

void <Traverse2everse)rder#node *tail$

{
*hile#tail+%&'""$

{

cout,,-tail./value01n;

tail%tail./prev;

}
Searching an element in dou'l* lin#ed list
node *3earch<"#node *head4 int ite!$
{
5hile #head +%&'""$
{
(f#head./value%%ite!$
return head;
head%head./next;
}
return &'"";
}
25
Inserting an element in dou'l* lin#ed list

At the 'eginning o$ the list

At the end o$ the list

A$ter a gi)en element

3e$ore a gi)en element


26
Inserting at the 'eginning
void <(nsert7e8in#node **head4 node **tail4 int ite!$
{
node *ptr;
ptr%#node *$ !alloc#si9eof#node$$;
ptr./value%ite!;
(f#*head%%&'""${
ptr./next%ptr./prev%&'"";
*head%*tail%ptr;}
lse{
ptr./prev%&'"";
ptr./next%*head;
#*head$./prev%ptr;
*head%ptr;
}
a = c
X
!tr
Inserting at end
void <(nsertnd#node **head4 node **tail4 int ite!$
{
node *ptr;
ptr%#node *$ !alloc#si9eof#node$$;
ptr./value%ite!;
(f#*head%%&'""${
ptr./next%ptr./prev%&'"";
*head%*tail%ptr;}
lse{
ptr./next%&'"";
ptr./prev%*tail;
#*tail$./next%ptr;
*tail%ptr;
}
a = c
X
!tr
Inserting a$ter a gi)en element
void <(nsert:fter#node *head4 node **tail4 int ite!4 int
after$
{
node *ptr4*loc;
ptr%head;
loc%3earch<"#ptr4 after$;
(f#loc%%&'""$
return;
ptr%ne*node#$;
ptr./value%ite!;
(f#loc./next%%&'""${
ptr./next%&'"";
loc./next%ptr;
ptr./prev%*tail;
*tail%ptr; }
lse{
ptr./prev%loc;
ptr./next%loc./next;
#loc./next$./prev%ptr;
loc./next%ptr;}
}
A B C
head
tail
loc
X
!tr
X
!tr
Inserting 'e$ore a gi)en element
void <(nsert7efore#node **head4 node *tail4 int ite!4 int
=efore$
{
node *ptr4*loc;
ptr%*tail;
loc%3earch<"#ptr4 =efore$;
(f#loc%%&'""$
return;
ptr%ne*node#$;
ptr./value%ite!;
(f#loc./prev%%&'""${
ptr./prev%&'"";
loc./prev%ptr;
ptr./next%*head;
*head%ptr; }
lse{
ptr./prev%loc./prev;
ptr./next%loc;
#loc./prev$./next%ptr;
loc./prev%ptr;}
}
A B C
head
tail
loc
X
!tr
X
!tr
Deletion $rom the 'eginning
void <<elete7e8in#node **head4 node **tail$
{
node *ptr;
(f#*head%%&'""$
return;
ptr%*head;
(f#*head%%*tail$
{ *head%*tail%&'"";;*only one ele!ent*;
}
lse{
#ptr./next$./prev%&'"";
*head%ptr./next;
}
free#ptr$;
}
A B C
head
tail
!tr
Deletion $rom the end
void <<eletend#node **head4 node **tail$
{
node *ptr;
(f#*head%%&'""$
return;
ptr%*tail;
(f#*head%%*tail$
{ *head%*tail%&'"";;*only one ele!ent*;
}
lse{
#ptr./prev$./next%&'"";
*tail%ptr./prev;
}
free#ptr$;
}
A B C
!tr
head
tail
Deleting a$ter a gi)en element
void <<elete:fter#node *head4 node **tail4 int after$
{
node *ptr4*loc;
ptr%head;
loc%3earch<"#ptr4 after$;
(f#loc%%&'""$
return;
lse(f##loc./next$./next%%&'""$
{ ptr%loc./next;
loc./next%&'"";
*tail%loc;
free#ptr$;
}
lse
{ ptr%loc./next;
loc./next%ptr./next;
#ptr./next$./prev%loc;
free#ptr$;
}
}
A B C
loc
tail
head
!tr
!tr
Deleting 'e$ore a gi)en element
void <<elete7efore#node **head4 int =efore$
{
node *ptr4*loc;
ptr%*head;
loc%3earch<"#ptr4 =efore$;
(f#loc%%&'""$
return;
lse(f##loc./prev$./prev%%&'""$
{ ptr%loc./prev;
loc./prev%&'"";
*head%loc;
free#ptr$;
}
lse
{ ptr%loc./prev;
loc./prev%ptr./prev;
#ptr./prev$./next%loc;
free#ptr$;
}
}
A B C
loc
tail
head
!tr
!tr
Circular linked lists

Similar to linear lin#ed list e-ce!t


35

The last element in the list points to


the $irst element in the list

typedef struct nodeType {
int value;
struct nodeType *next;
} node;

node *head;
a ' c d
head
/!erations on circular lin#ed lists

The o!erations are similar to those o$ linear lin#ed lists

The $ollo4ing exceptions are there%


36

Insertion: when inserting new node at


the end of the list, its next pointer field
is made point to the first node

Testing the end of the list: while testing


for the end of the list, we need to
compare the next pointer field with the
address of the first node

You might also like