gpt4 book ai didi

c - 带 c 的双链表

转载 作者:行者123 更新时间:2023-11-30 19:03:59 24 4
gpt4 key购买 nike

好的,现在我必须在 c 上创建双链表。有 7 个函数作用于 main。

追加。插入在。删除。打印。print_revers。新节点。新DLL。

我只能修改5个函数追加、insertAt、deleteAt、打印、print_reverse

终于我可以进行追加、打印、print_reverse但是,由于索引的原因,我无法进行 insertAt、deleteAt。

1.我不明白为什么代码

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

使内存发生碰撞。为了使用索引,我需要移动节点来收集位置并连接到新节点。但它不起作用...

2.还有返回什么;的角色?我还没有见过这样的返回。

3.我如何使用索引进行deleteAt?我认为deleteAt和insertAt有非常相似的算法。所以我尝试首先插入并最后删除。但我写的效果不太好..

我可以在互联网上找到很多双链表的数据。但我找不到使用索引....我只听了两个月的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;
}

最佳答案

您在索引处插入的部分有问题:

temp = malloc 错误,应该以 temp = head 开头。

在插入中:

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

temp->next 不应该是 head->next,而应该是 newnodenewnode->next 应该是 temp->next 等。

关于c - 带 c 的双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53183172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com