【发布时间】:2020-08-24 11:53:00
【问题描述】:
我正在用“c”编写单链表程序,而不使用动态内存分配。但它正在进入无限循环。整个程序:
#include <stdio.h>
struct SingleLinkedList
{
int data;
struct SingleLinkedList* next;
};
typedef struct SingleLinkedList sll;
void insertAtStart(sll** head, int data)
{
sll newNode = { data, NULL };
newNode.data = data;
if (*head == NULL)
{
*head = &newNode;
return;
}
newNode.next = *head;
*head = &newNode;
}
void insertAtEnd(sll** head, int data)
{
sll newNode = { data, NULL };
newNode.data = data;
if (*head == NULL)
{
*head = &newNode;
return;
}
sll* node = *head;
while (node -> next != NULL)
{
node = node -> next;
}
node -> next = &newNode;
}
void insertAfterNode(sll **head, int data)
{
int nodeNum, count = 1;
printf("\nEnter the node number to insert after: ");
scanf("%d", &nodeNum);
if (*head == NULL)
{
printf("\nThe List is empty!\n");
return;
}
sll *prevNode = *head;
while (count < nodeNum && prevNode -> next != NULL)
{
prevNode = prevNode -> next;
count++;
}
if (count < nodeNum)
{
printf("\nThere are only %d nodes in the list\n", count);
return;
}
sll newNode = { data, NULL };
newNode.next = prevNode -> next;
prevNode -> next = &newNode;
}
int choiceSelection()
{
int choice;
printf("\nSelect an Option:\n");
printf("1. Insert At Beginning\n");
printf("2. Insert At Last\n");
printf("3. Insert After Certain Node\n");
printf("4. Print all nodes\n");
printf("5. Exit\n");
scanf("%d", &choice);
return choice;
}
int dataEntry()
{
int data;
printf("\nEnter the data: ");
scanf("%d", &data);
return data;
}
void print(sll* node)
{
int count = 1;
while(node != NULL)
{
printf("\n----------------%d----------------\n", count);
printf("Data: %d", node -> data);
printf("\tAddress: %p", node);
printf("\tNext: %p\n", node -> next);
node = node -> next;
count++;
}
}
int main()
{
sll *head = NULL;
enum option {
InsertAtStart = 1,
InsertAtEnd = 2,
InsertAfterNode = 3,
Print = 4,
Exit = 5,
} choice;
while (choice != Exit)
{
choice = choiceSelection();
switch (choice)
{
case InsertAtStart:
insertAtStart(&head, dataEntry());
break;
case InsertAtEnd:
insertAtEnd(&head, dataEntry());
break;
case InsertAfterNode:
insertAfterNode(&head, dataEntry());
break;
case Print:
print(head);
break;
case Exit:
break;
default:
printf("\nIncorrect Choice..Please choose among 1, 2, 3, 4, 5\n");
break;
}
}
printf("\nExiting!");
return 0;
}
输出是:
Select an Option:
1. Insert At Beginning
2. Insert At Last
3. Insert After Certain Node
4. Print all nodes
5. Exit
1
Enter the data: 2
Select an Option:
1. Insert At Beginning
2. Insert At Last
3. Insert After Certain Node
4. Print all nodes
5. Exit
4
----------------1----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------2----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------3----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------4----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------5----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------6----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------7----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------8----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------9----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------10----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------11----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
----------------12----------------
Data: 0 Address: 0x7ffe2e94c0c0 Next: 0x7ffe2e94c0c0
--------^C
需要手动终止。
谁能告诉我问题出在哪里?或者不使用动态内存分配是不可能的?
【问题讨论】:
-
您总是应该尽可能地独立开发新功能。这段代码大部分与构建链表的问题无关;硬编码一些值,尝试构建一个包含两个元素的列表,然后看看会发生什么。
-
choice一开始没有初始化。您的 while 循环正在处理未定义的行为。 -
insertAtStart()函数试图在列表中嵌入一个指向局部变量的指针;这是灾难的根源。您需要一个(静态)分配的结构数组,您可以根据需要从中分配条目。 -
只是为了澄清:一旦函数返回,局部变量就会停止存在。因此,一旦函数返回,所有指向这些局部变量的指针都变为dangling pointers。
标签: c data-structures memory-management linked-list structure