问答题 栈和队列
   实验目的:
   (1)掌握栈和队列的数据结构的特点。
   (2)熟练掌握在两种存储结构上实现栈和队列的基本运算。
   (3)学会利用栈和队列解决一些实际问题。
   (4)掌握和理解本实验中出现的一些基本的C语言语句。
   (5)体会算法在程序设计中的重要性。
   实验内容:
   (1)写一算法将一顺序栈中的元素依次取出,并打印元素值。
   (2)写一算法将一链栈中的元素依次取出,并打印元素值。
   (3)写一算法将一顺序队列中的元素依次取出,并打印元素值。
   (4)写一算法将一链队列中的元素依次取出,并打印元素值。
【正确答案】(1)
   #define NULL '\0'    /*用顺序栈处理元素按逆序输出*/
   #define DATATYPE char
   #define MAXSIZE 100
   #include"stdio.h"
   typedef struct
   {DATATYPE data[MAXSIZE];
       int top;
   }SEQSTACK;
   void initstack(SEQSTACK*s)    /*顺序栈初始化算法*/
   {  s->top=-1;)
       void push(SEQSTACK*s,DATATYPE x)  /*顺序栈入栈算法*/
       {  if(s->top==MAXSIZE-1)
       {printf("overflow\n");exit(0);}
           else
           {  s->top++;
               s->data[s->top]=x;
       }
   }
   int empty(SEQSTACK*s)    /*顺序栈判栈空算法*/
   { if(s->top==-1)
           return 1;
       else
           return 0;
       }
   DATATYPE pop(SEQSTACK*s)    /*顺序栈出栈算法*/
   {DATATYPE x;
       if(empty(s))
       {printf("underflow\n");x=NULL;)
       else
       {x=s->data[s->top];
           s->top--:
       }
       return x;
   }
   main()
   { SEQSTACK s,*p;
       char x,y;
       p=&s;
       initstack(p);
       while((x=getchar())!='$')
       push(p,x);
       while(!empty(p))
       { y=pop(p);
           printf("/%3c",y);
       }
   }
   (2)
   #define DATATYPE int    /*用链栈处理整数序列按逆序输出*/
   #define NULL 0
   typedef struct snode
   {DATATYPE data;
       struct snode*next;
   }LINKSTACK;
   LINKSTACK*top=NULL;
   void pushstack(DATATYPE x)    /*链栈入栈算法*/
   {LINKSTACK*p;
       p=(LINKSTACK*)malloc(sizeof(LINKSTACK));
       p->data=x;
       p->next=top;
       top=p;
   }
   DATATYPE popstack()    /*链栈出栈算法*/
   {LINKSTACK*p;
       DATATYPE v;
       if(top==NULL)
       {printf("underflow\n");v=NULL;
       }
       else
       {  v=top->data;
           p=top;
           top=top->next;
           free(p);
       }
       return v;
   }
   main()
   { int x,y;
       scanf("/%d",&x);
       while(x!=0)
       {pushstack(x);scanf("/%d",&x);
       }
       while(top!=NULL)
       {    y=popstack();
           printf("/%4d",y);}
       }
   (3)
   #define NULL '\0'    /*用循环队列处理元素顺序输出*/
   #define DATATYPE char
   #define MAXSIZE 100
   #include"stdio.h"
   typedef struct
   {DATATYPE data[MAXSIZE];
       int front,rear;
   }SEQUEUE;
   void initqueue(SEQUEUE*q)    /*循环队列初始化算法*/
   {  q->front=-1; q->rear=-1;}
   void enqueue(SEQUEUE*q,DATATYPE x)    /*循环队列入队算法*/
   {  if(q->front==(q->rear+1)/%MAXSIZE)
       {printf("queue is full\n");exit(0);}
       else
       {  q->rear=(q->rear+1)/%MAXSIZE;
           q->data[q->rear]=x;
       }
   }
   int empty(SEQUEUE*q)    /*循环队列判队空算法*/
   { if(q->rear==q->front)
           return 1;
       else
           return 0;
   }
   DATATYPE dequeue(SEQUEUE*q)    /*循环队列出队算法*/
   {DATATYPE v;
       if(empty(q))
       {printf("queue is null\n");v=NULL;}
       else
       {  q->front=(q->front+1)/%MAXSIZE;
           v=q->data[q->front];
       }
       return v;
   }
   main()
   { SEQUEUE a,*q;
       char x,y;
       q=&a;
       initqueue(q);
       while((x=getchar())!='$')
       enqueue(q,x);
       while(!empty(q))
       {  y=dequeue(q);
           printf("/%3c",y);
       }
   }
   (4)
   #define DATATYPE int    /*用链队列处理整数序列顺序输出*/
   #define NULL 0
   typedef struct qnode
   {DATATYPE data;
       struct qnode*next;
   }LINKNODE;
   typedef struct
   {LINKNODE*front,*rear;)LINKQUEUE;
   void initlinkqueue(LINKQUEUE*q)    /*链队列初始化算法*/
   { q->front=(LINKNODE*)malloc(sizeof(LINKNODE));
       q->front->next:NULL;q->rear=q->front;
   }
   int emptylinkqueue(LINKQUEUE*q)    /*链队列判队空算法*/
   {  int v;
       if(q->front==q->rear)
           v=1;
       else
           v=0;
       return v;
   }
   void enlinkqueue(LINKQUEUE*q,DATATYPE x)  /*链队列入队算法*/
   {  q->rear->next=(LINKNODE*)malloc(Sizeof(LINKNODE));
       q->rear=q->rear->next;
       q->rear->data=x;
       q->rear->next=NULL;
   }
   DATATYPE dellinkqueue(LINKQUEUE*q)    /*链队列出队算法*/
   {LINKNODE*p;
       DATATYPE v;
       if(emptylinkqueue(q))
       {printf("queue if empty\n");v=NULL;}
       else
       {p=q->front->next;
           q->front->next=p->next;
           if(p->next==NULL)
               q->rear=q->front;
           v=p->data;
           free(p);
       }
       return v;
   }
   main()
   {LINKQUEUE*q,a;
       int x,y;
       q=&a;
       initlinkqueue(q);
       scanf("/%d",&x);
       while(x!=0)
       {enlinkqueue(q,x); scanf("/%d",&x);
       }
       while(!emptylinkqueue(q))
       {  y=dellinkqueue(q);
           printf("/%4d",y);
           }
   }
【答案解析】