链表输入
·
#include <stdio.h>
#include <stdlib.h>
struct List
{
int data;
struct List *next;
} * a;
struct List *cin();
void cout(struct List *head);
int main()
{
a = cin();
cout(a);
return 0;
}
struct List *cin()
{
struct List *head, *p1, *p2;
head = malloc(sizeof(struct List));
p1 = p2 = head;
do
{
p2->next = p1;
p2 = p1;
scanf("%d", &p2->data);
p2->next = NULL;
p1 = malloc(sizeof(struct List));
} while (p2->data != -1);
return head;
}
//第二种 换汤不换药
// struct List *read()
// {
// struct List *head, *p1, *p2;
// head = malloc(sizeof(struct List));
// p1 = p2 = head;
// do
// {
// p2->next = p1;
// p2 = p1;
// scanf("%d", &p2->data);
// p1 = malloc(sizeof(struct List));
// } while (p2->data != -1);
// p2->next = NULL;
// return head;
// }
void cout(struct List *head)
{
if(head->data==-1) return ;
while (head)
{
printf("%d", head->data);
head = head->next;
if(head->data!=-1)
printf(" ");
if(head->data==-1)break;
}
}
更多推荐



所有评论(0)