---有头双链表---
1.创建文件
2.在double_link_list.h上写
#ifndef __DOUBLE_LINK_LIST_H__
#define __DOUBLE_LINK_LIST_H__
typedef int DataType;
//一般节点
struct node
{
DataType data;
struct node *next;
struct node *prev;
};
//带头节点
struct double_link_list
{
DataType nodenum;
struct node *first;
struct node *last;
};
struct double_link_list* head_create_double_list();//创建双链表
void printf_double_list(struct double_link_list*L);//打印链表
#endif
3.在double_link_list.c写
#include <stdio.h>
#include "double_link_list.h"
#include <stdlib.h>
struct double_link_list * head_create_double_list()//创建双链表
{
struct double_link_list * L = malloc(sizeof(struct DL*));
DataType d;
L->nodenum=0;
L->first=NULL;
L->last =NULL;
while (1)
{
scanf("%d",&d);
if (d==0)
{
break;
}
struct node *pnew = malloc(sizeof(struct node*));
pnew->data=d;
pnew->next=NULL;
pnew->prev=NULL;
//加入链表
if (L->first==NULL)
{
L->first=pnew;
L->last =pnew;
}
else
{
//头插法
pnew->next=L->first;
L->first->prev=pnew;
L->first=pnew;
//尾插法
// pnew->prev=L->last;
// L->last->prev=pnew;
// L->last=pnew;
}
L->nodenum++;
}
return L;
}
void printf_double_list(struct double_link_list*L)
{
struct node *p=L->first;
while (p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
4.main.c 写
#include <stdio.h>
#include "double_link_list.h"
int main()
{
struct double_link_list *L=head_create_double_list();
printf_double_list(L);
return 0;
}
5.编译并运行
6.后面会有增删查改