主题:[讨论]栈与队列的实现
实验:栈与队列的实现
①分别创建最大长度为10的顺序栈与循环顺序队列。
②将1、2、3、4、5、6依次入栈,然后若栈不空,边弹栈,边打印输出,边入队,直到入队完毕。
③若队列不空,将元素边出队,边打印输出,边入栈。
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 5
#define MAXQSIZE 10
typedef int ElemType;
typedef struct StackNode
{ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
typedef struct
{ElemType *base;
int front;
int rear;
}SqQueue;
ElemType InitStack(SqStack **S)
{(*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!(*S)->base) exit(OVERFLOW);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
ElemType StackEmpty(SqStack S)
{if(S.top==S.base) return OK;
else return ERROR;
}
ElemType Push(SqStack *S,ElemType e)
{if(S->top-S->base>=S->stacksize)
{S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
ElemType Pop(SqStack *S,ElemType e)
{if(S->top==S->base) return ERROR;
e=*--S->top;
printf("%d\n",e);
return e;
}
ElemType InitQueue(SqQueue **Q)
{(*Q)->base=(ElemType *)malloc(MAXQSIZE * sizeof(ElemType));
if(!(*Q)->base) exit(OVERFLOW);
(*Q)->front=(*Q)->rear=0;
return OK;
}
ElemType QueueEmpty(SqQueue Q)
{if(Q.front==Q.rear) return OK;
else return ERROR;
}
ElemType EnQueue(SqQueue *Q,ElemType x)
{if((Q->rear+1)%MAXQSIZE==Q->front) return ERROR;
Q->base[Q->rear]=x;
Q->rear=(Q->rear+1)%MAXQSIZE;
return OK;
}
ElemType DeQueue(SqQueue *Q,ElemType x)
{if(Q->front==Q->rear) return ERROR;
x=Q->base[Q->front];
printf("%d\n",x);
Q->front=(Q->front+1)%MAXQSIZE;
return x;
}
main()
{int i;
SqStack *S;
SqQueue *Q;
ElemType e,x;
InitStack(&S);
InitQueue(&Q);
printf("Enter 1 to 6:\n");
for(i=0;i<6;i++)
{scanf("%d",&e);
Push(S,e);
}
while(!StackEmpty(*S))
EnQueue(Q,Pop(S,e));
while(!QueueEmpty(*Q))
Push(S,DeQueue(Q,x));
}
程序表面看似正确,但调试后发现有点小错误,错误显示 Null pointer assignment。 [color=FF0000][size=3]请诸位高手指点迷津![/size][/color] [em10]
①分别创建最大长度为10的顺序栈与循环顺序队列。
②将1、2、3、4、5、6依次入栈,然后若栈不空,边弹栈,边打印输出,边入队,直到入队完毕。
③若队列不空,将元素边出队,边打印输出,边入栈。
#include "stdio.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 5
#define MAXQSIZE 10
typedef int ElemType;
typedef struct StackNode
{ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
typedef struct
{ElemType *base;
int front;
int rear;
}SqQueue;
ElemType InitStack(SqStack **S)
{(*S)->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!(*S)->base) exit(OVERFLOW);
(*S)->top=(*S)->base;
(*S)->stacksize=STACK_INIT_SIZE;
return OK;
}
ElemType StackEmpty(SqStack S)
{if(S.top==S.base) return OK;
else return ERROR;
}
ElemType Push(SqStack *S,ElemType e)
{if(S->top-S->base>=S->stacksize)
{S->base=(ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
ElemType Pop(SqStack *S,ElemType e)
{if(S->top==S->base) return ERROR;
e=*--S->top;
printf("%d\n",e);
return e;
}
ElemType InitQueue(SqQueue **Q)
{(*Q)->base=(ElemType *)malloc(MAXQSIZE * sizeof(ElemType));
if(!(*Q)->base) exit(OVERFLOW);
(*Q)->front=(*Q)->rear=0;
return OK;
}
ElemType QueueEmpty(SqQueue Q)
{if(Q.front==Q.rear) return OK;
else return ERROR;
}
ElemType EnQueue(SqQueue *Q,ElemType x)
{if((Q->rear+1)%MAXQSIZE==Q->front) return ERROR;
Q->base[Q->rear]=x;
Q->rear=(Q->rear+1)%MAXQSIZE;
return OK;
}
ElemType DeQueue(SqQueue *Q,ElemType x)
{if(Q->front==Q->rear) return ERROR;
x=Q->base[Q->front];
printf("%d\n",x);
Q->front=(Q->front+1)%MAXQSIZE;
return x;
}
main()
{int i;
SqStack *S;
SqQueue *Q;
ElemType e,x;
InitStack(&S);
InitQueue(&Q);
printf("Enter 1 to 6:\n");
for(i=0;i<6;i++)
{scanf("%d",&e);
Push(S,e);
}
while(!StackEmpty(*S))
EnQueue(Q,Pop(S,e));
while(!QueueEmpty(*Q))
Push(S,DeQueue(Q,x));
}
程序表面看似正确,但调试后发现有点小错误,错误显示 Null pointer assignment。 [color=FF0000][size=3]请诸位高手指点迷津![/size][/color] [em10]