Constructing a linked list using pointers

Hello. I have been working on a homework assignment for a couple of days now; have looked here and at a couple of other sites, but am still stuck. Below is my code to date. Could someone please point me in the right direction? What am I missing? Thanks in advance for any advice.

The question: Read in a sequence of numbers and build a linked list using the pointer type.

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

struct node {
	int data;
	node *next;}

void main()
{

node *q;
head = new node;
head->data = 0;
head->next = null;
node *p;
p = head;
cin >> i;
while(i != -1)
{
	q = new node;
	q->data = i;
	head->data++;
	q->next = null;
	p->next = q;
	p = q;
	cin >> i;
}

p = head;
	while(p != null)
	{
	cout << p->data;
	p = p->next;
	}

return;

}
*/
main has to return int, 'head' is never declared nor is 'i', null (lowercase) shouldn't work
Thanks for the pointers (no pun intended)! I'm still getting a bit of odd output, it's not only printing the sequence, but also the next number in the chain. Example: if the input is
12 2 11 56 23, the output is 6 0 12 2 11 56 23. Interesting...
Topic archived. No new replies allowed.