第1个回答 2008-10-11
//在创建链表的函数create中当输入0时结束输入
#include <stdio.h>
#include <malloc.h>
typedef struct Node//链表数据结构
{
int val;
struct Node * next;
} NODE;
NODE node;
NODE * creat()//创建新链表的函数,当输入的值为0时创建完成
{
int x = 0;
int cycle = 1;
NODE *p, *q, *head;
head = &node;
q = p = head;
while(cycle)
{
printf("请输入结点值: ");
scanf("%d", &x);
if(0 != x)
{
p = (NODE *)malloc(sizeof(node));//建立下一结点
p->val = x;
q->next = p;
q = p;
}
else cycle = 0;
}
p->next = NULL;
p = head;
head = head->next;
return head;
}
int display(NODE * head)//显示链表head所有值的函数
{
NODE * p = head;
while(NULL != p)
{
printf("%d,", p->val);
p = p->next;
}
printf("\n");
return 0;
}
int main()
{
NODE * testnode = creat();//测试一下个函数的功能
printf("新建的链表的结点的值:\n ");
display(testnode);
return 0;
}本回答被提问者采纳
第2个回答 2020-06-04
你的代码很有问题啊。在VS2013上面跑都不能跑。
你的意思是如果不读取到May就一直往下读取建立链表吧。
帮你修改了一下。应该可以了。
测试环境:DevC++
测试程序
123456789101112131415161718192021222324#include<stdio.h>#include<string.h>#include"malloc.h"#define len sizeof(link)typedef struct linklist {char a[4]; struct linklist *next; }link;main(){ link *head=(link *)malloc(len); link *p,*q; memset(head->a, '\0', sizeof(char)* 4); head->next = NULL; scanf("%s",head->a); q=head; while(strcmp(q->a,"May")!=0){ p=(link *)malloc(len); q->next=p; q=p; scanf("%s",p->a);
} p->next=NULL;}
第3个回答 2008-10-11
#include <stdio.h>
#include <malloc.h>
typedef struct _Node
{
struct _Node *next;
int num;
} Node;
void main()
{
Node *head = NULL, *node, *cur;
int n;
while (-1 != scanf("%d", &n))
{
node = (Node *)malloc(sizeof(Node));
node->num = n;
node->next = NULL;
if (head == NULL)
{
head = node;
}
else
{
cur->next = node;
}
cur = node;
}
cur = head;
while (cur)
{
printf("%d ", cur->num);
node = cur;
cur = cur->next;
free(node);
}
printf("\n");
}
输入^Z结束程序输入
第4个回答 2008-10-11
manchaozhao的答案支持