Linked List problem ?

Mar 22, 2015 at 12:01am
This code keeps giving me an error after I input the array size.
I am using Windows btw. Something in the code is stopping the run time,
I cannot see what it is. Thanks !
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct other{
	int v;
	struct other *next;
}; 
other *head = NULL; 
other *ptr, *Pptr, *nbox;

void randomset(int B[], int sz)
{
	for (int i = 0; i < sz; i++)
	{B[i] = 0 + rand() % 51;}
}
void createlist(int B[], int sz)
{
	for (int i = 0; i < sz; i++)
	{
		nbox = new other;
		nbox->v = B[i];
		if(head == NULL)
		{
			head = nbox;
			nbox->next = NULL;
		}
		else
		{
			ptr = head;
			Pptr = NULL;
			while(ptr != NULL)
			{Pptr = ptr; ptr = ptr->next;}
			if(Pptr = NULL)
			{head = nbox; Pptr->next = ptr;}
			else
			{Pptr->next = nbox; nbox->next = ptr;}
		}
	}
}
void output()
{
	ptr = head;
	while(ptr != NULL)
	{cout << ptr->v << "  "; ptr = ptr->next;}
	cout << endl;
}
int main()
{
	int size;
	
	cout << "Array Capacity ? "; cin >> size;
	int A[size];
	randomset(A, size);
	createlist(A, size);
	output();
	
	return 0;
}


Mar 22, 2015 at 12:04am
1
2
3
int size;
cout << "Array Capacity ? "; cin >> size;
int A[size];


You cant do this. If you want this to work. You'd have to allocate memory.

1
2
3
int size;
cout << "Array Capacity ? "; cin >> size;
int* A = new int[size];


Edit: Next time, please provide the errors you get. Fixing this code makes the program run fine on my IDE. Also, dont forget to delete the the array once you're done.
Last edited on Mar 22, 2015 at 12:05am
Mar 22, 2015 at 12:06am
Okay Thanks. Can you explain what you just did? please.
Mar 22, 2015 at 12:09am
As far as I know. An array size has to be consistant. For example.

1
2
int x = 5;
int y[x];


wont work. It has to be

1
2
const int x = 5;
int y[x];


So thats why, when you try to fill the array size with user input, the input is not const and wont work.

What I did, was simply allocate memory. Which basically means. As far as I know, the program recognizes how big size is and then makes enough space for it in the memory section, which makes it work. Again, I dont know how it works behind the scenes since Im not an expert. But feel free to google it because there are lots of answers there. Its called the new operator, or just google c++ memory allocation. Also, since you allocated memory,you'd have to delete it, otherwise you will get memory leak. I should mention aswell, if you prefer watching videos, there are lots of videos on youtube explaining this aswell.
Last edited on Mar 22, 2015 at 12:10am
Mar 22, 2015 at 12:26am
Alright. I understand. Thanks much !
Mar 22, 2015 at 12:53am
HELP!! However, it's still giviing me an error. It is located in the create list function. Any ideas?
Mar 22, 2015 at 12:54am
You cant just say its giving me an error. Tell us what the error says, we aint wizards. Its running fine for me. Whats the error?
Mar 22, 2015 at 12:57am
Well. Remember what I said for next time. Your problem is this lol -

if(Pptr = NULL)

Change it to this -

if(Pptr == NULL)
Mar 22, 2015 at 12:58am
lol your right, Windows has stopped working properly is what it says after i enter the array size.
Mar 22, 2015 at 1:00am
WTF. -________-

I was just looking for a syntax error like this....... thanks man.
Topic archived. No new replies allowed.