显示链接列表C ++的所有值时重复的值 [英] Duplicate value when show all value of linked list C++

查看:49
本文介绍了显示链接列表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);
	
}

推荐答案

问题是你在每个节点存储 str 作为数据(并且你要覆盖 str 内容)。

你可以解决这样的问题传递的str内容的副本,或者更好的是,使用std :: string对象:



The problem is you are storing in every node the address of str as data (and you are overwriting the str content).
You may solve such a problem making a copy of the passed str content or, better, using std::string objects:

#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]




    Please note:

    • The above is ugly C++ code.
    • You must perform cleanup, that is eventually pair every new call with the corresponding delete.
    • [/update]


      我认为现在是时候停止猜测你的代码在做什么了。是时候看到你的代码正在执行并确保它能达到预期的效果。



      调试器是你的朋友。它会告诉你你的代码到底在做什么。

      一步一步地执行,检查变量,你会发现它有一个停止做你期望的点。

      掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

      http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

      https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



      注意保存列表根的变量。
      I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

      The debugger is your friend. It will show you what your code is really doing.
      Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
      Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
      http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
      https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

      pay attention to the variable that hold the root of the list.


      这篇关于显示链接列表C ++的所有值时重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