Initialiaze values of a Singly Liked List by a constructor

Hi. I have to create a constructor that allows a new linked list to be populated with ten consecutive values, starting at 0. Then I need to print the list! So i want to check if the functions I wrote for those are ok. 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
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
#include <iostream>
#include <string>
#include <stdexcept>

using namespace std;

template <typename E> class SLinkedList;	// forward declaration to be used when declaring SNode

template <typename E>
class SNode {					// singly linked list node
private:
	E elem;					// linked list element value
	SNode<E> *next;				// next item in the list
	friend class SLinkedList<E>;		// provide SLinkedList access
};

template <typename E>
class SLinkedList {				// a singly linked list
public:
	SLinkedList();				// empty list constructor
	SLinkedList(SNode<E>* v);
	~SLinkedList();				// destructor
	bool empty() const;			// is list empty?
	E& front();					// return front element
	void printList(SLinkedList<E> &list);
	void addFront(const E& e);		// add to front of list
	void removeFront();			// remove front item list
	int size() const;					// list size
private:
	SNode<E>* head;				// head of the list
	int     n;							// number of items
};

template <typename E>
SLinkedList<E>::SLinkedList()			// constructor
	: head(NULL), n(0) { }

template <typename E>
SLinkedList<E>::SLinkedList(SNode<E>* v){     //THIS IS WHAT I NEED HELP WITH
	SNode<E>* v = new SNode<E>;
	for (int i = 0; i < 10; i++)
		v->elem = i;
}

template <typename E>
bool SLinkedList<E>::empty() const		// is list empty?
{
	return head == NULL; // can also use return (n == 0);
}



template <typename E>
E& SLinkedList<E>::front() 		// return front element
{
	if (empty()) throw length_error("empty list");
	return head->elem;
}

template <typename E>
SLinkedList<E>::~SLinkedList()			// destructor
{
	while (!empty()) removeFront();
}

template<typename E>
void SLinkedList<E>::printList(SLinkedList<E> &list)     //IS THIS OK????
{
	for (int i = 0; i < list.size(); i++)
	{
		cout << list << " ";
	}
	cout << endl;
}

template <typename E>
void SLinkedList<E>::addFront(const E& e) {	// add to front of list
	SNode<E>* v = new SNode<E>;		// create new node
	v->elem = e;				// store data
	v->next = head;				// head now follows v
	head = v;				// v is now the head
	n++;
}

template <typename E>
void SLinkedList<E>::removeFront() {		// remove front item
	if (empty()) throw length_error("empty list");
	SNode<E>* old = head;			// save current head
	head = old->next;			// skip over old head
	delete old;				// delete the old head
	n--;
}

template <typename E>
int SLinkedList<E>::size() const {				// list size
	return n;
}
I’ve spotted a couple of issues in your singly linked list, but I’m not good at exercises based on pointers, so you’d better wait to see if someone else comes to give further help.

In general, you’d better provide a minimum compilable example of your code if you want other people to help you: in this case the code to see your template working was missing.
Despite appearances, people hate adding trivial code just to perform some test.

Let me also suggest you not to duplicate your question on several forums:
https://codereview.stackexchange.com/questions/176185/initializing-values-of-a-singly-linked-list-with-a-constructor
Again: people won’t spend their time working on your code if they think you could already have received the right answer somewhere else.

What I’ve spotted so far:
- you probably don't want to add a sequence of SNode<T>, because it implies they have to be created before being passed to the function (and there’s no constructor in class SNode, but the default one); I interpreted you wanted to add a sequence of <T>.
- If you add “using namespace std” you’d better avoid duplicate names from the standard namespace (for example empty()).
- Code should be self explanatory: adding trivial comment to any row creates like a ‘background noise’ that makes code harder to read.
- In C++ you should use nullptr instead of NULL (look at your constructor and empty() method).
- You should ask yourself: “Why am I passing an instance of my class to a method of the same class?” (sometimes it’s a good idea, sometimes it isn’t).

Hints:
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
// Hi. I have to create a constructor that allows a new linked list to be 
// populated with ten consecutive values, starting at 0. Then I need to print 
// the list! So i want to check if the functions I wrote for those are ok.
#include <iostream>
#include <string>
#include <stdexcept>

template <typename E> class SLinkedList;    // forward declaration to be used 
                                            // when declaring SNode

template <typename E>
class SNode {                               // singly linked list node
private:
    E elem;                                 // linked list element value
    SNode<E> *next;                         // next item in the list
    friend class SLinkedList<E>;            // provide SLinkedList access
};

template <typename E>
class SLinkedList {                         // a singly linked list
public:
    SLinkedList();                          // empty list constructor
    SLinkedList(E* v, size_t howmany);
    ~SLinkedList();                         // destructor
    bool empty() const;                     // is list empty?
    E& front();                             // return front element
    void printList();
    void addFront(const E& e);              // add to front of list
    void removeFront();                     // remove front item list
    int size() const;                       // list size
private:
    SNode<E>* head;                         // head of the list
    int     n;                              // number of items
};

template <typename E>
SLinkedList<E>::SLinkedList()               // constructor
    : head(NULL), n(0) { }

template <typename E>
SLinkedList<E>::SLinkedList(E* v, size_t howmany) //THIS IS WHAT I NEED HELP WITH
{
    for(size_t i = 0; i < howmany; i++)
        addFront(v[i]);
}

template <typename E>
bool SLinkedList<E>::empty() const          // is list empty?
{
    return head == NULL; // can also use return (n == 0);
}



template <typename E>
E& SLinkedList<E>::front()                  // return front element
{
    if (empty()) { throw std::length_error("empty list"); }
    return head->elem;
}

template <typename E>
SLinkedList<E>::~SLinkedList()              // destructor
{
    while (!empty()) removeFront();
}

template<typename E>
void SLinkedList<E>::printList()            //IS THIS OK????
{
    SNode<E>* tmp = head;
    while(tmp != nullptr)
    {
        std::cout << tmp->elem << ' ';
        tmp = tmp->next;
    }
    std::cout << '\n';
}

template <typename E>
void SLinkedList<E>::addFront(const E& e) { // add to front of list
    SNode<E>* v = new SNode<E>;             // create new node
    v->elem = e;                            // store data
    v->next = head;                         // head now follows v
    head = v;                               // v is now the head
    n++;
}

template <typename E>
void SLinkedList<E>::removeFront() {        // remove front item
    if(empty()) { throw std::length_error("empty list"); }
    SNode<E>* old = head;                   // save current head
    head = old->next;                       // skip over old head
    delete old;                             // delete the old head
    n--;
}

template <typename E>
int SLinkedList<E>::size() const {          // list size
    return n;
}

int main()
{
    SLinkedList<int> sll;
    sll.addFront(1);
    sll.addFront(2);
    sll.addFront(3);
    sll.addFront(4);
    sll.printList();

    int mynodes[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    SLinkedList<int> sll2(mynodes, 10);
    sll2.printList();

    return 0;
    std::cout << "\n Press ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Output:
4 3 2 1
20 19 18 17 16 15 14 13 12 11

Topic archived. No new replies allowed.