如何建立一个带头结点的单向链表

来源:百度知道 编辑:UC知道 时间:2024/07/04 19:19:28
怎样建立?每一步都是为什么?

struct node
{
int data;
node *next;
};
最主要的首先在结构建立两个域,数据域,指针域,数据域用来保存数据,指针域用来指向下一个节点的地址;
node * createnode()
{
node *s,*r,*h;
int x;
h=new node;
r=h;
scanf("%d",&x);
if(x!=-1)
{
s=new node;
s.data=x; r.next=r; r=s;
}
r.next='\0';
return h;
}

void shownode(node *head)
{
node *pt;
pt=head.next;
if(pt==NULL) printf("this list is NULL");
else{
do
{
printf("list data ->%d",pt.data);
pt=pt.next;

}while(pt!=NULL);
}

}

void main()
{
node * h=createnode();
shownode(h);

getchar();
getchar();

}