#include<iostream.h>
#include<assert.h>
typedef struct node*ptrtype;
typedef itemtype itemtype;
struct node{
itemtype item;
ptrtype next;
};
class stack{
private:
ptrtype top;
puplic:
stack();
stack(const stack&s);
~stack();
int isempty();
int isfull(ptrtype p);
int push(itemtype newitem);
int pop();
int gettop(itemtype &item);
const ptrtype gettopptr();
};
stack::stack(){top=NULL;}
stack::stack(const stack &s){
if(s.gettopptr()==NULL)
top=NULL;
else{
top=new node;
assert(top!=NULL);
top->item = s.gettopptr()->item;
ptrtype newlist=top;
for(const node* curlist=s.gettopptr()->next;curlist
!=NULL;curlist->next)
{
newlist->next=new node;
assert(newlist->next !=NULL);
newlist=newlist->next;
newlist->item = curlist->item;
}
newlist->next=NULL;
}
}
stack::~stack(){
int more = pop();
while(more) more = pop();
}
int stack::isempty(){
return(top==NULL);
}
int stack::isfull(temp)){
return(0);
} else{
temp->item = newitem;
temp->next = top;
top=temp;
return(1);
}
}
int stack::pop(){
if(isempty()){
return(0);
}
else{
ptrtype temp =top; top = top->next;
delete temp;
return(1);
}
}
int stack::gettop(itemtype &item){
if (isempty()){
return(0);
}
else{
itemm = top->item; return(1);
}
}
const ptrtype stack::gettopptr()
{return(top);};
void main(){
stack s;
int k;
int b;
while (1)
{
assert();
cout<<" Please select one of the following \n";
cout<<" 1- isempty\n";
cout<<" 2- isfull\n";
cout<<" 3- push\n";
cout<<" 4- pop\n";
cout<<" 5- gettop\n";
cout<<" 6- exit\n\n"<<endl;
cin>>k
switch(k)
{
case 1:{
s.isempty();
break;
}
case 2:{
cin>>b
s.isfull(B);
break;
}
case 3:{
cin>>b;
s.push(B);
break;
}
case 4:{
s.pop();
break;}
case 5:{
cin>>b
s.gettop(B);
break;}
}
case 6: exit(1);
default :cout<<" wrong selection";
}}
}تم تعديل هذه المشاركة بواسطة احمد غريب في 25 مايو 2005 في 20:41