本文介绍了显示链接列表C ++的所有值时重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我输入3次,有3个值差异。显示链表的所有值时,它与最后一个值重复。有人帮我解决这个问题。谢谢!
输入:1 2 3(3个值)
输出:3 3 3
I enter 3 times with 3 value difference. When show all value of linked list, it was duplicated with last value. Anybody help me about this problem. Thanks!
Input: 1 2 3 ( 3 values )
Output: 3 3 3
#include <iostream>
using namespace std;
struct Node{
Node *next;
char *data;
};
void initNode(struct Node *head, char *n){
head->next = NULL;
head->data = n;
}
void addNode(struct Node *head, char *n){
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
Node *cur = head;
while(cur){
if(cur->next == NULL){
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void display(struct Node *head){
Node *list = head;
while(list != NULL){
cout << list->data <<endl;
list = list->next;
}
}
int main(){
char str[30];
cout << "Enter 1st: ";
cin.getline(str, 30);
Node *head = new Node;
initNode(head, str);
cout << "Enter 2nd: ";
cin.getline(str, 30);
addNode(head, str);
cout << "Enter 3th: ";
cin.getline(str, 30);
addNode(head, str);
cout << endl << "Show all value of linked list: " << endl;
display(head);
}
推荐答案
#include <iostream>
using namespace std;
struct Node{
Node *next;
string data;
};
void initNode(struct Node *head, const string & n){
head->next = NULL;
head->data = n;
}
void addNode(struct Node *head, const string & n){
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
Node *cur = head;
while(cur){
if(cur->next == NULL){
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void display(struct Node *head){
Node *list = head;
while(list != NULL){
cout << list->data <<endl;
list = list->next;
}
}
int main(){
string str;
cout << "Enter 1st: ";
cin >> str;
Node *head = new Node;
initNode(head, str);
cout << "Enter 2nd: ";
cin >> str;
addNode(head, str);
cout << "Enter 3th: ";
cin >> str;
addNode(head, str);
cout << endl << "Show all value of linked list: " << endl;
display(head);
}
[update]
[update]
引用:
但是我想用char而不是字符串,我该怎么办?
but i want use char not string, what must I do?
由于您使用 C ++
,因此不应使用 C
-like字符串,无论如何...
Since you are using C++
, you shouldn't use C
-like strings, anyway...
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
Node *next;
char *data;
};
void initNode(struct Node *head, const char * data){
// TODO: check passed 'data' pointer
head->next = NULL;
head->data = new char[strlen(data)+1];
strcpy(head->data, data);
}
void addNode(struct Node *head, const char * data){
// TODO: check passed 'data' pointer
Node *newNode = new Node;
newNode->data = new char[strlen(data)+1];
strcpy(newNode->data, data);
newNode->next = NULL;
Node *cur = head;
while(cur){
if(cur->next == NULL){
cur->next = newNode;
return;
}
cur = cur->next;
}
}
void display(struct Node *head){
Node *list = head;
while(list != NULL){
cout << list->data <<endl;
list = list->next;
}
in main()
{
char str[30];
cout << "Enter 1st: ";
cin.getline(str, 30);
Node *head = new Node;
initNode(head, str);
cout << "Enter 2nd: ";
cin.getline(str, 30);
addNode(head, str);
cout << "Enter 3th: ";
cin.getline(str, 30);
addNode(head, str);
cout << endl << "Show all value of linked list: " << endl;
display(head);
}
请注意:
- 上面是丑陋的C ++代码。
- 你必须执行清理,最终每次
new
使用相应的删除
进行调用。 [/ update] - The above is ugly C++ code.
- You must perform cleanup, that is eventually pair every
new
call with the correspondingdelete
. [/update]
Please note:
这篇关于显示链接列表C ++的所有值时重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!