简体   繁体   English

带c的双链表

[英]double linked list with c

ok, now i have to make double linked list on c.好的,现在我必须在 c 上制作双链表。 there are 7 functions which act on main.有 7 个函数作用于 main。

append.附加。 insertAt.插入。 deleteAt.删除。 print.打印。 print_revers.打印_反转。 newnode.新节点。 newDLL.新的DLL。

i can amend only 5 functions append, insertAt, deleteAt, print, print_reverse我只能修改 5 个函数 append、insertAt、deleteAt、print、print_reverse

finally i can make append,print,print_reverse however, i can't make insertAt,deleteAt, because of index.最后,我可以进行 append、print、print_reverse 但是,由于索引的原因,我无法进行 insertAt、deleteAt。

1. i can't understand why the code 1.我不明白为什么代码

else {
    while (index-- >= 0) {
      temp = temp->next;
    }

make memory collide.让记忆碰撞。 for using index, i need to move node to collect position and connect to newnode.为了使用索引,我需要移动节点来收集位置并连接到新节点。 but it doesn't work...但它不起作用......

2. also what's return; 2.还有什么回报; 's role?的作用? i have not seen such type of return.我还没有见过这种类型的回报。

3. how can i make deleteAt using index? 3.如何使用索引进行deleteAt? i think deleteAt and insertAt have quiet similar algoritum.我认为 deleteAt 和 insertAt 有类似的算法。 so i try to make insertAt first and deleteAt last.所以我尝试首先插入并删除最后。 but what i write doesn't work well..但我写的东西不好用..

i can find a lot of data of doublelinkedlist on internet.我可以在互联网上找到很多双链表的数据。 but i can't find using index.... i have been listening c language lecture on only two month, so sorry about the spagettii code...但我找不到使用索引....我只听了两个月的 c 语言讲座,所以对 spagettii 代码感到抱歉...

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int val;
    struct Node *prev;
    struct Node *next;
} Node;

typedef struct {
    Node *head;
    int size;
} DLL;

Node *newnode(int n)
{
    Node *temp = (Node *)malloc(sizeof(Node));
    temp->val = n;
    temp->prev = NULL;
    temp->next = NULL;
    return temp;
}

DLL *newDLL() {
    DLL *temp = (DLL *)malloc(sizeof(DLL));
    temp->head = NULL;
    temp->size = 0;
    return temp;
}

void append(DLL *list, Node *newnode) {
    struct Node* temp = list->head;
    struct Node* newNode = newnode;
    if (list->head == NULL) {
        list->head = newNode;
        list->size++;
        return;
    }
    while (temp->next != NULL) {
        temp = temp->next;
    }
    list->size++;
    temp->next = newNode;
    newNode->prev = temp;
}

void insertAt(DLL *list, int index, Node *newnode) {

    struct Node* temp = (Node *)malloc(sizeof(Node));

    if (index < 0 || index >= list->size + 1) {
        printf("out of range\n");
    }
    else if (index == 0) {
        newnode->next = list->head;
        list->head->prev = newnode;
        list->head = newnode;
    }
    else {
        while (index-- >= 0) {
          temp = temp->next;
        }

    temp->val = newnode->val;
    temp->next = list->head->next;
    list->head->next = temp;
    temp->prev = list->head;

    if (temp->next != NULL)
        temp->next->prev = temp;
    }
}

void deleteAt(DLL *list, int index) {
    //save reference to first link

    struct Node* temp = (Node *)malloc(sizeof(Node));

    //if only one link
    if (list->head->next == NULL) {
        list->head->prev = NULL;
    }
    else {
        list->head->next->prev = NULL;
    }

    list->head = list->head->next;
    //return the deleted link
    return;
}

void print(DLL *list) {
    struct Node* temp = list->head;
    printf("Forward: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->next;
    }
    printf("\n");
}

void print_reverse(DLL *list) {
    struct Node* temp = list->head;
    if (temp == NULL) return; // empty list, exit
    // Going to last Node
    while (temp->next != NULL) {
        temp = temp->next;
    }
    // Traversing backward using prev pointer
    printf("Reverse: ");
    while (temp != NULL) {
        printf("[%d] ", temp->val);
        temp = temp->prev;
    }
    printf("\n");
}

int main() {
    DLL *list = newDLL();
    int i;
    for (i = 1; i < 6; i++) {
        append(list, newnode(i));
    }

    print(list);
    deleteAt(list, -1);
    deleteAt(list, 5);
    deleteAt(list, 0);
    print(list);
    deleteAt(list, 2);
    print(list);
    deleteAt(list, 2);
    print(list);
    insertAt(list, -1, newnode(6));
    insertAt(list, 3, newnode(6));
    insertAt(list, 0, newnode(7));
    print(list);
    insertAt(list, 1, newnode(8));
    print(list);
    insertAt(list, 4, newnode(9));
    print(list);
    print_reverse(list);

    return 0;
}

The part where you insert at index has problems:在索引处插入的部分有问题:

temp = malloc is wrong, it should start with temp = head . temp = malloc是错误的,它应该以temp = head

in the insertion:在插入中:

temp->val = newnode->val;
temp->next = list->head->next;
list->head->next = temp;
temp->prev = list->head;

temp->next should not be head->next , it should be newnode . temp->next不应该是head->next ,它应该是newnode newnode->next should be temp->next etc. newnode->next应该是newnode->next temp->next等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM