資源簡介
已知Q是一個非空隊列,S是一個空棧。僅用隊列和棧的ADT函數和少量工作變量,編寫一個算法,將隊列Q中的所有元素逆置
代碼片段和文件信息
#include
#include
#define?OK?1
#define?OVERFLOW?-1
#define?ERROR?0
typedef?struct
{
int?*base;
int?*top;
int?stacksize;
}SqStack;
typedef?struct?QNode
{
int?data;
struct?QNode?*next;
}QNode*QueuePtr;
typedef?struct
{
QueuePtr?front;
QueuePtr?rear;
}linkQueue;
int?InitStack(SqStack?&S)
{
S.base=(int*)malloc(100*sizeof(int));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=100;
return?OK;
}
int?Push(SqStack?&Sint?e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(int?*)realloc(S.base(S.stacksize+100)*sizeof(int));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=100;
}
*S.top++=e;
return?OK;
}
int?Pop(SqStack?&S)
{
int?e;
if(S.top==S.base)
return?ERROR;
e=*--S.top;
re
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2429??2010-05-26?22:04??queueandstack.cpp
-----------?---------??----------?-----??----
?????????????????2429????????????????????1
評論
共有 條評論