首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迭代时列表结构的变化

迭代时列表结构的变化
EN

Stack Overflow用户
提问于 2021-08-04 13:34:41
回答 1查看 77关注 0票数 0

我使用下面的代码来迭代一个链接列表

代码语言:javascript
复制
void display()
{
    if (head == NULL)
    {
        printf("\n");
        return;
    }
    struct node *current = head;
    while (current->next != NULL)
    {

        printf("%3c", current->data); // *(current).data

        current = current->next; // current = *(current).next;

    } //printf( "\n" );
    printf("%3c", current->data);
    printf("    <-->  ");
    while (current != NULL)
    {
        printf("%3c", current->data); // *(current).data
        current = current->prev;      // current = *(current).prev;
    }
    printf("\n");
}

我在这个函数中有一个分割错误,我无法显示

不知道为什么会发生这种事。

我运行调试器并拍摄了截图:

以上两个图像是从调用到迭代时显示的。

在时间循环上。奇怪的是在第一张照片里

元素为Y->O->R->x->K。

而当迭代在while循环中继续时

在遇到R之后,下一个数据奇怪地是一些\000

我从来没有插入过那个,而且它是如何改变的?

相同的迭代,因为没有发生修改。

最小可复制代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 50
#define MAX_LEN 30
char get(int index);
int search(char);
struct twoInts
{
    int int1;
    int int2;
};

struct node
{ // list 'node' struct
    char data;
    struct node *next;
    struct node *prev;
};

void init();
void display();

struct node *head;

int main()
{
    int index;
    char key;

    struct twoInts *arr[MAX_LEN];
    char buffer[BUFFER_SIZE];
    init();

    int i = 0;
    int summation[10] = {89, 79, 82, 75, 85, 76, 65, 66, 83};
    for (; i < 8; i++)
    {
        int sum = summation[i];
        insert((char)sum);
        display();
    }

    char removeList[] = {'B', 'S', 'A', 'O', 'R', 'K', 'Y', 'U', 'L', '\0'};
    i = 0;

    // insert again
    char addList[] = "ZPSLVMBCT";
    i = 0;
    while ((key = addList[i]) != '\0')
    {
        insert(key);
        display();
        i++;
    }
    key = 'x';
    index = 2;
    insertAfter(key, index);
    printf(xyz-1\n");
    int length = len();
    printf("xyz-2\n");

    // printf("\ninsert %c after index %d: (%d)\n", key, index, length);
    display();
}

/* Initialize the list. */
void init()
{
    head = NULL;
}
int len()
{
    struct node *tmp = head;
    int length = 0;
    while (tmp != NULL)
    {
        length++;
        tmp = tmp->next;
    }
    return length;
}
/* Print the content of the list. */
void display()
{
    if (head == NULL)
    {
        printf("\n");
        return;
    }
    struct node *current = head;
    while (current->next != NULL)
    {

        current = current->next; // current = *(current).next;

    } //printf( "\n" );
    while (current != NULL)
    {
        current = current->prev; // current = *(current).prev;
    }
}

void insert(char c) //  at the end
{
    /* special case: list is empty, need to change head  */
    if (head == NULL)
    { /* the list is empty */
        head = malloc(sizeof(struct node));
        head->data = c;
        head->next = NULL;
        head->prev = NULL;
    }
    else
    {
        struct node *tmp = head;
        while (tmp->next != NULL)
            tmp = tmp->next;
        struct node *last = malloc(sizeof(struct node));
        last->data = c;
        last->next = NULL;
        last->prev = tmp;
        tmp->next = last;
    }
}

void insertAfter(char c, int index)
{

    struct node *tmp = head;
    int currIndex = 0;
    while (tmp->next != NULL)
    {
        if (currIndex == index)
            break;

        currIndex++;
        tmp = tmp->next;
    }
    if (currIndex == index)
    {
        struct node *newNode = malloc(sizeof(struct node));
        newNode->data = c;
        newNode->prev = tmp;
        newNode->next = tmp->next;
        struct node *nextNode = tmp->next;
        if (tmp->next != NULL)
        {
            tmp->next = &newNode;
            nextNode->prev = &newNode;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-04 16:07:34

好吧我已经解决了你的问题。问题在您的insertAfter函数中,您在其中声明struct node newNode。这将在堆栈上创建一个对象,而列表的其余部分则存储在中。当insertAfter函数退出时,这会导致堆栈被打开,内存无法访问。您应该将这一行替换为调用malloc

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68652219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档