Printing Element in a Linked List

My code is pretty straight-forward so I'm not sure why it isn't working, but basically my printList function locks up at soon as it calls my get functions. What wrong with them?


Test 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
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include "LinkedList.h"
using namespace std;

template<typename T>
void printList(LinkedList<T> &list)
{
  for (int i = 0; i < list.getSize(); i++)
  {
    cout << list.get(i) << " ";
  }
  cout << endl;
}

int main(int argc, char *argv[])
{
    // Create a list for strings
    LinkedList<string> list;

    // Add elements to the list
    list.add("America");
    cout << "(1) ";
    printList(list);

    list.add("Canada");
    cout << "(2) ";
    printList(list);

    list.add("Russia");
    cout << "(3) ";
    printList(list);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


LinkedList Class function definitions in use:
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
#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__
#include <cstdlib>
#include <stdexcept>

template<typename T>
class Node{
      public:
             T element;      // Element contained in the node
             Node *next;     // Pointer to the next node
             Node()          // No-argconstructor
             {
              next = NULL;
             }
             Node(T element) // Constructor
             {
              this->element = element;
              next = NULL;
             }
};

template<typename T>
class LinkedList{
      private:
             Node<T>* head;
             Node<T>* tail;
             int size;
      public:
             LinkedList(){       //constructor
                  size = 0;
             }      
             void add(T element);            //basic add node function
             T getFirst();                   //basic get functions
             T getLast();                    //      ""
             T get(int index);               //      ""
             int getSize();                  //gets size
};

template<typename T>              //adds new node to list
void LinkedList<T>::add(T element){
     if(size == 0){
           head = new Node<T>(element);
           tail = head;
           size++;
     }else{
           tail->next = new Node<T>(element);
           tail = tail->next;
           size++;
     }
}

template<typename T>                //returns head
T LinkedList<T>::getFirst(){
      return head->element;
}

template<typename T>                //returns tail
T LinkedList<T>::getLast(){
      return tail->element;
}

template<typename T>                //returns element at index
T LinkedList<T>::get(int index){
      if(index < 0 || index >= size){
            //error   
      }else if(index == 0){
            getFirst();
      }else if(index == size - 1){
            getLast();
      }else{
            Node<T> *current = head;
            for(int i = 0; i < index; i++){
                  current = current->next;
            }
            return current->element;
      }      
}

template<typename T>                //returns size of list
int LinkedList<T>::getSize(){
    return size;
}

Your get() function fails to return any value if index = 0 or size-1. This may be causing the failure since index = 0 is the 1st case handled in your printList() function.
You can fix it easily. Instead of
1
2
}else if(index == 0){
            getFirst();// return value not used here 

Just return the value given by getFirst().
1
2
}else if(index == 0){
            return getFirst();


Likewise for the getLast() case.

Don't ignore compiler warnings, such as "not all control paths return a value". That's the problem here.
Last edited on
Sweet, that's it.
And yeah, I know I shouldn't ignore warning, and I didn't, it's just that Dev C++ is old and crappy and I have to use it for my class. At any rate, making stupid mistakes like these and not having an automatic fix on hand has been hugely useful in improving my code, so I'm kinda reluctant to switch to a better compiler until I no longer need to use this one.
Topic archived. No new replies allowed.