栈和队列
栈的抽象数据类型定义
ADT Stack{
数据对象:D={ai|ai∈ElemSet,i=1,2,…,n,n≥0}
数据关系:R={<\ai-1,ai>|ai-1,ai∈D,i=1,2,3,…,n}
基本操作:
InitStack(&S)
操作结果:构造一个空栈S.
DestroyStack(&S)
初始条件:栈S已存在。
操作结果:销毁栈S.
ClearStack(&S)
初始条件:栈S已存在。
操作结果:将S清为空栈。
StackEmpty(S)
初始条件:栈S已存在。
操作结果:若S为空栈,则返回true,否则返回False.
StackLength(S)
初始条件:栈S已存在。
操作结果:返回S的数据元素个数,即栈的长度。
GetTop(S,&e)
初始条件:栈S已存在且非空。
操作结果:用e返回S的栈顶元素。
Push(&S,e)
初始条件:栈S已存在。
操作结果:插入元素e为新的栈顶元素。
Pop(&S,&e)
初始条件:栈S已存在且非空。
操作结果:删除S的栈顶元素,并用e返回其值。
StackTraverse(S,visit())
初始条件:栈S已存在且非空。
操作结果:从栈底用到栈顶依次对S的每个数据元素调用函数visit().
}ADT Stack
栈的物理存储结构
- 栈的顺序存储表示(一维数组)
- 静态顺序栈
- 动态顺序栈
- 栈的链式存储
队列的抽象数据类型定义
ADT Queue{
数据对象:D={ai|ai∈ElemSet,i=1,2,..,n,n>=0}
数据关系:R={
队列的物理存储结构
- 顺序存储(一位数组)
- 循环队列
- 链式存储
代码实现
#include<stdio.h>
#include<malloc.h>
#define STACK_INIT_SIZE 1
#define STACKINCREMENT 10
typedef char ElemType;
struct SqStack
{
ElemType *elem;
ElemType *top;
int stacksize;
}
int InitStack(struct SqStack *S)
{
S->elem=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!S->elem)
{
printf("初始化失败");
return 0;
}
else
{
S->top=S->elem;
S->stacksize=STACK_INIT_SIZE;
printf("初始化栈成功\n");
return 1;
}
}
int Push(struct SqStack *s,ElemType x)
{
if(S->top-S-elem>=stacksize)
{
S->elem=(ElemType *)realloc(S->elem,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->elem)
{
printf("开辟空间失败\n");
return 0;
}
else
{
S->top=S->elem+s->stacksize;
S->stacksize+=STACKINCREMENT;
}
}
*s->top=x;
S->top++;
return x;
}
int Pop(struct SqStack *S,ElemType x)
{
if(S->top=S->elem)
{
printf("栈空\n");
return 0;
}
else
{
S->top--;
x=*S->top;
return x;
}
}
int GetTop(struct SqStack *S,ElemType x)
{
if(S->top==S->elem)
{
printf("栈空\n");
return 0;
}
else
{
x=*(S->top-1);
return x;
}
}
int StackLength(struct SqStack *S)
{
int n;
n=S->top-S->elem;
return (n);
}
void StackEmpty(struct SqStack *S)
{
if(S->top==S->elem)
printf("栈为空\n");
else
printf("栈不为空\n");
}
int VisitStack(struct SqStack *S)
{
int k=0;
while(S->top!=S->elem)
{
--s->top;
k++;
printf("%c",*S->top);
}
S->top+=k;
return 1;
}
int ClearStack(struct SqStack *S)
{
if(S->top==S->elem)
return 0;
else
{
S->top=S->elem;
return 1;
}
}
int DestoryStack(struct SqStack *S)
{
free(S->elem);
S->elem=NULL;
S->top=NULL;
S->stacksize=0;
return 1;
}
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct QNoe;
{
ElemType data;
struct QNode *next;
}
typedef struct
{
QNode *front;
QNode *rear;
} LinkQueue;
int InitQueue(LinkQueue *Q)
{
Q->fromt=Q->rear=(QNode *)malloc(sizeof(QNode));
if(!Q->front)
return 0;
else
{
Q->front->next=NULL;
return 1;
}
}
int EnQueue(LinkQueue *Q,ElemType x)
{
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
if(!p)
return 0;
p->data=x;
p->next=NULL;
Q->rear->next=p;
return x;
}
int GetHead(LinkQueue *Q,ElemType x)
{
if(Q->front==Q->rear)
return 0;
else
x=Q->front->next->data;
return x;
}
int QueueLength(LinkQueue *Q)
{
int i=0;
QNode *p;
if(Q->front!=Q->rear)
{
p=Q->front;
while(p->next!=NULL)
{
i++;
p=p->next;
}
}
return i;
}
int DeQueue(LinkQueue *Q,ElemType x)
{
QNode *p;
if(Q->front==Q->rear)
return 0;
p=Q->front->next;
x=p->data;
Q->front->next=p->next;
if(p==Q->rear)
Q->rear=Q->front;
free(p);
return x;
}
void QueueEmpty(LinkQueue *Q)
{
if(Q->font=Q->rear)
printf("对空\n");
else
printf("队不空\n");
}
void clearQueue(LinkQueue *Q)
{
QNode *p,*r;
p=Q->front;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
Q->rear=Q->front;
}
void DestroyQueue(LinkQueue *Q)
{
Q->front=Q->rear=NULL;
}
void VisitQueue(LinkQueue *Q)
{
QNode *p;
if(Q->front=Q->rear)
printf("队中没有元素”);
else
{
p=Q->front->next;
while(p)
{
printf("%c",p->data);
p=p->next;
}
}
}