《数据结构》程序填空复习题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《数据结构》程序填空复习题

说明:本文档中涉及到的算法并非本书的全部,有些可根据此处的情况自行看书和作业题,黑色为综合练习上的题目,红色为我另增加的题,这些空的选择是根据我个人的经验来决定的并不能完全代表中央电大的出卷老师,因此一定不能有肯定就考这些题目的想法。不能放弃其他内容的复习,切记!!!

一、线性表

1.设线性表为(6,10,16,4),以下程序用说明结构变量的方法建立单向链表,并输出链表中各结点中的数据。

#define NULL 0

void main( )

{NODE a,b,c,d,*head,*p;

a.data=6;

b.data=10;

c.data=16;

d.data=4; /*d是尾结点*/

head= (1);

a.next=&b;

b.next=&c;

c.next=&d;

(2); /*以上结束建表过程*/

p=head; /*p为工作指针,准备输出链表*/

do

{printf(“%d\n”, (3));

(4);

}while( (5));

}

答案:

(1)&a

(2)d next=NULL

(3)p->data

(4)p=p->next

(5)p!=NULL

2. 以下函数在head为头指针的具有头结点的单向链表中删除第i个结点,

struct node

{ int data;

struct node *next;

};

typedef struct node NODE

int delete(NODE *head,int i )

{

NODE *p,*q;

int j;

q=head;

j=0;

while((q!=NULL)&&( ___(1)_____))

{

___(2)_____;

j++;

}

if(q==NULL)

return(0);

p= ___(3)_____;

___(4)_____=p->next;

free(___(5)_____);

return(1);

}

答案:

(1)j

(2)q=q->next

(3)q->next

(4)q->next

(5)p

3.将新元素插入到线性表中的第i位,MAX是数组的个数,a[0]用以存放线性表长度,b存放待插入的元素值,i存放插入的位置,n存放线性表长度

{

int a[MAX];

int i,j,b,n;

scanf(“%d%d%d”,&b,&i,&n);

for(j=1;j<=n;j++)

scanf(“%d”,&a[j]);

a[0]=n;

for(j=n; (1);j- -)

(2);

(3);

(4);

for(j=1;j<=a[0];j++)

printf(“%5d\n”,a[j]);

}

答案:

(1)j>=i

(2)a[j+1]=a[j]

(3)a[i]=b

(4)a[0]=n+1

4.用头插法建立带头结点且有n个结点的单向链表的算法

NODE *create(n)

{

NODE *head,*p,*q;

int i

p=(NODE *)malloc(sizeof(NODE));

(1);

(2);

(3);

for(i=1;i<=n;i++)

{

p=(NODE *)malloc(sizeof(NODE));

p->data=i;

if(i==1)

(4);

else

{

(5);

(6);

}

}

return(head);

}

答案:

(1)head=p

(2)p->next=NULL

(3)q=p

(4)p->next=NULL

(5)p->next=q->next

(6)q->next=p

一、栈

1. 以下函数为链栈的进栈操作,x是要进栈的结点的数据域,top为栈顶指针

struct node

{ ElemType data;

struct node *next;

};

struct node *top ;

void Push(ElemType x)

{

struct node *p;

p=(struct node*)malloc(___(1)_____);

p->data=x;

___(2)_____;

}

答案:

(1)sizeof (struct node)

(2)p->next=top

(3)top=p

二、队列

1. 以下函数为链队列的入队操作,x为要入队的结点的数据域的值,front、rear分别是

链队列的队头、队尾指针

struct node

{ ElemType data;

struct node *next;

};

struct node *front,*rear;

void InQueue(ElemType x)

{

struct node *p;

p= (struct node*) ___(1)_____;

p->data=x;

p->next=NULL;

___(2)_____;

rear= ___(3)_____;

}

答案:

(1)malloc(sizeof (struct node))

(2)rear->next=p

(3)p

2. 以下函数为链队列的出队操作(链队列带有头结点),出队结点的数据域的值由x返

回,front、rear分别是链队列的队头、队尾指针

struct node

{ ElemType data;

struct node *next;

};

struct node *front,*rear;

ElemType OutQueue()

{

ElemType x;

if(___(1)_____) {

printf("队列下溢错误!\n");

exit(1);

}

else {

相关文档
最新文档