下边都是我自己写的,都是经过编译的,调试用的main函数给注释掉了
不过遗憾是没有写注释,不过函数名字见名知意,希望你会满意。
用数组作为储存结构的顺序栈
#include
#include
using namespace std;
const int INCREMENT = 20;
template
class SeqStack
{
public:
SeqStack(int size = 50);
~SeqStack();
void Push(T x);
bool Pop(T& e);
bool IsEmpty();
bool IsFull();
int Length();
bool getTop(T& x);
void MakeEmpty();
void Output();
private:
T* elements;
int top;
int maxSize;
void overflowProcess();
};
template
SeqStack
{
top = -1;
maxSize = sz;
elements = new T[maxSize];
assert(elements != NULL);
}
template
SeqStack
{
delete []elements;
}
template
void SeqStack
{
if(IsFull())
{
overflowProcess();
}
elements[++top] = x;
}
template
bool SeqStack
{
if(IsEmpty())
{
return false;
}
else
{
e = elements[top--];
return true;
}
}
template
bool SeqStack
{
if(top == -1)
return true;
return false;
}
template
bool SeqStack
{
if(top == maxSize -1)
{
return true;
}
return false;
}
template
bool SeqStack
{
if(IsEmpty())
{
return false;
}
x = elements[top];
}
template
int SeqStack
{
return (top+1);
}
template
void SeqStack
{
top = -1;
}
template
void SeqStack
{
T* newElems = new T[maxSize+INCREMENT];
if(newElems == NULL)
{
cerr << "Memory allocate error!" << endl;
exit(1);
}
for(int i=0;i<=top;i++)
{
newElems[i] = elements[i];
}
maxSize = maxSize + INCREMENT;
delete []elements;
elements = newElems;
}
template
void SeqStack
{
int i = 0;
if(!IsEmpty())
{
cout << "---top---" << endl;
for(i=top;i>=0;i--)
{
cout << elements[i] << endl;
}
cout << "---base---" << endl;
}
}
/*int main()
{
SeqStack
int i=0;
int e;
for(;i<60;i++)
{
s.Push(i*2);
}
cout << "length:" << s.Length() << endl;
s.Output();
return 0;
}*/
用链表作为储存结构的链式栈
#include
#include
using namespace std;
const int INCREMENT=20;
template
class StackNode
{
public:
StackNode(T _data,StackNode
{
data = _data;
next = link;
}
StackNode(StackNode
{
next = link;
}
StackNode
T data;
private:
};
template
class LinkStack
{
public:
LinkStack(){top = NULL;}
~LinkStack(){makeEmpty();}
bool isEmpty(){return (top == NULL);}
bool push(T x);
bool pop(T &x);
int getSize();
bool getTop(T &e);
void outPut();
void makeEmpty();
private:
StackNode
};
template
void LinkStack
{
StackNode
while(top!=NULL)
{
p = top;
top = top->next;
delete p;
}
}
template
bool LinkStack
{
StackNode
if(!e)
{
cerr << "Memory allocate error!" << endl;
return false;
}
e->next = top;
top = e;
return true;
}
template
bool LinkStack
{
if(!isEmpty())
{
x = top->data;
StackNode
top = top->next;
delete e;
return true;
}
return false;
}
template
bool LinkStack
{
if(!isEmpty())
{
x = top->data;
return true;
}
return false;
}
template
int LinkStack
{
StackNode
int count = 0;
while(p!=NULL)
{
count++;
p = p->next;
}
return count;
}
template
void LinkStack
{
StackNode
cout << "---top---" << endl;
while(p!=NULL)
{
cout << p->data << endl;
p = p->next;
}
cout << "---base---" << endl;
};
/*int main()
{
LinkStack
char e;
int a = 65;
for(int i=0;i<5;i++)
{
s.push(char(a+i));
}
s.outPut();
s.makeEmpty();
s.outPut();
for(int i=0;i<6;i++)
{
s.pop(e);
cout << s.getSize()<< endl;
s.outPut();
}
return 0;
}*/
lihai