#include
#include
struct T
{
int data;
struct T *next;
}*TNode, *THead,*p;
void main()
{
THead=(struct T *)malloc(sizeof(struct T));
if(NULL==THead)
printf("errer\n");
THead->next=NULL;
THead->data=0;
TNode=(struct T *)malloc(sizeof(struct T));
if(NULL==TNode)
printf("errer\n");
THead->next=TNode;
TNode->data=1;
TNode->next=NULL;
for(int i=1;i<5;i++)
{
TNode=(struct T *)malloc(sizeof(struct T));
if(NULL==TNode)
printf("errer\n");
TNode->next=THead->next;
THead->next=TNode;
TNode->data=i+1;
}
p=THead;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return ;
}
头插法
#include
#include
struct T
{
int data;
struct T *next;
}*TNode, *THead,*p;
void main()
{
THead=(struct T *)malloc(sizeof(struct T));
if(NULL==THead)
printf("errer\n");
THead->next=NULL;
THead->data=0;
TNode=(struct T *)malloc(sizeof(struct T));
if(NULL==TNode)
printf("errer\n");
THead->next=TNode;
TNode->data=1;
TNode->next=NULL;
p=TNode;
for(int i=1;i<5;i++)
{
TNode=(struct T *)malloc(sizeof(struct T));
if(NULL==TNode)
printf("errer\n");
p->next=TNode;//其实就是这块的不同,头插法是在头节点的后面插,而尾插法是在尾部插,所以要有一个指针跟着尾巴
TNode->next=NULL;
TNode->data=i+1;
p=TNode;
}
p=THead;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return ;
}
尾插法
#include
#include
struct
T
{
int
data;
struct
T
*next;
}*TNode,
*THead,*p;
void
main()
{
THead=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==THead)
printf("errer\n");
THead->next=NULL;
THead->data=0;
TNode=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==TNode)
printf("errer\n");
THead->next=TNode;
TNode->data=1;
TNode->next=NULL;
for(int
i=1;i<5;i++)
{
TNode=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==TNode)
printf("errer\n");
TNode->next=THead->next;
THead->next=TNode;
TNode->data=i+1;
}
p=THead;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return
;
}
头插法
#include
#include
struct
T
{
int
data;
struct
T
*next;
}*TNode,
*THead,*p;
void
main()
{
THead=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==THead)
printf("errer\n");
THead->next=NULL;
THead->data=0;
TNode=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==TNode)
printf("errer\n");
THead->next=TNode;
TNode->data=1;
TNode->next=NULL;
p=TNode;
for(int
i=1;i<5;i++)
{
TNode=(struct
T
*)malloc(sizeof(struct
T));
if(NULL==TNode)
printf("errer\n");
p->next=TNode;//其实就是这块的不同,头插法是在头节点的后面插,而尾插法是在尾部插,所以要有一个指针跟着尾巴
TNode->next=NULL;
TNode->data=i+1;
p=TNode;
}
p=THead;
while(p!=NULL)
{
printf("%d",p->data);
p=p->next;
}
return
;
}
尾插法
student
*create()
{
int
i;
int
s;
student
*h=null,*info;
/*
student指向结构体的指针*/
for(;;)
{
info=(student
*)malloc(sizeof(student));
/*申请空间*/
if(!info)
/*如果指针info为空*/
{
printf("\nout
of
memory");
/*输出内存溢出*/
return
null;
/*返回空指针*/
}
inputs("enter
no:",info->no,11);
/*输入学号并校验*/
if(info->no[0]=='@')
break;
/*如果学号首字符为@则结束输入*/
inputs("enter
name:",info->name,15);
/*输入姓名,并进行校验*/
printf("please
input
%d
score
\n",n);
/*提示开始输入成绩*/
s=0;
/*计算每个学生的总分,初值为0*/
for(i=0;i
score[i]);
/*输入成绩*/
if(info->score[i]>100||info->score[i]<0)
/*确保成绩在0~100之间*/
printf("bad
data,repeat
input\n");
/*出错提示信息*/
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i];
/*累加各门课程成绩*/
}
info->sum=s;
/*将总分保存*/
info->average=(float)s/n;
/*求出平均值*/
info->order=0;
/*未排序前此值为0*/
info->next=h;
/*将头结点做为新输入结点的后继结点*/
h=info;
/*新输入结点为新的头结点*/
}
return(h);
/*返回头指针*/
}
/*输入字符串,并进行长度验证*/
inputs(char
*prompt,
char
*s,
int
count)
{
char
p[255];
do{
printf(prompt);
/*显示提示信息*/
scanf("%s",p);
/*输入字符串*/
if(strlen(p)>count)printf("\n
too
long!
\n");
/*进行长度校验,超过count值重输入*/
}while(strlen(p)>count);
strcpy(s,p);
/*将输入的字符串拷贝到字符串s中*/
}