Linked List Problem

For some reason my program is running funky. Can anyone tell me if its the linked list implementation or the other code

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  template <class T>
struct ListNode{
    T item;
    ListNode * next;
};

template <class T>
class LinkedList: public List<T>  {
private:
    ListNode<T>* head;
    int count;

public:
     LinkedList();
     ~LinkedList();
    virtual bool isEmpty ();
    virtual int getLength ();
    virtual void insert (int pos,T item);
    virtual T remove (int pos);
    virtual T retrieve (int pos);
    
};



//constructor
template <class T>
LinkedList<T>::LinkedList(){
    head= NULL;
    count =0;
}
// isempty
template <class T>
bool LinkedList<T>::isEmpty (){
    if (head == NULL){
        return false;
    }
    return true;
    
}
template<class T>
int LinkedList<T>::getLength(){
    
    return count;
    
}





template <class T>
T LinkedList<T>::retrieve(int pos){
    ListNode<T>* temp;
    
    if (isEmpty()){
        throw -1;
    }
    if (pos > count){
        throw -1;
    }
    for (int i=0; i<pos ; i++) {
        temp= temp->next;
    }
    return temp->item;
}

//Removes a node from the position
//probably should do an exception what if the pos was not random but given by user
// if given by user or random probably best to do exception
template <class T>
T LinkedList<T>::remove (int pos){
    ListNode<T>* temp;
    if (isEmpty()){
        throw -1;
    }
    if (pos>count){
        throw -1;
    }
    for (int i=0; i<pos ; i++) {
        temp= temp->next;
        

    }
    ListNode<T>* holder= temp->next;
    temp->next = holder->next;
    count--;
    return holder-> item;
    
}

//inserts a T item into the random pos
template <class T>
void LinkedList<T>::insert(int pos,T item){
    
    ListNode<T> * temp=head;
    
    for (int i=1; i<pos ; i++) {
        temp= temp->next;
    }
    ListNode<T> * temp2= new ListNode<T>;
    temp2->item=item;
    
    temp2= temp-> next;
    temp-> next=temp2;
    count++;
}



template <class T>
LinkedList<T>::~LinkedList(){
    ListNode<T>* temp= head;
    ListNode<T>* a;
    while (isEmpty()==false){
        a=temp;
        temp = temp->next;
        delete temp;
        
    }
}
    



closed account (j3Rz8vqX)
Explain funky?

What exact behavior seems off? It might give us a better clue as to what to look for, instead of scouring your code for simple mistakes.
Last edited on
Topic archived. No new replies allowed.