Skip to content
Snippets Groups Projects
Commit 2b52f7c1 authored by haquin elouan's avatar haquin elouan
Browse files

Ajout des listes chaines

parent 00894b03
Branches
No related merge requests found
......@@ -11,14 +11,18 @@ private:
public:
Array(uint16_t p_nbValues) { m_values = (T*)calloc(p_nbValues, sizeof(T)); }
Array(Array& p_other) { m_values = calloc(p_other.getLength()); for (uint16_t i = 0; i < p_other.getLength(); i++) m_values[i] = p_other.getValues()[i]; }
Array(uint16_t p_nbValues) {m_values = (T*)calloc(p_nbValues, sizeof(T)); m_length = p_nbValues;}
Array(Array& p_other) { m_length = p_other.getLength(); m_values = calloc(p_other.getLength()); for (uint16_t i = 0; i < p_other.getLength(); i++) m_values[i] = p_other.getValues()[i]; }
//Utility
void replaceAt(uint16_t p_index, T p_newVal) { m_values[p_index] = m_values[p_index] != nullptr ? p_newVal : exit(-1); }
void deleteAt(uint16_t p_index) { m_values[p_index] = m_values[p_index] != nullptr ? nullptr : exit(-1); }
uint16_t searchFor(T p_val) { uint16_t ind = 0; for (uint16_t i; i < m_length; i++) { m_values[i] == p_val ? ind = i : 0; } return ind; }
//SETTERS
void setValueAt(uint16_t p_index, T p_val) { if (p_index > m_length)return; m_values[p_index] = p_val; }
//GETTERS
T getValueAt(uint16_t p_index) { T val = m_values[p_index] != (int)nullptr ? m_values[p_index] : (int)nullptr; return val; }
T* getValues() { return m_values; }
......
#pragma once
#include <stdlib.h>
#include "Node_LinkedList.h"
template <typename T>
class LinkedList : public std::_Container_base {
private:
Node<T>* m_tail;
Node<T>* m_head;
public:
LinkedList() { m_tail = nullptr; m_head= nullptr; }
LinkedList(uint16_t p_length) {
m_tail = new Node<T>();
m_head = new Node<T>();
m_tail->setPrev(m_head);
m_head->setNext(m_tail);
m_tail->setNext(nullptr);
m_head->setPrev(nullptr);
for (uint16_t i = 1; i < p_length; ++i) insertAt(i, new Node<T>());
}
void insertAt(uint16_t p_ind, Node<T>* p_other) {
Node<T>* currNode = new Node<T>();
currNode = m_head;
for (uint16_t i = 0; i < p_ind && currNode->getNext() != nullptr; i++) currNode = currNode->getNext();
if(currNode->getNext() != nullptr)
currNode->getNext()->setPrev(p_other);
if(currNode->getPrev() != nullptr)
currNode->getPrev()->setNext(p_other);
p_other->setPrev(currNode->getPrev());
p_other->setNext(currNode->getNext());
}
void deleteAt(uint16_t p_ind) {
Node<T> *currNode = new Node<T>();
currNode = m_head;
for (uint16_t i = 0; i < p_ind && currNode->getNext() != nullptr; i++) currNode = currNode->getNext();
currNode->getPrev()->setNext(currNode->getNext());
currNode->getNext()->setPrev(currNode->getPrev());
}
void replace(Node<T>* p_other, Node<T>* p_new) {
if (p_other == nullptr)
return;
p_new->getNext()->setPrev(p_other->getNext() != nullptr && p_other->getNext()->getPrev() != nullptr ? this : nullptr);
p_new->getPrev()->setNext(p_other->getPrev() != nullptr && p_other->getPrev()->getNext() != nullptr ? this : nullptr);
p_new->setNext(p_other->getNext());
p_new->setPrev(p_other->getPrev());
p_new->setValue(p_other->getValue());
delete p_other;
}
int search(Node<T>* p_other) {
Node<T> currNode = new Node<T>();
currNode = m_head;
for (uint16_t i = 0; currNode->getNext() != nullptr; i++) {
currNode = currNode->getNext();
if (currNode->getNext() == p_other->getNext()
&& currNode->getPrev() == p_other->getPrev()
&& currNode->getValue() == p_other->getValue())
return i;
}
return -1;
}
//Getters
Node<T>* getTail() { return m_tail; }
Node<T>* getHead() { return m_head; }
//Setter
Node<T>* setTail(Node<T>* p_other) { m_tail = p_other; }
Node<T>* setHead(Node<T>* p_other) { m_head = p_other; }
};
\ No newline at end of file
#pragma once
template <typename T>
class Node : public std::_Container_base {
private:
Node* m_next;
Node* m_prev;
T m_value;
public:
Node() { m_value = (int)0; m_prev = nullptr; m_next = nullptr; }
Node(T p_val, Node* p_next, Node* p_prev) {m_value = p_val; m_prev = p_prev; m_next = p_next;}
~Node() { delete m_next; delete m_prev; }
//Getters
Node* getNext(){ return m_next; }
Node* getPrev() { return m_prev; }
T& getValue() { return m_value; }
//Setter
void setNext(Node<T>* p_other) { m_next = p_other; }
void setPrev(Node<T>* p_other) { m_prev = p_other; }
T& setValue(T p_value) { m_value = p_value; }
};
\ No newline at end of file
#include <iostream>
#include "Vector3D.h"
#include "Array.h"
#include "LinkedList.h"
int main()
{
Vector3D<float> v1(1.0, -3.0f, 2.0f);
......@@ -8,6 +10,9 @@ int main()
Array<int> a1(15);
LinkedList<int> L1(15);
system("Pause");
return 0;
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment