Linked List Function Implementations

I tried to create an insert function to add an item, a remove function to remove one item, and a remove all function to remove all nodes. I am not sure how to go about this. Please help ?

I am asked to:

void insert(const T& item, const T& location) – Inserts a new node in the linked list with value item just before the first occurrence of the node with the value location. If no node is value with the value location, append the new item to the end of the list.

• void remove(const T& item) – Remove the first occurrence of the node with value item from the list. If there is no node with value item in the list, do nothing.

• void remove_all(const T& item) – Remove all occurrences of nodes with value item from the list.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
 #ifndef LIST_H
#define LIST_H

#include <iostream>

class ListRangeException {};

template <typename T>
class ListNode {
public:
    T data;
    ListNode<T>* next;
};

template <typename T>
class List {
protected:
    ListNode<T>* head;
public:
    List() { head = NULL; }
    ~List() { clear(); }
    
    void append(const T& item);
    
    void clear() { while (head) remove(0); }
    
    T get(int location) const;
    
    void insert(const T& item, int location);
    void move(int location, int newLocation);
    void remove(int location);
    
    void print() const;
    int size() const;
    
    T& operator[] (int location);
    
    void traverse(void(*f)(T&));
};

template <typename T>
void List<T>::append(const T& item) {
    ListNode<T>* p = new ListNode<T>;
    p->data = item;
    p->next = NULL;
    if (!head)
        head = p;
    else {
        ListNode<T>* q = head;
        while (q->next)
            q = q->next;
        q->next = p;
    }
}

template <typename T>
void List<T>::insert(const T& item, int location) {
    if (location < 0 || location >= size())
        throw ListRangeException();
    if (location == 0) {
        ListNode<T>* p = new ListNode<T>;
        p->data = item;
        p->next = head;
        head = p;
    }
    else {
        ListNode<T>* p = head;
        for (int i = 0; i < location - 1; i++)
            p = p->next;
        ListNode<T>* q = new ListNode<T>;
        q->data = item;
        q->next = p->next;
        p->next = q;
    }
}

template <typename T>
void List<T>::print() const {
    //	ListNode<T>* p = head;
    //	while (p) {
    //		std::cout << " " << p->data;
    //		p = p->next;
    //	}
    for (ListNode<T>* p = head; p; p = p->next)
        std::cout << " " << p->data;
    std::cout << std::endl;
}

template <typename T>
void List<T>::remove(int location) {
    if (location < 0 || location >= size())
        throw ListRangeException();
    if (!location) {
        ListNode<T>* p = head;
        head = head->next;
        delete p;
    }
    else {
        ListNode<T>* p = head;
        for (int i = 0; i < location - 1; i++)
            p = p->next;
        ListNode<T>* q = p->next;
        p->next = q->next;
        delete q;
    }
}

template <typename T>
int List<T>::size() const {
    int count = 0;
    for (ListNode<T>* p = head; p; p = p->next)
        count++;
    return count;
}

template <typename T>
T& List<T>::operator[] (int location) {
    if (location < 0 || location >= size())
        throw ListRangeException();
    ListNode<T>* p = head;
    for (int i = 0; i < location; i++)
        p = p->next;
    return p->data;
}

template <typename T>
void List<T>::traverse(void(*f)(T&)) {
    ListNode<T>* p = head;
    while (p) {
        f(p->data);
        p = p->next;
    }
}

#endif



Topic archived. No new replies allowed.