就业数据资源平台
当前位置:首页 > C语言程序设计
2013年计算机二级C语言上机试题及答案5

给定程序中,函数fun的功能是将带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:
  10、8、6、4、2。
  请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。
  注意:源程序存放在考生文件夹下的BLANK1.C中。
  不得增行或删行,也不得更改程序的结构!
  给定源程序:
  #include <stdio.h>
  #include <stdlib.h>
  #define N 5
  typedef struct node {
  int data;
  struct node *next;
  } NODE;
  void fun(NODE *h)
  { NODE *p, *q, *r;
  /
  p = h->__1__;
  /
  if (p==__2__) return;
  q = p->next;
  p->next = NULL;
  while (q)
  { r = q->next; q->next = p;
  p = q; q = __3__;
  }
  h->next = p;
  }
  NODE *creatlist(int a[])
  { NODE *h,*p,*q; int i;
  h = (NODE *)malloc(sizeof(NODE));
  h->next = NULL;
  for(i=0; i<N; i++)
  { q=(NODE *)malloc(sizeof(NODE));
  q->data=a[i];
  q->next = NULL;
  if (h->next == NULL) h->next = p = q;
  else { p->next = q; p = q; }
  }
  return h;
  }
  void outlist(NODE *h)
  { NODE *p;
  p = h->next;
  if (p==NULL) printf("The list is NULL!\n");
  else
  { printf("\nHead ");
  do
  { printf("->%d", p->data); p=p->next; }
  while(p!=NULL);
  printf("->End\n");
  }
  }
  main()
  { NODE *head;
  int a[N]={2,4,6,8,10};
  head=creatlist(a);
  printf("\nThe original list:\n");
  outlist(head);
  fun(head);
  printf("\nThe list after inverting :\n");
  outlist(head);
  }
  解题思路:
  本题是考察使用链表方法,对链表的结点数据进行降序排列。
  第一处:使用结构指针p,来控制链表的结束,p必须指向h结构指针的next指针,来定位p的初始位置。所以应填写:h->next。
  第二处:判断p指针是否结束,所以应填写:0。
  第三处:q指向原q的next指针,所以应填:r。
给定程序MODI1.C中函数fun的功能是: 计算s所指字符串中含有t所指字符串的数目, 并作为函数值返回。
  请改正函数fun中指定部位的错误, 使它能得出正确的结果。
  注意: 不要改动main函数, 不得增行或删行, 也不得更改程序的结构!
  给定源程序:
  #include <stdio.h>
  #include <string.h>
  #define N 80
  int fun(char *s, char *t)
  { int n;
  char *p , *r;
  n=0;
  while ( *s )
  { p=s;
  /****
  r=p;
  while(*r)
  if(*r==*p) { r++; p++; }
  else break;
  /****
  if(*r= 0)
  n++;
  s++;
  }
  return n;
  }
  main()
  { char a[N],b[N]; int m;
  printf("\nPlease enter string a : "); gets(a);
  printf("\nPlease enter substring b : "); gets( b );
  m=fun(a, b);
  printf("\nThe result is : m = %d\n",m);
  }
  解题思路:
  第一处: 程序中子串是由变量t来实现的,再根据下面while循环体中语句可知,所以应改为:r=t;。
  第二处: 是判断相等的条件,所以应改为:if(*r==0)。
就业数据资源平台