Professional Documents
Culture Documents
Group C Assign 19
Group C Assign 19
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
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.
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.
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;
Conclusion(write)
Questions: (write)