Google Search

Wednesday, October 27, 2010

CIRCULAR LIST

CIRCULAR LIST

/* CIRCULAR LIST */

#include
template
class linklist
{
public:
int count;
struct link
{
T data;
struct link *prev;
struct link *next;
}*first,*last;

linklist()
{first=last=NULL;count=0;}
void create();
void insert();
void dlete();
int IsEmpty();
void search();
void sort();
void displayright();
void displayleft();
};


template
int linklist::IsEmpty()
{
if(first==NULL)
return 1;
else
return 0;
}


template
void linklist::create()
{
T val;
cout<<"\nEnter the value to be Created : "; cin>>val;
struct link *node;
node=new struct link;
node->data=val;
node->next=NULL;
node->prev=NULL;
if(first==NULL)
first=last=node;
else
{
last->next=node;
node->prev=last;
last=node;
last->next=first;
first->prev=last;
}
count++;
}


template
void linklist::insert()
{
struct link *node,*current;
T val;
int p=0,pos;
current=first;
cout<<"\nEnter the value to be inserted : "; cin>>val;
node=new struct link;
node->data=val;
node->next=NULL;node->prev=NULL;
lp1:
cout<<"\nEnter the position u want to insert(1 - "<>pos;
if((pos>count+1)||(pos<1)) {cout<<"\nwrong position...Enter new position..\n";goto lp1;} else { if(pos==1) { if(first==NULL) first=last=node; else { node->next=first;
node->prev=first->prev;
first->prev=node;
first=node;
last->next=first;
}
}
else if(pos==count+1)
{
if(last==NULL)
first=last=node;
else
{
node->next=last->next;
last->next=node;
node->prev=last;
last=node;
first->prev=last;
}
}
else
{
while(current!=NULL)
{
p++;
if(pos==p)
break;
current=current->next;
}
node->next=current;
node->prev=current->prev;
current->prev->next=node;
current->prev=node;

}
}
count++;
}


template
void linklist::dlete()
{
T val;
cout<<"\nEnter the value to be deleted : "; cin>>val;
struct link *current,*temp;
current=first;
while(current!=NULL)
{
if(current->data==val)
break;
current=current->next;
}

if(current==NULL)
cout<<"Data not found\n"; else if((current==first)&&(current==last)) first=last=NULL; else if(current==first) { temp=first->prev;
first=first->next;
first->prev=temp;
last->next=first;
}
else if(current==last)
{temp=last->next;
last=last->prev;
last->next=temp;
first->prev=last;
}
else
{
current->prev->next=current->next;
current->next->prev=current->prev;
}
count--;
}

template
void linklist::sort()
{
struct link *i,*j;
int p=0;
T temp;
if(IsEmpty())
cout<<"\nList is Empty\n"; else { for(i=first,p=0;pnext)
for(j=i->next;j!=last;j=j->next)
if((i->data)>(j->data))
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
}
cout<<"\nThe sorted values : "; displayright(); } template
void linklist::search()
{
struct link *current=first;
T val;
int flag=0;
cout<<"\nEnter the value to be searched : "; cin>>val;
if(IsEmpty())
cout<<"\nList is Empty\n"; else { while(current!=last) { if(current->data==val)
{flag=1; break;}
current=current->next;
}
if(last->data==val)
flag=1;
if(flag==0)
cout<<"\nData not found in the list"; else cout<<"\n"<
void linklist::displayright()
{
struct link *current;
int i=0;
current=first;
if(current==NULL)
cout<<"\nList is empty"; else { while(i<=count) { cout<<"\t"<data;
current=current->next;i++;
}cout<
void linklist::displayleft()
{
struct link * current;
int i=0;
current=last;
if(last==NULL)
cout<<"\n List is empty"; else { while(i<=count) { cout<<"\t"<data;
current=current->prev;i++;
}cout< lst;
int ch;

cout<<"\n\tImplemention of Circular Linked List\n"; cout<<"\t====================================\n"; do { cout<<"\n\t\tMENU\n\n"; cout<<"\t1.CREATE"; cout<<"\t2.DELETE"; cout<<"\t3.ISEMPTY"; cout<<"\t4.SEARCH"; cout<<"\t5.SORT"; cout<<"\n\t6.DISPLAYRIGHT"; cout<<"\t7.DISPLAYLEFT"; cout<<"\t8.INSERT"; cout<<"\t9.EXIT"; cout<<"\n\t Enter ur choice : "; cin>>ch;
switch(ch)
{
case 1: lst.create();break;
case 2: lst.dlete();break;
case 3: if(lst.IsEmpty())
cout<<"\nList is Empty";
else
cout<<"\nList is not Empty";
break;
case 4: lst.search();break;
case 5: lst.sort();break;
case 6: lst.displayright();
break;
case 7:lst.displayleft();
break;
case 8:lst.insert();
break;
case 9:exit(0);
}
}while(ch<9);
}

0 comments:

Post a Comment