commit b97c5fce21b4da80f2efe3488e33602d879196aa
parent ccf8ea176a10b16a1a9df793817afb66ea9c75a4
Author: mjkloeckner <mjkloeckner@gmail.com>
Date: Sun, 16 Jun 2024 00:00:00 -0300
add simple linked list header
Diffstat:
| A | list.h | | | 121 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | main.cpp | | | 2 | ++ |
| A | node.h | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 195 insertions(+), 0 deletions(-)
diff --git a/list.h b/list.h
@@ -0,0 +1,121 @@
+#ifndef LIST_H_
+#define LIST_H_
+
+#include "node.h"
+
+template <typename Type>
+class List {
+private:
+ Node<Type> *first;
+ Node<Type> *cursor;
+ unsigned int size;
+
+public:
+ /*
+ * pos: crea una lista vacia
+ */
+ List();
+
+ /*
+ * pos: elimina la memoria de la lista, no de los datos
+ */
+ virtual ~List();
+
+ /*
+ * pos: indica si la Lista tiene algún elemento
+ */
+ bool isEmpty();
+
+ /*
+ * pos: devuelve la cantidad de elementos que tiene la Lista
+ */
+ unsigned int getSize();
+
+ /*
+ * pos: inserta el dato `element` al comienzo de la lista
+ */
+ void insert(Type element);
+
+ /*
+ * pos: inicializa el cursor
+ */
+ void startCursor();
+
+ /* pre: el cursor ha sido inicializado
+ * pos: avanza el cursor al siguiente elemento de la lista, devuelviendo
+ * falso si no quedan nodos por recorrer
+ */
+ bool forwardCursor();
+
+ /*
+ * pre: el cursor ha sido inicializado y está posicionado sobre une
+ * elemento de la lista
+ * pos: devuelve una referencia al nodo en el cual se encuentra el cursor
+ */
+ Node<Type> *getCursor();
+
+ /*
+ * pre: el cursor ha sido inicializado y está posicionado sobre une
+ * elemento de la lista
+ * pos: devuelve el dato del nodo en el cual se encuentra el cursor
+ */
+ Type getCursorData();
+};
+
+template <typename Type>
+List<Type>::List() {
+ this->first = NULL;
+ this->size = 0;
+ this->cursor = NULL;
+}
+
+template <typename Type>
+List<Type>::~List() {
+ while (this->first != NULL) {
+ Node<Type> *delNode = this->first;
+ this->first = delNode->getNext();
+ delete delNode;
+ }
+}
+
+template <typename Type>
+bool List<Type>::isEmpty() {
+ return (this->size == 0);
+}
+
+template <typename Type>
+unsigned int List<Type>::getSize() {
+ return this->size;
+}
+
+template <typename Type>
+void List<Type>::insert(Type element) {
+ Node<Type> *newNode = new Node<Type>(element);
+ newNode->setNext(this->first);
+ this->first = newNode;
+ this->size++;
+}
+
+template<class Type>
+void List<Type>::startCursor() {
+ this->cursor = NULL;
+}
+
+template<class Type>
+bool List<Type>::forwardCursor() {
+ this->cursor = this->cursor == NULL ? this->first : this->cursor->getNext();
+
+ return (this->cursor != NULL);
+}
+
+template<class Type>
+Node<Type> *List<Type>::getCursor() {
+ return this->cursor;
+}
+
+template<class Type>
+Type List<Type>::getCursorData() {
+ return this->cursor->getData();
+}
+
+#endif /* LIST_H_ */
diff --git a/main.cpp b/main.cpp
@@ -3,6 +3,8 @@
#include <string>
#include <sstream>
+#include "list.h"
+
#define INPUT_FILE_DELIM ','
int main (void) {
diff --git a/node.h b/node.h
@@ -0,0 +1,72 @@
+#ifndef NODE_H_
+#define NODE_H_
+#include <iostream>
+
+template <typename Type>
+class Node {
+private:
+ Type data;
+ Node<Type> *next;
+
+public:
+ /*
+ * pos: el nodo resulta inicializado con el dato dado y sin nodo siguiente
+ */
+ Node(Type newData);
+
+ /*
+ * pos: se libera la memoria utilizada
+ */
+ virtual ~Node();
+
+ /*
+ * pos: devuelve el dato almacenado en el nodo
+ */
+ Type getData();
+
+ /*
+ * pos: cambia el dato almacendo en el nodo por `data`
+ */
+ void setData(Type newData);
+
+ /*
+ * pos: devuelve una referencia al nodo siguiente
+ */
+ Node<Type>* getNext();
+
+ /*
+ * pos: cambia el nodo siguiente por `next`
+ */
+ void setNext(Node<Type> *newNext);
+};
+
+template <typename Type>
+Node<Type>::Node(Type newData) {
+ this->data = newData;
+ this->next = NULL;
+}
+
+template <typename Type>
+Node<Type>::~Node() {}
+
+template <typename Type>
+Type Node<Type>::getData() {
+ return this->data;
+}
+
+template <typename Type>
+void Node<Type>::setData(Type newData) {
+ this->data = newData;
+}
+
+template <typename Type>
+Node<Type> *Node<Type>::getNext() {
+ return this->next;
+}
+
+template <typename Type>
+void Node<Type>::setNext(Node<Type> *newNext) {
+ this->next = newNext;
+}
+
+#endif