问题是关于 c++ 中的单个列表及其操作、创建和显示
The question is regarding singly list in c++ and its operations, create and display
我长期以来一直在尝试 运行 代码,但它似乎 运行 不是我想要的方式。有一些我无法调试的逻辑错误。
下面的代码 运行 可以很好地创建一个链表,但它不会再次要求我使用 do 所做的选择,而 loop.It 只是突然结束执行。请帮我解决这个问题。
#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;
}
这应该可以解决您代码中的大部分问题
- 您正在分配内存但没有删除它。使用智能指针
- 您的函数具有 return 类型但没有 return。
- 不要使用字符串。使用字符串
- 不要取消引用未初始化的指针。
- 不要使用
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;
}
我长期以来一直在尝试 运行 代码,但它似乎 运行 不是我想要的方式。有一些我无法调试的逻辑错误。 下面的代码 运行 可以很好地创建一个链表,但它不会再次要求我使用 do 所做的选择,而 loop.It 只是突然结束执行。请帮我解决这个问题。
#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;
}
这应该可以解决您代码中的大部分问题
- 您正在分配内存但没有删除它。使用智能指针
- 您的函数具有 return 类型但没有 return。
- 不要使用字符串。使用字符串
- 不要取消引用未初始化的指针。
- 不要使用
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;
}