Sei sulla pagina 1di 17

1.

circularly linked list

#include <iostream>
using namespace std;
struct node{
int data;
node* next;
};
class link
{
public:
node* head=NULL;
node* tail=NULL;
node*temp=NULL;
void create();
void insert_start();
void insert_middle();
void insert_end();
void delete_first();
void delete_middle();
void delete_last();
void delete_end();
void display();
void search();
void sort();
void edit();
void reverse();
};
int main()
{
link first;
int i=1;
while(i=1)
{
cout<<"1.create"<<endl;
cout<<"2.insert_start"<<endl;
cout<<"3.insert_middle"<<endl;
cout<<"4.insert_end"<<endl;
cout<<"5.delete_first"<<endl;
cout<<"6.delete_middle"<<endl;
cout<<"7.delete_end"<<endl;
cout<<"8.display"<<endl;
cout<<"9.search"<<endl;
cout<<"10.sort"<<endl;
cout<<"11.edit"<<endl;
cout<<"12.reverse"<<endl;
cout<<"enter your choice"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:first.create();
break;
case 2:first.insert_start();
break;
case 3:first.insert_middle();
break;
case 4:first.insert_end();
break;
case 5:first.delete_first();
break;
case 6:first.delete_middle();
break;
case 7:first.delete_end();
break;
case 8:first.display();
break;
case 9:first.search();
break;
case 10:first.sort();
break;
case 11:first.edit();
break;
case 12:first.reverse();

}
}
return 0;
}
void link::create()
{
cout<<"enter the element"<<endl;
int value;
cin>>value;
node *temp=new node;
temp->data=value;
temp->next=temp;
if(head==NULL)
{
head=temp;
tail=temp;

}
else
{
tail->next=temp;
tail=temp;
tail->next=head;

}
}
void link::insert_start()
{
cout<<"enter the element"<<endl;
int n;
cin>>n;
node* temp=new node;
temp->data=n;
temp->next=head;
head=temp;
tail->next=head;

}
void link::insert_middle()
{
cout<<"enter the element"<<endl;
int j;
cin>>j;
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
node* temp=new node;
node* pre=new node;
node* cur=new node;
temp->data=j;
cur=head;
for(int a=1;a<pos;a++)
{
pre=cur;
cur=cur->next;
}
pre->next=temp;
temp->next=cur;
}
void link::insert_end()
{
node* temp=new node;
cout<<"enter the element"<<endl;
int b;
cin>>b;
temp->data=b;
temp->next=temp;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
tail->next=head;
}
}
void link::delete_first()
{
node* temp=new node;
temp=head;
head=head->next;
tail->next=head;
delete temp;
}
void link::delete_middle()
{
int pos;
cout<<"enter the position"<<endl;
cin>>pos;
node* cur=new node;
node* pre=new node;
cur=head;
for(int a=1;a<pos;a++)
{
pre=cur;
cur=cur->next;
}
cur=cur->next;
pre->next=cur;
}
void link::delete_end()
{
node* cur=new node;
node* pre=new node;
cur=head;
while(cur->next!=head)
{
pre=cur;
cur=cur->next;
}
tail=pre;
tail->next=head;
delete cur;
}
void link::display()
{
node *cur=new node;
cur=head;
while(cur->next!=head)
{
cout<<cur->data<<endl;
cur=cur->next;
}
cout<<cur->data<<endl;
}
void link::search()
{
cout<<"enter the element"<<endl;
int n;
cin>>n;
node* cur=new node;
int i=1;
cur=head;
while(cur->next!=head)
{
i++;
cur=cur->next;
}
i++;
cur=head;
int a;
for( a=1;a<=i;a++)
{
if(cur->data==n)
{
cout<<"the element is present at"<<a<<"position"<<endl;
break;
}
cur=cur->next;
}
if(a==i+1)
{
cout<<"the element is not present"<<endl;
}

}
void link::sort()
{
node*pre=new node;
node *cur=new node;
int n=0,t;
cur=head;
while(cur->next!=head)
{
n++;
cur=cur->next;
}
n++;
cur=head;
pre=head->next;
for(int i=1;i<=n-1;i++)
{
for(int j=i+1;j<=n;j++)
{
if(cur->data < pre->data)
{
t=cur->data;
cur->data=pre->data;
pre->data=t;
}
pre=pre->next;
}
cur=cur->next;
pre=cur->next;
}
}
void link::edit()
{
node* cur=new node;
cout<<"enter the element"<<endl;
int n;
cin>>n;
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
cur=head;
for(int i=1;i<pos;i++)
{
cur=cur->next;
}
cur->data=n;
}
void link::reverse()
{
node* cur=new node;
node* pre=new node;
int n=0;
int t;
cur=head;
while(cur->next!=head)
{
n++;
cur=cur->next;
}
n++;
cur=head;
pre=cur->next;
for(int i=1;i<=n/2;i++)
{
for(int j=i+1;j<=n-i;j++)
{
pre=pre->next;
}
t=cur->data;
cur->data=pre->data;
pre->data=t;
cur=cur->next;
pre=pre->next;
}
}

2.Linked list(single)

