c - C 中链表的问题

标签 c while-loop linked-list singly-linked-list function-definition

我收到的提示要求使用 c 语言的程序来实现链接列表,并为用户提供在链接列表上执行不同功能的选项。需要的功能是:

  • isempty():检查列表是否为空并返回指示是否为空的值
  • add():向列表尾部添加一个元素
  • insert():在列表中的特定索引处插入元素
  • find():查找存储特定值的索引。 。如果函数失败,则返回一个值,表明
  • get():获取存储在特定索引处的值
  • remove():删除列表中第一次出现的特定值。如果函数失败,则返回一个指示该值的值
  • replace():替换存储在特定索引处的值
  • delete():删除特定索引处的元素。 。如果函数失败,则返回一个值,表明
  • list():以[a, b, c]的格式打印列表的元素

当我调用 add_tail 函数时,它允许我输入我想要添加的值,但大约一分钟后,我在终端中收到一条消息,说程序已被终止。可能是什么原因造成的?我还遇到了主函数中用于打印菜单并获取用户输入的 while 循环的问题。当我第一次运行程序时,菜单将正常打印,但在操作完成后,菜单打印一次但跳过用户输入,然后再次打印但接受用户输入第二次。

头文件:

#ifndef LAB8_H_
#define LAB8_H_

#define FIND 'F'
#define EMPTY 'E'
#define ADD 'A'
#define INSERT 'I'
#define DELETE 'D'
#define REMOVE 'R'
#define REPLACE 'S'
#define GET 'G'
#define LIST 'L'
#define QUIT 'Q'
#define FAIL -100

struct node_t {
        struct node_t * next;
        int value;
};

struct node_t * create_node(int value);
struct node_t * add_tail(struct node_t * head, int value);
struct node_t * insert_node(struct node_t * head, int index, int value);
int is_empty(struct node_t * head);
int find_val(struct node_t * head, int value);
int get_value(struct node_t * head, int index);
struct node_t * replace_val(struct node_t * head, int index, int value);
struct node_t * remove_val(struct node_t * head, int value);
struct node_t * delete_node(struct node_t * head, int index);
void print_list(struct node_t * head);

#endif 

.c 文件:

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

struct node_t * create_node(int value)
{
        struct node_t * new_node = malloc(sizeof(struct node_t));
        new_node -> next = NULL;
        new_node -> value = value;

        return new_node;
}


struct node_t * add_tail(struct node_t * head, int value)
{
        struct node_t * tmp;

        if(head == NULL){
                printf("Cannot add tail to an empty list. Try again\n");
                return NULL;
        } else{
                while(head != NULL){
                        if(head -> next == NULL){
                                tmp = create_node(value);
                                head -> next = tmp;
                        } else{
                                head = head -> next;
                        }
                }
        }

        return head;
}


struct node_t * insert_node(struct node_t * head, int index, int value)
{
        struct node_t * tmp;
        struct node_t * new;

        if(index < 0){
                printf("Index cannot be a negative number. Please try again\n");
                return head;
        }

        if(index == 0){
                tmp = head;
                head = create_node(value);
                head -> next = tmp;
        } else {
                tmp = head;

                while(tmp != NULL){
                        if (index == 1){
                                struct node_t * prev = tmp;
                                tmp = tmp -> next;
                                new = create_node(value);
                                new -> next = tmp;
                                prev -> next = new;
                                head = prev;
                        } else if((tmp -> next == NULL) && (index != 0)){
                                printf("The index is not found in the bounds of the list. Try again\n");
                                return head;
                        } else{
                                tmp = tmp -> next;
                                index--;
                        }
                }
        }

        return head;
}

int is_empty(struct node_t * head)
{
        if(head == NULL){
                return 0;
        } else{
                return 1;
        }
}

int find_val(struct node_t * head, int value)
{
        int index = 0;

        if(head == NULL){
                printf("Cannot find value in empty list! Try again\n");
                index = -100;
        } else{
                while(head != NULL){
                        if((head -> next == NULL) && (head -> value != value)){
                                printf("The value does not exist in the list\n");
                                index = -100;
                        } else if(head -> value == value){
                                return index;
                        } else{
                                head = head -> next;
                                index++;
                        }
                }
        }

        return index;
}

