怎么建立一个带头节点的链表?
怎么建立一个不带头节点的链表?
能给我他们之间的对比程序并说明一下
我在这里先谢大家了
/*不带头结点的*/
#define NULL 0
#include<malloc.h>
#include <stdio.h>
typedef int datatype;
typedef struct link_node{
datatype info;
struct link_node *next;
}node;
typedef node* nodelink;
nodelink buildlink() /* 尾插法*/
{
datatype x;
nodelink head,s;
nodelink p2;
head=NULL;
p2=NULL;
printf("please input the link:");
scanf("%d",&x);
while(x!=0)
{s=(nodelink)malloc(sizeof(node));
s->info=x;
if (head==NULL) head=s; else p2->next=s;
p2=s;
scanf("%d",&x);
}
if(p2) p2->next=NULL;
return(head);
}
nodelink creatlink() /***头插法***/
{ nodelink head,p;
datatype x;
head=NULL;
printf("please input the link:");
scanf("%d",&x);
while(x!=0)
{ p=(nodelink)malloc(sizeof(node)) ;
p->info=x;
p->next=head;
head=p;
scanf("%d",&x);
}
return head;
}
#include<stdio.h>
#define NULL 0
typedef int datatype;
typedef struct hlink{
datatype info;
struct hlink *next;
}node;
typedef node* nodelink;
/* 头插法建立带头节点的单链表*/
nodelink tcreathlink()
{ datatype x;
node *head,*s;
head=(node*)malloc(sizeof(node));
printf("please input the datas:");
scanf("%d",&x);
while(x)
{ s=(node*)malloc(sizeof(node));
s->info=x;
s->next=head->next;
head->next=s;
scanf("%d",&x);
}
return head;
}
nodelink tbuildhlink() /*带头节点的尾插法*/
{
datatype x;
node *head,*s;
node *p2;
head=(node *)malloc(sizeof(node));
p2=head;
printf("please input the datas:");
scanf("%d",&x);
while(x!=0)
{s=(node *)malloc(sizeof(node));
s->info=x;
p2->next=s;
p2=s;
scanf("%d",&x);
}
if(p2) p2->next=NULL;
return(head);
}
[此贴子已经被作者于2006-10-10 17:23:26编辑过]