Professional Documents
Culture Documents
Submitted by:
Muhamad Humaish Reg# (18-EE-029)
Program:
#include<iostream>
#include<cstdlib>//The free function is defined in <cstdlib> header file.
using namespace std;
struct Node
{
int data;
struct Node* next;
}*head;
{
struct Node* Temp, * X;
Temp = new(struct Node);
if (Temp == NULL)
{
cout << "Empty Linked List" << endl;
return 0;
}
else
{
Temp->data = value;
Temp->next = NULL;
return Temp;
}
}
void InsertStart()
{
int value;
cout << " Enter the value" << endl;
cin >> value;
struct Node* Temp, * Y;
Temp = creatingnode(value);
if (head == NULL)
{
head = Temp;
head->next = NULL;
}
else
{
Y = head;
head = Temp;
head->next = Y;
}
cout << value << " Inserted at beginning" << endl;
}
void Insertlast()
{
int value;
cout << "Enter the value to be inserted: " << endl;
cin >> value;
struct Node* Temp, * X;
Temp = creatingnode(value);
X = head;
while (X->next != NULL)
{
X = X->next;
}
Temp->next = NULL;
X->next = Temp;
cout << value << " Inserted at Last" << endl;
}
void Insertatposition()
{
int value, pos, counter = 0;
cout << "Enter the value to be inserted: ";
cin >> value;
struct Node* Temp, * X;
struct Node* tail;
tail = NULL;
Temp = creatingnode(value);
cout << "Enter the postion at which node to be inserted: ";
cin >> pos;
int i;
X = head;
while (X != NULL)
{
X = X->next;
counter++;
}
if (pos == 1)
{
if (head == NULL)
{
head = Temp;
head->next = NULL;
}
else
{
tail = head;
head = Temp;
head->next = tail;
}
}
else if (pos > 1 && pos <= counter)
{
X = head;
for (i = 1; i < pos; i++)
{
tail = X;
X = X->next;
}
tail->next = Temp;
Temp->next = X;
}
else
{
cout << "Please enter a Valid Position";
}
}
void Deleteposition()
{
int Pos, I;
int counter = 0;
if (head == NULL)
{
cout << "Linked list Is empty" << endl;
return;
}
cout << "Enter the Position Of the Value To Be Deleted: " << endl;
cin >> Pos;
struct Node* X;
struct Node* tail;
tail = NULL;
X = head;
if (Pos == 1)
{
head = X->next;
}
else
{
while (X != NULL)
{
X = X->next;
counter++;
}
if (Pos > 0 && Pos <= counter)
{
X = head;
for (I = 1; I < Pos; I++)
{
tail = X;
X = X->next;
}
tail->next = X->next;
}
else
{
cout << " Please Enter A Valid Position" << endl;
}
free(X);//The free() function in C++ deallocates a block of
//memory previously allocated.
void Display()
{
struct Node* Temp;
if (head == NULL)
{
cout << "The List is Empty" << endl;
return;
}
Temp = head;
cout << "Values In The Linked List Are:" << endl;
while (Temp != NULL)
{
cout << Temp->data << "->";
Temp = Temp->next;
}
cout << "Null" << endl;
}
int main()
{
int Choice;
head = NULL;
while (1)
{
cout << "1.Add a Node at the beginning of the list" << endl;
cout << "2.Add a Node at the Last of the list" << endl;
cout << "3.Add a Node at a particular place in the list" << endl;
cout << "4.Deleting a Node in the list" << endl;
cout << "5.Display the Single link list" << endl;
cout << "6.Exit Program " << endl;
cout << "Please Select an Option : " << endl;
cin >> Choice;
switch (Choice)
{
case 1:
cout << "Inserting Node at Beginning: " << endl;
InsertStart();
cout << endl;
break;
case 2:
cout << "Inserting Node at Last: " << endl;
Insertlast();
cout << endl;
break;
case 3:
cout << "Inserting Node at a given position:" << endl;
Insertatposition();
cout << endl;
break;
case 4:
cout << "Delete a particular node: " << endl;
Deleteposition();
break;
case 5:
cout << "Display elements of link list" << endl;
Display();
cout << endl;
break;
case 6:
cout << "Exiting..." << endl;
exit(1);
break;
default:
cout << "Wrong choice" << endl;
}
} return 0;
}