如果有兴趣的,可以近来看看
#ifndef STACK_H#define STACK_H
class Stack
{
struct Link
{
void * data;
Link * next;
Link(void * dat,Link* nxt);
~Link();
}* head;
public:
Stack();
~Stack();
void push(void * dat);
void * peek();
void *pop();
};
#endif //STACK4_H ///:~
#include "Stack4.h"
using namespace std;
Stack::Link::Link(void * dat,Link * nxt)
{
data=dat;
next=nxt;
}
Stack::Link::~Link(){}
Stack::Stack()
{
head=0;
}
void Stack::push(void * dat)
{
head=new Link(dat,head);
}
void * Stack::peek()
{
return head->data;
}
void * Stack::pop()
{
if(head==0)
return 0;
void * result =head->data;
Link* oldHead=head;//我觉得在这两处红色字处是多余的,可以省略,
head=head->next;//因为在这里可以直接让head直接指向head->next;
delete oldHead;
return result;
}
Stack::~Stack()
{
}
各位有什么见解,能谈谈吗?