iT邦幫忙

0

想請問有大大能幫看,為甚麼輸入一連串數字後再輸入1會無法繼續執行。

#include<stdlib.h>
#include<stdio.h>

struct Node{
int num;
struct Node *next=NULL;
};

struct Node *head=NULL;//開頭指標

struct Node* newNode(int num){ //建檔副程式

struct Node* add=(struct Node*)malloc(sizeof(struct Node));  
if(add != NULL)
{
    add->num = num;
    add->next = NULL;
}
return add;

}

int Insert() //照順序插入
{
int num;
printf("Insert a number: ");
scanf("%d",&num);
struct Node* add = newNode(num); //將欲插入值生成節點
struct Node* ptr = head; //利用ptr找出欲插入位置
struct Node* pre = NULL;
if(ptr==NULL){
head=add;
}
else{
while(ptr!=NULL && ptr->num > num)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr->num == num){
printf(" %d已存在串列中\n",num);
return 0;
}
if(ptr==NULL)
{
printf("%d",num);
pre->next=add;
return 0;
}
if(pre==NULL)
{
printf("%d",num);
add->next=ptr;
head=add;
return 0;
}
pre->next=add;
add->next=ptr;
}
}

int Delete() //刪掉所有同樣的資料
{
int num;
printf("Delete a number: ");
scanf(" %d",&num);
struct Node* ptr = head;
struct Node* del = NULL;
if(ptr == NULL) //head為空則刪除失敗
{
printf("Not in the list!\n");
return 0;
}
while(ptr!=NULL && ptr->num==num){
if(ptr->num == num) //head為欲刪除值
{
del=ptr;
head=ptr->next;
ptr=head;
free(del);
}
}
if(ptr!=NULL){
while(ptr->next != NULL) //尋找整個串列
{
if(ptr->next->num == num)
{
del=ptr->next;
ptr->next=del->next;
free(del);
}
else{
ptr = ptr->next;
}
}
if(ptr->next == NULL){
printf("Not in the list!\n");
}
}
}

int main(void){

int i;
int op=5;
while(op!=0){
	printf("  操作選項表\n==============\n[1]加入資料\n[2]刪除資料\n[3]列印\n[4]結束\n");
	printf("輸入選項:");
	scanf("%d",&op);
	switch(op){
		case 1:
			Insert();
			break;
		case 2:
			Delete();
			break;
		case 3:
			struct Node* temp;
			temp=head;
			if(temp==NULL){
				printf("  No data\n");
				break;
			}
			else{
				while(temp->next!=NULL){
					printf("%d->",temp->num);
					temp=temp->next;
				}
				printf("%d",temp->num);
			}
			printf("\n");
			break;
			
		case 4:
			op=0;
			printf("____E N D____");
			break;
		default:
			printf("ERROR!!!!\n");
	}
}
return 0;

}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
DanSnow
iT邦好手 1 級 ‧ 2020-09-30 23:16:10
最佳解答

你在 Insert 中的這段:

    while (ptr != NULL && ptr->num > num) {
      pre = ptr;
      ptr = ptr->next;
    }

這段離開迴圈的條件是有: ptrNULLptr 中存的數字大於 num,但是你的下一段是:

    if (ptr->num == num) {
      printf(" %d已存在串列中\n", num);
      return 0;
    }

這邊沒有管 ptr 是不是 NULL 就直接存取的裡面的值,當然會出錯

1
海綿寶寶
iT邦大神 1 級 ‧ 2020-09-30 23:36:23

只改兩個地方
修改前為--Before--
修改後為--After--
改後成為如下

#include<stdlib.h>
#include<stdio.h>

//--Before---
// struct Node{
// int num;
// struct Node *next=NULL;
// };
//--After--
typedef struct Node {
int num;
struct Node *next;
} Node;

struct Node *head=NULL;//開頭指標

struct Node* newNode(int num){ //建檔副程式

struct Node* add=(struct Node*)malloc(sizeof(struct Node));
if(add != NULL)
{
add->num = num;
add->next = NULL;
}
return add;

}

int Insert() //照順序插入
{
int num;
printf("Insert a number: ");
scanf("%d",&num);
struct Node* add = newNode(num); //將欲插入值生成節點
struct Node* ptr = head; //利用ptr找出欲插入位置
struct Node* pre = NULL;
if(ptr==NULL){
head=add;
}
else{
while(ptr!=NULL && ptr->num > num)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr->num == num){
printf(" %d已存在串列中\n",num);
return 0;
}
if(ptr==NULL)
{
printf("%d",num);
pre->next=add;
return 0;
}
if(pre==NULL)
{
printf("%d",num);
add->next=ptr;
head=add;
return 0;
}
pre->next=add;
add->next=ptr;
}
}

int Delete() //刪掉所有同樣的資料
{
int num;
printf("Delete a number: ");
scanf(" %d",&num);
struct Node* ptr = head;
struct Node* del = NULL;
if(ptr == NULL) //head為空則刪除失敗
{
printf("Not in the list!\n");
return 0;
}
while(ptr!=NULL && ptr->num==num){
if(ptr->num == num) //head為欲刪除值
{
del=ptr;
head=ptr->next;
ptr=head;
free(del);
}
}
if(ptr!=NULL){
while(ptr->next != NULL) //尋找整個串列
{
if(ptr->next->num == num)
{
del=ptr->next;
ptr->next=del->next;
free(del);
}
else{
ptr = ptr->next;
}
}
if(ptr->next == NULL){
printf("Not in the list!\n");
}
}
}

int main(void){

int i;
int op=5;
struct Node* temp; //After
while(op!=0){
printf(" 操作選項表\n==============\n[1]加入資料\n[2]刪除資料\n[3]列印\n[4]結束\n");
printf("輸入選項:");
scanf("%d",&op);
switch(op){
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
// struct Node* temp; //Before
temp=head;
if(temp==NULL){
printf(" No data\n");
break;
}
else{
while(temp->next!=NULL){
printf("%d->",temp->num);
temp=temp->next;
}
printf("%d",temp->num);
}
printf("\n");
break;

	case 4:
		op=0;
		printf("____E N D____");
		break;
	default:
		printf("ERROR!!!!\n");
}

}
return 0;

}

測試結果如下

$ ./list
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:1
Insert a number: 10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:1
Insert a number: 100
100  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
100->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:1
Insert a number: 200
200  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
200->100->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:2
Delete a number: 100
Not in the list!
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
200->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:1
Insert a number: 260
260  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
260->200->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:1
Insert a number: 400
400  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
400->260->200->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:2
Delete a number: 260
Not in the list!
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:3
400->200->10
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:2
Delete a number: 12
Not in the list!
  操作選項表
==============
[1]加入資料
[2]刪除資料
[3]列印
[4]結束
輸入選項:4
____E N D____

另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like

我要發表回答

立即登入回答