Single Linked – List

/*
Single LinkedList
*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
int info;
node *next;
};

class Linkedlist
{
private:
node *start;
public:
Linkedlist();
void createlist();
void traversal();
void search(int);
void insertbegin();
void insertlast();
void insertafter(int);
void insertbefore(int);
void createsortedlist();
void delbegin();
void dellast();
void delgiven(int);
void merge(Linkedlist,Linkedlist);
void split();
int count();
void oddeven();
void join(Linkedlist);
};
Linkedlist::Linkedlist()
{
start=NULL;
}
void Linkedlist::createlist()
{
node *nnode,*last;
char choice;

do
{
nnode=new node;
cout<<“\nEnter Data: “;
cin>>nnode->info;
nnode->next=NULL;
if(start==NULL)
{
start=nnode;
}
else
{
last->next=nnode;
}
last=nnode;
cout<<“\nDo you want to continue(Y/N): “;
cin>>choice;
}while(choice==’y’||choice==’Y’);
}

void Linkedlist::traversal()
{
node *temp;
temp=start;
while(temp!=NULL)
{
cout<<temp->info<<“,”;
temp=temp->next;
}
}

void Linkedlist::search(int key)
{
node *temp;
temp=start;

while(temp!=NULL)
{
if(key==temp->info)
{
cout<<“\nNumber Found.”;
break;
}
else
{
temp=temp->next;
}
}
if(temp==NULL)
{
cout<<“\nNot Found.”;
}
}

void Linkedlist::insertbegin()
{
node *nnode;
nnode=new node;
cout<<“\nEnter data: “;
cin>>nnode->info;
nnode->next=start;
start=nnode;
}

void Linkedlist::insertlast()
{
node *nnode,*temp;
temp=start;

if(start!=NULL)
{
while(temp->next!=NULL)
{
temp=temp->next;
}
}
nnode=new node;
cout<<“\nEnter data: “;
cin>>nnode->info;
nnode->next=NULL;
if(start==NULL)
{
start=nnode;
}
else
{
temp->next=nnode;
}

}
void Linkedlist::insertafter(int key)
{
node *temp,*nnode;
temp=start;
while(temp!=NULL)
{
if(temp->info==key)
{
nnode=new node;
cout<<“Enter Data: “;
cin>>nnode->info;
nnode->next=temp->next;
temp->next=nnode;
break;
}
else
temp=temp->next;
}
if(temp==NULL)
{
cout<<“Key Not Found.”;
}
}
void Linkedlist::insertbefore(int key)
{
node *temp,*nnode,*prev;
temp=start;
prev=NULL;

while(temp!=NULL)
{
if(temp->info==key)
{
nnode=new node;
cout<<“Enter data: “;
cin>>nnode->info;
if(prev==NULL)
{
start=nnode;
}
else
{
prev->next=nnode;
}
nnode->next=temp;
break;
}
else
{
prev=temp;
temp=temp->next;
}
}
}
void Linkedlist::createsortedlist()
{
node *nnode,*temp,*prev;
char c;
do
{
nnode=new node;
cout<<“\nEnter Data: “;
cin>>nnode->info;
if(start==NULL)
{
start=nnode;
nnode->next=NULL;
}
else
{
temp=start;
prev=NULL;
while(temp!=NULL)
{
if(temp->info>nnode->info)
{
break;
}
else
{
prev=temp;
temp=temp->next;
}
}
if(prev==NULL && temp==start)
{
nnode->next=start;
start=nnode;
}
else
{
if(prev!=NULL && temp==NULL)
{
prev->next=nnode;
nnode->next=NULL;
}
else
{
prev->next=nnode;
nnode->next=NULL;
}
}
}
cout<<“\nDo you want to continue(Y/N): “;
cin>>c;
}while(c==’Y’||c==’y’);
}
void Linkedlist::delbegin()
{
node *temp;
temp=start;

if(start!=NULL)
{
temp=start;
start=start->next;
delete(temp);
}
else
{
cout<<“\nUnderflow.”;
}
}
void Linkedlist::dellast()
{
node *temp,*prev;
if(start!=NULL)
{
temp=start;
prev=NULL;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
if(prev==NULL)
{
start=NULL;
}
else
{
prev->next=NULL;
}
delete temp;
}
else
{
cout<<“\nUnderflow”;
}
}
void Linkedlist::delgiven(int key)
{
node *temp,*prev;
if(start!=NULL)
{
prev=NULL;
temp=start;
while(temp!=NULL)
{
if(temp->info==key)
{
if(prev==NULL)
{
start=start->next;
}
else
{
prev->next=temp->next;
}
break;
}
else
{
prev=temp;
temp=temp->next;
}
}
}
else
cout<<“\nUnderflow.”;

if(temp==NULL)
{
cout<<“\nKey Not Found.”;
}
else
delete temp;
}
void Linkedlist::merge(Linkedlist l1,Linkedlist l2)
{
node *temp1,*temp2,*last,*nnode;
temp1=l1.start;
temp2=l2.start;
while(temp1!=NULL && temp2!=NULL)
{
nnode=new node;
nnode->next=NULL;
if(temp1->info>temp2->info)
{
nnode->info=temp2->info;
temp2=temp2->next;
}
else
{
nnode->info=temp1->info;
temp1=temp1->next;
}
if(start==NULL)
{
start=nnode;
}
else
{
last->next=nnode;
}
last=nnode;
}

if(temp1==NULL)
{
while(temp2!=NULL)
{
nnode=new node;
nnode->next=NULL;
nnode->info=temp2->info;
temp2=temp2->next;
last->next=nnode;
last=nnode;
}
}
else
{
while(temp1!=NULL)
{
nnode=new node;
nnode->next=NULL;
nnode->info=temp1->info;
temp1=temp1->next;
last->next=nnode;
last=nnode;
}
}
}
void Linkedlist::split()
{
node *oddstart,*evenstart,*last1,*last2,*nnode,*temp;

oddstart=NULL;
evenstart=NULL;
temp=start;
while(temp!=NULL)
{
nnode=new node;
nnode->info=temp->info;
nnode->next=NULL;
if(nnode->info % 2==0)
{
if(evenstart==NULL)
{
evenstart=nnode;
}
else
{
last1->next=nnode;
}
last1=nnode;
}
else
{
if(oddstart==NULL)
{
oddstart=nnode;
}
else
{
last2->next=nnode;
}
last2=nnode;
}
temp=temp->next;
}
cout<<“\nOdd Information Contents are: “;
while(oddstart!=NULL)
{
cout<<oddstart->info<<“,”;
oddstart=oddstart->next;
}
cout<<“\nEven Information contents are; “;
while(evenstart!=NULL)
{
cout<<evenstart->info<<“,”;
evenstart=evenstart->next;
}
cout<<“\n”;
}
int Linkedlist::count()
{
node *temp;
int count=0;

temp=start;

while(temp!=NULL)
{
count++;
temp=temp->next;
}
return(count);
}
void Linkedlist::oddeven()
{
node *temp;
int odd=0,even=0;

temp=start;

while(temp!=NULL)
{
if(temp->info%2==0)
{
even++;
}
else
{
odd++;
}
temp=temp->next;
}
cout<<“\nNumber of Odd information: “<<odd;
cout<<“\nNumber of Even information: “<<even;
cout<<“\nTotal Nodes: “<<odd+even;
}
void Linkedlist::join(Linkedlist l2)
{
node *temp;
temp=start;
while(temp->next!=NULL)
temp=temp->next;

temp->next=l2.start;
cout<<“\nContents of Linked list After Joining: “;
temp=start;
while(temp!=NULL)
{
cout<<temp->info<<“,”;
temp=temp->next;
}
}
int main()
{
Linkedlist l;
void merge();
void join();
int c,n;
char s;
clrscr();

do
{
cout<<“\nChoose From Following: “;
cout<<“\n1.Createlist: “;
cout<<“\n2.Traversal: “;
cout<<“\n3.Search: “;
cout<<“\n4.Insert at Beginning: “;
cout<<“\n5.Insert at Last: “;
cout<<“\n6.Insertafter: “;
cout<<“\n7.Insertbefore: “;
cout<<“\n8.Create Sorted List: “;
cout<<“\n9.Delete From Begin: “;
cout<<“\n10.Delete from Last: “;
cout<<“\n11.Delete Given Information: “;
cout<<“\n12.Merge Lists: “;
cout<<“\n13.Split List: “;
cout<<“\n14.Count Number of Nodes in the list: “;
cout<<“\n15.Count Number of Odd and even inforamtion: “;
cout<<“\n16.Join.”;
cout<<“\n\nEnter Your choice: “;
cin>>c;
switch(c)
{
case 1: l.createlist();
break;
case 2: cout<<“\nContents of List are: “;
l.traversal();
break;
case 3: cout<<“\nEnter number to search: “;
cin>>n;
l.search(n);
break;
case 4: l.insertbegin();
break;
case 5: l.insertlast();
break;
case 6: cout<<“\nEnter Key: “;
cin>>n;
l.insertafter(n);
break;
case 7: cout<<“\nEnter Key: “;
cin>>n;
l.insertbefore(n);
break;
case 8: l.createsortedlist();
break;
case 9: l.delbegin();
break;
case 10:l.dellast();
break;
case 11:cout<<“\nEnter Information to be Deleted: “;
cin>>n;
l.delgiven(n);
break;
case 12:merge();
break;
case 13:l.split();
break;
case 14:n=l.count();
cout<<“Number of Nodes are: “<<n;
break;
case 15:l.oddeven();
break;
case 16:join();
break;
default: cout<<“\nInvalid Choice.”;
};
cout<<“\nDo you wish to continue(Y/N): “;
cin>>s;

}while(s==’Y’||s==’y’);
exit(1);

getch();

return(0);
}
void merge()
{
Linkedlist l1,l2,l3;

cout<<“\nList1: \n”;
l1.createlist();

cout<<“\n\nList2: \n”;
l2.createlist();
l3.merge(l1,l2);
l3.traversal();
}
void join()
{
Linkedlist l1,l2;
cout<<“\nLinkedlist 1: \n”;
l1.createlist();
cout<<“\nLinkedlist 2: \n”;
l2.createlist();
l1.join(l2);
}
/*
OUTPUT:
——-
Choose From Following:
1.Createlist:
2.Traversal:
3.Search:
4.Insert at Beginning:
5.Insert at Last:
6.Insertafter:
7.Insertbefore:
8.Create Sorted List:
9.Delete From Begin:
10.Delete from Last:
11.Delete Given Information:
12.Merge Lists:
13.Split List:
14.Count Number of Nodes in the list:
15.Count Number of Odd and even inforamtion:
16.Join.

Enter Your choice: 1

Enter Data: 2

Do you want to continue(Y/N): y

Enter Data: 3

Do you want to continue(Y/N): y

Enter Data: 5

Do you want to continue(Y/N): n

Do you wish to continue(Y/N): n
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.