C++ Program to implement a single linked list
C++ Program to implement a single linked list and Perform the following operations.
/**************************************************************
Author: Arun Vishnu M V
Web: www.arunmvishnu.com
Description: C++ Program to implement a single linked list and Perform the following operations
1: Insertioin
2: Deletion
3: Display
***************************************************************/
#include
#include
#include
// Creating a NODE Structure
struct node
{
int data; // data
struct node *next,*start; // link to next node and previous node
};
// Creating a class LIST
class list
{
struct node *start;
public:
void create(); // to create a list
void insert(); // insertion
void del(); // deletion
void show(); // show
};
// Creating a new node
void list::create()
{
struct node *nxt_node,*pre_node;
int value,no,i;
start=nxt_node=pre_node=NULL;
cout<<"\nCREATING A NEW LIST.........\n\nHow many nodes : ";
cin>>no;
for(i=1;i<=no;i++)
{
cout<<"Enter "<>value;
nxt_node=new node;
if(pre_node!=NULL)
pre_node->next=nxt_node;
if(start==NULL)
start=nxt_node;
nxt_node->data=value;
nxt_node->next=NULL;
pre_node=nxt_node;
}
cout<<"\nThe list is created!";
show();
}
// Displaying LIST
void list::show()
{
int data;
struct node *ptr=start;
cout<<"\n\nThe List is \n";
while(ptr!=NULL)
{
data=ptr->data;
cout< ";
ptr=ptr->next;
}
cout<<"\b\b\b ";
getch();
}
// Insert node at any position
void list::insert()
{
int position,dat;
struct node *ptr_b,*ptr_f,*ptr,*nxt_node,*pre_node;
cout<<"\nInsertion of a new node \n";
cout<<"Enter the DATA after which the new node is to be inserted.\n";
cout<<"[ If the data is not found the new node will be";
cout<<" created at first ]\n\t->: ";
cin>>position;
cout<<"Enter the data to insert: ";
cin>>dat;
ptr_b=start;
ptr=new node;
ptr->data=dat;
if(start!=NULL)
{
while(ptr_b->next!=NULL && ptr_b->data!=position)
{
ptr_b=ptr_b->next;
}
}
else
{
clrscr();
start=nxt_node=pre_node=NULL;
nxt_node=new node;
if(pre_node!=NULL)
pre_node->next=nxt_node;
if(start==NULL)
start=nxt_node;
nxt_node->data=dat;
nxt_node->next=NULL;
pre_node=nxt_node;
return;
}
if(ptr_b->next==NULL && ptr_b->data!=position)
//Insertion at first
{
ptr->next=start;
start=ptr;
}
else if(ptr_b->next==NULL && ptr_b->data==position)
//insertion at the end of list
{
ptr_b->next=ptr;
ptr->next=NULL;
}
else
//Insertion between two nodes
{
ptr_f=ptr_b->next;
ptr->next=ptr_b->next;
ptr_b->next=ptr;
}
cout<<"\nNew node is inserted!!";
getch();
} //End of insertion
//Delete node from any position
void list::del()
{
int position,dat;
struct node *ptr_b,*ptr_f,*ptr,*pntr;
cout<<"Enter the data to be beleted: ";
cin>>position;
ptr=start;
while(ptr->next!=NULL && ptr->data!=position)
{
pntr=ptr;
ptr=ptr->next;
}
if(ptr->next==NULL && ptr->data!=position)
//Data not found
{
cout<<"\nData not found!!";
getch();
return;
}
else if(ptr->next==NULL && ptr->data==position)
//Deletion from the end of list
{
ptr_b=pntr;
if(pntr->next)// Only one node
start=NULL;
dat=ptr->data;
ptr_b->next=NULL;
}
else
//Deletion between two nodes or first node
{
dat=ptr->data;
if(start==ptr) // delete first node
{
start=ptr->next;
ptr_f=start;
}
else // Deletion between two nodes
{
dat=ptr->data;
ptr_b=pntr;
ptr_b->next=ptr->next;
//ptr_f=ptr_b->next;
}
}
delete ptr;
cout<<"\nThe node is deleted!!\nData= "<>choice;
switch(choice)
{
case 1: // Insertion
l.insert();
break;
case 2: // Deletion
l.del();
break;
case 3: // Display
l.show();
break;
case 4: // Exit
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!!";
getch();
break;
}
}
return 0;
}
//---------------------- END--------------------
sir,
am doin my BE CSE..i did not understand the programs…plz explain line by line in detail…plz sir plz sir plz sir..i’ll be very grateful to u..