Linked List
Linked List
Linked List
List Overview
Linked lists
Abstract data type (ADT)
Basic operations of linked lists
Insert, find, delete, print, etc.
Variations of linked lists
Circular linked lists
Doubly linked lists
Linked Lists / Slide 3
List Operations
Useful operations
createList(): create a new list (presumably empty)
copy(): set one list to be a copy of another
clear(); clear a list (remove all elements)
insert(X, ?): Insert element X at a particular position
in the list
remove(?): Remove element at some position in
the list
get(?): Get element at a given position
update(X, ?): replace the element at a given position
with X
find(X): determine if the element X is in the list
length(): return the length of the list.
Linked Lists / Slide 4
List Operations
We need to decide what is meant by “particular
position”; we have used “?” for this.
List Operations
If we use the “current” marker, the following
four methods would be useful:
Implementing Lists
We have designed the interface for the List; we
now must consider how to implement that
interface.
Implementing Lists using an array: for example,
the list of integers (2, 6, 8, 7, 1) could be
represented as:
current size
A 2 6 8 7 1
3 5
1 2 3 4 5
Linked Lists / Slide 7
List Implementation
insert(9,3); current position is 3. The new list would
thus be: (2, 6, 8, 9, 7, 1)
We will need to shift everything to the right of 8 one
place to the right to make place for the new element ‘9’.
current size
step 1: A 2 6 8 7 1
3 5
1 2 3 4 5 6
Implementing Lists
next():
current size
A 2 6 8 9 7 1
4 6
1 2 3 4 5 6 5
Linked Lists / Slide 9
Implementing Lists
Implementing Lists
remove(): removes the element at the current
index
current size
Step 1: A 2 6 8 9 1
5 6
1 2 3 4 5 6
5
current size
Step 2: A 2 6 8 9 1
5 5
1 2 3 4 5
Implementing Lists
find(X): traverse the array until X is located.
int find(int X)
{
int j;
for(j=1; j <= size; j++ )
if( A[j] == X )
break;
Implementing Lists
Other operations:
find
Worst-case: may have to search the entire array
Average-case: search at most half the array.
Linked List
A linked list is a data structure that can store an indefinite amount of
items.
These items are connected using pointers in a sequential manner.
There are two types of linked list:
1. Singly-linked list
2. Doubly-linked list
Linked Lists / Slide 17
Nodes
The elements of a linked list are called
the nodes.
A node has two fields
• Data
• Next
• The data field contains the data being
stored in that specific node. It cannot just
be a single variable. There may be many
variables presenting the data section of a
node. The next field contains the address
of the next node. So this is the place
where the link between nodes is
established.
Linked Lists / Slide 18
class list
{
Private:
node *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
};
Linked Lists / Slide 21
Node creation
Need a pointer of a node type
Insert the value in its data field
Next field of node would be declared as NULL
If the linked list is still empty
It means if the head is equal to NULL then we
can conclude that the linked list is empty.
If there is just one node in linked lists, then it
is called both head and tail.
Linked Lists / Slide 22
void display()
{
node *temp=new node;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
Linked Lists / Slide 25
Insertion
Deletion
Linked Lists / Slide 26
INSERTION
Inserting a new node in the linked list is called
insertion.
A new node is created and inserted in the linked
list.
There are three cases considered while inserting a
node:
Insertion at the start
Insertion at the end
Insertion at a particular position
Linked Lists / Slide 27
New_node next=head;
Head=new_node;
Linked Lists / Slide 28
While(P!=insert _position)
{
P=P next;
}
Store_next=p next;
P next=new_node;
New_node next=store_next;
Linked Lists / Slide 31
While(P next!=null)
{
P=P next;
}
P next=new_node;
New_node next=null;
Linked Lists / Slide 34
DELETION
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
Linked Lists / Slide 38
Our algorithm should be capable of finding a node that comes before the last node.
This can be achieved by traversing the linked list. We would make two temporary
pointers and let them move through the whole linked list. At the end, the previous
node will point to the second to the last node and the current node will point to the
last node, i.e. node to be deleted. We would delete this node and make the previous
node as the tail.
Linked Lists / Slide 39
void delete_last()
{
node *current=new node;
node *previous=new node;
current=head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
Linked Lists / Slide 40
Deletion at a Particular
Position
In linked list, we can delete a specific node. The process
of deletion is simple. Here we don’t use the head and tail
nodes. We ask the user to input the position of the node
to be deleted. After that, we just move two temporary
pointers through the linked list until we reach our specific
node. Now, we delete our current node and pass the
address of the node after it to the previous pointer. This
way, the current node is removed from the linked list and
the link is established between its previous and next
node.
Linked Lists / Slide 41
1. Starting node
2. Ending node
3. Internal node
Linked Lists / Slide 43
else
{
q next=p next; // internal node
delete(p);
}
}
return head;
}
Linked Lists / Slide 45
For example:
Input:14,21,11,30,10
If key to be search is 15 then the function should
return false.
If key to be search is 14, then the function should
return true.
Linked Lists / Slide 48
Iterative Algorithm
C++ implementation of
iterative function
\* check whether the value x is present in linked list */
bool search(struct node* head, int x)
{
struct node* current=head; //initialize current
while(current!=NULL)
{
if(current key==x)
return true;
current=current next;
}
return false;
}
Linked Lists / Slide 51
Recursive Algorithm
bool search(head, x)
1.If head is NULL, return false
2.If head’s key is same as x, return true
3.Else return search(head next,x)
Linked Lists / Slide 52
Linked Lists / Slide 53
Linked Lists / Slide 54
cnt=0;
while(p!=null)
{
cnt=cnt+1;
p=p next;
}
Linked Lists / Slide 56
Linked List
Picture of our list (2, 6, 7, 8, 1) stored as a
linked list:
head
2 6 8 7 1 size=5
current
Linked Lists / Slide 59
Linked List
Note some features of the list:
Need a head to point to the first node of the list.
Otherwise we won’t know where the start of the
list is.
The current here is a pointer, not an index.
The next field in the last node points to nothing.
We will place the memory address NULL which
is guaranteed to be inaccessible.
Linked Lists / Slide 60
Linked List
Actual picture in memory:
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
Linked Lists / Slide 61
Doubly-linked List
Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
To move back one node, we have to start at
the head of the singly-linked list and move
forward until the node before the current.
To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:
64 AL
Linked Lists / Slide 65
Doubly-linked List
Need to be more careful when adding or
removing a node.
Consider add: the order in which pointers are
reorganized is important:
head 2 6 8 7 1 size=5
current
Linked Lists / Slide 66
In the next step, a programmer points the prevNode of the newNode to the node with
value 6.
newNode->setprev( current );
66 AL
Linked Lists / Slide 67
In the fourth step, the nextNode of the node with value 6 is pointing to the newNode i.e.
the node with value 9. Point the current to the newNode and add one to the size of the list
current->setNext( newNode );
current = newNode;
size++;
Now the
67 newNode has been inserted between
AL node with value 6 and node with value 8.
Linked Lists / Slide 68
Doubly-linked List
1. newNode->setNext( current->getNext() );
current
head 2 6 8 7 1 size=5
newNode 9 1
Linked Lists / Slide 69
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
current
head 2 6 8 7 1 size=5
newNode 9 1
Linked Lists / Slide 70
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
current
head 2 6 8 7 1 size=5
2 3
newNode 9 1
Linked Lists / Slide 71
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );
current
head 2 6 8 7 1 size=5
2 4 3
newNode 9 1
Linked Lists / Slide 72
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );
5. current = newNode;
6. size++;
head 2 6 8 7 1 size=6
2 4 3
newNode 9 1
current
Linked Lists / Slide 73
new_node next=head;
head prev=new_node;
new_node prev=null;
head=new_node;
A B C
Linked Lists / Slide 75
while(p next!=null)
{
p=p next;
}
p next-new_node;
new_node prev=p;
new_node next=null;
Linked Lists / Slide 77
Linked Lists / Slide 78
While(p next!=null)
{
p=p next;
}
store_prev=p prev;
store_prev next=null;
delete(p);
Linked Lists / Slide 81
Linked Lists / Slide 82
Circularly-linked lists
The next field in the last node in a singly-linked
list is set to NULL.
Moving along a singly-linked list has to be done
in a watchful manner.
Doubly-linked lists have two NULL pointers:
prev in the first node and next in the last node.
A way around this potential hazard is to link the
last node with the first node in the list to create
a circularly-linked list.
Linked Lists / Slide 85
Assignment#01
Create a Doubly linked lists and perform
following operations on list
1.Create A New Doubly Linked List
2.Display the Doubly Linked List
3.Add to an Empty Doubly Linked List
4.Add at Starting of the Doubly Linked List
5.Add at Ending
6.Add After a Node
7.Add Before a Node
8.Delete a Node
9.Reverse the Doubly Linked List
Quiz#01
Problem Statement:
87 AL
Linked Lists / Slide 88
head 2 6 8 7 1 size=5
current
6
8
size=5
head 2
1
Linked Lists / Slide 89
Josephus Problem
A case where circularly linked list comes in
handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like
to choose a leader.
The way they decide is that all 10 sit in a circle.
They start a count with person 1 and go in
clockwise direction and skip 3. Person 4
reached is eliminated.
The count starts with the fifth and the next
person to go is the fourth in count.
Eventually, a single person remains.
Linked Lists / Slide 90
Josephus Problem
N=10, M=3
4
3 5
2 6
1
7
10
8
9
Linked Lists / Slide 91
Josephus Problem
N=10, M=3
eliminated
4
3 5
2 6
1
7
10
8
9
Linked Lists / Slide 92
Josephus Problem
N=10, M=3
eliminated
4
3 5
8
2 6
1
7
10
9
Linked Lists / Slide 93
Josephus Problem
N=10, M=3
eliminated
4
3 5
8
6
2
1
7
10
9
Linked Lists / Slide 94
Josephus Problem
N=10, M=3
eliminated
4
3 5
8
6
2
1 7
10
9
Linked Lists / Slide 95
Josephus Problem
N=10, M=3
eliminated
4
5
8
6
2
1 7
3
10
9
Linked Lists / Slide 96
Josephus Problem
N=10, M=3
eliminated
4
5
8
6
2
1 7
10
9
Linked Lists / Slide 97
Josephus Problem
N=10, M=3
eliminated
4
5
8
6
2
1 7
10
9
Linked Lists / Slide 98
Josephus Problem
N=10, M=3
eliminated
4
5
8
6
2
10
1
Linked Lists / Slide 99
Josephus Problem
N=10, M=3
eliminated
4
5
8
10
6
Linked Lists / Slide 100
Josephus Problem
#include "CList.cpp"
void main(int argc, char *argv[])
{
CList list;
int i, N=10, M=3;
for(i=1; i <= N; i++ ) list.add(i);
list.start();
while( list.length() > 1 ) {
for(i=1; i <= M; i++ ) list.next();
cout << "remove: " << list.get() << endl;
list.remove();
}
cout << "leader is: " << list.get() << endl;
}
Linked Lists / Slide 101
Josephus Problem
Using a circularly-linked list made the solution
trivial.
The solution would have been more difficult if
an array had been used.
This illustrates the fact that the choice of the
appropriate data structures can significantly
simplify an algorithm. It can make the algorithm
much faster and efficient.
Linked Lists / Slide 102
A B C
Head
A B C
Head
Linked Lists / Slide 104