Create Lists(Nodes)

I want to input nodes and it's on void create() but it won't output on void display()
It gives a message like this:
Unhandled exception at 0x00EC966B in Project12.exe: 0xC0000005: Access violation reading location 0x00000000.
Break continue

How to fix this? Thank you!

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
  #include<iostream>
using namespace std;

void create();
void addBegin(struct Node**);
//void specific();
//void delBegin();
//void delEnd();
//void delSpecific();
//void reverse();
//void sort();
void display();

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

struct Node* initNode(int data){
	struct Node *head = (struct Node*)malloc(sizeof(struct Node));
	head->info = data;
	head->next = NULL;
	return head;
}

struct Node *head;
int input, choice;
char ch;

int main()
{
	int choice;
	cout << "1. Create List \n\n";
	cout << "2. Add at beginning \n\n";
	cout << "3. Add at a specific node \n\n";
	cout << "4. Delete at the beginning \n\n";
	cout << "5. Delete at the end \n\n";
	cout << "6. Delete at a specific node \n\n";
	cout << "7. Reverese \n\n";
	cout << "8. Sort \n\n";
	cout << "9. Display \n\n";
	cout << endl;
	cout << "Enter your choice: ";
	cin >> choice;
	switch (choice)
	{
	case 1:
		create();
		break;
	case 2:
		addBegin(&head);
		break;
	/*case 3:
		specific();*/
	/*	case 4:
		case 5:
		case 6:
		case 7:
		case 8:*/
		case 9:
		display();
	}
	system("pause>0");
	return 0;
}

void create()
{
	Node *p;
	cout << "How many nodes do you want to input? ";
	cin >> input;
	cout << "Insert " << input << " nodes: ";
	for (int i = 0; i < input; i++){
		p = new Node;
		cin >> p->info;
		p->next = NULL;
	}
	cout << "Go back to main menu? [Y/N]";
	cin >> ch;
	if (ch == 'Y' || ch == 'y'){
		system("cls");
		main();
	}
	if (ch == 'N' || ch == 'n')
		create();
}

void addBegin(struct Node **head){
	int data;
	cout << "Insert a node on the beginning: ";
	cin >> data;
	struct Node *NewNode = initNode(data);
	NewNode->next = *head;
	*head = NewNode;
	
	cout << "Go back to main menu? [Y/N]";
	cin >> ch;
	if (ch == 'Y' || ch == 'y'){
		system("cls");
		main();
	}
	//if (ch == 'N' || ch == 'n')
	//	addBegin(&head);
}

void display()
{
	struct Node* cur = head;
	cout << "\n\nList is : " << endl;

	do {
		cout << cur->info << endl;
	} while ((cur = cur->next) != NULL);
	cout << "Go back to main menu? [Y/N]";
	cin >> ch;
	if (ch == 'Y' || ch == 'y'){
		system("cls");
		main();
	}
	if (ch == 'N' || ch == 'n')
		display();
}

//void specific()
//{
//	system("cls");
//	int node;
//	cout << "What number of node: ";
//	cin >> node;
//	cout << "What data? ";
//
//
//} 
The loop at line 73 creates a node but doesn't actually add it to the list.

This would be better C++ if you made some changes.
- remove initNode() and instead make a constructor for Node:
1
2
3
4
struct Node {
    Node(int i) : info(i), next(nullptr) {}
     // rest of your code
};


Also, if you make a separate class for the list itself then many of the functions become members of the class:

1
2
3
4
5
6
7
8
9
10
11
class List {
private:
    Node *head;
public:
    List() : head(nullptr) {}
    ~List();

    addBegin();
    create();
    display();
};


Why do it this way? For one, it will remove some inconsistency: addBegin() currently takes a pointer to head, but create() uses the global variable called head. Also it means you can have multiple lists.
Last edited on
Where should i put this code?
1
2
3
4
struct Node {
    Node(int i) : info(i), next(nullptr) {}
     // rest of your code
};
closed account (SECMoG1T)
here ...
1
2
3
4
5
6
struct Node 
{
        Node(int i) : info(i), next(nullptr) {}
	int info;
        Node *next;
};
Topic archived. No new replies allowed.