c-如果初始输入为-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: Endn----------------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: %dn", *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, index, 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: Endn----------------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 index, 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之外,函数和其他一切都可以工作。程序退出。我想知道怎么了。我尝试对代码::块进行调试,但没有显示任何结果。

我想要实现什么

  1. 创建一个没有节点的链表
  2. 对链表的4个核心函数进行编码(插入/删除/打印/搜索(

我试过什么

  1. 分散printf((语句进行调试
  2. 检查stackoverflow是否存在类似问题
  3. 查看了极客对极客的代码

知道问题的原因是什么吗?对于这类问题,有任何可能的解决方案吗?

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

最新更新