niusouti.com

下面程序的功能是建立一个有 3 个 结 点的单向循环链表,然后求各个 结 点数值域 data 中数据的和。请填空。include <stdio.h>include <stdlib.h>struct NODE{ int data;struct NODE *next;};main(){ struct NODE *p,*q,*r;int sum=0;p=(struct NODE*)malloc(sizeof(struct NODE));q=(struct NODE*)malloc(sizeof(struct N

题目

下面程序的功能是建立一个有 3 个 结 点的单向循环链表,然后求各个 结 点数值域 data 中数据的和。请填空。

include <stdio.h>

include <stdlib.h>

struct NODE{ int data;

struct NODE *next;

};

main()

{ struct NODE *p,*q,*r;

int sum=0;

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

q=(struct NODE*)malloc(sizeof(struct NODE));

r=(struct NODE*)malloc(sizeof(struct NODE));

p->data=100; q->data=200; r->data=200;

p-> next =q; q-> next =r; r-> next =p;

sum=p->data+p->next->data+r->next->next 【 19 】 ;

printf("%d\n",sum);

}


相似考题

3.论述题 3 :针对以下 C 语言程序,请按要求回答问题( 18 分)已知 link.c 源程序如下:/*link.c 程序对单向链表进行操作 , 首先建立一个单向链表 , 然后根据用户的选择可以对其进行插入节点 、删除节点和链表反转操作 */#include#includetypedef struct list_node *list_pointer; // 定义链表指针typedef struct list_node{ // 定义链表结构int data;list_pointer link;}list_node;// 用到的操作函数:list_pointer create(); // 建立一个单向链表void insert(list_pointer *p_ptr, list_pointer node); // 在 node 后加入一个新的节点void delete_node(list_pointer *p_ptr, list_pointer trail, list_pointer node);// 删除前一个节点是 trail 的当前节点 nodevoid print(list_pointer ptr); // 打印链表节点中的值list_pointer invert(list_pointer lead); // 反转链表int main(){list_pointer ptr=NULL;list_pointer node, trail;list_pointer *p = &ptr;int choose, location, i;printf("you should create a link first:\n");// 建立一个单向链表:ptr=create(); /* ptr 指向链表的第一个节点 */print(ptr);// 根据用户的不同选择进行相应的操作:printf("input number 0, you can quit the program\n");printf("input number 1, you can insert a new node to link\n");printf("input number 2, you can delete a node from the link\n");printf("input number 3, you can invert the link\n");printf("please input your choice\n");scanf("%d", &choose);while(choose!=0){switch(choose){case 1:printf("you will insert a node to the link\n");printf("please input the location of the node:\n");scanf("%d", &location);node = ptr;i = 1;while(i<location){node = node->link;i++;}insert(p, node); /* p 为指向 ptr 的指针 */print(ptr);break;case 2:printf("you will delete a node from the link\n");printf("please input the location of the node:\n");scanf("%d", &location);node = ptr;if(location ==1)trail = NULL;trail = ptr;i = 1;while(i<location){trail = trail->link;i++;}node = trail->link;delete_node(p, trail, node);print(ptr);break;case 3:printf("you will invert the link\n");ptr = invert(ptr);print(ptr);break;default:break;return -1;}printf("please input your choice\n");scanf("%d", &choose);}return 0;}// 根据用户的输入数值建立一个新的单向链表:list_pointer create(){int i, current, length;list_pointer p1, p2, head;printf("please input the node number of the link:\n");scanf("%d", &length);printf("the number of the link is : %d\n", length);printf("please input the data for the link node:\n");i =0;p1= p2= (list_pointer) malloc(sizeof(list_node));head = p1;for(i = 0; iscanf("%d", ¤ t);p1->data = current;p2->link = p1;p2 = p1;p1 = (list_pointer) malloc(sizeof(list_node));}p2->link = NULL;return head;}……( 1 )画出主函数 main 的控制流程图。( 10 分)( 2 ) 设计一组测试用例 , 尽量使 main 函数的语句覆盖率能达到 100% 。 如果认为该函数的语句覆盖率无法达到 100% ,需说明原因。( 8 分)

更多“ 下面程序的功能是建立一个有 3 个 结 点的单向循环链表,然后求各个 结 点数值域 data 中数据的和。请填空。include stdio.hinclude stdlib.hstruct NODE{ int d”相关问题
  • 第1题:

    以下程序的功能是:建立一个带布头结点的单向链表,并将存储在数组中的字符依次存储到链表的各个结点中,请从与下划线处号码对应的一组选项中选择出正确的选项

    #include <stdlib.h>

    struct node

    {char data; struct node *next;};

    (48) CreatList(char*s),

    {struct node *h,*p,*q;

    h=(struct node*)malloc(sizeof(struct node));

    p=q=h;

    while(*s!="\0")

    { p=(struct node*)malloc(sizeof(struct node));

    p->data= (49) ;

    q->next=p;

    q= (50) ;

    s++;

    }

    p->next="\0";

    return h;

    }

    main()

    { char str[]="link list";

    struct node*head;

    head=CreatList(str);

    }

    (1)

    A.char*

    B.struct node

    C.struct node*

    D.char


    正确答案:C

  • 第2题:

    以下程序实现栈的入栈和出栈的操作。其中有两个类:一个是节点类node,它包含点值和指向上一个节点的指针 prev;另一个类是栈类 stack, 它包含栈的头指针 top。

    生成的链式栈如下图所示。

    〈IMG nClick=over(this) title=放大 src="tp/jsj/2jc++j28.1.gif"〉

    下面是实现程序,请填空完成此程序。

    include 〈iostream〉

    using namespace std;

    class stack;

    class node

    {

    int data;

    node *prev;

    public:

    node(int d, node *n)

    {

    data=d;

    prev=n;

    }

    friend class stack;

    };

    class stack

    {

    node *top; //栈头

    public:

    stack()

    {

    top=0;

    }

    void push(int i)

    {

    node *n=【 】;

    top=n;

    }

    int pop()

    {

    node *t=top;

    if (top)

    {

    top=top-〉prev;

    int c= t-〉data;

    delete t;

    return c;

    }

    return 0;

    }

    int main ()

    {

    stack s;

    s.push(6);

    s.push(3);

    s.push (1);

    return 0;

    }


    正确答案:new node(itop)
    new node(i,top) 解析:本题考核友元类以及对象成员的应用,属于综合考题。本程序中定义了两个类node和stack,用于实现堆栈的压入和弹出操作。其中,类 stack是类node的友元类,这样类stack中的成员可以访问类node中的所有成员。在类node中,定义两个私有变量:整型变量data和对象指针prev。变量data用于保存节点的数值,而对象指针prey用于指向上一节点。在类node的构造函数中,形参是数据d和对象指针n。在类stack中,定义了一个私有变量,栈顶指针top,并在构造函数中赋值0(即指针为空)。
    函数push()实现入栈操作,即把形参i压入栈中,那么此时应该创建一个新的节点,并让这个节点的prev指针指向栈顶,即top。然后让top指针指向新的节点。所以在push()函数中应填入“node*n=new node(i,top)”。类stack中的pop()函数实现数据的弹出功能。先定义了一个对象指针t指向栈顶节点。然后判断堆栈是否为空,如果为空,则返回0,否则就弹出栈顶节点的值。那么应该先将栈顶指针后退一个节点,然后把对象t指针指向的节点值弹出,并删除节点t。

  • 第3题:

    请补充函数fun(),该函数的功能是建立一个带头结点的单向链表并输出到文件“out98.dat”和屏幕上,各结点的值为对应的下标,链表的结点数及输出的文件名作为参数传入。

    注意:部分源程序给出如下。

    请勿改动主函数main 和其他函数中的任何内容,仪在函数fun()的横线上填入所编写的若干表达式或语句。

    试题程序:

    include<stdio. h>

    include<conio. h>

    include<stdlib. h>

    typedef struct ss

    {

    int data;

    struct ss *next;

    } NODE;

    void fun(int n,char*filename)

    {

    NODE *h,*p, *s;

    FILE *pf;

    int i;

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

    h->data=0;

    for (i=1; i {

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

    s->data=【 】;

    【 】;

    p=【 】

    }

    p->next=NULL;

    if ( (pf=fopen (filename, "w") ) ==NULL)

    {

    printf {"Can not open out9B.clat! ");

    exit (0);

    }

    p=h;

    fprintf (pf, "\n***THE LIST***\n");

    print f ("\n***THE LIST***\n")

    while (p)

    {

    fprintf (pf, "%3d", p->data)

    printf ("%3d",p->data);

    if (p->next ! =NULL)

    {

    fprintf (pf, "->");

    printf ("->");

    }

    p=p->next;

    }

    fprintf (pf, "\n");

    printf ("\n");

    fclose (pf);

    p=h;

    while (p)

    {

    s=p;

    p=p->next;

    free (s);

    }

    }

    main()

    {

    char * filename="out98. dat";

    int n;

    clrscr ();

    printf (" \nInput n: ");

    scanf ("%d", &n);

    fun (n, filename);

    }


    正确答案:I p->next=s p->next
    I p->next=s p->next 解析:第一空:题目要求各结点的值为对应的下标,头结点的值为0,其他结点的值从1开始,所以此空应填i。第三空:为了将结点p和结点s连接起来,应将结点p的next 指针指向结点s。第三空:为了通过for 循环形成链表,每执行完一次循环操作,都要将指针p 指向下一个结点。

  • 第4题:

    阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。

    [说明]

    下面程序实现十进制向其它进制的转换。

    [C++程序]

    include"ioStream.h"

    include"math.h"

    include

    typedef struct node {

    int data;

    node*next;

    }Node;

    Class Transform.

    {

    DUDlic:

    void Trans(int d,int i); //d为数字;i为进制

    void print();

    private:

    Node*top;

    };

    void Transform.:Trans(int d,int i)

    {

    int m,n=0;

    Node*P;

    while(d>0)

    {

    (1);

    d=d/i;

    p=new Node;

    if(!n){

    p->data=m;

    (2);

    (3);

    n++;

    }

    else{

    p->data=m;

    (4);

    (5);

    }

    }

    }

    void Transform.:print()

    {

    Node*P;

    while(top!=NULL)

    {

    p=top;

    if(p->data>9)

    cout<<data+55;

    else

    cout<<data;

    top=p->next;

    delete p;

    }

    }


    正确答案:(1) m=d%i (2) top=p (3) top->next=NULL (4) p->next=top (5) top=p
    (1) m=d%i (2) top=p (3) top->next=NULL (4) p->next=top (5) top=p 解析:本题考查C++编程,主要考查了链表的使用。
    所有的问题只出在函数Trans中,它的功能是完成将十进制数d转换为任意进制i的数,并存在数组中。函数中首先定义了一个指向链表结点的指针,然后开始进行转换,进制转换应该是一个很常见的问题,就是不断的求模运算,所以(1)处应填入“m=d%i”。然后,我们要把求模的结果保存到链表结点中,并使链表首指针指向该结点,结点中指向下一个结点”的指针设为空,所以(2)处应填入“top=p”,(3)处应填入“top->next=NULL”。由于求模运算是从低位到高位逐位求出的,所以在我们在进行完第二次求模运算后,应该将第二次运算的结果放到链表首位,所以(4)处应填入“P->next=top”,(5)处应填入“top=p”。

  • 第5题:

    函数min()的功能是:在带头结点的单链表中查找数据域中值最小的结点。请填空includestruc

    函数min()的功能是:在带头结点的单链表中查找数据域中值最小的结点。请填空

    include <stdio.h>

    struct node

    { int data;

    struct node *next;

    };

    int min(struct node *first)/*指针first为链表头指针*/

    { struct node *p; int m;

    p=first->next; re=p->data; p=p->next;

    for( ;p!=NULL;p=【 】)

    if(p->data<m ) re=p->data;

    return m;

    }


    正确答案:p->next
    p->next 解析:本题考查的知识点是:链表的筛选。题目要求筛选出链表中最小的值,所以需要先定义一个临时变量,并将第1个值赋给该变量,就好像本题程序中定义的变量 m。然后遍历整个链表,拿链表中的每一个值跟m比较,如果找到比m小的值,就让m等于该值,这样遍历结束后,m中就是该链表的最小值了。题目中的空位于for循环的第3个表达式处,这里的for循环就是用来遍历整个链表的,所以该表达式需要完成的任务是:将循环变量p指向当前结点的下一个结点。故不难得知应填p->next。

  • 第6题:

    以下程序的功能是:建立一个带有头结点的甲—向链表,并将存储在数组中的字符依次转存到链表的各个结点中,请从与下划线处号码对应的一组选项中选择出正确的选项。

    #include <stdlib.h>

    struct node

    { char data; struct node *next: };

    (1) CreatList(char *s)

    {

    struct node *h,*p,*q;

    h = (struct node *)malloc sizeof(struct node));

    p=q=h;

    while(*s! ='\0')

    {

    p = (struct node *)malloc(sizeof (struct node));

    p->data = (2) ;

    q->next = p;

    q - (3) ;

    S++;

    }

    p->next='\0';

    return h;

    }

    main()

    {

    char str[]="link list";

    struct node *head;

    head = CreatList(str);

    }

    (1)

    A.char*

    B.struct node

    C.struct node*

    D.char


    正确答案:C

  • 第7题:

    链表题:一个链表的结点结构

    struct Node

    {

    int data ;

    Node *next ;

    };

    typedef struct Node Node ;

    (1)已知链表的头结点head,写一个函数把这个链表

    逆序( Intel)


    正确答案:

    Node * ReverseList(Node *head) //链表逆序
    {
    if ( head == NULL || head->next == NULL )
    return head;
    Node *p1 = head ;
    Node *p2 = p1->next ;
    Node *p3 = p2->next ;
    p1->next = NULL ;
    while ( p3 != NULL )
    {
    p2->next = p1 ;
    p1 = p2 ;
    p2 = p3 ;
    p3 = p3->next ;
    }
    p2->next = p1 ;
    head = p2 ;
    return head ;
    }

  • 第8题:

    下而程序实现十进制向其他进制的转换。

    [C++程序]

    include"ioStream.h"

    include"math.h"

    include <conio.h>

    typedef struct node{

    int data;

    node *next;

    }Node;

    class Transform

    {

    public:

    void Trans(int d,int i); //d为数字;i为进制

    void print();

    private:

    Node *top;

    };

    void Transform.:Trans(int d,int i)

    {

    int m,n=0;

    Node *P;

    while(d>0)

    {

    (1) ;

    d=d/i;

    p=new Node;

    if(!n){

    P->data=m;

    (2) j

    (3) ;

    n++;

    }

    else{

    p->data=m;

    (4) ;

    (5) ;

    }

    }

    }

    void Transform.:print()

    {

    Node *P;

    while(top!=NULL)

    {

    p=top;

    if(P->data>9)

    cout<<data+55:

    else

    cout<<data;

    top=p->next;

    delete P;

    }

    }


    正确答案:(1)m=d%i (2) top=p (3) top->next=NULL (4) p->next=top (5) top=p
    (1)m=d%i (2) top=p (3) top->next=NULL (4) p->next=top (5) top=p 解析:本题考查C++编程,主要考查了链表的使用。
    所有的问题只出在函数Trans中,它的功能是完成将十进制数d转换为任意进制i的数,并存在数组中。函数中首先定义了一个指向链表节点的指针,然后开始进行转换,进制转换应该是一个很常见的问题,就是不断地求模运算,所以(1)处应填入“m=d%i”。然后,我们要把求模的结果保存到链表节点中,并使链表首指针指向该节点,节点中指向下一个节点的指针设为空,所以(2)处应填入top=p,(3)处应填入top->next=NULL。由于求模运算是从低位到高位逐位求出的,所以在进行完第二次求模运算后,应该将第二次运算的结果放到链表首位,所以(4)处应填入p->next=top,(5)处应填入top=p。

  • 第9题:

    以下程序的功能是:建立一个带有头结点的单向链表,并将存储在数组中的字符依次转存到链表的各个结点中,请填空。 #include <stdlib.h> stuct node { char data; struet node * next; }; stntct node * CreatList(char * s) { struet node *h,*p,*q; h = (struct node * ) malloc(sizeof(struct node) ); p=q=h; while( * s! ='\0') { p = (struct node *) malloc ( sizeof(struct node) ); p - > data = ( ) q- >next=p; q=p; a++; p- > next ='\0'; return h; } main( ) { char str[ ]= "link list"; struet node * head; head = CreatList(str);

    A.*s

    B.s

    C.*s++

    D.(*s)++


    正确答案:A
    解析:本题要求建立一个stmctnode类型的数据链表,函数CreatList将字符串"linklist"的首地址传给指针变量s,可以推断建立的链表一定与"linklist",有关,由CreatList(char*s)函数中所定义的变量及其他语句可知,h,p,q用于建立的链表,h表示头指针,p用于记录开辟的新结点,而q用作将新结点与已建立的链表相连的中间变量,所建立链表各个结点的data依次存放的是”linklist",中的各个字符,所以应填空*s。

  • 第10题:

    设有一个不带头结点的单向链表,头指针为head,结点类型为NODE,每个结点包含一个数据域data和一个指针域next,该链表有两个结点,p指向第二个结点(尾结点),按以下要求写出相应语句。新开辟一个结点,使指针s指向该结点,结点的数据成员data赋值为1。


    正确答案:s=(NODE*)malloc(sizeof(NODE));s->data=1;

  • 第11题:

    问答题
    下列给定程序是建立一个带头结点的单向链表,并用随机函数为各结点赋值。函数fun()的功能是:将单向链表结点(不包括头结点)数据域为偶数的值累加起来,并作为函数值返回。  请改正函数fun中的错误,使它能得出正确的结果。  注意:部分源程序在文件MODII.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构!  试题程序:#include #include #include typedef struct aa{ int data; struct aa *next;}NODE;int fun(NODE *h){ int sum=0; NODE *p; p=h->next; /*********found*********/ while(p->next) {  if(p->data%2==0)   sum+=p->data;  /*********found*********/  p=h->next; } return sum;}NODE *creatlink(int n){ NODE *h,*p,*s; int i; h=p=(NODE *)malloc(sizeof(NODE)); for(i=1;idata=rand()%16;  s->next=p->next;  p->next=s;  p=p->next; } p->next=NULL; return h;}outlink(NODE *h){ NODE *p; p=h->next; printf("The LIST: HEAD"); while(p) {  printf("->%d",p->data);  p=p->next; } printf("");}main(){ NODE *head; int sum; system("CLS"); head=creatlink(10); outlink(head); sum=fun(head); printf("SUM=%d",sum);}

    正确答案:

    (1)错误:while(p->next)
    正确:while(p)或while(p!=NULL)
    (2)错误:p=h->next;
    正确:p= p ->next;
    解析:

      错误1:执行p=p->next后,p指针已经指向链表第一个包含数据域的结点。fun函数的while循环判断当前指针p指向的结点是否存在,若存在则对该结点数据域进行判断操作,而不是判断p指针的指针域是否为空。
      错误2:fun函数的while循环中判断结束后指针指向下一个结点,操作为p=p->next。

  • 第12题:

    问答题
    设有一个不带头结点的单向链表,头指针为head,结点类型为NODE,每个结点包含一个数据域data和一个指针域next,该链表有两个结点,p指向第二个结点(尾结点),按以下要求写出相应语句。删除链表的第一个结点。

    正确答案: head=head->next;
    解析: 暂无解析

  • 第13题:

    以下程序中函数fun的功能是:构成一个如图所示的带头结点的单词链表,在结点的数据域中放入了具有两个字符的字符串。函数disp的功能是显示输出该单链表中所有结点中的字符串。请填空完成函数disp。[*]

    include<stdio.h>

    typedef struct node /*链表结点结构*/

    {char sub[3];

    struct node *next;

    }Node;

    Node fun(char s) /*建立链表*/

    { … }

    void disp(Node *h)

    { Node *


    正确答案:

  • 第14题:

    已知bead指向一个带头结点的单向链表,链表中每个结点包含数据域(data)和指针域(next),数据域为整型。以下函数求出链表中所有连接点数据域的和值作为函数值返回。请在横线处填入正确内容。

    { int data; struct link *next;}

    main()

    { struct link *head;

    sam(______);

    {stmct link *p;int s=0;

    p=head->next;

    while(p){s+=p->data;p=p->next;}

    return(s);}


    正确答案:

  • 第15题:

    阅读下列程序说明和C程序,已知其输出为“1 2 3 4 5 6 7 8 9 10”。将应填入(n)处的字句写在对应栏内。

    [说明]

    本程序包含的函数及其功能说明如下:

    (1)函数first_insert()的功能是在已知链表的首表元之前插入一个指定值的表元;

    (2)函数reverse_copy()的功能是按已知链表复制出一个新链表,但新链表的表元链接顺序与

    已知链表的表元链接顺序相反;

    (3)函数Print_link()用来输出链表中各表元的值;

    (4)函数free_link()用来释放链表全部表元空间。

    [程序]

    include <stdio. h >

    include <malloe. h >

    typodef struct node {

    int val;

    struct node * next;

    } NODE;

    void first_insert(NODE * * p,int v)

    { NODE *q = (NODE *) malloe(sizeof(NODE));

    q->val = v; q->next = *p; /* 为新表元赋值*/

    * p =(1); }

    NODE * reverse_copy( NODE * p)

    { NODE * u;

    for(u=NULL; p!=NULL; p=p->next) first_insert((2));

    return u;

    }

    void printlink(NODE * p )

    { for(;(3)) prinff("%d\t", p->val);

    printf(" \n");

    }

    void free_link( NODE * p)

    { NODE * u;

    while(p! =NULL) { u=p->next;free(p);(4); }

    void main( ) { NODE * link1 , * link2;

    int i;

    link1 = NULL;

    for(i=1; i<= 10; i+ + )first_insert(&linkl, i);

    link2 = reverse_copy(link1 );

    (5);

    free_link( linkl ) ;free_link(link2); }


    正确答案:(1)q (2)&up->val (3)p!=NULL;P= p->next (4)P=u (5)print_link(link2)
    (1)q (2)&u,p->val (3)p!=NULL;P= p->next (4)P=u (5)print_link(link2) 解析:(1)定义新表元并且给予赋值后需要更新链首指针,使其指向新表元;
    (2)为了由空链表生成原来链表的逆序链,可以自链首表元P起遍历其每一个表元,调用first_insert函数将这个表元的值插入到新链表u中;
    (3)此处的循环是自链首表元p起循环遣历链表,循环结束条件是p!=NULL,每循环一次,将p指向下一个表元,即P=P->next;
    (4)释放链表全部表元空间是自链首表元起,循环释放每一个表元空间;
    (5)此处定义了程序的输出。由于main函数中链表 link1、link2分别为:
    “10987654321”及
    “123456789 10”,
    因而此处应该是调用print_link函数输出link2。

  • 第16题:

    针对以下C语言程序,请按要求回答问题。

    已知link. c源程序如下:

    /*link. c程序对单向链表进行操作,首先建立一个单向链表,然后根据用户的选择可以对其进行插入结点、删除结点和链表反转操作*/

    include<stdio. h>

    include<stdlib. h>

    typedef struct list_node * list_pointer; //定义链表指针

    typedef struct list_node{ //定义链表结构

    int data;

    list_pointer link;

    }list_node;

    //用到的操作函数

    list_pointer create(); //建立一个单向链表

    void insert(list_pointer * p_ptr,list_pointer node); //在node后加入一个新的结点

    void delete_node(list_pointer * p_ptr,list_pointer trail,list_pointer node);

    //删除前一个结点是trail的当前结点node

    void print(list_pointer * p_ptr); //打印链表结点中的值

    list_pointer invert(list_pointer lead); //反转链表

    int main()

    {

    list_pointer ptr=NULL;

    list_pointer node,trail;

    list_pointer * P=&ptr;

    int choose,location,i;

    printf("you should create a link first:\n");

    //建立一个单向链表

    prt=create(); //ptr指向链表的第一个结点

    print(ptr);

    //根据用户的不同选择进行相应的操作:

    printf("input number 0,you can quit the program\n");

    printf("input number 1,you can insert a new node to link\n"):

    printf("input number 2,you can delete a node from the link\n");

    printf("input number 3,you can invert the link\n"):

    printf("please input you choice\n");

    scanf("%d",&choose);

    while(choose!=0){

    switch(choose){

    case 1:

    i=1:

    while(i<location){

    node=node->link;

    i++:

    }

    insert(p,node); //p为指向ptr的指针

    print(ptr);

    break;

    case 2:

    printf("you will delete a node from the link\n");

    printf("please input the location of the node:\n");

    scanf("%d",&location):

    node=ptr;

    if(location==1)

    trail=NULL;

    trail=ptr;

    i=1:

    while(i<location){

    trail=trail->link:

    i++:

    }

    node=trail->link;

    delete_node(p,trail,node);

    print(ptr);

    break;

    case 3:

    printf("you will invert the link\n");

    ptr=invert(ptr);

    print(ptr);

    break;

    default;

    break;

    return -1;

    }

    printf("please input you choice\n");

    scanf("%d". &choose):

    }

    return 0;

    //根据用户的输入值建立一个新的单向链表:

    list_pointer create()

    {

    int i,current,length;

    list_pointer p1,p2,head;

    printf("please input the node number of the link:\n");

    scanf("%d". &length):

    printf("the number of the link is:%d",length);

    printf("please input the data for the link node:\n");

    i=0;

    p1=p2=(list_pointer)malloc(sizeof(list_node));

    head=p1;

    for(i=1;i<length;i++){

    scanf("%d",&current);

    p1->data=current;

    p2->link=p1;

    p2=p1;

    p1=(list_pointer)malloc(sizeof(list_node));

    }

    p2->link=NULL;

    return head;

    }

    画出主函数main的控制流程图。


    正确答案:主函数的控制流程如下图所示。
    主函数的控制流程如下图所示。

  • 第17题:

    以下程序的功能是建立—个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束(链表头结点的data域不放数据,表空的条件是ph->next==NULL),请填空。

    include<stdio.h>

    struct list { int data;struct list *next;};

    struct list * creatlist()

    { struct list *p,*q,*ph;int a;ph=(struct list *)malloc(sizeof(struct


    正确答案:
    解析: 本题考查的是链表这一数据结构对结构体变量中数据的引用。链表的特点是结构体变量中有两个域,一个是数据,另一个是指向该结构体变量类型的指针,用以指明链表的下一个结点。

  • 第18题:

    以下程序把三个NODEIYPE型的变量链接成—个简单的链表,并在while循环中输出链表结点数据域中的数据。请填空。

    include<stdio.h>

    struct node

    { int data;struct node*next;);

    typedef struct node NODETYPE;

    main()

    { NODETYPEa,b,c,*h,*p;

    a.data=10;b.data=20;c.data=30;h=&a;

    anext=&b;b.next=&c;c,next='\0';

    p=h;

    while(p){printf("%d,",p->data):【 】;}

    printf("\n");

    }


    正确答案:P++
    P++ 解析:本题主要考查的是将NODETYPE型的变量链接成—个简单的链表,利用typedef把NODETYPE变成struct node的别名,当执行while循环时,首先判断是否到了最后—个链表结点,如果没有则引用结构体中的成员data,然后指向下—个链表结点,继续判断,因此,此处应填的是p++指向下—个链表结点。

  • 第19题:

    下列给定程序中,是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。

    请改正程序指定部位的错误,使它能得到正确结果。

    [注意] 不要改动main函数,不得增行或删行,也不得更改程序的结构。

    [试题源程序]

    include<stdio.h>

    include<stdlib.h>

    typedef struct aa

    {

    int data;

    struct aa *next;

    }NODE;

    fun(NODE *h)

    {

    int max=-1;

    NODE *p;

    /***********found************/

    p=h;

    while(p)

    {

    if(p->data>max)

    max=p->data;

    /************found************/

    p=h->next;

    }

    return max;

    }

    outresult(int s, FILE *Pf)

    {

    fprintf(pf, "\nThe max in link: %d\n", s);

    }

    NODE *creatlink(int n, int m)

    {

    NODE *h, *p, *s, *q;

    int i, x;

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

    h->data=9999;

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

    {

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

    s->data=rand()%m; s->next=p->next;

    p->next=s; p=p->next;

    }

    p->next=NULL;

    return h;

    }

    outlink(NODE *h, FILE *pf)

    {

    NODE *p;

    p=h->next;

    fprintf(Pf, "\nTHE LIST:\n\n HEAD");

    while(P)

    {

    fprintf(pf, "->%d", P->datA); p=p->next;

    }

    fprintf(pf, "\n");

    }

    main()

    {

    NODE *head; int m;

    head=cteatlink(12,100);

    outlink(head, stdout);

    m=fun(head);

    printf("\nTHE RESULT"\n");

    outresult(m, stdout);

    }


    正确答案:(1)错误:p=h; 正确:p:h->next; (2)错误:p=h->next; 正确:p=p->next;
    (1)错误:p=h; 正确:p:h->next; (2)错误:p=h->next; 正确:p=p->next; 解析:程序中使用while循环语句并结合结构指针p来找到数据域中的最大值。
    错误1:P指向形参结构指针h的next指针,所以应改为:p=h->next;
    错误2:p指向自己的下一个结点,所以应改为:p=p->next

  • 第20题:

    以下程序中函数fun的功能是:构成—个如图所示的带头结点的单向链表,在结点的数据域中放入了具有两个字符的字符串。函数disp的功能是显示输出该单向链表中所有结点中的字符串。请填空完成函数disp。

    include<stdio.h>

    typedef struct node /*链表结点结构*/

    { char sub[3];

    struct node *next;

    }Node;

    Node fun(char s) /* 建立链表*/

    { ...... }

    void disp(Node *h)

    { Node *p;

    p=h->next;

    while([ ])

    {printf("%s\n",p->sub);p=[ ];}

    }

    main()

    { Node *hd;

    hd=fun(); disp(hd);printf("\n");

    }


    正确答案:p!=NULL 或 p 或 p!=0 或 p!='0' p->next 或 (*P).next
    p!=NULL 或 p 或 p!=0 或 p!='0' p->next 或 (*P).next 解析:此题主要考核的是用指针处理链表。自定义结构体类型名为Node,并定义一个指向结点类型的指针next。用Node来定义头结点指针变量h,并定义另—个指针变量p指向了第—个结点,在满足p未指向最后—个结点的空指针时,输出p所指向结点的字符串,所以第—个空填p!=NULL或p或p!=0或p!='\0',然后将p指向下一个非空结点,所以第二个空填p->next或与其等效的形式,反复执行直到所有的结点都输出,即遇到p的值为NULL。

  • 第21题:

    有如图所示的双链表结构,请根据图示完成结构体的定义:

    { int data;

    【18】 } node;


    正确答案:
    structaa*lhead,*rchild;

  • 第22题:

    设有一个不带头结点的单向链表,头指针为head,结点类型为NODE,每个结点包含一个数据域data和一个指针域next,该链表有两个结点,p指向第二个结点(尾结点),按以下要求写出相应语句。删除链表的第一个结点。


    正确答案:head=head->next;

  • 第23题:

    问答题
    设某带头结头的单链表的结点结构说明如下:typedef struct nodel{int data struct nodel*next;}node;试设计一个算法:void copy(node*headl,node*head2),将以head1为头指针的单链表复制到一个不带有头结点且以head2为头指针的单链表中。

    正确答案: 一边遍历,一边申请新结点,链接到head2序列中。
    解析: 暂无解析