简体   繁体   English

C程序-链表问题

[英]C Program - Linked List Problems

I have a program that simulates a text editor. 我有一个模拟文本编辑器的程序。 It lets users add lines of text to a list in whatever particular manner they choose depending on the command they send. 它使用户可以根据他们发送的命令以任何特定方式将文本行添加到列表中。

One of the functions lets users move backwards through the list to view their lines (there's another one that lets them move forward, but that one's not being problematic). 其中一个功能使用户可以在列表中向后移动以查看其行(还有另一个功能可以使他们向前移动,但是这并不成问题)。

There's also functions to let users insert or append text. 还有让用户插入或附加文本的功能。 Insert has the line put in before the current line while append has it set after. 插入在当前行之前插入行,而插入在当前行之后设置行。 One problem I'm having is the way insert puts in text. 我遇到的一个问题是插入文本的方式。

User hits i for insert, puts text in via the standard input ( stdin ), and then hits CTRL + D (in a Linux environment) to simulate NULL and to return back to command mode. 用户按下i进行插入,通过标准输入( stdin )放入文本,然后按下CTRL + D (在Linux环境中)以模拟NULL并返回到命令模式。 After that, if you go to navigate through the list, it seems to enter the last line at the top of the list and everything follows suit backwards. 之后,如果您要浏览列表,则似乎在列表顶部输入了最后一行,所有内容都向后移动。 At one point, I had inserted 4 lines of text and it did an infinite loop of the last 2 lines and ruined the text file. 有一次,我插入了4行文本,它对最后2行进行了无限循环,并破坏了文本文件。

I believe it has to do with my logic in linking the lists, but I'm having a hard time visualizing them. 我相信这与链接列表的逻辑有关,但是我很难想象它们。 Here are the problematic functions: 这是有问题的功能:

void insert_line(char *t)
{
    /* Allocate and clear (i.e. set all to 0) */
    struct line *new_line = calloc(1, sizeof(struct line));

    new_line->text = t;

    if(current_line == NULL)
        head = current_line = new_line;
    else
    {
        new_line->prev = current_line->prev;
        new_line->next = current_line;
        current_line->next = new_line;
        current_line = new_line;

        if(current_line->prev == NULL)
            head = current_line;
    }
}

This must be terribly mucked up - the way it infinite loops the text sometimes and always puts the text in backwards. 这必须彻底解决-有时会无限循环文本,并始终将文本倒转。 This is how I utilize the insert function: 这就是我利用insert功能的方式:

else if(command[0] == 'i')
    {
        char * line;
        while((line = get_line(stdin)) != NULL)
            insert_line(line);
     }

get_line reads the text one line at a time and returns it until EOF is reached. get_line读取一行文本,并将其返回直到到达EOF。 I know the get_line function is working because my instructor wrote it for us to use. 我知道get_line函数正在工作,因为我的讲师编写了该函数供我们使用。

//
// Function: previous_line
// Moves the current_line pointer to the previous node, if any, in the linked-list.
//
void previous_line(void)
{
    if(current_line == NULL)
        printf("Error: No Lines Exist.\n");
    else if(current_line->prev != NULL) {
        current_line = current_line->prev;
        printf("%s\n", current_line->text);
    }
    else
        printf("Error: Already beginning-of-line.\n");
}

This one is weird, when I append text in the middle of text, the next_line function works fine, but then when I run this to go back through the list, it shows nothing of what I've added. 这很奇怪,当我在文本中间添加文本时, next_line函数可以正常工作,但是当我运行它以遍历列表时,它什么也没显示。

Draw it on paper (a box for each line and some arrows for next and prev) 画在纸上(每行一个方框,下一个和上一个箭头)

This bit has problems - should be fairly clear when you draw it. 这一点有问题-绘制时应该很清楚。

new_line->prev = current_line->prev;
new_line->next = current_line;
current_line->next = new_line;
current_line = new_line;

If you are trying to append the newline to the text file, you should be doing 如果您尝试将换行符追加到文本文件中,则应该这样做

new_line->prev = current_line;
current_line->next = new_line;

current_line = new_line;

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

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