Skip to content

Instantly share code, notes, and snippets.

@mayankdawar
Created October 2, 2019 12:16
Show Gist options
  • Save mayankdawar/83d6c960ca0cc09d5e2acff15930ac16 to your computer and use it in GitHub Desktop.
Save mayankdawar/83d6c960ca0cc09d5e2acff15930ac16 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
};
class link
{
public:
Node *head, *tail;
int n;
link()
{
head=NULL;
tail=NULL;
}
void create()
{
Node *temp;
temp =new Node;
cout<<"Enter an element:";
cin>>n;
temp->data=n;
temp->next=NULL;
if(head== NULL)
{
head=temp;
tail=head;
}
else{
tail->next=temp;
tail=temp;
}
}
void insert()
{
Node *temp,*cur,*prev;
cur=head;
prev=NULL;
temp = new Node;
int ch,pos,count=1;
cout<<"Enter element:";
cin>>n;
temp->data=n;
temp->next=NULL;
cout<<"1: at Start\n2: at end\n3: in between\n\n";
cout<<"Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
{
temp->next=head;
head=temp;
break;
}
case 2:
{
tail->next=temp;
temp=tail;
break;
}
case 3:
{
cout<<"Enter Position:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
prev->next=temp;
temp->next=cur;
}
else{
cout<<"Not able to create";
}
}
}
}
void display()
{
Node *temp;
temp=head;
while(temp !=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void delet()
{
Node *temp,*cur,*prev;
cur=head;
prev=NULL;
int count =1,ch,pos;
cout<<"1: First Node\n2: Last Node\n3: in between\n\n";
cout<<"Enter Your Choice:";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
{
cout<<"Deleted Element:"<<head->data;
head=head->next;
}
case 2:
{
while(cur != tail)
{
prev=cur;
cur=cur->next;
}
if(cur==tail)
{
cout<<"Deleted Element:"<<cur->data;
prev->next=NULL;
tail=prev;
}
}
case 3:
{
cout<<"Enter Position:";
cin>>pos;
while(count != pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(pos== count)
{
cout<<"Deleted Element:"<<cur->data;
prev->next=cur->next;
}
else {
cout<<"Not able to delete";
}
break;
}
}
}
void search()
{
int pos=0,n;
bool flag=false;
Node *temp;
temp = head;
cout<<"Enter valued to be searched:";
cin>>n;
while(temp != NULL)
{
pos++;
if(temp->data== n)
{
flag=true;
cout<<"Element "<<temp->data<<"found at position "<<pos<<endl;
}
temp=temp->next;
}
if(!flag)
{
cout<<"Element "<<n<<" not Found in the List";
}
}
};
int main() {
link l;
int ch;
while(1)
{
cout<<"\n**************************************************************8";
cout<<"\n1:Create\n2:Insert\n3:Delete\n4:Search\n5:Display\n6:Exit\n";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.delet();
break;
case 4:
l.search();
break;
case 5:
l.display();
break;
case 6:
return 0;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment