c语言 - 为什么我的程序突然终止?


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
static int COUNT = 1;
typedef struct node NODE;
struct node
{
int data;
NODE *next;
};
NODE *START = NULL;
NODE *create_node() {
NODE *n = malloc(sizeof(NODE));
n->data = 0;
n->next = NULL;
return n;
}
void insert_at_beginning() {
NODE *n, *temp;
n = create_node();
printf("Enter a number: ");
scanf("%d",&n->data);
if(START == NULL) {
START = n;
} else {
temp = START;
START = n;
n->next = temp;
}
COUNT++;
printf("Successfully Inserted!n");    
}
void insert_at_a_position() {
int position, count;
NODE *n, *temp, *t;
printf("Enter position at which you want to insert: ");
scanf("%d", &position);
if(COUNT<position) {
printf("Out of Bound! Please try again.n");
} else
{
count = 1;
n = create_node();
printf("Enter a number: ");
scanf("%d",&n->data);
temp = START;
while(count != position) {
count++;                    //To stoping the variable at given position
t = temp;                   //Getting Previous node where we will set link to  n
temp = temp->next;          //getting next element whose link will be attached to n to form complete linked list
}
n->next = temp;
t->next = n; 
COUNT++;
printf("Successfully Inserted!n");  
}
}
void insert_at_end() {
NODE *n, *temp;
temp = START;
n = create_node();
printf("Enter a number: ");
scanf("%d", &n->data);
while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}
temp->next = n;
COUNT++;
printf("Successfully Inserted!n");  
}
void display() {
NODE *temp;
temp = START;
while (temp!=NULL)
{
printf("%d", temp->data);
temp = temp->next;
}

}
int main() {
int ch;
printf("1.Insert at beginningn2.Insert at midn3.Insert at endn4.Delete from beginningn5.Delete from positionn6.Delete from endn7.Display");
printf("nEnter your choice: ");
scanf("%d", &ch);
while (ch!=0)
{
switch(ch) {
case 1:
insert_at_beginning();
break;
case 2:
insert_at_a_position();
break;
case 3:
insert_at_end();
break;
case 4:
display();
break;
default:
printf("Wrong Choice!!");
}
printf("Enter Your Choice: ");
scanf("%d",&ch);
}

return 0;
}

insert_at_beginninginsert_at_end工作正常,但insert_at_enddisplay功能显示问题

display函数中:程序进入无限循环

insert_at_end中:程序通过while循环(即,打印"我在这里"节点的时间号),但随后它突然终止,没有在给定位置分配值。

对于初学者来说,将节点的初始指针声明为全局变量以及函数依赖于全局变量时是一个坏主意。

也不清楚为什么静态变量 COUNT 在最初列表为空时由 1 而不是 0 初始化。

static int COUNT = 1;

它应该由零初始化

static int COUNT = 0;

insert_at_beginning和insert_at_end运行良好

你错了。函数insert_at_end无效。

void insert_at_end() {
NODE *n, *temp;
temp = START;
n = create_node();
printf("Enter a number: ");
scanf("%d", &n->data);
while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}
temp->next = n;
COUNT++;
printf("Successfully Inserted!n");  
}

例如,当列表为空时,即指针START等于 NULL 时,可以调用该函数。 在这种情况下,函数中的指针 START 不会更改。

此外,即使指针 START 不等于 NULL,在此循环之后

while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}

指针温度等于 NULL。所以下一句话

temp->next = n;

调用未定义的行为。

该函数至少可以按以下方式编写

void insert_at_end() {
NODE *n;
n = create_node();
printf("Enter a number: ");
scanf("%d", &n->data);
if ( START == NULL )
{
START = n;
}
else
{
NODE *temp = START;
while ( temp->next !=NULL ) 
{
printf("I am here!");
temp = temp->next;
}
temp->next = n;
COUNT++;
printf("Successfully Inserted!n");  
}
}

至于函数insert_at_a_position那么相对于全局变量 COUNT 存在混淆。正如我最初指出的那样,当列表为空时,COUNT 等于 1。因此,有效位置可以小于 COUNT。考虑到用户可以输入例如等于 0 的位置值。

所以例如这个 if 语句

if(COUNT<position) {
printf("Out of Bound! Please try again.n");
} else

应该像

if ( !( position < COUNT ) ) {
printf("Out of Bound! Please try again.n");
} else

同样,当用户输入 0 时,此循环

count = 1;
//...
while(count != position) {
//...

可以调用未定义的行为。

同样,如果列表为空,则 START 等于 NULL,则函数中的 START 不会更改。

此外,如果用户输入等于 1 的位置,那么在这种情况下,while 循环将不会执行。在这种情况下,指针t具有不确定的值,因为它未在循环外部初始化。所以这个说法

t->next = n; 

再次调用未定义的行为。

函数可以通过以下方式定义

void insert_at_a_position() {
int position;
printf("Enter position at which you want to insert: ");
scanf("%d", &position);
if( !( position < COUNT ) ) {
printf("Out of Bound! Please try again.n");
} else
{
NODE *n = create_node();
printf("Enter a number: ");
scanf("%d",&n->data);
NODE *temp = START;
NODE *prev = START;
while( position-- != 0 ) 
{
prev = temp;
temp = temp->next;        
}
n->next = temp;
if ( prev == NULL ) START = n;
else prev->next = n;
COUNT++;
printf("Successfully Inserted!n");  
}
}

您正在取消引用 NULL 指针。

while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}
temp->next = n;  // temp is NULL here!!

您可以将循环条件更改为:

while (temp->next != NULL) {

但在这样做之前,您应该检查温度是否NULL

temp = START;
if (temp == NULL) {
insert_at_beginning();
return;
}

旁白:只写更标准:

if (!temp)

if (temp == NULL)

最新更新