consolidate all repos to one for archive
This commit is contained in:
95
semester_2/programiranje_2/naloga0802/LinkedList.h
Normal file
95
semester_2/programiranje_2/naloga0802/LinkedList.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#ifndef NALOGA0802_LINKEDLIST_H
|
||||
#define NALOGA0802_LINKEDLIST_H
|
||||
|
||||
#include "Node.h"
|
||||
|
||||
template<typename T>
|
||||
class LinkedList {
|
||||
private:
|
||||
Node<T> *head = nullptr;
|
||||
public:
|
||||
bool isEmpty() const;
|
||||
|
||||
unsigned int getSize() const;
|
||||
|
||||
void add(T value);
|
||||
|
||||
T at(unsigned int index) const;
|
||||
|
||||
~LinkedList();
|
||||
|
||||
void insertAt(unsigned int index, T value);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
bool LinkedList<T>::isEmpty() const {
|
||||
return (head == nullptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int LinkedList<T>::getSize() const {
|
||||
Node<T> *tmp = head;
|
||||
unsigned int ret = 0;
|
||||
while (tmp != nullptr) {
|
||||
ret++;
|
||||
tmp = tmp->getNext();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LinkedList<T>::add(T value) {
|
||||
Node<T> *va = new Node<T>(value, nullptr);
|
||||
if (head == nullptr) {
|
||||
head = va;
|
||||
} else {
|
||||
Node<T> *tmp = head;
|
||||
Node<T> *last;
|
||||
while (tmp != nullptr) {
|
||||
last = tmp;
|
||||
tmp = tmp->getNext();
|
||||
}
|
||||
last->setNext(va);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T LinkedList<T>::at(unsigned int index) const {
|
||||
Node<T> *tmp = head;
|
||||
unsigned int count = 0;
|
||||
for (int i = 0; i < index && tmp != nullptr; i++) {
|
||||
tmp = tmp->getNext();
|
||||
}
|
||||
return tmp->getValue();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
LinkedList<T>::~LinkedList() {
|
||||
Node<T> *tmp = head;
|
||||
Node<T> *last;
|
||||
while (tmp != nullptr) {
|
||||
last = tmp;
|
||||
tmp = tmp->getNext();
|
||||
delete last;
|
||||
}
|
||||
delete tmp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void LinkedList<T>::insertAt(unsigned int index, T value) {
|
||||
Node<T> *pre;
|
||||
Node<T> *cur = head;
|
||||
Node<T> *temp = new Node<T>(value, nullptr);
|
||||
if(head != nullptr) {
|
||||
for (int i = 0; i < index && cur != nullptr; i++) {
|
||||
pre = cur;
|
||||
cur = cur->getNext();
|
||||
}
|
||||
pre->setNext(temp);
|
||||
temp->setNext(cur);
|
||||
}else{
|
||||
head = temp;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //NALOGA0802_LINKEDLIST_H
|
||||
Reference in New Issue
Block a user