ADT List Source Code in C++
Following is the source code for an ADT List in C++
#include<iostream.h>
#include<stdlib.h>
#include<ctype.h>
template<class Item>
class LIST
{
private:
struct node
{
Item item;
node *next;
node(Item x, node* t)
{
item=x;
next=t;
}
};
typedef node *list;
list head;
Item read_element()
{
Item x;
x=cin.get();
while(isspace(x))
x=cin.get();
return (x);
}
void deleteLISTcontent()
{
for(list t= head; t!=0; head=t)
{
t=head->next;
delete head;
}
};
public:
LIST()
{
head=0;
}
LIST(const LIST& rhs)
{
head=0;
*this=rhs;
}
LIST& operator=(const LIST& rhs)
{
if(this==&rhs)
return *this;
deleteLISTcontent();
list t=rhs.head,p,q;
if(t!=0)
{
head=new node(t->item,0);
t=t->next;
p=head;
while(t!=0)
{
q=new node(t->item,0);
p->next=q;
p=q;
t=t->next;
}
}
return *this;
}
~LIST()
{
deleteLISTcontent();
}
int null()
{
return head==0;
}
void cons(Item x) //cons
{
head=new node(x,head);
}
Item car() //car
{
Item v=head->item;
list t=head->next;
delete head;
head=t;
return v;
}
list cdr()
{
return head->next->next;
}
/* void readlist_element()
{
char letter;
letter=read_element();
if(letter=='(')
while((letter=read_element())!=')')
return readlist_element();
else if(isalpha(letter))
return (cons(letter,readlist_element()));
}
list readlist()
{
char letter;
letter=read_element();
if(letter=='(')
return readlist_element();
else
cout<<"Error on input format.\n";
} */
void read()
{
int i=0, j; Item x; list p;
Item a[80];
x=read_element();
if(x=='(')
while((x=read_element()) != ')')
a[i++]=x;
else
{
cout<<"error on input format"<<endl;
return;
}
if(i>0)
p=new node(a[--i],p);
while(i>0)
p=new node(a[--i],p);
head=p;
}
void print()
{
cout<<"(";
for(list t=head; t!=0;t=t->next)
cout<<t->item;
cout<<")"<<endl;
}
};
main()
{
LIST<char> L,M;
cout<<"enter list ";
L.read();
M=L;
cout<<"entered list is"<<" ";
L.print();
cout<<"second object is";
M.print();
}
