Lab - 03 .: Singly Linked List and Its Applications: Objectives
Lab - 03 .: Singly Linked List and Its Applications: Objectives
Lab - 03 .: Singly Linked List and Its Applications: Objectives
Objectives
Understanding of:
• Singly Linked List
• Applications of SLLs
Tools Required
a) PC with Windows 7 Professional
b) Visual Studio 2010
Linked Lists:
A linked list is a data structure that can store an indefinite number of items. These items are
connected using pointers in a sequential manner.
There are two types of linked list; singly-linked list, and doubly-linked list. In a singly-
linked list, every element contains some data and a link to the next element. On the other
hand, every node in a doubly-linked list contains some data, a link to the next node and a
link to the previous node.
The elements of a linked list are called the nodes. A node has two fields i.e. data and 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.
No matter how many nodes are present in the linked list, the very first node is called head
and the last node is called the tail. If there is just one node created, then it is called both head
and tail.
struct node
{ int
data;
node *next;
};
Now, we will write a function for the node creation. The process of creating node is very
simple. We need a pointer of a node type (which we defined) and we will insert the value in
its data field. The next field of node would be declared as NULL as it would be the last node
of linked list.
1. Linking the newly created node with tail node. Means passing the address of a new
node to the next pointer of a tail node.
2. The tail pointer should always point to the last node. So, we will make our tail pointer
equal to a new node.
}
}
Display All Nodes:
Now we have a working linked list which allows creating nodes. If we want to see that what
is placed in our linked list, then we will have to make a display function. The logic behind
this function is that we make a temporary node and pass the address of the head node to it.
Now we want to print all the nodes on the screen. So, we need a loop which runs as many
times as nodes exist. Every node contains the address of the next node so the temporary
node walks through the whole linked list. If the temporary node becomes equal to NULL,
then the loop would be terminated.
void display( )
{
node *temp=new node;
temp=head; while(temp!
=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
Basically, two operations are performed on linked lists:
• Insertion
• Deletion
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:
{ pre=cur;
cur=cur->next;
}
temp->data=value; pre-
>next=temp; temp-
>next=cur;
}
Deletion:
The process of deletion is also easy to implement. he basic structure is to declare a temporary
pointer which points the node to be deleted. Then a little bit of working on links of nodes.
There are also three cases in which a node can be deleted:
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;
}
{
node *current=new
node; node
*previous=new node;
current=head;
for(int i=1;i<pos;i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
}
TASKS
Task #.1: Estimated Time: 40 Mins.
Write C++ program to give implementation of singly linked list and should have the following
functionality.
-InsertFirst ( ? )
-InsertLast ( ? )
-InsertSpecific ( ? )
-DisplayList ( ? )
-DisplaySize ( ? )
-DeleteFirst ( ? )
-DeleteLast ( ? )
-DeleteSpecific( ? )
Write C++ program to add 5 nodes in a linklist. Now add values of first 2 nodes and subtract
values of last two nodes and then add both resultants and make its placement at the value of 3rd
Node.
14-2