#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list: public node
{
node *head;
node *tail;
public:
list()
{
head = NULL;
tail = NULL;
}
void insert_beg(int);
void insert_end(int);
void insert_pos(int,int);
void del_first();
void del_end();
void del_pos(int);
void display();
void create();
void search();
void edit();
void sort();
void reverse();
};
int main()
{
list l;
int ch,val,pos;
do
{
cout<<"Enter your choice\n1.Create\n2.Insert in the beginning\n3.Insert at
end\n4.Insert at position\n5.Delete first\n6.Delete at end\n7.Delete at a
position\n8.Display\n9.Search\n10.Edit\n11.Sort\n12.Reverse\n13.Exit\n";
cin>>ch;
switch(ch)
{
case 1:
{
l.create();
break;
}
case 2:
{
cout<<"Enter value to be inserted\n";
cin>>val;
l.insert_beg(val);
break;
}
case 3:
{
cout<<"Enter value to be inserted\n";
cin>>val;
l.insert_end(val);
break;
}
case 4:
{
cout<<"Enter the value to be inserted and the
position\n";
cin>>val>>pos;
l.insert_pos(val,pos);
break;
}
case 5:
{ l.del_first();
break;

}
case 6:
{
l.del_end();
break;
}
case 7:
{
cout<<"Enter the position\n";
cin>>pos;
l.del_pos(pos);
break;
}
case 8:
{
l.display();
break;
}
case 9:
{
l.search();
break;
}
case 10: {
l.edit();
break;
}
case 11:
{
l.sort();
break;
}
case 12:
{
l.reverse();
break;
}
case 13:
goto sameena;

}}
while(1);
sameena:
return 0;
}
void list::insert_end(int val)
{
node *temp=new node;
temp->data=val;
temp->next=NULL;
if(head == NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void list::insert_beg(int val)
{
node *temp=new node;
temp->data=val;
temp->next=head;
head=temp;
}
void list::insert_pos(int val,int pos)
{
node *temp=new node;
node *pre=new node;
node *cur=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=cur;
cur=cur->next;
}
pre->next=temp;
temp->data=val;
temp->next=cur;

}
void list::del_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
void list::del_end()
{
node *cur=new node;
node *pre=new node;
cur=head;
while(cur->next!=NULL)
{
pre=cur;
cur=cur->next;
}
tail=pre;
pre->next=NULL;
delete cur;
}
void list::del_pos(int pos)
{
node *cur=new node;
node *pre=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=cur;
cur=cur->next;
}
pre->next=cur->next;
}
void list::display()
{
node *temp=new node;
temp=head;
cout<<temp->data;
temp=temp->next;
while(temp!=NULL)
{
cout<<"->"<<temp->data;
temp=temp->next;
}
cout<<endl;
}
void list::create()
{ int val;
cout<<"Enter a value\n";
cin>>val;
node *temp=new node;
temp->data=val;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
void list::search()
{ int che,i=0,t=0;
node *temp=new node;
cout<<"enter value to be searched\n";
cin>>che;
temp=head;
while(temp!=NULL)
{
i++;
if(temp->data==che)
t=i;
temp=temp->next;
}
if(t==0)
cout<<"Value not found\n";
else
cout<<"Value found at "<<t<<"th position\n";
}
void list::edit()
{ node *cur=new node;
node *pre=new node;
int value,pos;
cur=head;
cout<<"Enter the position and the value\n";
cin>>pos>>value;
for(int i=1;i<pos;i++)
{pre=cur;
cur=cur->next;
}
cur->data=value;
}
void list::sort()
{
node*pre=new node;
node *cur=new node;
int n=0,t;
cur=head;
while(cur!=NULL)
{
n++;
cur=cur->next;
}
cur=head;
pre=head->next;
for(int i=1;i<=n-1;i++)
{
for(int j=i+1;j<=n;j++)
{
if(cur->data < pre->data)
{
t=cur->data;
cur->data=pre->data;
pre->data=t;
}
pre=pre->next;
}
cur=cur->next;
pre=cur->next;
}
}
void list::reverse()
{
node *pre=new node;
node *cur= new node;
int n=0,t;
cur=head;
while(cur!=NULL)
{
n++;
cur=cur->next;
}
cur=head;
pre=head->next;
for(int i=1;i<=n/2;i++)
{
for(int j=i+1;j<=(n-i);j++)
{
pre=pre->next;
}
t=cur->data;
cur->data=pre->data;
pre->data=t;
cur=cur->next;
pre=cur->next;
}
}

3.Doubly linked list

#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
node *pre;
};
class link
{
public:
node* head=NULL;
node* tail=NULL;
void create();
void insert_begin();
void insert_middle_start();
void insert_middle_end();
void insert_end();
void display();
void delete_first();
void delete_end();
void delete_mid_begin();
void delete_mid_end();
void search();
void sort();
void edit();
void reverse();
};
int main()
{
link first;
while(1)
{
cout<<"1.create"<<endl;
cout<<"2.insert_begin"<<endl;
cout<<"3.insert_middle_start"<<endl;
cout<<"4.insert_middle_end"<<endl;
cout<<"5.insert_end"<<endl;
cout<<"6.display"<<endl;
cout<<"7.delete_first"<<endl;
cout<<"8.delete_end"<<endl;
cout<<"9.delete_mid_begin"<<endl;
cout<<"10.delete_mid_end"<<endl;
cout<<"11.search"<<endl;
cout<<"12.sort"<<endl;
cout<<"13.edit"<<endl;
cout<<"14.reverse"<<endl;
cout<<"enter your choice"<<endl;
int n;
cin>>n;
switch(n)
{
case 1:first.create();
break;
case 2:first.insert_begin();
break;
case 3:first.insert_middle_start();
break;
case 4:first.insert_middle_end();
break;
case 5:first.insert_end();
break;
case 6:first.display();
break;
case 7:first.delete_first();
break;
case 8:first.delete_end();
break;
case 9:first.delete_mid_begin();
break;
case 10:first.delete_mid_end();
break;
case 11:first.search();
break;
case 12:first.sort();
break;
case 13:first.edit();
break;
case 14:first.reverse();
}
}
return 0;
}
void link::create()
{
cout<<"enter the element"<<endl;
int i;
cin>>i;
node* temp=new node;
temp->data=i;
temp->pre=NULL;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
temp->pre=tail;
tail=temp;
}
}
void link::display()
{
node* cur=new node;
cur=head;
while(cur!=NULL)
{
cout<<cur->data<<endl;
cur=cur->next;
}
}
void link::insert_begin()
{
node* temp=new node;
cout<<"enter the element"<<endl;
int j;
cin>>j;
temp->data=j;
temp->pre=NULL;
temp->next=head;
head=temp;
}
void link::insert_end()
{
cout<<"enter the element"<<endl;
int i;
cin>>i;
node* temp=new node;
temp->data=i;
temp->pre=NULL;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
temp->pre=tail;
tail=temp;
}

}
void link::insert_middle_start()
{
node* temp=new node;
cout<<"enter the element"<<endl;
int n;
cin>>n;
int pos;
cout<<"enter the position"<<endl;
cin>>pos;
temp->data=n;
temp->next=NULL;
node* cur=new node;
node* pre=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=head;
cur=cur->next;
}
pre->next=temp;
temp->pre=pre;
temp->next=cur;
cur->pre=temp;
}
void link::insert_middle_end()
{
node* temp=new node;
cout<<"enter the element"<<endl;
int n;
cin>>n;
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
temp->data=n;
temp->next=NULL;
node* cur=new node;
node* pre=new node;
pre=tail;
for(int i=1;i<pos;i++)
{
cur=pre;
pre=pre->pre;
}
pre->next=temp;
temp->pre=pre;
temp->next=cur;
cur->pre=temp;
}
void link::delete_first()
{
node*temp=new node;
temp=head;
head=head->next;
head->pre=NULL;
delete temp;
}
void link::delete_end()
{
node* temp=new node;
temp=tail;
tail=tail->pre;
tail->next=NULL;
delete temp;
}
void link::delete_mid_begin()
{
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
node* cur=new node;
node* pre=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=head;
cur=cur->next;
}
cur=cur->next;
pre->next=cur;
cur->pre=pre;
}
void link::delete_mid_end()
{
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
node* cur=new node;
node* pre=new node;
pre=tail;
for(int i=1;i<pos;i++)
{
cur=pre;
pre=pre->pre;
}
pre=pre->pre;
pre->next=cur;
cur->pre=pre;

}
void link::search()
{
cout<<"enter the element"<<endl;
int n;
cin>>n;
node* cur=new node;
int i=1;
cur=head;
while(cur!=NULL)
{
if(cur->data==n)
{
cout<<"the element is present at"<<i<<"position"<<endl;
break;
}
cur=cur->next;
i++;
}

if(cur==NULL)
{
cout<<"the element is not present"<<endl;
}
}
void link::sort()
{
node*pre=new node;
node *cur=new node;
int n=0,t;
cur=head;
while(cur!=NULL)
{
n++;
cur=cur->next;
}
cur=head;
pre=head->next;
for(int i=1;i<=n-1;i++)
{
for(int j=i+1;j<=n;j++)
{
if(cur->data < pre->data)
{
t=cur->data;
cur->data=pre->data;
pre->data=t;
}
pre=pre->next;
}
cur=cur->next;
pre=cur->next;
}
}
void link::edit()
{
node* cur=new node;
cout<<"enter the element"<<endl;
int n;
cin>>n;
cout<<"enter the position"<<endl;
int pos;
cin>>pos;
cur=head;
for(int i=1;i<pos;i++)
{
cur=cur->next;
}
cur->data=n;
}
void link::reverse()
{
node* cur=new node;
node* pre=new node;
int n=0;
int t;
cur=head;
while(cur!=NULL)
{
n++;
cur=cur->next;
}
cur=head;
pre=cur->next;
for(int i=1;i<=n/2;i++)
{
for(int j=i+1;j<=n-i;j++)
{
pre=pre->next;
}
t=cur->data;
cur->data=pre->data;
pre->data=t;
cur=cur->next;
pre=pre->next;
}
}

Potrebbero piacerti anche