我需要获取用户输入字符串并将它们放入用c ++编写的链表中。我已经做到这一点,以便我得到用户输入并将其放入列表的一部分,我的问题是它只将一个保存到列表中。我认为问题是我只在链表中有一个对象并继续覆盖它,据说我已经尝试修复它但无法找到方法。我还必须按照字母顺序组织列表,但一次只能一步。这是我的代码:
#include<iostream>
#include<string>
using namespace std;
struct Node{
string data;
Node *next;
};
void addToList(Node *head);
void deleteFromList(Node *head);
void printList(Node *head);
int main(){
bool quit = false;
int choice;
Node *head = new Node;
head->next = NULL;
while (!quit){
cout << "1. add to list" << endl
<< "2. delete from list" << endl
<< "3. print list" << endl
<< "4. quit" << endl;
cin >> choice;
switch(choice){
case 1: addToList(head);
break;
case 2: deleteFromList(head);
break;
case 3: printList(head);
break;
case 4: quit = true;
break;
default:
cout << "That is not a valid input, quitting program";
quit = true;
}
}
}
void addToList(Node *head){
bool quit = false;
string temp;
Node *current = new Node;
current->next = NULL;
while (!quit){
cout << "Enter a word(quit to stop)";
cin >> temp;
if (temp == "quit"){
quit = true;
}
else{
current->data = temp;
current -> next = current;
}
}
return;
}
void deleteFromList(Node *head){
string deletion;
cout << "Which value do you want to delete from the list? ";
cin >> deletion;
Node *prev = head;
Node *current = head->next;
while (current)
{
if (current->data == deletion){
prev->next = current->next;
delete current;
return;
}
prev = current;
current = current->next;
}
if (!current){
cout << "That value is not in the list" << endl;
}
}
void printList(Node *head){
if (!head)
{
cout << "Nothing is in the list." << endl;
return;
}
Node *current = new Node;
current->next = head;
while (current)
{
cout << current->data << endl;
current = current->next;
}
}
答案 0 :(得分:1)
在函数addToList中,即使用户添加多个单词,也只分配一个节点。当用户未输入“退出”时,您需要在循环中分配新节点。以下代码应该是正确的:
void addToList(Node *head){
bool quit = false;
string temp;
Node *current;
while (!quit){
cout << "Enter a word(quit to stop)";
cin >> temp;
if (temp == "quit"){
quit = true;
}
else{
// Allocate the new node here:
current = new Node;
current->data = temp;
// the new node is inserted after the empty head
// because head is an empty node in your implementation:
current->next = head->next;
head -> next = current;
}
}
return;
}
在功能printList中,您不需要实例化新节点:
void printList(Node *head){
// head->next, because the head is an empty node in your implementation,
// but be carefull, head had to never be NULL in your program,
// so you should also check if head is NULL.
if (!head->next)
{
cout << "Nothing is in the list." << endl;
return;
}
Node *current;
// set current to head->next, because the head is empty in your implementation:
current = head->next;
while (current)
{
cout << current->data << endl;
current = current->next;
}
}
答案 1 :(得分:0)
您也可以尝试此操作
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
void printlinkedlist(node* node)
{
int c=0; //taken just for good looking output
while(node!=NULL)
{
if(c>0)
{
cout<<"->"<<node->data;
node = node->next;
}
else
{
cout<<node->data;
node = node->next;
c++;
}
}
}
int main() {
int n;
cout<<"Enter no. of nodes=";
cin>>n;
int num,c=0; //initialized c for setting head with second node..
node* head = new node; //initialized head node
node * temp = new node; //initialized temp node
cin>>num;
head->data=num;
for(int i=2;i<=n;i++)
{
if(c==0)
{
cin>>num;
temp->data=num;
head->next=temp; //head point to second node i.e. temp
c++;
}
else
{
cin>>num;
node * temp1 = new node; //initialize other temp node for every value
temp1->data=num;
temp->next=temp1; //point to temp1 to temp
temp=temp1; //set temp as temp1
}
}
printlinkedlist(head);
}
答案 2 :(得分:-1)
您应该编辑addToList 替换:
current->next=current;
使用:
head->next=current;
Brcouse你用指针指责当前的电流。 顺便说一下,你的问题是:为什么这段代码不能正确添加数据?希望它有所帮助