为什么我的程序突然终止?
Why does my program is terminating abruptly?
#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 beginning\n2.Insert at mid\n3.Insert at end\n4.Delete from beginning\n5.Delete from position\n6.Delete from end\n7.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_beginning
和 insert_at_end
运行良好,但 insert_at_end
和 display
功能出现问题
在display
函数中:程序进入无限循环
In insert_at_end
: 程序经过 while 循环(即打印 "I am here" 时间节点数)但随后它突然终止而没有在给定位置分配值。
您正在取消引用 NULL 指针。
while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}
temp->next = n; // temp is NULL here!!
您可以将循环条件更改为:
while (temp->next != NULL) {
但在你这样做之前,你应该检查 temp 不是 NULL
:
temp = START;
if (temp == NULL) {
insert_at_beginning();
return;
}
旁白:更标准的写法是:
if (!temp)
比
if (temp == NULL)
对于初学者来说,将指向节点的初始指针声明为全局变量以及当函数依赖于全局变量时不是一个好主意。
另外,当列表最初为空时,为什么静态变量 COUNT 被初始化为 1 而不是 0 也不清楚。
static int COUNT = 1;
应该用零初始化
static int COUNT = 0;
insert_at_beginning and insert_at_end is working perfectly
你错了。函数 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;
}
指针 temp 等于 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");
}
}
#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 beginning\n2.Insert at mid\n3.Insert at end\n4.Delete from beginning\n5.Delete from position\n6.Delete from end\n7.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_beginning
和 insert_at_end
运行良好,但 insert_at_end
和 display
功能出现问题
在display
函数中:程序进入无限循环
In insert_at_end
: 程序经过 while 循环(即打印 "I am here" 时间节点数)但随后它突然终止而没有在给定位置分配值。
您正在取消引用 NULL 指针。
while(temp!=NULL) {
printf("I am here!");
temp = temp->next;
}
temp->next = n; // temp is NULL here!!
您可以将循环条件更改为:
while (temp->next != NULL) {
但在你这样做之前,你应该检查 temp 不是 NULL
:
temp = START;
if (temp == NULL) {
insert_at_beginning();
return;
}
旁白:更标准的写法是:
if (!temp)
比
if (temp == NULL)
对于初学者来说,将指向节点的初始指针声明为全局变量以及当函数依赖于全局变量时不是一个好主意。
另外,当列表最初为空时,为什么静态变量 COUNT 被初始化为 1 而不是 0 也不清楚。
static int COUNT = 1;
应该用零初始化
static int COUNT = 0;
insert_at_beginning and insert_at_end is working perfectly
你错了。函数 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;
}
指针 temp 等于 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");
}
}