我的代碼停在中間並且沒有錯誤 c++

艾哈邁德·希沙姆

案例 1 工作正常,但案例 2 和 3 只是停止循環和程序,之後我無法選擇任何其他案例!我想知道為什麼它會阻止循環進行,因為這裡的選擇永遠不會等於 0,我相信這是阻止循環前進的唯一原因!提前致謝。

我還檢查了功能,所有功能對我來說都很好,我不確定問題是否出在它們身上

#include "stdafx.h"
#include <iostream>
using namespace std;
void insert_node(int new_data);
void print_node();
void delete_node();
int new_data;
char choice;
struct node {
    int data;
    node* next;
};

node* head = NULL;

void main()
{
    do
    {

        cout << "Enter 1 to insert a new node \n";
        cout << "Enter 2 to disply the list \n";
        cout << "Enter 3 to delete the last node \n";
        cin >> choice;
        switch (choice)
        {
        case '1':
            cout << "Enter the data \n";
            cin >> new_data;
            insert_node(new_data);
            break;
        case '2':
            if (head != NULL)
                print_node();
            else
                cout << "SORRY, your list is empty \n";
            break;
        case '3':
            delete_node();
            break;

        default:
            cout << "Invalid entry \n";
        }

    } while (choice != '0');
}

void insert_node(int new_data)
{
    node* NewNode = new node;
    NewNode->data = new_data;
    if (head == NULL)
        head = NewNode;
    else
    {
        NewNode->next = head;
        head = NewNode;
    }
}
void print_node()
{
    node* printer=head;
    do
    {
        cout << printer->data<<" - ";
        printer = printer->next;
    } while (printer != NULL);
}
void delete_node()
{
    if (head == NULL)
        cout << "no node to be deleted \n";
    else
    {

        node* curr = head;
        node* prev = NULL;
        while (curr->next != NULL)
        {
            prev = curr;
            curr = curr->next;
        }
        if (prev == NULL)
        {
            delete(curr);
            head = NULL;
            return;
        }
        prev->next = NULL;
        delete(curr);
    }
}
約翰

這是被竊聽的

void insert_node(int new_data)
{
    node* NewNode = new node;
    NewNode->data = new_data;
    if (head == NULL)
        head = NewNode;
    else
    {
        NewNode->next = head;
        head = NewNode;
    }
}

它應該是

void insert_node(int new_data)
{
    node* NewNode = new node;
    NewNode->data = new_data;
    NewNode->next = head;
    head = NewNode;
}

next將第一個節點添加到列表時,您的版本設置失敗正如您所看到的,沒有必要進行head == NULL特殊處理。

因為您一開始就沒有正確創建列表,所以任何從列表中打印或刪除項目的嘗試都可能會失敗。

並且您的print_node函數將在空列表中失敗。它應該是一個 while 循環而不是一個 do ... while 循環。

void print_node()
{
    node* printer=head;
    while (printer != NULL)
    {
        cout << printer->data<<" - ";
        printer = printer->next;
    }
}

你的delete_node功能在我看來也很可疑。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在我的 C++ Windows 服務代碼中出現 C26xxx 錯誤

C ++中簡單代碼中的奇怪語法錯誤

C++:錯誤:全局命名空間中沒有名為“signbit”的成員

有沒有辦法從 c# 中的代碼單擊事件

為什麼此代碼中沒有 server.emit 並且我在節點 js 中無法理解此代碼?

即使代碼沒有錯誤,代碼塊也無法運行代碼

在 C++ 錯誤中沒有匹配的轉換函數

C++ 中的狀態模式錯誤(沒有合適的構造函數)

我無法向 GitHub 提交文件,並且沒有錯誤消息

有沒有辦法在clang中檢測C++代碼中的編譯器-fxxxx標誌?

第 2 次運行代碼後的 C 雙免費錯誤

C++ 2D 算法代碼讀取數組錯誤

Power Bi 錯誤代碼:28- 設備上沒有剩餘空間

UseMethod("select") 中的錯誤:沒有適用於 'select' 的方法應用於類 "c('integer', 'numeric')"

為什麼我的代碼給出錯誤:“sqlite3.OperationalError:沒有這樣的列:”?

與 C 程序一起執行的 Perl 代碼將錯誤的字節長度寫入 Windows 中的內存

如何暫停直到發出信號並且沒有 C 打印?

如何解決使用 C 代碼更改主函數輸入時的分段錯誤?

C#沒有裝箱轉換CS0315錯誤

帶有for循環的C ++中的分段錯誤

當我點擊 E 並且光線投射沒有擊中對象時,我收到此錯誤消息:NullReferenceException:未將對象引用設置為對象的實例

這段代碼得到一個錯誤,這些變量沒有被分配到,我不知道為什麼

有人能發現我代碼中的錯誤嗎?我不知道我錯過了什麼部分,我搞砸了什麼

C fscanf 給出分段錯誤。說缺少文件(我的文件名為 lab4.dat 並且位於同一位置)

為什麼我的並行代碼比沒有並行更慢

有人可以幫我指出我的代碼輸出錯誤的地方嗎?

C++ 中的向量和計算 - 並行代碼比串行慢

我的代碼並沒有完全按照我想要的方式工作。[解決了]

使用帶有 VBA excel 的 Internet Explorer 獲取 HTML 鏈接(沒有代碼錯誤但結果錯誤)