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
|
41
semester_2/programiranje_2/naloga0802/Node.h
Normal file
41
semester_2/programiranje_2/naloga0802/Node.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef NALOGA0802_NODE_H
|
||||
#define NALOGA0802_NODE_H
|
||||
|
||||
template <typename T>
|
||||
class Node{
|
||||
private:
|
||||
T value;
|
||||
Node *next = nullptr;
|
||||
public:
|
||||
Node(T value, Node *next);
|
||||
|
||||
void setValue(T value);
|
||||
|
||||
void setNext(Node *next);
|
||||
|
||||
T getValue() const;
|
||||
|
||||
Node *getNext() const {
|
||||
return next;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
Node<T>::Node(T value, Node *next) : value(value), next(next) {}
|
||||
|
||||
template<typename T>
|
||||
void Node<T>::setValue(T value) {
|
||||
Node::value = value;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void Node<T>::setNext(Node *next) {
|
||||
Node::next = next;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T Node<T>::getValue() const {
|
||||
return value;
|
||||
}
|
||||
|
||||
#endif //NALOGA0802_NODE_H
|
51
semester_2/programiranje_2/naloga0802/README.md
Normal file
51
semester_2/programiranje_2/naloga0802/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
Implementirajte lastno implementacijo enosmerno povezanega seznama (https://en.wikipedia.org/wiki/Linked_list) za poljubni tip (LinkedList).
|
||||
|
||||
Implementacijo šablono razredov napišite vse v posamezno datoteko .h (brez .cpp).
|
||||
|
||||
Implementirajte šablono razreda Node, ki ima:
|
||||
instančni spremenljivki:
|
||||
value, ki je poljubnega tipa, in
|
||||
next, ki kaže na naslednika (tipa Node*) v seznamu.
|
||||
konstruktor z 2 vrednostima (next ima privzeto vrednost nullptr)
|
||||
get in set metode.
|
||||
|
||||
Implementirajte šablono razreda LinkedList, ki ima:
|
||||
instančno spremenljivko head, ki predstavlja začetek vašega seznama oz. vaše prvo vozlišče. V primeru, da je prazen seznam, naj ima privzeto vrednost nullptr.
|
||||
metodo isEmpty(), ki vrne true, če je seznam prazen, sicer naj vrne false,
|
||||
metodo getSize(), ki vrne dolžino seznama,
|
||||
metodo add(T value), ki doda vrednost na konec seznama,
|
||||
metodo at(unsigned int index), ki vrne vrednost, ki se nahaja na podanem indeksu,
|
||||
destruktor, kjer je potrebno izbrisati vsa ustvarjena vozlišča.
|
||||
|
||||
V programu main ustvarite en primerek LinkedList in testirajte vse metode.
|
||||
|
||||
Primer:
|
||||
|
||||
int main(){
|
||||
LinkedList<int> list;
|
||||
|
||||
std::cout << "List " << (list.isEmpty() ? "IS" : "IS NOT") << " empty." << std::endl << std::endl;
|
||||
|
||||
list.add(1);
|
||||
list.add(4);
|
||||
list.add(10);
|
||||
|
||||
std::cout << "List " << (list.isEmpty() ? "IS" : "IS NOT") << " empty." << std::endl << std::endl;
|
||||
|
||||
std::cout << "List (size " << list.getSize() << "): " << std::endl;
|
||||
for(int i=0; i<list.getSize(); i++)
|
||||
std::cout << list.at(i) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Izhod:
|
||||
|
||||
List IS empty.
|
||||
|
||||
List IS NOT empty.
|
||||
|
||||
List (size 3):
|
||||
1
|
||||
4
|
||||
10
|
31
semester_2/programiranje_2/naloga0802/main.cpp
Normal file
31
semester_2/programiranje_2/naloga0802/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include "LinkedList.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
|
||||
LinkedList<int> list;
|
||||
|
||||
std::cout << "List " << (list.isEmpty() ? "IS" : "IS NOT") << " empty." << std::endl << std::endl;
|
||||
|
||||
|
||||
list.add(1);
|
||||
list.add(4);
|
||||
list.add(10);
|
||||
list.insertAt(2,5);
|
||||
|
||||
std::cout << "List " << (list.isEmpty() ? "IS" : "IS NOT") << " empty." << std::endl << std::endl;
|
||||
|
||||
std::cout << "List (size " << list.getSize() << "): " << std::endl;
|
||||
|
||||
for(int i=0; i<list.getSize(); i++)
|
||||
std::cout << list.at(i) << std::endl;
|
||||
|
||||
list.insertAt(3,3);
|
||||
std::cout << "\n";
|
||||
|
||||
for(int i=0; i<list.getSize(); i++)
|
||||
std::cout << list.at(i) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user