CH6 - Linked List
CH6 - Linked List
Algorithm
Chapter Five
Linked List
Review of Pointers and Structures
int *ptr;
• The expression, *ptr, denotes the memory cell to which ptr point
to.
cout<< timeObject.hour; or
cout<<timeptr->hour;
• This is the only case in C++ where you are allowed to refer to a data
type (in this case list) before you have even finished defining it!
• Arrays are simple but we must specify their size at construction time.
This has its own drawbacks.
• If you construct an array with space for n, tomorrow you may need
n+1. Here comes a need for a more flexible system.
• This implies that one need not know the size of the list in advance.
Memory is also efficiently utilized.
Cont.
Array Linked Lists
Physically Contiguous Logically Contiguous Only
Fixed Length Changeable Length
Direct access Sequential Access
Cost of access =O(1) Cost of access =O(n)
Memory utilization= not efficient Memory utilization= efficient
Memory requirement=less Memory requirement=high
Linear and binary search=O(logN) Linear and binary search=O(N)
Types of Linked Lists
• Singly Linked List: In a singly linked list, each node has only one
pointer, commonly called “next” since it points to the next node in
the sequence. The first node is called the “head” of the list. The last
node in the list points to NULL.
• A doubly linked list: has two pointer variables, one pointing forward
to the next node and one pointing back to the previous node.
Singly Linked Lists
Creating Linked Lists in C++
• This linked list has four nodes in it, each with a link to the next
node in the series.
• The last node has a link to the special value NULL, which any
pointer (whatever its type) can point to, to show that it is the
last link in the chain.
• The first problem that we face is how to add a node to the list.
if (start_ptr == NULL)
start_ptr = temp;
Cont.
• It is harder if there are already nodes in the list. In this case, the secret
• The loop will terminate when temp2 points to the last node in the
chain, and it knows when this happens because the next pointer in
that node will point to NULL. When it has found it, it sets the pointer
from that last node to point to the node we have just declared:
Cont.
temp2->next = temp;
• The link temp2->nxt in this diagram is the link joining the last two
nodes.
• The full code for adding a node at the end of the list is shown below,
in its own function:
void add_node_at_end () {
node *temp, *temp2; // Temporary pointers
temp = new node; // Reserve space for new node and fill it with data
cout << "Please enter the name of the person: ";
cin >> temp->name;
cout << "Please enter the age of the person : ";
cin >> temp->age;
cout << "Please enter the height of the person : ";
cin >> temp->height;
temp->nxt = NULL; // Set up link to this
node
if (start_ptr == NULL)
start_ptr = temp;
else {
temp2 = start_ptr; != NULL) {
while (temp2->nxt // We
know this is not NULL - list not empty!
temp2 = temp2->nxt; // Move to next link in chain
}
temp2->nxt = temp;
}
}
Adding at some position
• ?????????????????
Displaying the list of nodes
• Having added one or more nodes, we need to display the list of nodes
on the screen. This is comparatively easy to do. Here is the method:
4. Make the temporary pointer point to the same thing as the next
pointer of the node it is currently indicating.
Cont.
• The temporary pointer moves along the list, displaying the details
of the nodes it comes across. At each stage, it can get hold of the next
node in the list by using the next pointer of the node it is currently
pointing to. Here is the C++ code that does the job:
temp = start_ptr;
do {
if (temp ==
NULL)
cout <<
"End of
list" <<
endl;
else { // Display details for what temp
points to cout << "Name : " << temp->name <<
endl;
cout << "Age : " << temp->age << endl;
Navigating through the List
• One thing you may need to do is to navigate through the list, with a pointer
that moves backwards and forwards through the list, like an index pointer in
an array. This is certainly necessary when you want to insert or delete a node
from somewhere inside the list, as you will need to specify the position.
• We will call the mobile pointer current. First of all, it is declared, and set to
the same value as the start_ptr pointer:
node *current;
current = start_ptr;
• Notice that you don't need to set current equal to the address of the start
pointer, as they are both pointers. The statement above makes them both
point to the same thing:
Cont.
• It's easy to get the current pointer to point to the next node in the list
(i.e. move from left to right along the list).
current = current->next;
• In fact, we had better check that it isn't pointing to the last item in the
list. If it is, then there is no next node to move to:
if (current->next == NULL)
cout << "You are at the end of the list." << endl;
else
current = current->next;
Cont.
• Moving the current pointer back one step is a little harder. This is
because we have no way of moving back a step automatically from
the current node.
• The only way to find the node before the current one is to start at the
beginning, work our way through and stop when we find the node
before the one we are considering at the moment.
• We can tell when this happens, as the next pointer from that node
will point to exactly the same place in memory as the current pointer
(i.e. the current node).
Cont.
Previous Current
Start
Stop here
NULL
• First of all, we had better check to see if the current node is also not the
• If not, check through all the nodes in turn until we detect that we are just
• we can alter the details for that particular node in the list:
cout << "Please enter the new name of the person: ";
cin >> current->name;
cout << "Please enter the new age of the person : ";
cin >> current->age;
cout << "Please enter the new height of the person :
";
cin >> current->height;
Deleting a node from the List
• When it comes to deleting nodes, we have three choices: Delete a
node from the start of the list, delete one from the end of the list, or
delete one from somewhere in the middle.
• When a node is deleted, the space that it took up should be reclaimed.
Otherwise the computer will eventually run out of memory space. This is
done with the delete instruction:
delete temp; // Release the memory pointed to by temp
• However, we can't just delete the nodes directly as it would break the
chain. We need to reassign the pointers and then delete the node at the
last moment.
Deleting the First node in the List
• Here is how we go about deleting the first node in the linked list:
start_ptr
NULL
temp 2
temp1
• First, you will need to check weather the list is empty or not.
• Second, if the list has only one node, you need to see if that
node is the node you want to delete.
• Third, you will need to navigate through the list until you find
the node you want to delete.
• Fourth, you will need to assign the next pointer of the
previous node to the node after the one you want to delete.
• Finally you delete the node.
EX
•You will notice that each node in the list has two pointers, one to
the next node and one to the previous one - again, the ends of the
list are defined by NULL pointers.
Cont.
• With the doubly linked list, we can move the current pointer
backwards and forwards at will.
Creating Doubly Linked Lists
• The nodes for a doubly linked list would be defined as follows:
struct node{
char name[20];
node *nxt; // Pointer to next node
node // Pointer to previous node
*prv;
};node *current;
node *head; // Pointer to tag the first
node node *tail; // Pointer to tag the last
node
current = new node;
strcpy(current->name, "Fred“);
head= current;
tail = current;
current->nxt = NULL;
Cont.
• We have also included some code to declare the first node and set its
pointers to NULL. It gives the following situation:
void add_node_at_start () {
// Declare a temporary pointer and move it to current
node *temp = current;
if(temp==NULL){
node *temp1; temp1= new
node;
cout<<"enter name"<<endl;
cin>>temp1->name ;
current=temp1;
current->nxt = NULL; current-
>prv = NULL; head=
current;
tail = current;
}
else{
• After that, a new node is declared, and the name is accepted . The nxt
pointer of this new node is set to NULL to indicate that this node will be
the new end of the list. The prv pointer of the new node is linked into the
last node of the existing list. The nxt pointer of the current end of the list
is set to the new node.
Contd.
• Adding a node at the end of a doubly linked list
void add_node_at_end () {
// Declare a temporary pointer and move it to the
end
node *temp = current;
if(temp==NULL)
{
node *temp1;
temp1= new node;
cout<<"enter name"<<endl;
cin>>temp1->name ;
current=temp1;
current->nxt = NULL;
current->prv =
NULL; head=
current;
tail = current;
else
{
while (temp->nxt != NULL)
temp = temp->nxt; // Declare a new node
and link it in node *temp2;
temp2 = new node;
cout<<"enter name"<<endl;
cin>>temp2->name ; // Store the new name in the node
temp2->nxt = NULL; // This is the new start of the list
temp2->prv = temp; // Links to current list
temp->nxt = temp2;
tail= temp2; //make tail point to the
last node
}
}
Displaying a Doubly Linked list
Displaying a doubly linked list forward
• Elements of a doubly linked list are displayed forward using the nxt
pointer to traverse forward and the head pointer to tag the first node.
while(temp!=NULL)
{
cout<<temp->name<< " ";
temp=temp->nxt;
}
cout<<endl;
}}
Contd.
Displaying a doubly linked list Backward
• Elements of a doubly linked list are displayed backward using the prv
pointer to traverse backward and the tail pointer to tag the last
node.
while(temp!
=NULL)
{
cout<<temp-
>name<< " ";
Deleting a Node from a Doubly Linked list
Deleting a node from the start of a doubly linked list
• First you should check if the link is empty or not. Also if the link
has one or more than one element.
void delete_node_at_start ( ) {
// Declare a temporary pointer and move it to current
node *temp1 = current;
node *temp2;
if( temp1==NULL){
cout<<"empity list"<<endl;
}
else
{
if(temp1->prv == NULL && temp1->nxt == NULL )
{
current= NULL;
head= NULL;
tail=NULL;
delete temp1;
}
else{
while (temp1->prv != NULL)
{ temp2 = temp1;
temp1 = temp1->prv; }
delete temp1;
temp2->prv = NULL;
head = temp2;
//make head point to the first
node
}
Cont.
Deleting a node from the end of a doubly linked list
void delete_node_at_end ( ) {
// Declare a temporary pointer and move it to current
node *temp1 = current;
node *temp2;
if( temp1==NULL){
c
out<
<"e
mpit
y
list"
<<en
else
{
if(temp1->prv == NULL && temp1->nxt ==
NULL)
{
current= NULL;
head= NULL;
tail=NULL;
delete temp1;
}
else{
while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt; }
delete temp1;
temp2->nxt = NULL;
tail = temp2;
//make tail point to the last
node
Cont.
Deleting a node from the middle of a doubly linked list
void delete_middle_node() {
node *temp1, *temp2, temp3;
const int SIZE = 20;
char n[SIZE];
if (temp1 == NULL)
cout <<
"The list
is empty!"
<< endl;
else
{
cout << "Enter Name" << endl;
cin.ignore();
cin.getline(n, SIZE);
temp1 = head;
if (temp1->prv == NULL &&
temp1->nxt == NULL)
{
if(strcmp(t
emp1- }
else
{