{
int num; //分数
struct student *pnext;//指向下一个节点
};
typedef struct student STU;//创建一个自定义类型STU
STU *create( int first);//创建头节点
STU *insert (STU *head,int data);//插入一个节点
void printall(STU *head);//打印出所有链表数据
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "mynode.h"
#include <stdio.h>
#include <stdlib.h>
STU *create(int first)
{
STU *head ;//创建一个指针,指向结构体
head=NULL;//指针为空初始化
head=(STU*)malloc(sizeof(STU));//为指针分配内存
if (head==NULL)
{
printf("头结点分配内存失败");
return NULL;
}
head->num=first;//根据穿入的值,初始化第一个int变量
head->pnext=NULL;//初始化结构体的指针
//printf("%d",head->num);
return head;
}
STU *insert (STU *head,int data)
{
STU *last=head; //指向头结点
while(last->pnext!=NULL)//让last指向最后一个节点
{
last=last->pnext;
}
STU * pstu=(STU *)malloc(sizeof(STU));//分配内存
if(pstu==NULL)
{
return NULL;//分配内存失败,返回
}
last->pnext=pstu;//传人下一个节点//初始化数据
pstu->num=data;//初始化数据
pstu->pnext=NULL;
return head;//
}
void printall(STU *head)
{
printf("\n\n打印出的链表数据 \n\n");
STU *go;//遍历按所有的元素
go=head;//从第一个元素开始
do
{
printf("\n%d,%x,%x",go->num,go,go->pnext);//打印链表元素
go=go->pnext;//循环递增,遍历链表
} while (go!=NULL);
}
void main(){
STU * pnode1= create(8);//创建一个指针,初始化
insert(pnode1,1);
insert(pnode1,2);
insert(pnode1,3);
printall(pnode1);
system("pause");
}