LinkedList problem

Hi, I am very new to C++ (C# is my normal language), I am having some problems with a LinkedList. Could someone please tell me where I am going wrong. I would really appreciate any help anyone can provide

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
#include "stdafx.h"
#include <iostream>

class LinkedList{
private:	
	struct node {
		int data;
		node *next;
	};
	node *start;
	
public:
	//LinkedList();
	void addToEnd(node *& start, int num);
	void print();
	int count();
	//~LinkedList();
};

void LinkedList::addToEnd(node *& start, int num){
	node *q, *t; //temporary nodes

	//if the list is empty
	if(start == NULL){
		start = new node;
		start->data = num;
		start->next = NULL;
	} else {
		q = start;	
		while(q->next != NULL){ 
			q = q->next; //go to the next node
		}	
		t=new node;
		t->data=num;
		t->next=NULL;
		q->next=t;
	}
}

void LinkedList::print(){
	using namespace std;
	node *q; //temporary node
	cout<<endl;
	cout<<endl;
	for(q = start; q != NULL; q = q->next)
		cout<<" "<<q->data;
}

	int _tmain(int argc, _TCHAR* argv[]){
	using namespace std;
	LinkedList testList;
	cout<<endl;
	testList.addToEnd(testList, 233515);
	testList.addToEnd(testList, 444425);
	testList.addToEnd(testList, 333999);
	testList.addToEnd(testList, 344556);
	testList.addToEnd(testList, 678686);
	testList.print();	
	return 0;
}

Last edited on
What exactly is the problem you're seeing?

On a quick look through I doubt it will compile. On lines 53 to 57 you are calling addToEnd() with the first parameter of a instance to LinkedList but you haven't got an addToEnd() method that will accept that as the first parameter, it's expecting a node ......something. I think you have you're syntax mixed up here. You either want a node * start or a node & start the first is a pointer the second is a reference. They are both effectively pointers but the syntax of how you use them is different. In your case I would probably use the pointer as it ties in with the syntax you are using - reference uses the dot type notation rather than the ->.
If you want to pass a LinkedList then either create an overridden addToEnd() or change the parameters of the one you have.

The mechanics of your linked list code seems fine, you may want to create a constructor to initialise you start member to NULL though, and a destructor to clear the list..
closed account (z05DSL3A)
The first argument for addToEnd is not needed as far as I can see, and as bnbertha said you need to initialise start to NULL


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
#include "stdafx.h"
#include <iostream>

class LinkedList
{
private:	
    struct node 
    {
        int data;
        node *next;
     };

    node *start;
	
public:
    LinkedList();
    void addToEnd(/*node *& start,*/ int num);
    void print();
    int count();
    //~LinkedList();
};

LinkedList::LinkedList()
:start(NULL)
{
}

void LinkedList::addToEnd(/*node *& start,*/ int num)
{
    node *q, *t; //temporary nodes

    //if the list is empty
    if(start == NULL)
    {
        start = new node;
        start->data = num;
        start->next = NULL;
    } 
    else 
    {
        q = start;	
        while(q->next != NULL)
        { 
            q = q->next; //go to the next node
        }
	
        t=new node;
        t->data=num;
        t->next=NULL;
        q->next=t;
    }
}

void LinkedList::print()
{
    using namespace std;
    node *q; //temporary node
    cout<<endl;
    cout<<endl;
    for(q = start; q != NULL; q = q->next)
    {
        cout<<" "<<q->data;
    }
}

	
int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std;

    LinkedList testList;
    cout<<endl;

    testList.addToEnd(/*testList,*/ 233515);
    testList.addToEnd(/*testList,*/ 444425);
    testList.addToEnd(/*testList,*/ 333999);
    testList.addToEnd(/*testList,*/ 344556);
    testList.addToEnd(/*testList,*/ 678686);

    testList.print();	

    return 0;
}
Last edited on
..... or just take that parameter out. Why didn't I think of that? :)
I'm just going to point out. If you look up the C++ STL (Standard Template Library) It has linked-lists etc already developed for you. Just incase your not aware.
Topic archived. No new replies allowed.