C++数据结构学习:栈和队列
2008-03-08 21:36:28 来源:WEB开发网 闁靛棴鎷�

核心提示:栈和队列是操作受限的线性表,似乎每本讲数据结构的数都是这么说的,C++数据结构学习:栈和队列,有些书按照这个思路给出了定义和实现;但是很遗憾,这本书没有这样做,栈和队列的实现是如此的简单,这也是我对于原书重复建设不满的最大原因,所以,原书中的做法是重复建设
栈和队列是操作受限的线性表,似乎每本讲数据结构的数都是这么说的。有些书按照这个思路给出了定义和实现;但是很遗憾,这本书没有这样做,所以,原书中的做法是重复建设,这或许可以用不是一个人写的这样的理由来开脱。
栈的定义和实现
#ifndef Stack_H
#define Stack_H
#include "List.h"
template
{
public:
void Push(Type value)
{
Insert(value);
}
Type Pop()
{
Type p = *GetNext();
RemoveAfter();
return p;
}
Type GetTop()
{
return *GetNext();
}
List
List
};
#endif

#ifndef Queue_H
#define Queue_H
#include "List.h"
template
{
public:
void EnQueue(const Type &value)
{
}
Type DeQueue()
{
Type p = *GetNext();
RemoveAfter();
IsEmpty();
return p;
}
Type GetFront()
{
return *GetNext();
}
List
List
};
#endif

#ifndef StackTest_H
#define StackTest_H
#include "Stack.h"
void StackTest_int()
{
cout << endl << "整型栈测试" << endl;
cout << endl << "构造一个空栈" << endl;
Stack
for (int i = 1; i <= 20; i++) a.Push(i);
while (!a.IsEmpty()) cout << a.Pop() << ' ';
cout << endl;
}
#endif
#ifndef QueueTest_H
#define QueueTest_H
#include "Queue.h"
void QueueTest_int()
{
cout << endl << "整型队列测试" << endl;
cout << endl << "构造一个空队列" << endl;
Queue
cout << "将1~20入队,然后再出队" << endl;
for (int i = 1; i <= 20; i++) a.EnQueue(i);
while (!a.IsEmpty()) cout << a.DeQueue() << ' ';
cout << endl;
}
#endif
【后记】没什么好说的,你可以清楚的看到,在单链表的基础上,栈和队列的实现是如此的简单,这也是我对于原书重复建设不满的最大原因。

更多精彩
赞助商链接