修复运行程序时不断出现的错误?我试图让头指向第一个地址,然后能够首先通过将创建新节点的函数传递*,用户可以在运行时为其提供数据,然后首先指向新节点!我这样做对吗?
#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) 当您创建一个新节点时,您没有将其设置为 NULL。这是不见的。 newNode->next = NULL;
4)你处于无限循环中。 option
从未更新过。我补充了一句。此外,菜单应在用户给出选择后显示。
5) 不检查内存分配。如果malloc
返回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);
}
}