LINK LIST IMPLEMENTATION - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 27, 2010

LINK LIST IMPLEMENTATION

LINK LIST IMPLEMENTATION

/* LINK LIST IMPLEMENTATION */

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

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


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


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






template
void linklist::insert()
{
struct link *node,*current,*previous;
T val;
int p=0,pos;

current=previous=first;
cout<<"\nEnter the value to be inserted : "; cin>>val;
node=new struct link;
node->data=val;
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;
first=node;
}
}
else if(pos==count+1)
{
if(last==NULL)
first=last=node;
else
{
last->next=node;
last=node;
}
}
else
{
while(current!=NULL)
{
p++;
if(pos==p)
break;
previous=current;
current=current->next;
}
node->next=current;
previous->next=node;
}
}
count++;
}







template
void linklist::dlete()
{
struct link *prev, *current;
T val;
if(!IsEmpty())
{
cout<<"\nEnter the value to be deleted : "; cin>>val;
prev=current=first;
while(current!=NULL)
{
if(current->data==val)
break;
else
{
prev=current;
current=current->next;
}
}
if(current==NULL)
cout<<"data not found\n"; else if((current==first)&&(current==last)) first=last=NULL; else if(current==first) first=first->next;
else if(current==last)
{prev->next=NULL;last=prev;}
else
prev->next=current->next;
count--;
}
else
cout<<"\nList is empty"; } template
void linklist::sort()
{
struct link *i,*j;
T temp;
if(IsEmpty())
cout<<"\nList is Empty\n"; else { for(i=first;i!=NULL;i=i->next)
for(j=i->next;j!=NULL;j=j->next)
if((i->data)>(j->data))
{
temp=i->data;
i->data=j->data;
j->data=temp;
}
}
cout<<"\nThe sorted values : "; display(); } template
void linklist::search()
{
struct link *current=first;
T val;
cout<<"\nEnter the value to be searched : "; cin>>val;
if(IsEmpty())
cout<<"\nList is Empty\n"; else { while(current!=NULL) { if(current->data==val)
break;
current=current->next;
}
if(current==NULL)
cout<<"\nData not found in the list"; else cout<<"\n"<
void linklist::display()
{
struct link *current;
current=first;
if(IsEmpty())
cout<<"\nlist is empty"; else { cout<<"\nList is :\n"; while(current!=NULL) { cout<<"\t"<data<next;
}
}
}


int main()
{
linklist lst;
int data,ch;
do
{
cout<<"\n\t\tMENU\n"; cout<<"\t1.CREATE"; cout<<"\t2.DELETE"; cout<<"\t3.INSERT"; cout<<"\t4.SORT\n"; cout<<"\t5.SEARCH"; cout<<"\t6.ISEMPTY"; cout<<"\t7.DISPLAY"; cout<<"\t8.EXIT"; cout<<"\n\t Enter ur choice: "; cin>>ch;
switch(ch)
{
case 1: lst.create();break;
case 2: lst.dlete();break;
case 3: lst.insert();break;
case 4: lst.sort();break;
case 5: lst.search();break;
case 6: if(lst.IsEmpty())
cout<<"\nList is empty";
else
cout<<"\nList is not empty";
break;
case 7: lst.display();break;
case 8: exit(0);
}
}while(ch<8);
}

No comments:

Post a Comment