c语言求解 建立链表程序。当输入0时表示链表输入结束,0不计入该链表。

建立链表程序。当输入0时表示链表输入结束,0不计入该链表。注意:只允许在/******start******/和/******end******/之间添加代码。
其中:
1 2 3 4 5 0
是键盘输入的。
#include <stdio.h>
#include <stdlib.h>

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

struct node * create();
void output(struct node *head);

int main()
{
struct node *head;

printf("Input:\n");
head = create();
printf("Output:\n");
output(head);
return 0;
}

struct node * create()
{
/******start******/

/******end******/
}

void output(struct node *head)
{
if (head == NULL)
{
printf("No data!\n");
return ;
}
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

#include <stdio.h>
#include <stdlib.h>

//链表数据结构
typedef struct _list
{
    int n;

    struct _list *next;
}LIST;

//创建一个新的节点
LIST *list_node_new(int n)
{
    LIST *node;

    node=malloc(sizeof(LIST));
    if(!node)
        return NULL;

    node->n=n;
    node->next=NULL;

    return node;
}

//创建一个新的链表
LIST *list_new(void)
{
    return list_node_new(0);
}

//向链表添加一个节点
int list_add(LIST *list,int data)
{
    LIST *node;

    //头节点
    if(list->n == 0 && list->next == NULL)
    {
        list->n=data;
        return;
    }

    while(list->next)
        list=list->next;

    node=list_node_new(data);
    if(!node)
        return -1;
    list->next=node;

    return 0;
}

//销毁链表
void list_destory(LIST *list)
{
    LIST *temp;

    while(list)
    {
        temp=list;
        list=list->next;
        free(temp);
    }
}

//遍历打印链表中的每一个节点
void list_print(LIST *list)
{
    while(list)
    {
        printf("%d\n",list->n);
        list=list->next;
    }
}

int main(int argc,char **argv)
{
    LIST *list;
    int n;

    list=list_new();

    while(1)
    {
        scanf("%d",&n);

        if(n == 0)
            break;

        list_add(list,n);
    }

    list_print(list);
    list_destory(list);

    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-14

参考代码:

#include <stdio.h>
#include <stdlib.h>

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

struct node * create();
void output(struct node *head);

int main()
{
    struct node *head;
    printf("Input:\n");
    head = create();
    printf("Output:\n");
    output(head);
    return 0;
}

struct node * create()
{
    /******start******/
    struct node *head, *p, *q;
    int num = 0;
    head = p = q = NULL;
    scanf("%d", &num);
if (num != 0)
{
head = p = (struct node *)malloc(sizeof(struct node));
}
    while (num != 0)
    {
        p->data = num;
        scanf("%d", &num);
        if (num != 0)
        {
            q = (struct node *)malloc(sizeof(struct node));
            p->next = q;
p = q;
        }
p->next = NULL;
    }
    return head;
    /******end******/
}

void output(struct node *head)
{
    if (head == NULL)
    {
        printf("No data!\n");
        return ;
    }
    while (head != NULL)
    {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
}

第2个回答  2015-06-14
struct node *create() {
/******start******/
struct node *head = NULL,*p,*q;
int num;
head = q = (struct node *)malloc(sizeof(struct node));
while(1) {
scanf("%d",&num);
if(num == 0) break;
if(q == head) {
q->data = num;
p = q;
q = NULL;
}
else {
p->next = (struct node *)malloc(sizeof(struct node));
p->next->data = num;
p = p->next;
}
}
p->next = NULL;
return head;
/******end******/
}

本回答被网友采纳