Linked List
Linked List
Linked List
LISTS
Array vs Linked List
node node
Array
● The link contains a pointer (an address) that identifies the next
element in the list.
• Insertion
• Deletion
• Retrieval
• Traversal
8
Declarations
● First you must declare a data structure
that will be used for the nodes.
● For example, the following struct could
be used to create a list where each node
holds a float:
struct ListNode
{
float value;
struct ListNode *next;
};
Linked List Implementation/Coding Issues in C
struct list {
int data; data next
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;
a b c
1 NULL 2 NULL 3 NULL
struct node{
int data;
struct node *next;
};
struct node *ptr;
ptr
?
?
The Free Operator in C
• Function free deallocates memory- i.e.
the memory is returned to the system so
that the memory can be reallocated in
the future.
?
free(ptr);
ptr
Insertion of a node Into
Linked Lists
The Scenario
● You have a linked list
− Perhaps empty, perhaps not
− Perhaps ordered, perhaps not
head 48 17 142 //
p=getnode()
info(p)=x
next(p)=list
list=p
Inserting to the End
head 48 17 142 // 93 //
Don’t Worry!
Algorithm- Inserting a Node
to the End
p=list
while(next(p)!=NULL)
p=next(p)
q=getnode()
info(q)=x
next(q)=NULL
next(p)=q
Inserting to the Middle
head 17 48 93
142 / //
/142
head_ptr tail_ptr
head_ptr tail_ptr
head_ptr tail_ptr
tail_ptr
head_ptr
}
x=info(p)
next(q)=NULL
freenode(p)
Delete – an inside node
List before deletion:
tail_ptr
head_ptr
head_ptr tail_ptr
top
6
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
top
6
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
7 st.push(7);
top
6
List Stack Example
Java Code
Stack st = new Stack();
8
st.push(6);
st.push(1);
7 st.push(7);
top st.push(8);
6
List Stack Example
Java Code
Stack st = new Stack();
8
st.push(6);
st.push(1);
7 st.push(7);
top st.push(8);
st.pop();
1
6
List Stack Example
Java Code
Stack st = new Stack();
st.push(6);
st.push(1);
7 st.push(7);
top st.push(8);
st.pop();
1
6
Linked Implementation as Queue
Link Link
3 data data
Link Link
data
Lin Lin
k k
data data 4
Lin Lin Lin Lin
k k k k
Types of linked lists
● Singly linked list
● Begins with a pointer to the first node
pointer
● Allows traversals both forwards and backwards
Figure A circular linked list with an external pointer to the last node
Algorithm- Inserting a Node
to the front of Circular List
p=getnode()
info(p)=x
if(list==NULL)
– next(p)=p, list=p
next(p)=next(list)
next(list)=p
Algorithm- Inserting a Node
at the end of Circular List
p=getnode()
info(p)=x
if(list==NULL)
{
next(p)=p,
list=p
}
next(p)=next(list)
next(list)=p
list=p
Doubly-Linked Lists
▪It is a way of going both directions in a
linked list, forward and reverse.
▪ Many applications require a quick access
to the predecessor node of some node in
list.
Advantages over Singly-linked Lists
Quick update operations:
such as: insertions, deletions at both ends (head and tail),
and also at the middle of the list.
A nodein a doubly-linked list storetwo
references:
A next link; that points to the next node in the list, and
A prev link; that points to the previous node in the list.
Doubly Linked List
A doubly linked list provides a natural prev next
implementation of the List ADT
Nodes implement Position and store:
• element node
• link to the previous node
• link to the next node elem
Special trailer and header nodes
header trailer
elements
Sentinel Nodes
To simplify programming, two special nodes
have been added at both ends of the doubly-
linked list.
Head and tail are dummynodes, also called
sentinels, do not store any data elements.
Head: header sentinel has a null-prev reference
(link).
Tail: trailer sentinel has a null-next
reference (link).