Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab - 03 .: Singly Linked List and Its Applications: Objectives

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Lab Manual Data Structures & Algorithms| CSL - 221

Lab - 03 . : Singly Linked List and its Applications

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.

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

Implementation of Singly Linked Lists:


As linked list consists of nodes, we need to declare a structure which defines a single node.
Our structure should have at least one variable for data section and a pointer for the next
node. In C++, our code would look like this:

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.

Create a New Node:


The creation of a new node at the end of linked list has two steps:

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.

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

C++ code for creation of a new node would be as follows:

void create_node(int value)


{
node *temp=new node;
temp->data=value; temp-
>next=NULL; if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;

}
}
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.

The code for displaying nodes of linked list is given below:

void display( )
{
node *temp=new node;
temp=head; while(temp!
=NULL)
{

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

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:

1. Insertion at the start


2. Insertion at the end
3. Insertion at a particular position

The diagrammatic demonstration of insertion at start is given below:

The code for this segment is as below:

void insert_start(int value)


{
node *temp=new node;
temp->data=value; temp-
>next=head; head=temp;

The programmatic demonstration of insertion at particular position is given below:


void insert_position(int pos, int value)
{
node *pre=new node;
node *cur=new node;
node *temp=new node;
cur=head;
for(int i=1;i<pos;i++)

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

{ 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:

1. Deletion at the start


2. Deletion at the end
3. Deletion at a particular position

The diagrammatic demonstration of deletion at start is given below:

The code for this segment is as below:


void
delete_first()
{
node *temp=new
node; temp=head;
head=head->next;
delete temp;
}

The diagrammatic demonstration of deletion at last is given below:

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

The code for this segment is as below:

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;
}

The diagrammatic demonstration of deletion at specific position is given below:

The code for this segment is as below:

void delete_position(int pos)

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

{
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( ? )

Task #.2: Estimated Time: 30 Mins.


Write C++ program to delete each alternate node of a singly linked list.

For Example: 1->2->3->4->5->6->7->8->NULL


should be restructured to 1->3->5-
>7->NULL

Task #.3: Estimated Time: 30 Mins.

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.

Department of Computer Sciences, BULC Lab


Lab Manual Data Structures & Algorithms| CSL - 221

14-2

Department of Computer Sciences, BULC Lab

You might also like