想实现一个初始化双链表。
main.c
#include <stdio.h>
#include <stdlib.h>
#include "db.h"
int main(){
pnode *n=NULL;
n=createlist();
}
db.h
#ifndef DB_H_INCLUDED
#define DB_H_INCLUDED
struct nod{
int data;
struct nod *pre,*next;
};
typedef struct nod *pnode,node;
pnode createlist();
int insertlist();
int finded();
int deletelist();
void print();
int getlen();
#endif // DB_H_INCLUDED
db.c
#include <stdio.h>
#include <stdlib.h>
#include "db.h"
pnode createlist(){
pnode *tail=NULL;pnode *newnode=NULL;
pnode *head=(pnode *)malloc(sizeof(node));
int length=0;
if(NULL==head){
printf("没有分配到空间");
exit(EXIT_FAILURE);
}
head->data=0;
head->next=NULL;
head->pre=NULL;
head=tail;
printf("请输入双链表的长度");
scanf("%d",&length);
for(int i =0;i<length;i++){
newnode=(pnode *)malloc(sizeof(node));
if(NULL==newnode){
printf("新节点分配内存失败");
exit(EXIT_FAILURE);
}
printf("请输入第%d个值",i+1);
scanf("%d",&newnode->data);
newnode->next=head->next;
head->next->pre=newnode;
head->next=newnode;
newnode->pre=head;
}return head;
}