微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果初始输入为 -1,为什么函数会退出?

如何解决如果初始输入为 -1,为什么函数会退出?

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

/*EVERYTHING WORKS EXCEPT FOR INITIAL -1*/

// Implementing a Node Structure - This represents a node of data
typedef struct _listnode
{
    int item; // Data
    struct _listnode *next; // Linkage
} ListNode;

// Core Functions of a Linked List
void printList(ListNode *head);
ListNode* findNode(ListNode *head,int index);
int insertNode(ListNode **ptrHead,int index,int value);
void removeNode(ListNode **ptrHead,int index);


int main()
{
    // Instantiate a Linked List
    ListNode *head = NULL,*temp;
    /*
    head is a pointer variable that will eventually point to the firstNode.
    temp is a pointer variable that points to the lastNode. This is used in from Linked List functions.
    */
    int num_to_store;
    printf("Enter an Integer to Store (-1 to end):\n");
    scanf("%d",&num_to_store);

    while (num_to_store != -1)
    {
        // Initialize a Linked List
        if (head == NULL)
        {
            head = malloc(sizeof(ListNode));
            temp = head;
        }
        else
        {
            temp->next = malloc(sizeof(ListNode));
            temp = temp->next;
        }
        temp->item = num_to_store;
        printf("Enter an Integer to Store (-1 to end):\n");
        scanf("%d",&num_to_store);
    }
    temp->next = NULL;

    // Menu-Driven Application
    int user_choice,index,value,*p,option_3;
    puts("");
    printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
    scanf("%d",&user_choice);
    while (user_choice != -1)
    {
        switch(user_choice)
        {
        case 1:
            printList(head);
            break;
        case 2:
            printf("Enter index to search:\n");
            scanf("%d",&index);
            p = findNode(head,index);
            if (p != NULL) printf("Node Item: %d\n",*p);
            break;
        case 3:
            printf("Enter index to insert:\n");
            scanf("%d",&index);
            printf("Enter value to insert:\n");
            scanf("%d",&value);
            option_3 = insertNode(&head,value);
            if (option_3 == 0) printf("NODE INSERTED!\n");
            break;
        case 4:
            printf("Enter index to remove:\n");
            scanf("%d",&index);
            removeNode(&head,index);
            break;

        }

        // Prompt User to Make Another Selection
        puts("");
        printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
        scanf("%d",&user_choice);

    }

    return 0;
}

void printList(ListNode *head)
{
    // Linked List is Empty
    if (head == NULL)
    {
        printf("LINKED LIST IS EMPTY!\n");
    }

    // Linked List is Not Empty
    else
    {
        printf("LINKED LIST: ");
        while (head != NULL)
        {
            printf("%d ",head->item);
            head = head->next;
        }
        puts("");
    }
}

ListNode* findNode(ListNode *head,int index)
{
    // Linked List is Empty or Index is Invalid
    if (head == NULL || index < 0)
    {
        printf("INVALID!\n");
        return NULL;
    }

    // Linked List is not Empty,Check if Index > len(Linked List)
    else
    {
        while (index > 0)
        {
            head = head->next;
            if (head == NULL)
            {
                printf("INVALID!\n");
                return NULL;
            }
            index--;
        }
        printf("INDEX FOUND!\n");
        return head;
    }
}

int insertNode(ListNode **ptrHead,int value)
{
    ListNode *cur,*pre;
    // Linked List is Empty || Insert at Index 0
    if ((*ptrHead) == NULL || index == 0)
    {
        cur = *ptrHead;
        *ptrHead = malloc(sizeof(ListNode));
        (*ptrHead)->item = value;
        (*ptrHead)->next = cur;
        return 0;
    }
    // Insert in the Middle
    else
    {
        pre = findNode(*ptrHead,index-1);
        if (pre != NULL)
        {
            cur = pre->next; // This is temporary
            pre->next = malloc(sizeof(ListNode)); // L connects to nN
            pre->next->item = value;
            pre->next->next = cur; // nN connects to R
        }
        return 0;
    }
    return -1;
}

void removeNode(ListNode **ptrHead,int index)
{
    ListNode *cur,*pre;
    // Linked List is Empty
    if ((*ptrHead == 0) || index < 0)
    {
        printf("INVALID!\n");
    }

    // Remove firstNode
    else if (index == 0)
    {
        cur = findNode(*ptrHead,index);
        *ptrHead = cur->next;
        free(cur); // Unallocate the memory
    }

    // Remove Middle
    else
    {
        pre = findNode(*ptrHead,index-1);
        cur = findNode(*ptrHead,index);
        pre->next = cur->next;
        cur->next = NULL;
    }
}

大家好,我正在用 C 语言编写一个带有 4 个核心函数的链表程序。除了当我编译代码并输入 -1 时,这些函数和其他一切都有效。程序退出。我想知道怎么了。我尝试在 code::blocks 上进行调试,但没有显示任何内容

我想达到什么目的?

  1. 创建一个没有节点的链表。
  2. 编写链表的 4 个核心功能(插入/删除/打印/搜索)。

我尝试了什么?

  1. 分散 printf() 语句进行调试。
  2. 检查了 stackoverflow 是否存在类似问题。
  3. 在 geeksforgeeks 上查看了代码

知道问题的原因可能是什么吗?以及针对此类问题的任何可能的解决方

解决方法

当您输入 -1 时,将跳过整个 while (num_to_store != -1) 循环,并且 temp 将保持未初始化状态,这将使 temp->next = NULL; 调用未定义的行为。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。