行业混子 2018-03-04 06:53 采纳率: 0%
浏览 1168
已采纳

开始学习C语言,高人指点

想实现一个初始化双链表。
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;
}


图片说明

  • 写回答

13条回答 默认 最新

  • 梦之启航 2018-03-04 08:55
    关注

    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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