Lined list implementation

Hi,

AM hoping someone can help me with my code. This is a single linkedlist code.
WHile i start entering my values to build the list, i get a message saying the program has stopped running.. can anyone help me..

Jessi

Code:
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# include <iostream>
using namespace std;

struct Node
{
	int info;
	Node* link;
};

class LinkedList
{
	private:
     	Node *first;
    	Node *last;
    	int count;
	public:
       LinkedList();
       void insertbeg(int newItem);
       void print() ;
       void insertend(int newItem);
       void deleteNode(int deleteItem);
       void buildListForward();
       int front() const;
       ~LinkedList();
       void traverse_and_print();
       void destroyList();
};

LinkedList::LinkedList()
{
       first = last = NULL;
       count = 0;
}

void LinkedList::destroyList()
{
	Node * temp;

			temp = first; //set temp to the current node
			first = first->link; //advance first to the next node
			delete temp; //deallocate the memory occupied by temp

		last = NULL; //initialize last to NULL; first has already been set to NULL by the while loop
		count = 0;


}

LinkedList::~LinkedList()
{
	destroyList();
}

int LinkedList :: front() const
{

return first->info; //return the info of the first node
}//end


void LinkedList::print()
{
	Node * current; //pointer to traverse the list
	current = first; //set current point to the first node
	while (current != NULL) //while more data to print
	{
	cout << current->info << " ";
	current = current->link;
	}
}

void LinkedList::traverse_and_print()
{
    Node *p = first;
    if (first == NULL)
    {
        cout << "The list is empty" << endl;
        return;
    }

    cout << "LinkedList: ";
    /* A basic way of traversing a linked list */
    while (p != NULL)
    { /* while there are some more nodes left ,output the value */
        cout << p->info;
        p = p->link;
    }
    cout << endl;
}
/*
int linkedListType :: length()
{
   return count;
}
*/


void LinkedList :: insertbeg(int newitem)
{

  Node *newNode;
  if (first == NULL)
  {
     newNode->info = newitem;
     newNode->link = NULL;
     first = newNode;
     last = newNode;
  }
}

void LinkedList :: insertend(int newitem)
{
  Node *newNode;
  newNode->info = newitem;
  newNode->link = NULL;
  last->link = newNode;
  last = newNode;
}

void LinkedList :: buildListForward()
{
	int num;
	cout << "Enter a list of integers ending with -999. "<< endl;
	cin >> num;
	first = NULL;
	while (num != -999)
	{
		Node *newNode = NULL;
		newNode->info = num;
		newNode->link = NULL;
		if (first == NULL)
		{
			first = newNode;
			last = newNode;
		}else
		{
			last->link = newNode;
			last = newNode;
		}
		cin >> num;
	}

}



int main()
{

	LinkedList list1;
	list1.buildListForward();
	cout << " Printing the list" << endl;
    list1.print();

    return 0;
}
Last edited on
I thought the Node *newNode = NULL would be an issue, i removed that NULL and ran it again.. still the same issue.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void LinkedList :: buildListForward()
{
	int num;
	cout << "Enter a list of integers ending with -999. "<< endl;
	cin >> num;
	first = NULL;
	while (num != -999)
	{
		Node *newNode;
		newNode->info = num;
		newNode->link = NULL;
		if (first == NULL)
		{
			first = newNode;
			last = newNode;
		}else
		{
			last->link = newNode;
			last = newNode;
		}
		cin >> num;
	}

}
If we have a NULL pointer, is it safe to dereference it?

If we have a pointer pointing to some random place in memory, is it safe to dereference it?

Perhaps we can point the pointer toward some memory we allocate?
Makes sense but when you create a new node like Node *newNode; where should we initially point it to?

Node *newNode; does not create a new node. It creates a pointer to a node. One must point it at a node.

Surely whatever material you're using to learn tells you how to dynamically allocate a node that you can point the pointer to?
Node *newNode = new Node;

Thank you - this worked.
Topic archived. No new replies allowed.