1.创造一个链表基本形式
typedef struct Node//创建一个链表
{
struct student data;//数据域
struct Node *next;//指针域
}List;
2. 创造头节点
List *CreatHead()//头节点的创造与初始化
{
List *headNode=(List *)malloc(sizeof(List));
headNode->next=NULL;
return headNode;
};
3.创造新节点
List *CreatNode(struct student data)//创造新节点
{
List *newNode=(List*)malloc(sizeof(List));
newNode->data=data;
newNode->next=NULL;
return newNode;
};
4.插入节点(头插尾插)
void insertNode(List *headNode,struct student data)//链表的头插
{
List *newNode=CreatNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
void inserttrail(List *headNode,struct student data)//链表的尾插
{
List *p=headNode;
List *newNode=CreatNode(data);
while(p->next!=NULL)
{
p=p->next;
}
p->next=newNode;
}
5.打印链表
void printflist(List *headNode)//打印链表
{
List *pmove=headNode->next;
printf("name\tmath\tid\n");
while (pmove!=NULL)
{
printf("%s\t%d\t%d\n",pmove->data.name,pmove->data.math,pmove->data.id);
pmove=pmove->next;
}
}
6.主函数
int main ()
{
List *List=CreatHead();//*创造链表
for(int i=0;i<=3;i++)
{
inserttrail(List,i);
}
printflist(List);
return 0;
}