早教吧作业答案频道 -->其他-->
数据结构的题目第一题答案已经给出来了想要知道具体过程!1.假设以数组S[0..m-1]作为循环队列的存储结构,同时设变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素
题目详情
数据结构的题目
第一题答案已经给出来了 想要知道具体过程!
1.假设以数组S[0..m-1]作为循环队列的存储结构,同时设变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素个数为 (rear-front+m)%m .
2.指出下述程序段的功能是什么?
(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);}
第一题答案已经给出来了 想要知道具体过程!
1.假设以数组S[0..m-1]作为循环队列的存储结构,同时设变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素个数为 (rear-front+m)%m .
2.指出下述程序段的功能是什么?
(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);}
▼优质解答
答案和解析
1.假设以数组S[0..m-1]作为循环队列的存储结构,同时设变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素个数为 (rear-front+m)%m .
对于普通队列,如果变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素个数为 rear-front .
考虑到这里是循环队列,所以队列中元素个数为 (rear-front+m)%m.
2.指出下述程序段的功能是什么?
(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
把栈S里的元素逆序.
(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);
}
把栈S1中的元素按序(注意不是逆序)添加到栈S2中
(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);
}
}
删除栈S中值为m的元素
(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
把Q的元素逆序.
(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);}
把Q1的元素按序复制到Q2中
对于普通队列,如果变量front和rear分别指向队头元素的前一个位置和队尾元素位置,则队列中元素个数为 rear-front .
考虑到这里是循环队列,所以队列中元素个数为 (rear-front+m)%m.
2.指出下述程序段的功能是什么?
(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
把栈S里的元素逆序.
(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);
}
把栈S1中的元素按序(注意不是逆序)添加到栈S2中
(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);
}
}
删除栈S中值为m的元素
(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
把Q的元素逆序.
(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);}
把Q1的元素按序复制到Q2中
看了 数据结构的题目第一题答案已经...的网友还看了以下:
足球比赛的计分情况如下:胜一场得3分,负一场得0分,平一场得1分,已知A,B,C,D四个球队分在同 2020-04-26 …
以下哪一项不是队列的基本运算?A.从队尾插入一个新元素B.从队列中删除第1个元素C.判断一个队列是 2020-05-23 …
以下哪一个不是队列的基本运算?A.从队尾插入一个新元素B.从队列中删除第1个元素C.判断一个队列是 2020-05-24 …
若有5个球队进行单循环比赛,已知它们的比赛结果为:1队胜2、3队;2队胜3、4、5队;4队胜1、3 2020-06-12 …
一队学生去军事训练,走到半路,队长有事要从队头通知到队尾,通讯员以18米/分的速度从队头至队尾又返 2020-06-14 …
一队学生去军事训练,走到半路,队长有事要从队头通知到队尾,通讯员以18米/分的速度从队头至队尾又返 2020-06-14 …
一队学生去军事训练,走到半路,队长有事要从队头通知到队尾,通讯员以18米/分的速度从队头至队尾又返 2020-06-14 …
甲、已两队运一批货,甲队每天能运送64.4吨,比已队每天多运3/4,如果甲、已两队同时运送,当甲队运 2020-12-15 …
c++实现一个队列的问题1)加入一个元素到队列(入队IN);2)将一个元素移出队列(出队OUT);3 2020-12-22 …
已知队列(13,2,11,34,41,77,5,7,18,26,15),第一个进入队列的元素是13, 2020-12-24 …