大家帮忙看看我的通用栈错在哪里
//stack.h#ifndef STACK_H_
#define STACK_H_
template <class T>
class LinkStack
{
public:
LinkStack(){top = NULL;}
~LinkStack() {};
struct node {
T data;
node *next;
};
void SetEmpty();
bool IsEmpty();
bool Push(T element);
bool Pop(T & element);
void show();
private:
node * top;
};
#endif
// stack.cpp
#include "stack.h"
template <class T> void LinkStack <T>::SetEmpty()
{
node * temp;
while (top != NULL)
{
temp = top;
top = top->next;
delete temp;
}
}
template <class T> bool LinkStack <T>::IsEmpty()
{
return top == NULL;
}
template <class T> bool LinkStack <T>::Push(T element)
{
node *temp = new node();
if (temp == NULL)
return false;
temp->data = element;
temp->next = top;
top = temp;
return true;
}
template <class T> bool LinkStack <T>::Pop(T & element)
{
if (IsEmpty())
return false;
node *q = top;
element = top->data;
top = top->next;
delete q;
return true;
}
template <class T> void LinkStack <T>::show(T & element)
{
node *temp = top;
std::cout << std::endl << "----top----" << std::endl;
while (temp->next != NULL)
{
std::cout << " " << temp->data <<std::endl;
temp = temp->next;
}
std::cout << "----top----" << std::endl << std:endl;
}
//main.cpp
#include <iostream>
#include "stack.h"
int main(int argc, char **argv)
{
LinkStack<int> st;
int a = 7;
st.Push(a);
st.Push(5);
st.Push(8);
st.show();
return 0;
}