0% found this document useful (0 votes)
14 views10 pages

Linked List Deletion Insertion

This C++ program defines a Node class to create a linked list. It includes functions to insert nodes at the beginning, after a given node, and at the end of the list. It also includes functions to delete nodes from the beginning, after a given node, and from the end of the list. The main function tests these functions by creating a sample linked list and calling the various insertion and deletion functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Linked List Deletion Insertion

This C++ program defines a Node class to create a linked list. It includes functions to insert nodes at the beginning, after a given node, and at the end of the list. It also includes functions to delete nodes from the beginning, after a given node, and from the end of the list. The main function tests these functions by creating a sample linked list and calling the various insertion and deletion functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

#include <iostream>

#include <cstdlib>
using namespace std;
class Node{
public:
int val;
Node* next;
Node(int item)
{
val = item;
next = NULL;
}
};
void insertAtFirst(int
item);//Complete
void insertAfterNode(int item, Node*
LOC); //complete
void insertAtLast(int item); //
complete
void delAtFirst(); // complete
void delAfterNode(Node* LOC);
//complete
void delAtLast(); // complete
void display(); //Complete
Node* head;
int main()
{
Node* one = new Node(11);
head = one;
Node* LOC = one;
insertAtFirst(12);
insertAtFirst(13);
insertAfterNode(67, LOC);
insertAtLast(14);

delAtFirst();
delAfterNode(LOC);
delAtLast();

display();
int option;
char opt;
do
{

cout<<"\nEnter Option Which You


want to do\n1)Insertion at First\
n2)Insertion after Given Node\
n3)Insertion at Last\n4)Deletion at
First\n5)Deletion after Given Node\
n6)Deletion at Last Node\n";
cin>>option;
switch(option) {
case 1:
int item;
cout<<"Enter Item to be
Inserted: ";
cin>>item;
insertAtFirst(item);
display();
break;
case 2:
cout<<"Enter Item to be
Inserted: ";
cin>>item;
insertAfterNode(item, LOC);
display();
break;
case 3:
cout<<"Enter Item to be
Inserted: ";
cin>>item;
insertAtLast(item);
display();
break;
case 4:
delAtFirst();
display();
break;
case 5:
delAfterNode(LOC);
display();
break;
case 6:
delAtLast();
display();
break;
default:
cout<<"\nPlease Insert Correct
Option\n";
} // End of Switch Case

cout<<"\nEnter Y for Continue and N


for Exit: ";
cin>>opt;
}while(opt=='y' || opt=='Y'); // End
of Do While Loop

} //End of Main

void display(){
Node* temp = head;
while(temp!=NULL)
{
cout<<"Next Address is: "<<temp-
>next<<"\tValue is: "<<temp-
>val<<endl;
temp = temp->next;
} //End of Display Function
}
void insertAtFirst(int item)
{
Node* temp =
(Node*)malloc(sizeof(temp));
if(temp==NULL)
{
cout<<"Overflow!!!\n";
return;
}
temp = new Node(item);
temp->next=head;
head=temp;
}// End of inserAtFirst Function

void insertAfterNode(int item, Node*


LOC)
{
Node* temp =
(Node*)malloc(sizeof(temp));
if(temp == NULL)
{
cout<<"Overflow!!!\n";
return;
}
temp = new Node(item);
if(head==NULL)
{
temp->next=head;
head=temp;
}
else
{
temp->next=LOC->next;
LOC->next=temp;
} // End of insertAfterNode
}

void insertAtLast(int item)


{
Node* temp =
(Node*)malloc(sizeof(temp));
if(temp==NULL)
{
cout<<"Overflow!!!\n";
return;
}
temp = new Node(item);
if(head==NULL)
{
temp->next=head;
head=temp;
}
else
{
Node* start = head;
while(start->next!=NULL)
{
start=start->next;
}
start->next=temp;
}
} // End of insertAtLast

void delAtFirst()
{
if(head==NULL)
{
cout<<"Underflow!!!\n";
return;
}
head = head->next;
} //End of delAtFirst

void delAfterNode(Node* LOC)


{
if(head==NULL)
{
cout<<"Underflow!!!\n";
return;
}
LOC->next= LOC->next->next;
} //End of delAfterNode

void delAtLast()
{
if(head==NULL)
{
cout<<"underflow!!!\n";
return;
}
if(head->next==NULL)
{
head=NULL;
return;
}
Node* start = head;
while(start->next->next!=NULL)
{
start=start->next;
}

start->next = NULL;
// free (start->next);
}

You might also like