出自:安阳师范学院-计算机应用技术-数据结构
已知有向图G=(V,E),其中V={V1,V2,V3,V4},E={,,,,},图G的拓扑序列是( )。
A.V1,V2,V3,V4
B.V1,V3,V2,V4
C.V1,V3,V4,V2
D.V1,V2,V4,V3
下列程序的功能是将所有小于0的元素移到全部大于等于0的元素之前。例如,有7个整数的原始序列为(x,x,-x,-x,x,x,-x),变换后数组中保存的序列是(-x,-x,-x,x,x,x,x)。请在程序处填入合适的内容,使其成为完整的算法。
f30(int a[],int n)
{ int k,m,temp;
m= (1) ;
while (a[m]<0 &&m<n)
m= (2) ;
k=m;
while (k<n)
{ while(a[k]>=0&&k<n)
k= (3) ;
if(k<n)
{ temp=a[k];
a[k]=a[m];
a[m]= (4) ;
m= (5) ;
}
}
}
(1)
(2)
(3)
(4)
(5)
阅读下列算法,并回答问题:
(1)设顺序表L=(3,7,11,14,20,51),写出执行f30(&L,15)之后的L;
(2)设顺序表L=(4,7,10,14,20,51),写出执行f30(&L,10)之后的L;
(3)简述算法的功能。
void f30(SeqList*L, DataType x)
{
int i =0, j;
while (i<L->length && x>L->data[i])i++;
if(i<L->length && x==L->data[i]) {
for(j=i+1;j<L->length;j++)
L->data[j-1]=L->data[j];
L->length--;
} else {
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=x;
L->length++;
}
}
(1)
(2)
(3)
阅读下列程序
void f32(int A[],int n)
{
int i,j,m=l,t;
for (i=0; i<n-l&&m; i++)
{
for (j=0; j<n; j++)
printf(“%d ”,A[j]);
printf(“\n”);
m=0:
for (j=1; j<n-i; j++)
if (A[j-1]>A[j])
{
t=A[j-l];
A[j-1]=A[j];
A[j]=t;
m=1;
}
}
}
回答问题:
已知整型数组A[ ]={34,26,15,89,42},写出执行函数调用f32(A,5)后的输出结果。
已知顺序表的表结构定义如下:
#define MAXLEN 100
typedef int KeyType;
typedef struct {
KeyType key;
InfoType otherinfo;
} NodeType;
typedef NodeType SqList[MAXLEN];
阅读下列程序。
Int f33(SqList R,NodeType X, int p, int q)
{ int m;
if (p>q) return -1;
m=(p+q)/2;
if (R[m].key==X.key) return m;
if (R[m].key>X.key) return f33(R,X,p,m-l);
else return f33(R,X,m+l,q);
}
请回答下列问题:
(1)若有序的顺序表R的关键字序列为(2,5,13,26,55,80,105),分别写出X.key=18和X.key=26时,执行函数调用f33(R,X,0,6)的函数返回值。
(2)简述算法f33的功能。
设栈S=(1,2,3,4,5,6,7),其中7为栈顶元素。请写出调用algo(&s)后栈S的状态。
void algo(Stack *S)
{
int i=0;
Queue Q; Stack T;
InitQueue(&Q);InitStack(&T);
while (!StackEmpty(S))
{
if((i=!i)!=0)Push(&T,Pop(&S));
else EnQueue(&Q,Pop(&S));
}
while(!QueueEmpty(Q))
Push(&S,DeQueue(&Q));
while(!StackEmpty(T))
Push(&S,Pop(&T));
}