In: Computer Science
CODE:
#include <iostream>
using namespace std;
struct node
{
int num;
node *nextptr;
} * head, *curnode;
node *newnode;
void addlist(int n);
void displayList(node *head);
void FindElement(int n);
void add_head();
void add_end();
void add_before();
void delete_head();
void delete_end();
void delete_value();
int main()
{
head = NULL;
curnode = NULL;
int n;
cout << "single Linked List : Add elements in a single linked list :" << endl;
cout << "-----------------------------------------------------------" << endl;
cout << " \n\nInput the number of nodes : ";
cin >> n;
addlist(n);
displayList(head);
FindElement(n);
add_head();
displayList(head);
add_end();
displayList(head);
add_before();
displayList(head);
delete_head();
displayList(head);
delete_end();
displayList(head);
delete_value();
displayList(head);
return 0;
}
void addlist(int n)
{
int num, i;
if (n >= 1)
{ // create the head then the other nodes
head = new node;
cout << " Enter data for node 1 : ";
cin >> num;
head->num = num;
head->nextptr = NULL;
curnode = head;
for (i = 2; i <= n; i++)
{
newnode = new node;
cout << " Enter data for node " << i << " :";
cin >> num;
newnode->num = num;
newnode->nextptr = NULL;
// next address of new node set as NULL
curnode->nextptr = newnode;
// previous node is linking with new node
curnode = newnode;
// previous node is advanced
}
}
}
void displayList(node *head)
{
cout << "\n\n";
node *cur;
int n = 1;
if (head == NULL)
{
cout << " No data found in the List yet." << endl;
}
else
{
cur = head;
cout << "Data entered in the list are :" << endl;
do
{
cout << " Data :" << n << " : " << cur->num << endl;
cur = cur->nextptr;
n++;
} while (cur != NULL);
}
}
void FindElement(int n)
{
cout << "\n\n";
node *curnode = NULL;
int ctr = 1;
int FindElem;
curnode = head;
cout << " Input the element you want to find : " << endl;
cin >> FindElem;
while (curnode != NULL)
{
if (curnode->num == FindElem)
break;
else
ctr++;
curnode = curnode->nextptr;
if (ctr == n + 1)
break;
}
if (ctr <= n)
cout << " Element found at node " << ctr << endl;
else
cout << " This element does not exist in linked list." << endl;
}
void add_head()
{
cout << "\n\n";
int num, i;
newnode = new node; //start creation of the new node
cout << " Enter data for node to add in the head : ";
cin >> num;
newnode->num = num;
newnode->nextptr = NULL; //End creation of the new node
if (head == NULL)
head = newnode; // if emplty list the new node will be the head
else
{
newnode->nextptr = head;
head = newnode;
}
}
void add_end()
{
cout << "\n\n";
int num, i;
newnode = new node; //start creation of the new node
cout << " Enter data for node to add in the End : ";
cin >> num;
newnode->num = num;
newnode->nextptr = NULL; //end creation of the new node
if (head == NULL)
head = newnode; // if emplty list the new node will be the head
else
{ // go to the end of the list
curnode = head;
while (curnode->nextptr != NULL)
{
curnode = curnode->nextptr;
}
curnode->nextptr = newnode;
}
}
void add_before()
{
cout << "\n\n";
int num, m, find = 0;
node *prevnode;
newnode = new node; //start creation of the new node
cout << " Enter data for node to add : ";
cin >> num;
newnode->num = num;
newnode->nextptr = NULL; //end creation of the new node
cout << "enter the number to add before it: ";
cin >> m;
if (head->num == m) // if we will add before head
{
newnode->nextptr = head;
head = newnode;
}
else
{
curnode = head;
while (curnode != NULL)
{
if (curnode->num == m)
{
find = 1;
break;
}
else
{
prevnode = curnode;
curnode = curnode->nextptr;
}
}
if (find == 0)
cout << " Element not found " << endl;
else
{
prevnode->nextptr = newnode;
newnode->nextptr = curnode;
}
}
}
void delete_head(){
cout<<"\n\nDeleting the head!";
node *temp = head;
head = head->nextptr;
free(temp);
}
void delete_end(){
cout << "\n\nDeleting the end!";
node *temp = head;
while(temp->nextptr->nextptr!=NULL){
temp = temp->nextptr;
}
temp->nextptr = NULL;
}
void delete_value(){
cout << "\n\nDeleting the value!";
int m, find = 0;
node *prevnode;
newnode = new node; //start creation of the new node
cout << " \nEnter data for node to delete : ";
cin >> m;
if (head->num == m) // if we will add before head
{
head = head->nextptr;
}
else
{
prevnode = head;
curnode = head->nextptr;
while (curnode->num != m)
{
prevnode = curnode;
curnode = curnode->nextptr;
}
prevnode->nextptr = curnode->nextptr;
}
}
OUTPUT: