我对C很新,并且正在尝试在C中实现链表。在代码中应该将节点添加到空链表或将节点插入列表开头的部分,后者但是在尝试将单个节点添加到空链表时会发生错误。其余的程序运行正常。
编辑:我在在线GDB调试器上运行代码,它在第111行显示错误。如果我删除了该代码块,它不再抛出错误。但是,我仍然无法弄清楚为什么会在那里抛出错误。
分段错误。
在main.c:111的AddAfter()中的0x0000000000400927
111 while(prevNode-> next!= NULL)
以下是my code的详细信息。
/*
# main() is at the bottom.
*/
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
typedef struct node
{
int data;
struct node * next;
} node;
//Global Variable
node* head = NULL;
node* createList()
{
int n;
printf("Loading...\n");
Sleep(1);
printf("How many nodes would you like to have in the list ?\n");
scanf("%d",&n);
int i;
node* temp = NULL;
node* nodeJumper = NULL;
for (i = 0; i<n; i++)
{
temp = malloc(sizeof(node));
printf("Enter node %d : ",i+1);
scanf("%d", &(temp->data));
temp->next = NULL;
if (head == NULL)
head = temp;
else
{
nodeJumper = head;
while (nodeJumper->next != NULL)
nodeJumper = nodeJumper -> next;
nodeJumper->next = temp;
}
}
printf("Sucessfully created\n\n");
return head;
}
void printList()
{
node* nodeptr = NULL;
if (head == NULL)
printf("The list is empty.");
else
{
nodeptr = head;
printf("Your Linked List : ");
while(nodeptr != NULL)
{
printf(" %d>", nodeptr->data);
nodeptr = nodeptr->next;
}
printf("\n\n");
}
}
int countNode()
{
node* temp = head;
int count = 0;
while (temp!= NULL)
{ count++;
temp = temp-> next;
}
return count;
}
void AddAfter()
{
int pos, data;
printf("Loading...\n");
Sleep(1);
printf("Enter position number to insert new node: ");
scanf("%d",&pos);
printf("Enter a number to enter in the list: ");
scanf("%d", &data);
int count = countNode(); //Count no. of nodes in linked list
int i;
node* prevNode = head; //node before inserted new node.
node* temp = malloc(sizeof(node));
temp->data = data;
if (pos == count+1) //Append the list
{
while (prevNode->next != NULL)
prevNode= prevNode->next;
temp->next = NULL;
prevNode->next = temp;
}
else if (pos == 1) //Throws error while trying to add node to empty linked list
{
temp->next = head;
head = temp;
}
else if (pos <= count && pos >= 1)
{
for (i=1; i< pos-1; i++)
prevNode = prevNode->next;
temp->next = prevNode->next;
prevNode->next = temp;
}
else //Invalid positions input by user.
{
printf("Request unsucessful\n");
printf("Only positions 1 to %d are valid", count+1);
}
}
int printMenu()
{
int option;
printf("Select options from menu:\n");
printf("1> Create Linked List\n2> Add nodes anywhere in the linked list\n3> Quit\n\n");
printf("Enter option number here: ");
scanf("%d",&option);
return option;
}
int main()
{
printf("\n\t### Welcome to Linked list manager ###\n\n");
while(1)
{
int userChoice = printMenu();
if (userChoice == 1)
{
head = createList();
printList();
}
else if (userChoice == 2)
{
AddAfter();
printf("\n");
printList();
}
else if (userChoice ==3)
{
exit(0);
}
else
{
printf("Enter a valid option number.\n");
}
}
return 0;
}
我一直想弄清楚几个小时。任何指导都将非常感谢。
答案 0 :(得分:0)
只是一个下意识的反应,如果&#34; prevNode&#34;会发生什么?是无效还是NULL?如果你运行&#34;选项2&#34;在运行&#34;选项1&#34;之前,&#34; head&#34;将为NULL,并且您将进行segv。您应该添加一个签入&#34; AddAfter&#34;验证&#34; head&#34;不是NULL。