+ -
当前位置:首页 → 问答吧 → C高级程序应用题库

C高级程序应用题库

时间:2010-07-26

来源:互联网

1. 以下程序建立了一个带有头结点的单向链表,链表结点中的数据通过键盘输入,当输入数据为-1表示输入结束(链表头结点的data 域不放数据,表空的条件是ph->next = = NULL).
#include <stdio.h>
#include <malloc.h>
struct list
{
        int data;struct list *next;
};
    【1】     creatlist()
{
        struct list *p,*q,*ph;
        int a;
        ph=(struct list *) malloc(sizeof(struct list));
        p=q=ph;
        printf("Input an integer number,enter -1 to end:\n");
        scanf("%d",&a):
        while(a!=-1)
        {
                p=(struct list *)malloc(sizeof(struct list));
                p->data=a; q->next=p;    【2】     =p;
                scanf("%d",&a);
        }
        p->next='\0';
        return(ph);
}
void main()
{
        struct list *head;
        head=creatlist();
}

答案:【1】struct list *                 【2】q

2.下面min3函数的功能是:计算单向循环链表first中每3个相邻结点数据域中值的和,返回其中最小的值。请填空。
#include <stdio.h>
struct node
{
        int data;
        struct node * link;
};
int min3(struct node * first)
{
        struct node *p=first;
        int m,m3=p->data+p->link->data+p->link->link->data;
        for(p=p->link; p! =first; p=     【1】     )
        {
                m=p->data+p->link->data+p->link->link->data;
                if(     【2】     )
                        m3=m;
        }
        return(m3);
}
答案:【1】p->link                 【2】mm或m<=m3或m3>=m

3. 以下函数creat用来建立一个带头结点的单向链表,新产生的结点总是插在链表的末尾。单向链表的头指针作为函数值返回.请填空:
#include <stdio.h>
#include <malloc.h>
struct list {
        char data;
        struct list * next;
};
struct list * creat()
{
        struct list * h,* p,* q;
        char ch;
        h=    【1】     malloc(sizeof(struct list));
        p=q=h;
        ch=getchar();
        while(ch!='?')
        {
                p=    【2】     malloc(sizeof(struct list));
                p->data=ch;
                p->next=p;
                q=p;
                ch=getchar();
        }
        p->next='\0';
            【3】   
}
答案:【1】(struct list *)         【2】(struct list *)         【3】return(h); 或 return h; ;

作者: liuyulan1018   发布时间: 2010-07-26

看起来还不错,mark一下。

作者: ChiyuT   发布时间: 2010-07-26