#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
class sll{
private:
node *head,*tail;
public:
sll(){
head=0;
tail=0;
}
insert(int mydata){
node *tmp=new node;
tmp->data=mydata;
tmp->next=0;
int select;
cout<<"Select one of this option\n";
cout<<"1.insert element at first position\n";
cout<<"2.insert element at Any position\n";
cout<<"3.insert element at last position\n";
cin>>select;
switch(select){
case 1:if(head==0){
head=tmp;
tail=tmp;
}else{
tmp->next=head;
head=tmp;
}
break;
case 2:int cont;
if(head==0 && tail==0){
cout<<"please enter the no. at begin or end first\n";
}
else{
int inst;
tail=head;
cout<<"Enter the element after you want inserting\n";
cin>>inst;
while(tail->data!=inst)
{
cont=1;
tail=tail->next;
}
tmp->next=tail->next;
tail->next=tmp;
}
if(cont==0){
cout<<"plzzz enter correct element\n";
}
break;
case 3:if(tail==0){
tail=tmp;
head=tmp;
}else{
while(tail->next!=0)
{
tail=tail->next;
}
tail->next=tmp;
tmp->next=0;
}
break;
default : cout<<"wrong output\n";
}
}
void display()
{
node *tmp=head;
if(tmp == NULL)
{
cout << "NULL" << endl;
}
else
{
while(tmp!=0){
cout << tmp->data << endl;
tmp=tmp->next;
}
}
}
};
int main()
{
sll a;
char again;
int select,input;
do{
cout<<"Enter the given inputs \n";
cout<<"1.Insert the input\n";
cout<<"2.display element\n";
cin>>select;
switch(select){
case 1:cout<<"Enter the element\n";
cin>>input;
a.insert(input);
break;
case 2:cout<<"Your outputs\n";
a.display();
break;
default:cout<<"your select wrong output\n";
}
cout<<"Do you want to run again yes press y or Y (or) no\n";
cin>>again;
}while(again=='y'||again=='Y');
return 0;
}
using namespace std;
struct node{
int data;
node *next;
};
class sll{
private:
node *head,*tail;
public:
sll(){
head=0;
tail=0;
}
insert(int mydata){
node *tmp=new node;
tmp->data=mydata;
tmp->next=0;
int select;
cout<<"Select one of this option\n";
cout<<"1.insert element at first position\n";
cout<<"2.insert element at Any position\n";
cout<<"3.insert element at last position\n";
cin>>select;
switch(select){
case 1:if(head==0){
head=tmp;
tail=tmp;
}else{
tmp->next=head;
head=tmp;
}
break;
case 2:int cont;
if(head==0 && tail==0){
cout<<"please enter the no. at begin or end first\n";
}
else{
int inst;
tail=head;
cout<<"Enter the element after you want inserting\n";
cin>>inst;
while(tail->data!=inst)
{
cont=1;
tail=tail->next;
}
tmp->next=tail->next;
tail->next=tmp;
}
if(cont==0){
cout<<"plzzz enter correct element\n";
}
break;
case 3:if(tail==0){
tail=tmp;
head=tmp;
}else{
while(tail->next!=0)
{
tail=tail->next;
}
tail->next=tmp;
tmp->next=0;
}
break;
default : cout<<"wrong output\n";
}
}
void display()
{
node *tmp=head;
if(tmp == NULL)
{
cout << "NULL" << endl;
}
else
{
while(tmp!=0){
cout << tmp->data << endl;
tmp=tmp->next;
}
}
}
};
int main()
{
sll a;
char again;
int select,input;
do{
cout<<"Enter the given inputs \n";
cout<<"1.Insert the input\n";
cout<<"2.display element\n";
cin>>select;
switch(select){
case 1:cout<<"Enter the element\n";
cin>>input;
a.insert(input);
break;
case 2:cout<<"Your outputs\n";
a.display();
break;
default:cout<<"your select wrong output\n";
}
cout<<"Do you want to run again yes press y or Y (or) no\n";
cin>>again;
}while(again=='y'||again=='Y');
return 0;
}
Comments
Post a Comment