Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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);
	
}
Posted
Updated 10-Jan-16 18:24pm
v2

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:

C++
#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]
Quote:
but i want use char not string, what must I do?


Since you are using C++, you shouldn't use C-like strings, anyway...

C++
#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);
}



Please note:
  • The above is ugly C++ code.
  • You must perform cleanup, that is eventually pair every new call with the corresponding delete.
  • [/update]
 
Share this answer
 
v2
Comments
Dang Duy Hoang 12-Jan-16 22:04pm    
but i want use char not string, what must I do?
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900