Construction of Circular linked list help;
Oct 9, 2012 at 3:48pm UTC
I am trying to construct a circular linked list of integers but I am not able to compile the program because of a segmentation fault in my constructor method.
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
7 class princelist
8 {
9 private :
10 struct ListNode
11 {
12 int item;
13 ListNode *next;
14 };
15 ListNode *_head;
16 int _size;
25 public :
26 princelist(int n)
27 {
28 _size = n;
29 //head = NULL;
30
31 ListNode *newptr = _head;
32
33 for (int i = 1; i <= n; i++)
34 {
35 newptr = new ListNode;
36 newptr->item = i;
37 newptr = newptr->next;
38
39 cout << "yes" << endl;
40 cout << _head->item << endl;
41 }
42 cout << "out for loop" << endl;
43 // newptr = _head (commented out because this statement gives me a compilation error also)
43 }
44 };
46 int main(){
47 princelist test(3);
48 return 0;
49 }
Ouput
yes
0
yes
0
yes
0
out of for loop
Aborted(core dumped)
Anyone have any idea why my head->item is 0 instead of the first value of i which is 1 and any idea why there is a aborted(core dumped) error?
Oct 9, 2012 at 4:09pm UTC
1 2 3 4 5 6 7 8 9 10 11
ListNode *newptr = _head;
for (int i = 1; i <= n; i++)
{
newptr = new ListNode;
newptr->item = i;
newptr = newptr->next;
cout << "yes" << endl;
cout << _head->item << endl;
}
You never modify `head'.
> I am not able to
compile the program because of a
segmentation fault
A segmentation fault is a
runtime error, so your program did compile.
Oct 9, 2012 at 5:00pm UTC
lol hahaha i see the issue thanks alot man!
Topic archived. No new replies allowed.