Você está na página 1de 13

PROGRAM:

#include<iostream.h>
#include<conio.h>
class adt
{
public:
int a[100],s,n,d;
public:
void getdata();
void display();
void search();
void outdata();
}ob;
void adt::getdata()
{
int i;
cout<<"Enter the value of n:"<<endl;
cin>>n;
cout<<"Enter the numbers:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void adt::display()

{
int i;
cout<<"The values are"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void adt::search()
{
int i,f;
cout<<"Enter the values to be searched:"<<endl;
cin>>s;
f=0;
for(i=0;i<n;i++)
{
if(a[i]==s)
{
f=1;
cout<<"The position is"<<i+1<<endl;
}
}
if(f==0)
{
cout<<"Not found"<<endl;

}
}
void adt::outdata()
{
int i,*k;
cout<<"Enter the value to be deleted"<<endl;
cin>>d;
for(i=0;i<n;i++)
{
if(a[i]==d)
{
k=&a[i];
delete k;
}
else
cout<<"The values are"<<a[i]<<endl;
}
}
void main()
{
int n,l;
clrscr();
cout<<"Implementation of list adt"<<endl;
cout<<"The functions are"<<endl;
cout<<"\n1.INPUT\n2.DISPLAY\n3.SEARCH\n4.DELETE"<<endl;

ob.getdata();
ob.display();
ob.search();
ob.outdata();
getch();
}

OUTPUT:
IMPLEMENTATION OF LIST ADT
The functions are
1.INPUT
2.DISPLAY
3.SEARCH
4.DELETE
Enter the value of n:
3
Enter the numbers:
135
The values are
1
3
5
Enter the values to be searched:
3
The position is2
Enter the value to be deleted
5
The values are1
The values are3

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class slink
{
private:
struct node
{
int data;
node *next;
}*head;
public:
slink()
{
head=NULL;
}
void create();
void insert();
void delnode();
void display();
};
void slink::create()
{
head=new node;

cout<<"Enter the data for the new code:";


cin>>head->data;
head->next=NULL;
}
void slink::insert()
{
int n;
char p;
node *list,*prev,*temp;
list=head;
cout<<"Insert node at front/middle/last?f/m/l:"<<endl;
cin>>p;
if(p=='f')
{
temp=new node;
cout<<"Enter the data for the node:";
cin>>temp->data;
temp->next=head;
head=temp;
}
if(p=='m')
{
cout<<"Enter the data for the new node before:"<<endl;
cin>>n;
while(list->data!=n)

{
prev=list;
list=list->next;
}
temp=new node;
cout<<"Enter the data for the node:"<<endl;
cin>>temp->data;
prev->next=temp;
temp->next=list;
}
if(p=='l')
{
while(list->next!=NULL)
list=list->next;
temp=new node;
cout<<"Enter the data for the node:"<<endl;
cin>>temp->data;
temp->next=NULL;
list->next=temp;
}
}
void slink::delnode()
{
int n,p=0;
node *list,*prev,*temp;

list=head;
cout<<"Enter the data for the node to be deleted:"<<endl;
cin>>n;
if(head->data==n)
{
head=head->next;
p=1;
}
else
{
while((list->next!=NULL)&&(list->data==n))
{
prev=list;
list=list->next;
}
if((list->next!=NULL)&&(list->data==n))
{
temp=list;
prev->next=NULL;
p=1;
}
else if(list->data==n)
{
temp=list;
prev->next=list->next;

p=1;
}
delete(temp);
}
if(p==0)
{
cout<<"Node is not present";
}
}
void slink::display()
{
node *list;
list=head;
if(list==NULL)
cout<<"List is empty";
else
{
cout<<"The list is:";
while(list!=NULL)
{
cout<<list->data<<"<==>";
list=list->next;
}
cout<<"NULL";
}

getch();
}
void main()
{
clrscr();
int option;
slink s;
while(option!=5)
{
cout<<"1.Create()\n2.Insert()\n3.Delete()\n4.Display()\n5.Exit()"<<endl;
cout<<"Enter your option:";
cin>>option;
switch(option)
{
case 1:
s.create();
break;
case 2:
s.insert();
break;
case 3:
s.delnode();
break;
case 4:
s.display();

break;
case 5:
break;
}
}
}

Você também pode gostar