行业混子 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 electron 如何实现自定义安装界面
  • ¥15 关于#linux#的问题:子进程C运行“ls –l”命令,且显示“C运行ls-l命令”(语言-c语言)
  • ¥15 vs code配置c语言遇到这个问题
  • ¥15 vscode调试编译找不到gcc,只有cl,但是检查cmd是对的,控制面板的路径也更改了
  • ¥20 access中怎么分割分别获取一下图中的值
  • ¥15 keras_tcn已经安装成功,还是显示ModuleNotFoundError: No module named 'keras_tcn'
  • ¥15 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?