3栈和队列答案.docVIP

  • 160
  • 0
  • 约8.62千字
  • 约 8页
  • 2017-02-07 发布于重庆
  • 举报
3栈和队列答案

第3章 栈和队列 一、基础知识题 3.1 设将整数1,2,3,4依次进栈,但只要出栈时栈非空,则可将出栈操作按任何次序夹入其中,请回答下述问题:   (1)若入、出栈次序为Push(1), Pop(),Push(2),Push(3), Pop(), Pop( ),Push(4), Pop( ),则出栈的数字序列为何(这里Push(i)表示i进栈,Pop( )表示出栈)?   (2)能否得到出栈序列1423和1432?并说明为什么不能得到或者如何得到。   (3)请分析 1,2 ,3 ,4 的24种排列中,哪些序列是可以通过相应的入出栈操作得到的。 3.2 链栈中为何不设置头结点? 3.3 循环队列的优点是什么? 如何判别它的空和满? 3.4 设长度为n的链队用单循环链表表示,若设头指针,则入队出队操作的时间为何? 若只设尾指针呢? 3.5 指出下述程序段的功能是什么? (1) void Demo1(SeqStack *S){     int i; arr[64] ; n=0 ;     while ( StackEmpty(S)) arr[n++]=Pop(S);     for (i=0, i n; i++) Push(S, arr[i]);    } //Demo1 (2) SeqStack S1, S2, tmp;   DataType x;   ...//假设栈tmp和S2已做过初始化   while ( ! StackEmpty (S1))    {     x=Pop(S1) ;     Push(tmp,x);    }   while ( ! StackEmpty (tmp) )    {     x=Pop( tmp);     Push( S1,x);     Push( S2, x);    } (3) void Demo2( SeqStack *S, int m)    { // 设DataType 为int 型     SeqStack T; int i;     InitStack (T);     while (! StackEmpty( S))      if(( i=Pop(S)) !=m) Push( T,i);     while (! StackEmpty( T))      {       i=Pop(T); Push(S,i);      }    } (4)void Demo3( CirQueue *Q)    { // 设DataType 为int 型     int x; SeqStack S;     InitStack( S);     while (! QueueEmpty( Q ))      {x=DeQueue( Q); Push( S,x);}     while (! StackEmpty( s))      { x=Pop(S); EnQueue( Q,x );}    }// Demo3 (5) CirQueue Q1, Q2; // 设DataType 为int 型   int x, i , n= 0;   ... // 设Q1已有内容, Q2已初始化过   while ( ! QueueEmpty( Q1) )    { x=DeQueue( Q1 ) ; EnQueue(Q2, x); n++;}   for (i=0; i n; i++)    { x=DeQueue(Q2) ;   EnQueue( Q1, x) ; EnQueue( Q2, x);} 3.6 简述栈和线性表的差别。 3.7 写出下列程序段的输出结果(栈的元素类型SElemType为char)。 void main() { Stack S; char x,y; InitStack(S); x= ‘c’; y= ‘k’; Push(S,x); Push(S, ‘a’); Push(S,y); Pop(S,x); Push(S, ‘t’); Push(S,x); Pop(S,x); Push(S, ‘s’); while(!StackEmpty(S)) { Pop(S,y); printf(y); } printf(x); } 3.8 写出下列中缀表达式的后缀形式: (1) A * B * C (2) - A + B - C + D (3) A* - B + C (4) (A + B) * D + E / (F + A * D) + C (5) A B|| ! (E F) /*注:按C++的优先级*/ (6) !(A !( (B C)||(C D) ) )||(C E) 二、算法设计题:

文档评论(0)

1亿VIP精品文档

相关文档