(C++题目)编写一个栈类模板,

2024-12-04 03:19:17
推荐回答(2个)
回答1:

下边都是我自己写的,都是经过编译的,调试用的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::SeqStack(int sz)
{
top = -1;
maxSize = sz;
elements = new T[maxSize];
assert(elements != NULL);
}

template
SeqStack::~SeqStack()
{
delete []elements;
}

template
void SeqStack::Push(T x)
{
if(IsFull())
{
overflowProcess();
}
elements[++top] = x;
}

template
bool SeqStack::Pop(T &e)
{
if(IsEmpty())
{
return false;
}
else
{
e = elements[top--];
return true;
}
}

template
bool SeqStack::IsEmpty()
{
if(top == -1)
return true;
return false;
}

template
bool SeqStack::IsFull()
{
if(top == maxSize -1)
{
return true;
}
return false;
}

template
bool SeqStack::getTop(T &x)
{
if(IsEmpty())
{
return false;
}
x = elements[top];
}

template
int SeqStack::Length()
{
return (top+1);
}

template
void SeqStack::MakeEmpty()
{
top = -1;
}

template
void SeqStack::overflowProcess()
{
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::Output()
{
int i = 0;
if(!IsEmpty())
{
cout << "---top---" << endl;
for(i=top;i>=0;i--)
{
cout << elements[i] << endl;
}
cout << "---base---" << endl;
}
}

/*int main()
{
SeqStack s;
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 *link = NULL)
{
data = _data;
next = link;
}
StackNode(StackNode *link = NULL)
{
next = link;
}
StackNode *next;
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* top;
};

template
void LinkStack::makeEmpty()
{
StackNode *p;
while(top!=NULL)
{
p = top;
top = top->next;
delete p;
}
}

template
bool LinkStack::push(T x)
{
StackNode *e = new StackNode(x);
if(!e)
{
cerr << "Memory allocate error!" << endl;
return false;
}
e->next = top;
top = e;
return true;
}

template
bool LinkStack::pop(T &x)
{
if(!isEmpty())
{
x = top->data;
StackNode *e = top;
top = top->next;
delete e;
return true;
}
return false;
}

template
bool LinkStack::getTop(T &x)
{
if(!isEmpty())
{
x = top->data;
return true;
}
return false;
}

template
int LinkStack::getSize()
{
StackNode *p = top;
int count = 0;
while(p!=NULL)
{
count++;
p = p->next;
}
return count;
}

template
void LinkStack::outPut()
{
StackNode *p = top;
cout << "---top---" << endl;
while(p!=NULL)
{
cout << p->data << endl;
p = p->next;
}
cout << "---base---" << endl;
};

/*int main()
{
LinkStack s;
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;
}*/

回答2:

lihai