Help Linked List Search

I am getting an Unhandled exception at 0x00CB569E in AusitnHorton_Chapter17_7.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

and


It puts a little yellow arrow at this line:

cout << nodepointer->value << " "; //print current node

when I try to run this program.

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
116
117
118
//Program:		Make a ListNode and simple linked list class. Test the linked list class by adding varous numbers to the list and then test for membership.\
				//then include a search function that returns the position of x on the list, if not found return -1. 

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cctype>
using namespace std;

class LinkedList
{
protected:
	struct ListNode
	{
		double value;
		ListNode *next;

		ListNode(double value1, ListNode *next1)		//constructor
		{
			value = value1;
			next = next1;
		}
	};

	ListNode *head;

public:
	void add(double x);
	bool isMember(double x);
	int search(double x);
	void displayList();
};

int main()
{
	LinkedList list;

	double x;		// user input to search
	
	// adding numbers to list
	list.add(1.2);
	list.add(5.5);
	list.add(6.5);
	list.add(9.9);
	list.add(2);
	list.add(5);
	list.add(7);
	list.add(24);

	cout << "Enter a number to search if it is in the list: ";
	cin >> x;
	cout << "\n\nSearching...\n\n";

	// search for the number in the list
	int i = list.search(x);
	if (i != -1) { cout << "Number is found at position " << i << endl; }
	else if (i == -1) { cout << "Number was not found" << endl; }

	// display list to user
	cout << "Only these numbers are in the list: ";
	list.displayList();

	cout << endl << endl;
	system("pause");
}

// This fnc puts a new node at the beginning of the list
void LinkedList::add(double x)
{
	head = new ListNode(x, head);
}

bool LinkedList::isMember(double x)
{
	ListNode *nodepointer = head;	// start at the head of the list
	if (nodepointer->value != NULL)
	{
		if (nodepointer->value == x)
		{
			return true;
		}
		nodepointer = nodepointer->next;
	}
	return false;
}

int LinkedList::search(double x)
{
	ListNode *nodepointer = head;
	int i = 1;		// initialized value for true

	if (nodepointer->value != NULL)
	{
		if (nodepointer->value == x)
		{
			return i;
		}
		nodepointer = nodepointer->next;	// move to the next node

		i++;
	}

	return -1;		// if x is not found
}

void LinkedList::displayList()
{
	ListNode *nodepointer = head;	// start at the head of the list
	while (nodepointer)
	{
		cout << nodepointer->value << "  ";		//print current node
		nodepointer = nodepointer->next;		//moves to the next node
	}
}

Last edited on
> if (nodepointer->value != NULL)
gcc wrote:
warning: NULL used in arithmetic
¿what's that condition supposed to mean?
Also, ¿why do you only check the first cell?
Last edited on
I'm not sure. I've tried a few different things but nothing seems to be working.

I figured it out.

Thanks.
Last edited on
Okay, a few things:
1) You should define a default constructor for your class that initializes head to a null pointer:
30
31
32
33
public:
    LinkedList(): head(nullptr) {} // Or use 0 or NULL if you don't have a C++11 compiler
    void add(double x);
    // ... 
Right now, head seems to be getting initialized to some garbage value, which would explain the crash you're getting in displayList (because the last node points to a garbage value, not null).

2) Line 95 should just be while (nodepointer).
That way, you'll loop through all of the elements (or at least until you find the element you're looking for).

3) Can't you just implement isMember as
76
77
78
79
bool LinkedList::isMember(double x)
{
    return search(x) != -1;
}
?
Topic archived. No new replies allowed.