想实现一个初始化双链表。
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;
}
VS2017编译通过,并修复了一些逻辑bug(默认这是为了建立一个有头有尾的双向列表)。你可以试一下,代码如下:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include "db.h"
int main() {
pnode n ;
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 newnode = NULL;
pnode head = (pnode)malloc(sizeof(node));
pnode tail = (pnode)malloc(sizeof(node));
int length = 0;
if (NULL == head || NULL == tail) {
printf("没有分配到空间");
exit(EXIT_FAILURE);
}
head->data = 0;
head->next = tail;
head->pre = NULL;
tail->data = 0;
tail->next = NULL;
tail->pre = head;
printf("请输入双链表的长度");
scanf_s("%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_s("%d", &(newnode->data));
head->next->pre = newnode;
newnode->next = head->next;
head->next = newnode;
newnode->pre = head;
}return head;
}
应该在在前面添加 #include "db.c"
可以在每个C文件的最前面增加一个#include "includes.h"
加一个#include "includes.h"
请在程序最开始添加#include "includes.h",试一下是否可行
请在最开始添加#include "includes.h",试一下是否可行
VS2017中编译通过,修改了一些逻辑错误:
main.c
#include "db.h"
int main()
{
pnode n = NULL;
n = createlist();
}
db.h
#pragma once
#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();
//void print();
//int getlen();
#endif
db.cpp
#include<stdio.h>
#include<stdlib.h>
#include "db.h"
pnode createlist()
{
pnode tail = new node;
pnode head = new node;
int length = 0;
if (NULL == head)
{
printf("No space created!");
exit(EXIT_FAILURE);
}
head->data = 0;
head->next = tail;
head->pre = NULL;
tail->data = 0;
tail->next = NULL;
tail->pre = head;
printf("Please input the length of list.");
scanf_s("%d", &length);
for (int i = 0; i < length; i++)
{
pnode newnode = (pnode)malloc(sizeof(node));
if (NULL == newnode)
{
printf("New node space failed.");
exit(EXIT_FAILURE);
}
printf("Please input data of NO.%d node", i + 1);
scanf_s("%d", &newnode->data);
newnode->next = tail;
newnode->pre = tail->pre;
tail->pre = newnode;
newnode->pre->next = newnode;
}
return head;
}
加一个#include "includes.h"应该就可以了
你可以试着在程序最前面加一个#include"includes.h"
C语言现在都再哪些高级的应用中用到啊
在程序最前面加#include “inchudes.h”
在开头添加 #include 试一下是否可行
在程序最开始加上 #include"includes.h" 导入头文件