c++ - 问题是关于 c++ 中的单个列表及其操作、创建和显示

标签 c++ linked-list singly-linked-list

<分区>

我已经尝试运行代码很长时间了,但它似乎没有按照我想要的方式运行。有一些我无法调试的逻辑错误。 下面的代码在创建链接列表时运行良好,但它不会再次要求我使用 do while 循环所做的选择。它只是突然结束执行。请帮我解决这个问题。

 #include<iostream>
using namespace std;

typedef struct sll{
    char name[20];
    int rollno;
    struct sll *next;
}node;


node *create(node *first){
    node *a,*newnode;
    newnode=new sll;
    cout<<"--------------------------\n";
    cout<<"Enter Student Information\n";
    cout<<"Name- \t";
    cin>>newnode->name;
    cout<<"Roll No- ";
    cin>>newnode->rollno;
    cout<<"--------------------------\n";
    a->next=NULL;
    if(first==NULL)
        first=newnode;
    else{
        a=first;
        while(a->next!=NULL)
            a=a->next;
        a->next=newnode;
    }
    return newnode;
}
node *display(node *first){
    node *temp;
    if(first==NULL)
        cout<<"Empty List\n";
    else{
        temp=first;
        while(temp!=NULL){
            cout<<"Student Information\n";
            cout<<"--------------------------\n";
            cout<<"Name- ";
            cout<<temp->name;
            cout<<"\n";
            cout<<"Roll No- ";
            cout<<temp->rollno;
            cout<<"\n";
            cout<<"--------------------------\n";
            temp=temp->next;
        }   
    }
}
int main(){
    node *first=NULL;
    char n;
    int ch;
    do{
    cout<<"--------------------------\n";
    cout<<"Enter your choice\n";
    cout<<"1>Create LL\n2>Display\n";
    cout<<"--------------------------\n";
    cin>>ch;
    switch(ch){
        case 1: first=create(first);
                break;
        case 2: display(first);
                break;
//      case 3: insert_pos();
//              break;
//      case 4: display();
//              break;
//      case 5: create();
//              break;
        default: cout<<"Wrong Choice\n";
    }
    cout<<"Do you wish to continue? Y/N \n";
    cin>>n;
    }
    while(n!='n');
    return 0;
}

最佳答案

这应该可以解决您代码中的大部分问题

  • 您正在分配内存但没有删除它。使用智能指针
  • 您的函数有返回类型但不返回。
  • 不要使用字符串。使用字符串
  • 不要取消引用未初始化的指针。
  • 不要使用NULL。使用 nullptr 或完全删除它。
#include <iostream>
#include <memory>
#include <string>

struct node {
    std::string name;
    int rollno;
    std::unique_ptr<node> next;
};

void create(std::unique_ptr<node> &first)
{
    std::unique_ptr<node> newnode = std::make_unique<node>();
    std::cout << "--------------------------\n";
    std::cout << "Enter Student Information\n";
    std::cout << "Name- \t";
    std::cin >> newnode->name;
    std::cout << "Roll No- ";
    std::cin >> newnode->rollno;
    std::cout << "--------------------------\n";
    if (!first)
        first = std::move(newnode);
    else {
        auto a = first.get();
        while (a->next)
            a = a->next.get();
        a->next = std::move(newnode);
    }
}

void display(const std::unique_ptr<node> &first)
{
    if (!first)
        std::cout << "Empty List\n";
    else {
        auto temp = first.get();
        while (temp) {
            std::cout << "Student Information\n";
            std::cout << "--------------------------\n";
            std::cout << "Name- ";
            std::cout << temp->name;
            std::cout << "\n";
            std::cout << "Roll No- ";
            std::cout << temp->rollno;
            std::cout << "\n";
            std::cout << "--------------------------\n";
            temp = temp->next.get();
        }
    }
}

int main()
{
    std::unique_ptr<node> first;
    char n;
    int ch;
    do {
        std::cout << "--------------------------\n";
        std::cout << "Enter your choice\n";
        std::cout << "1>Create LL\n2>Display\n";
        std::cout << "--------------------------\n";
        std::cin >> ch;
        switch (ch) {
        case 1:
            create(first);
            break;
        case 2:
            display(first);
            break;
        default:
            std::cout << "Wrong Choice\n";
        }
        std::cout << "Do you wish to continue? Y/N \n";
        std::cin >> n;
    } while (n != 'n');
    return 0;
}

关于c++ - 问题是关于 c++ 中的单个列表及其操作、创建和显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58489016/

相关文章:

c++ - std::vector push_back 上的段错误/"Vector is not dereferencable"

c# - 在 C# 中调用 C++ dll 时找不到入口点

无法防止链表打印功能崩溃

java - 将多个信息存储到单链表中的一个节点中

c - 测试单链表性能时出现AddressSanitizer错误

c++ - -迂腐警告和 pthread_create

c++ - 我遇到了复制构造函数错误,但不知道我对这个错误的想法是否正确

javascript - 根据值从链表中删除节点

java - list(LinkedList).head 和 Node head 之间的区别?

java - java将两个链表合并成新的链表