Sie sind auf Seite 1von 8

Saksham Sharma

10914802719

Experiment No.5
Operations on a linked list

Problem: Program to perform traversal , insertion and


deletion operations on a linked list.

PROGRAM:
#include<iostream>
using namespace std;
struct Node //Declaring the structure of the Node
{
int data;
Node* next;
};
void insert (Node*& head,int key,int pos)
{
//Function to perform insertion on a linked list
Node* t = new Node;
t->data = key;
t->next = NULL;
if (head == NULL)//This means that the list is
empty
Saksham Sharma
10914802719

{
head = t;
return;
}
Node* p = head;
if (pos == 1)
//if position is 1 this means that we have to
insert at the beginning of the list
{
t->next = head;
head = t;
return;
}
pos -= 2;
while (pos--)
{
p = p->next;
}
t->next = p->next;
p->next = t;
}
void Display(Node* head)
//This function is for traversing and displaying
all the elements in the linked list
Saksham Sharma
10914802719

{
cout<<"Linked list is: ";
while (head)
{
cout << head->data << " ";
head = head->next;
}
}
void Delete(Node*& head,int pos)
//This function deletes the node at a particular
position
{
Node* p = head;
//We will point this pointer p on the node to
be deleted
Node* tail = head;
//Tail should point to the node just before the
node that is to be deleted
pos--;
while (pos--)
{
tail = p;
p = p->next;
}
Saksham Sharma
10914802719

if (p == head)
{
head = head->next;
delete p;
return;
}
tail->next = p->next;
p->next = NULL;
delete p;
//this will deallocate the memory of the node on
which p is pointing at
}
int main()
{
int key, pos,oper;
cout<< "1.Insert\n2.Traverse/Display\n3.Delete\n";
cout << "Enter the number of operations ";
cin >> oper;
int choice;
Node* head = NULL;
while (oper--)
{ cout << "Enter the choice ";
cin >> choice;
if (choice == 1)
Saksham Sharma
10914802719

{
cout << "Enter the position and the key
";
cin >> pos >> key;
insert(head, key, pos);
cout << endl;
continue;
}
if (choice == 2)
{
Display(head);
cout << endl;
}
else
{
cout << "Enter the position of the element
to delete ";
cin >> pos;
Delete(head,pos);
cout << endl;
}
}
return 0;
}
Saksham Sharma
10914802719

OUTPUT
Saksham Sharma
10914802719

VIVA QUESTIONS

Q1. What is singly linked list ?


Ans. Singly linked lists contain nodes which have a data field as
well as 'next' field, which points to the next node in line of
nodes. We can traverse a singly linked list only in a single
direction.

Q2. What is the disadvantage of a singly linked


list?
Ans. 1) It requires more space as pointers  are also stored  with
information.
2) Different amount of time is required to access each element.
3) If we have to go to a particular element then we have to go
through all those elements that come before that element.
4) we cannot traverse it from last & only from the beginning.
5) It is not easy to sort the elements stored in the linear linked
list.

Q3. What is the complexity of searching a


particular element in a singly linked list ?
Ans. Searching for an element in a singly linked list takes linear
time i.e. O(n)

Q4. Why do we use linked lists ?


Ans.Linked lists are often used because of their efficient
insertion and deletion. They can be used to implement stacks,
queues, and other abstract data types.

Q5. Why are linked lists better than arrays ?


Ans. One advantage of the linked list is that elements can be added
to it indefinitely, while an array will eventually get filled or
have to be resized (a costly operation that isn't always possible).
Elements are also easily removed from a linked list whereas removing
elements from an array leaves empty spaces that are a waste of
computer memory.
Saksham Sharma
10914802719

Q6. What are the operations of a singly linked


list?
Ans. Singly linked lists contain nodes which have a data field as
well as 'next' field, which points to the next node in line of
nodes. Operations that can be performed on singly linked lists
include insertion, deletion and traversal.

Q7. What is the costly operation in a singly linked


list?
Ans. Reversing a linked list may require extra space if we use an
auxiliary array and copy paste the elements back after reversing.
And it requires the use of extra pointers if we interchange the links of
the nodes.
Also searching for the element whose position is known also takes linear
time which may be costly sometimes.

Das könnte Ihnen auch gefallen