Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Group: C Roll No:

Assignment No: 8 Date:

Title:
Write C++ program to implement Singly Linked List and operations on it-
a) Add and delete node
b) Compute total number of nodes
c) Traverse List
d) Display list in reverse order using recursion
e) Concatenate two lists

Marks: Sign:
Group C
Assignment No 8

Title: Implementation of singly linked list(Write)


Problem Statement(Write)
Department of Computer Engineering has student's club named 'Pinnacle Club'. Students of
second, third and final year of department can be granted membership on request. Similarly
one may cancel the membership of club. First node is reserved for president of club and last
node is reserved for secretary of club. Write C++ program to maintain club member‘s
information using singly linked list. Store student PRN and Name. Write functions to:
a) Add and delete the members as well as president or even secretary.
b) Compute total number of members of club
c) Display members
d) Two linked lists exists for two divisions. Concatenate two lists.

Theory:
Linked List is a linear data structure and it is very common data structure which consists of
group of nodes in a sequence which is divided in two parts. Each node consists of its own
data and the address of the next node and forms a chain.

fig. 1 Linked List

 Singly Linked List: Singly linked lists contain nodes which have a data part as well
as an address part i.e. next, which points to the next node in sequence of nodes. The
operations we can perform on singly linked lists are insertion, deletion and traversal.

fig. 2 Singly Linked List


 What is a NODE?
Node is a combination of DATA and LINK.
 What is Data?
Is the part where the actual data is stored.
 What is a Link?
It is the link (pointer) to the next element of the list.
o Two ways of implementing link:
- It could be either an index to an array element (array implementation) OR
- A pointer variable containing the address of the next element (pointer)
Operations on linked list
• Creation
• Insertion
• Deletion
• Search
• Traverse

o Operations on linked list:


I. Insertion: A node can be added in four ways
 At the front of the SLL
 After a given node
 At the end of the SLL
 Before a given node

 Add a node at the front: The new node is always added before the head of the given
Linked List. And newly added node becomes the new head of SLL.
The following steps are to be followed:
1. Create the new node.
newnode=new Node(data);
2. If the list is empty, assign new node as head
head=newnode;
3. If the list is not empty, follow the steps given below
newnode->next=head;
head=newnode;

 Add a node at the end: The new node is always added after the last node of the given
Linked List.
The following steps are to be followed:
1. Create the new node.
newnode=new Node(data);
2. If the list is empty, assign new node as head
head=newnode;
3. If the list is not empty, follow the steps given below
Node *temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;

 Add a node after a given node: We are given pointer to a node as pre_node, and the
new node is inserted after the given node.
The following steps are to be followed:
1. Create the new node.
newnode=new Node(data);
2. Accept position.
a. If position is not valid, display ‘Invalid Position’ message.
3. If position is valid, follow the steps given below
Node *temp=head;
for(int i=1;i<=position;i++)
{
prev=temp;
temp=temp->next;
}
prev->next=newnode;
newnode->next=temp;

 Add a node before a given node: We are given pointer to a node as prev_node, and
the new node is inserted after the given node.
The following steps are to be followed:
1. Create the new node.
newnode=new Node(data);
2. Accept position.
a. If position is not valid, display ‘Invalid Position’ message.
3. If position is valid, follow the steps given below
Node *temp=head;
for(int i=1;i<position;i++)
{
prev=temp;
temp=temp->next;
}
prev->next=newnode;
newnode->next=temp;

II. Deletion: A node can be deleted in three way


 At the front of the SLL
 At the end of the SLL
 At the mid position

 Deleting a node at the beginning of the SLL


The following steps are followed
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:
cout<<"\nNode "<<head->data<<" deleted";
temp=head->next;
delete head;
head=temp;
 Deleting a node at the end of the SLL
The following steps are followed
1. If the list is empty, display a message ‘Empty List’.
2. If the list is not empty, follow the steps given below:
temp=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
cout<<"\nNode "<<temp->data<<" deleted";
delete temp;

 Deletion of a node at mid position


The following steps are to be followed:
1. Accept position.
a. If position is not valid, display ‘Invalid Position’ message.
2. If position is valid, follow the steps given below
while(counter<position)
{
prev=temp;
counter++;
temp=temp->next;
}
prev->next=temp->next;
cout<<"\nNode "<<temp->data<<" deleted.";
delete temp;

III. Traversing SLL:


Following steps are followed, to traverse Singly Linked List.
1. If list is empty then display ‘Empty List’ message.
2. If the list is not empty, follow the steps given below
temp=head;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}

IV. Concatenation of two Linked Lists:


1. Let us assume that the two linked lists are referenced by head1 and head2
respectively.
2. If the first linked list is empty then return head2.
3. If the second linked list is empty then return head1.
4. Store the address of the starting node of the first linked list in a pointer variable, say
temp.
temp=head1;
5. Move the temp to the last node of the linked list through simple linked list traversal
technique.
while(temp->next!=NULL)
temp=temp->next;
6. Store the address of the first node of the second linked list in the next field of the node
pointed by temp. Return head1.
temp->next=head2;

Conclusion(write)

Questions: (write)

1. Write C/C++ pseudo code to merge two singly linked list.

2. List and Explain types of linked list with example.

3. Write C/C++ psuedo code to search a node in Linked list.

4. Write C/C++ code to traverse list.

5. Write applications of linked list.

6. Write difference between Array and Linked List.

You might also like