struct node
{
int data;
struct node *next;
};
typedef struct node nodeType;
int main()
{
nodeType sOne,sTwo,sThree, *begin, *p;
clrscr();
sOne.data = 1;
sTwo.data = 2;
sThree.data = 3;
begin = &sOne;
sOne.next = &sTwo;
sTwo.next = &sThree;
sThree.next = '\0';
p=begin;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
getch();
return 0;
}
#include<alloc.h>
typedef struct node
{
int data;
struct node *next;
}nodeType;
nodeType *CreateList() /*这个方法的作用:返回头指针(头结点),头结点没有数据域*/
{
int i;
nodeType *begin,*end,*current;
begin = (nodeType *)malloc(sizeof(nodeType));
end = begin;
/* begin->data = 1000;*/
scanf("%d",&i);
while(i!=-1) /*输入为-1时,退出循环*/
{
current = (nodeType *)malloc(sizeof(nodeType));
current->data = i;
end->next = current;
end = current;
scanf("%d",&i);
}
end->next = '\0';
return begin;
}
int main()
{
nodeType *head;
head = CreateList();
while(head)
{/* 顺序访问链表中各结点的数据域*/
printf("%d ",head->data); /*头结点没有数据域,所以打印头结点时,数据是随即的。
head=head->next;
printf("%d ",head->data);
*/
}
getch();
return 0;
}