C链表和节点程序,为什么会出现未处理的异常?
C linked list and nodes program, why am I getting a unhanded exception?
如何修复我在 运行 程序中不断遇到的错误?我试图让 head 指向 first 的地址,然后能够通过一个将创建新节点的函数传递 *first,用户可以在 运行 时间给它数据,然后 first 将指向到新节点!我这样做对吗?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);
struct node{
int data;
struct node *next;
};
void main(){
int option = 0;
struct node *head;
struct node *first;
head = (struct node*)malloc(sizeof(struct node));
first = (struct node*)malloc(sizeof(struct node));
head->data= 0;
head->next = first;
first->data = 1;
first->next = NULL;
Menu();
scanf(" %d", option);
while(option != 6){
switch(option){
case 1:
addToStart(&first);
break;
case 3:
DisplayList(head);
break;
case 6:
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** first)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = *first;
*first = newNode; // transfer the address of newNode' to 'head'
}
void Menu(){
printf("1) Add a node.\n");
printf("3) Display all nodes.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d", temp->data); // show the data
temp = temp->next;
}
}
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);
int main(){
int option = 0;
struct node *head;
//struct node *first;
//head = (struct node*)malloc(sizeof(struct node));
//first = (struct node*)malloc(sizeof(struct node));
head = NULL;
option = 0;
while(option != 6){
Menu();
scanf(" %d", &option);
switch(option){
case 1:
addToStart(&head);
break;
case 3:
DisplayList(head);
break;
case 6:
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
struct node *lastNode = *head;
while(lastNode->next!=NULL)
lastNode = lastNode->next;
lastNode->next = newNode;
}
// *first = newNode; // transfer the address of newNode' to 'head'
}
void Menu(){
printf("\n1) Add a node.\n");
printf("3) Display all nodes.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d", temp->data); // show the data
temp = temp->next;
}
}
这有效。
你应该考虑的几点:
1) 是int main
,不是void main()
2) 当你做 scanf
时,给出你想要设置值的变量地址。应该是 scanf(" %d", &option);
而不是 scanf(" %d", option);
3) 当你创建一个新节点时,你没有将它的 next 设置为 NULL。这个不见了。 newNode->next = NULL;
4) 你陷入了无限循环。 option
从未更新过。我补充说。此外,菜单应在用户做出选择后显示。
5) 没有检查内存分配。如果malloc
returns NULL
怎么办?
6) 您对 first
和 head
感到困惑。约定是使用head
。
7) 我每次都会使用 typedef
而不是 struct node
。这留作练习:)
OP,此列表并不详尽。
如果你想倒序显示数字,把addToStart
中的else部分改成这样
else
{
newNode->next = *head;
*head = newNode;
}
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void addToStart (struct node** head);
void addToEnd (struct node** head);
void Menu();
void Size(struct node* head);
void DisplayList(struct node* head);
void SearchList(struct node* head);
int main(){
int option = 0;
struct node *head;
struct node *first;
head = (struct node*)malloc(sizeof(struct node));
first = (struct node*)malloc(sizeof(struct node));
head = NULL;
option = 0;
while(option != 6){
Menu();
scanf(" %d", &option);
switch(option){
case 1:
addToStart(&head);
break;
case 2:
addToEnd(&head);
break;
case 3:
DisplayList(head);
break;
case 4:
Size(head);
break;
case 5:
SearchList(head);
break;
case 6:
free(head);
free(first);
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node:\n");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
newNode->next = *head;
*head = newNode;
}
printf("%u,%u",&head,&newNode);
// *first = newNode; // transfer the address of newNode' to 'head'
}
void addToEnd (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node:\n");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
struct node *lastNode = *head;
while(lastNode->next != NULL){
lastNode = lastNode->next;
lastNode->next = newNode;
}
*head = newNode;
}
// *first = newNode; // transfer the address of newNode' to 'head'
}
void Size(struct node* head)
{
int len = 0;
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while(temp != NULL)
{
len++;
temp = temp->next;
}
printf("Size of list: %d", len);
}
void Menu(){
printf("\n1) Add a node.\n");
printf("2) Add node to end.\n");
printf("3) Display all nodes.\n");
printf("4) Display the length of list.\n");
printf("5) Search the list.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d ", temp->data); // show the data
temp = temp->next;
}
}
void SearchList(struct node* head){
int keynum;
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
printf("\nEnter number:\n");
scanf(" %d", &keynum);
if(temp->data == keynum){
printf("\n%d was found in the list!\n", keynum);
}
else{
printf("\n%d is not in the list!\n", keynum);
}
}
如何修复我在 运行 程序中不断遇到的错误?我试图让 head 指向 first 的地址,然后能够通过一个将创建新节点的函数传递 *first,用户可以在 运行 时间给它数据,然后 first 将指向到新节点!我这样做对吗?
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);
struct node{
int data;
struct node *next;
};
void main(){
int option = 0;
struct node *head;
struct node *first;
head = (struct node*)malloc(sizeof(struct node));
first = (struct node*)malloc(sizeof(struct node));
head->data= 0;
head->next = first;
first->data = 1;
first->next = NULL;
Menu();
scanf(" %d", option);
while(option != 6){
switch(option){
case 1:
addToStart(&first);
break;
case 3:
DisplayList(head);
break;
case 6:
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** first)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = *first;
*first = newNode; // transfer the address of newNode' to 'head'
}
void Menu(){
printf("1) Add a node.\n");
printf("3) Display all nodes.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d", temp->data); // show the data
temp = temp->next;
}
}
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void addToStart (struct node** head);
void Menu();
void DisplayList(struct node* head);
int main(){
int option = 0;
struct node *head;
//struct node *first;
//head = (struct node*)malloc(sizeof(struct node));
//first = (struct node*)malloc(sizeof(struct node));
head = NULL;
option = 0;
while(option != 6){
Menu();
scanf(" %d", &option);
switch(option){
case 1:
addToStart(&head);
break;
case 3:
DisplayList(head);
break;
case 6:
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
struct node *lastNode = *head;
while(lastNode->next!=NULL)
lastNode = lastNode->next;
lastNode->next = newNode;
}
// *first = newNode; // transfer the address of newNode' to 'head'
}
void Menu(){
printf("\n1) Add a node.\n");
printf("3) Display all nodes.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d", temp->data); // show the data
temp = temp->next;
}
}
这有效。
你应该考虑的几点:
1) 是int main
,不是void main()
2) 当你做 scanf
时,给出你想要设置值的变量地址。应该是 scanf(" %d", &option);
而不是 scanf(" %d", option);
3) 当你创建一个新节点时,你没有将它的 next 设置为 NULL。这个不见了。 newNode->next = NULL;
4) 你陷入了无限循环。 option
从未更新过。我补充说。此外,菜单应在用户做出选择后显示。
5) 没有检查内存分配。如果malloc
returns NULL
怎么办?
6) 您对 first
和 head
感到困惑。约定是使用head
。
7) 我每次都会使用 typedef
而不是 struct node
。这留作练习:)
OP,此列表并不详尽。
如果你想倒序显示数字,把addToStart
中的else部分改成这样
else
{
newNode->next = *head;
*head = newNode;
}
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void addToStart (struct node** head);
void addToEnd (struct node** head);
void Menu();
void Size(struct node* head);
void DisplayList(struct node* head);
void SearchList(struct node* head);
int main(){
int option = 0;
struct node *head;
struct node *first;
head = (struct node*)malloc(sizeof(struct node));
first = (struct node*)malloc(sizeof(struct node));
head = NULL;
option = 0;
while(option != 6){
Menu();
scanf(" %d", &option);
switch(option){
case 1:
addToStart(&head);
break;
case 2:
addToEnd(&head);
break;
case 3:
DisplayList(head);
break;
case 4:
Size(head);
break;
case 5:
SearchList(head);
break;
case 6:
free(head);
free(first);
exit(0);
break;
default:
printf("\nTry Again");
break;
}//switch end
}//while end
}
void addToStart (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node:\n");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
newNode->next = *head;
*head = newNode;
}
printf("%u,%u",&head,&newNode);
// *first = newNode; // transfer the address of newNode' to 'head'
}
void addToEnd (struct node** head)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
printf("\nEnter data for this node:\n");
scanf("%d", &newNode->data);
newNode->next = NULL;
if (*head==NULL)
{
*head = newNode;
}
else
{
struct node *lastNode = *head;
while(lastNode->next != NULL){
lastNode = lastNode->next;
lastNode->next = newNode;
}
*head = newNode;
}
// *first = newNode; // transfer the address of newNode' to 'head'
}
void Size(struct node* head)
{
int len = 0;
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while(temp != NULL)
{
len++;
temp = temp->next;
}
printf("Size of list: %d", len);
}
void Menu(){
printf("\n1) Add a node.\n");
printf("2) Add node to end.\n");
printf("3) Display all nodes.\n");
printf("4) Display the length of list.\n");
printf("5) Search the list.\n");
printf("6) Exit.\n");
}
void DisplayList(struct node* head){
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
while( temp!= NULL )
{
printf("Data: %d ", temp->data); // show the data
temp = temp->next;
}
}
void SearchList(struct node* head){
int keynum;
struct node *temp;
temp =(struct node*)malloc(sizeof(struct node));
temp = head;
printf("\nEnter number:\n");
scanf(" %d", &keynum);
if(temp->data == keynum){
printf("\n%d was found in the list!\n", keynum);
}
else{
printf("\n%d is not in the list!\n", keynum);
}
}