int get_value(struct node_t * head, int index)
{
        int value;

        if(index < 0){
                printf("Index cannot be a negative number. Try again\n");
                value = -100;
        } else if(head == NULL){
                printf("Cannot find index in empty list. Try again\n");
                value = -100;
        } else{
                while(head != NULL){
                        if(index == 0){
                                value = head -> value;

                        } else if((head -> next == NULL) && (index != 0)){
                                printf("Index does not exist in the bounds of the list. Try again\n");
                                value= -100;
                        } else{
                                head = head -> next;
                                index--;
                        }
                }
        }

        return value;
}

struct node_t * replace_val(struct node_t * head, int index, int value)
{
        struct node_t * tmp;

        if(index < 0){
                printf("Index cannot be a negative number. Try again\n");
                return NULL;
        } else if(head == NULL){
                printf("Cannot replace elements in an empty list. Try again\n");
                return NULL;
        } else{
                while(head != NULL){
                        if (index == 0){
                                tmp = head;
                                tmp -> value = value;
                                free(head);
                                head = tmp;
                        } else if((head -> next == NULL) && (index != 0)){
                                printf("Index does not exist is the bounds of the list. Try again\n");
                                return NULL;
                        } else{
                                head = head -> next;
                                index--;
                        }
                }
        }

        return head;
}

struct node_t * remove_val(struct node_t * head, int value)
{
        struct node_t * tmp;

        if(head == NULL){
                printf("Value cannot be found in an empty list. Try again\n");
                return NULL;
        } else{
                while(head != NULL){
                        if((head -> next == NULL) && (head -> value != value)){
                                printf("The value does not exist in the list. Try again\n");
                                return NULL;
                        } else if(head -> next -> value == value){
                                tmp = head -> next;
                                head -> next = tmp -> next;
                                free(tmp);
                        } else{
                                head = head -> next;
                        }
                }
        }

        return head;
}

struct node_t * delete_node(struct node_t * head, int index)
{
        struct node_t * tmp;

        if(index < 0){
                printf("Index cannot be a negative number. Try again\n");
                return NULL;
        } else if(head == NULL){
                printf("Cannot delete elements from an empty list. Try again\n");
                return NULL;
        } else{
                while(head != NULL){
                        if((head -> next == NULL) && (index != 0)){
                                printf("The index is not found in the bounds of the list. Try again\n");
                                return NULL;
                        } else if(index == 1){
                                tmp = head;
                                head = head -> next;
                                tmp -> next = head -> next;
                                head -> next = NULL;
                                free(head);
                                head = tmp;
                        } else{
                                head = head -> next;
                                index--;
                        }
                }
        }

        return head;
}

void print_list(struct node_t * head)
{
        if (head == NULL){
                printf("The list is empty. Nothing to print\n");
        } else{
                while(head != NULL){
                        if(head -> next != NULL){
                                printf("%d, ", head -> value);
                        } else{
                                printf("%d", head -> value);
                        }

                        head = head -> next;
                }
        }
}

主文件:

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

