33,319
社区成员
发帖
与我相关
我的任务
分享
int main()
{
stack * s1=new stack;
int i=1;
s1->push(&i);
s1->pop();
return 0;
}
int main()
{
stack* s1 = new stack;
int i = 1;
s1->push(&i);
s1->pop();
return 0;
}
#include <iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H
class stack
{
class link;
link* head;
public:
stack();
~stack();
void push(void* dat);
void* pop();
void* peek();
};
#endif
//#include "stack.h"
//#include <iostream>
//using namespace std;
class stack::link
{
public:
void* data;
link* next;
link(void* dat,link* nxt);
~link();
};
stack::link::link(void* dat,link* nxt)
{ data=dat;
next=nxt;
}
stack::link::~link()
{
}
stack::stack()
{ head=0;
}
stack::~stack()
{
}
void stack::push(void* dat)
{
head=new link(dat,head);
}
void* stack::pop()
{
if(head==0)
{
cout <<"stack is empty" <<endl;
return 0;
}
void* result=head->data;
cout<<*(int*)head->data<<endl;
link* oldhead;
oldhead=head;
head=head->next;
delete oldhead;
return result;
}
void* stack::peek()
{ if(head==0){
cout <<"stack is empty" <<endl;
return 0;}
return head->data;
}
//#include "stack.h"
int main()
{
stack* s1;
int i=1,j=2;
s1->push(&i);
s1->push(&j);
s1->pop();
s1->pop();
s1->pop();
return 0;
}