只是个人浅显的意见而已,不敢妄图说对。我只是希望我做的能对你有点帮助就行,别嘲笑我哈,我是新手。
#include
using namespace std;
struct StackNode
{
int data;
struct StackNode *Next;
};
struct Stack_T
{
struct StackNode *head;
int size;
};
class Stack
{
public:
Stack(){
Node = (new StackNode);
T = new Stack_T;
}
Stack_T *CreateStack();
void StackPush(Stack_T *pStack, int num);
int StackPop(Stack_T*pStack);
int StackTop(Stack_T*pStack);
int StackSize(Stack_T *pStack);
void FreeStack(Stack_T *pStack);
~Stack();
private:
StackNode *Node;
Stack_T *T;
};
Stack_T *Stack::CreateStack()
{
Stack *p =new Stack;
StackNode *head;
head = this->T->head;
head->Next = p->Node;
return this->T;
}
void Stack::StackPush(Stack_T *pStack, int num){
pStack->head->data = num;
}
int Stack::StackPop(Stack_T*pStack)
{
int num;
num = pStack->head->data;
pStack->head = pStack->head->Next;
return num;
}
int Stack::StackSize(Stack_T *pStack)
{
StackNode *p = pStack->head;
int n=0;
if (p == NULL)
{
cout << "空链表" << endl;
return 0;
}
else while (p != NULL)
{
p = p->Next; ++n;
}
return n;
}
void Stack::FreeStack(Stack_T *pStack)
{
StackNode *p, *q;
while (p != NULL)
{
p = pStack->head;
q = p;
p = q->Next;
delete q;
}
delete pStack;
}