Wrong Type of Output from Linked List

Write your question here.
Why did my output print an address instead of an integer? Note that I did not include the code from my header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
*	Chapter 15, Problem 1
*
*	Write a program to remove an element from a linked list; the remove
*	function should take just the element to be removed.  Is this function
*	easy to write and will it be fast?  Could this be made easier or faster
*	by adding an additional pointer to int?
*
*	Problem 2
*
*	Write a program that adds elements to a linked list in sorted order, rather
*	than at the begining.
*
*	Problem 3
*
*	Write a program to find an element in a linked list by name.
*
*/

#include <iostream>
#include "c15p1.h"

using namespace std;


//	Function Prototypes
void addHead(LinkedList *list, int* data);
void initializeList(LinkedList* list);
void displayLinkedList(LinkedList *list);




int main()
{
	//	Declare and initialize variables
	int x = 0;
	int choice = 0;
	LinkedList linkedList;
	initializeList(&linkedList);

	while (true)
	{
		// Print Menu
		cout << "Menu:" << endl <<
			'\t' << "0. Exit" << endl <<
			'\t' << "1. Add a node " << endl <<
			'\t' << "2. Delete a node " << endl <<
			'\t' << "3. Display node(s) " << endl << endl;

		cout << "Select a number from the Menu: ";
		cin >> choice;

		switch (choice)
		{
		case 0:
			return 0;
		case 1:
			cout << "Please enter an integer: ";
			cin >> x;
			addHead(&linkedList, &x);
			break;
		case 2:
			break;
		case 3:
			displayLinkedList(&linkedList);
			break;
		}
	}
}


void addHead(LinkedList* list, int* data)
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->x_node = data;
	if (list->head == NULL)
	{
		list->tail = node;;
		node->next = NULL;;
	}
	else
	{
		node->next = list->head;
	}
	list->head = node;
}


void initializeList(LinkedList* list)
{
	list->head = NULL;
	list->tail = NULL;
	list->current = NULL;
}



void displayNode(Node* node)
{
	cout << node->x_node << endl;
}



void displayLinkedList(LinkedList* list)
{
	cout << endl << "Linked List \n";
	Node* current = list->head;
	while (current != NULL)
	{
		cout << current->x_node << endl;
		current = current->next;
	}
}




Menu:
        0. Exit
        1. Add a node
        2. Delete a node
        3. Display node(s)

Select a number from the Menu: 1
Please enter an integer: 5
Menu:
        0. Exit
        1. Add a node
        2. Delete a node
        3. Display node(s)

Select a number from the Menu: 3

Linked List
0036FE60
Menu:
        0. Exit
        1. Add a node
        2. Delete a node
        3. Display node(s)

Select a number from the Menu:
Because you are storing the address
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void addHead(LinkedList* list, int* data)
{
	Node* node = (Node*)malloc(sizeof(Node));
	node->x_node = data;//<--- here, make data an int or int& or set like node->x_node = *data
	if (list->head == NULL)
	{
		list->tail = node;;
		node->next = NULL;;
	}
	else
	{
		node->next = list->head;
	}
	list->head = node;
}
Last edited on
Thank you. I don't have a good grasp of pointers yet.

The fix was as follows:

1. change "int* x;" to "int x;" inside the struct. I now know how egregious this error was.

2. change "data" to "*data" like you said.

I'm on to code the rest of my program.
Topic archived. No new replies allowed.