int main(void)
{
        struct node_t * head = NULL;
        int index, value;
        char choice;
        int cont = 0;

        while(cont == 0){
                printf("Welcome to the Linked List operator. Here are the functions that can be performed:\n");
                printf("A) Add element to the end of the list\n");
                printf("I) Insert and element at a specific index\n");
                printf("E) Check if the list is empty\n");
                printf("F) Find a value in the list and return the index\n");
                printf("G) Get the value at a specific index\n");
                printf("S) Replace the value at a specific index\n");
                printf("R) Remove the first occurance of a specific value\n");
                printf("D) Delete the element at a specific index\n");
                printf("L) List all the elements currently stored\n");
                printf("Q) Quit the program\n");
                printf("Please enter which action you would like to perform: ");
                scanf("%c", &choice);

                switch(choice){
                        case ADD:
                                printf("Please enter the value you would like to add: ");
                                scanf("%d", &value);

                                head = add_tail(head, value);

                                break;
                        case INSERT:
                                printf("Please enter the index at which you want to insert a new element: ");
                                scanf("%d", &index);
                                printf("Please enter the value you would like to insert: ");
                                scanf("%d", &value);

                                head = insert_node(head, index, value);

                                break;
                        case EMPTY:
                                if(is_empty(head) == 0){
                                        printf("The list is empty!\n");
                                } else if(is_empty(head) == 1){
                                        printf("The list is not empty!\n");
                                } else{
                                        printf("Something went wrong\n");
                                }

                                break;
                        case FIND:
                                printf("Please enter the value that you would like to find in the list: ");
                                scanf("%d", &value);

                                index = find_val(head, value);

                                if(index == FAIL){
                                        printf("Error. Try again\n");
                                } else{
                                        printf("The index that the value %d exists at is %d\n", value, index);
                                }

                                break;
                        case GET:
                                printf("Please enter the index for which you would like to know the value: ");
                                scanf("%d", &index);

                                if(value == FAIL){
                                        printf("Error. Try again\n");
                                } else{
                                        printf("The value of the element at index %d is %d\n", index, value);
                                }

                                break;
                        case REPLACE:
                                printf("Please enter the index of the element that you would like to replace: ");
                                scanf("%d", &index);
                                printf("Please enter the new value: ");
                                scanf("%d", &value);

                                if(replace_val(head, index, value) == NULL){
                                        printf("Error. Could not replace node\n");
                                } else{
                                        printf("Success. Here is the new list:\n");
                                        print_list(head);
                                }

                                break;
                        case REMOVE:
                                printf("Please enter the value that you would like to remove the first occurance of: ");
                                scanf("%d", &value);

                                if(remove_val(head, value) == NULL){
                                        printf("Error. Could not remove node\n");
                                } else{
                                        printf("Success! Here is the new list:\n");
                                        print_list(head);
                                }

                                break;
                        case DELETE:
                                printf("Please enter the index of the element you would like to delete: ");
                                scanf("%d", &index);

                                if(delete_node(head, index) == NULL){
                                        printf("Error. Could not delete selected element\n");
                                } else{
                                        printf("Success! Here is the new list:\n");
                                        print_list(head);
                                }

                                break;
                        case LIST:
                                printf("[");
                                print_list(head);
                                printf("]\n");

                                break;
                        case QUIT:
                                printf("You have chosen to quit the program. Goodbye!\n");
                                cont = 1;

                                break;
                        default:
                                printf("You entered an invalid choice. Please try again. Goodbye!\n");
                }
        }

        return 0;
}

最佳答案

函数 add_tail 中的 if 语句

    if(head == NULL){
            printf("Cannot add tail to an empty list. Try again\n");
            return NULL;

没有意义。应该将其删除。

在函数的 while 循环内,指针 head 发生变化。

while(head != NULL){
        if(head -> next == NULL){
                tmp = create_node(value);
                head -> next = tmp;
        } else{
                head = head -> next;
        }
}

添加节点后循环不会中断。它将再次添加一个新节点。即无限循环。

但是即使你会插入break语句

while(head != NULL){
        if(head -> next == NULL){
                tmp = create_node(value);
                head -> next = tmp;
                break;
        } else{
                head = head -> next;
        }
}

函数返回的指针将不是指向链表头节点的指针。

该函数可以通过以下方式定义

struct node_t * add_tail( struct node_t *head, int value )
{
    struct node_t *new_node = create_node( value );

    if ( head == NULL )
    {
        head = new_node;
    }
    else
    {
        struct node_t *current = head;
        
        while ( current->next != NULL ) current = current->next;

        current->next = new_node;
    }

    return head;
}

我确信您的程序中的代码还存在其他问题。您可以在更新您在此问题中引用的函数 add_tail 后提出新问题。

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

相关文章:

java - 创建一个简单的 "shopping cart"程序

从文件输入创建链接列表

Iphone Objective-c 全局变量不同的值

c - 用c实现终端命令

c - 如何使用 Fake Function Framework (FFF) 伪造一个采用 2D 数组作为输入的 C 函数?

C++ While 循环,usleep()/sleep() 如何不使用 90% 的 CPU? (Ubuntu 12.04)

c - 在 C 中使用 while(1) 循环作为临时状态机?

C 打印链表不起作用?

c - 如何在已排序的链表中添加数字?

c - strcat_s() 是否需要使用 realloc()?