Radu-Adrian Marcu / Mbed OS SOFT253_GroupA_AssignmentRepo

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

LinkedList.cpp

Committer:
FairyMental
Date:
2017-04-07
Revision:
57:dfcdda1e42b6
Parent:
54:53ee2d07d684
Child:
60:db8c5b7fc548

File content as of revision 57:dfcdda1e42b6:

#include "LinkedList.h"
#include <stdio.h>
#include <ctype.h>
/*
    Constructor
    Initialises a linked list using the limit given
*/
LinkedList::LinkedList(int limit)
{
    sizeLimit = limit;
    currentSize = 0;
    head = NULL; 
}

void LinkedList::addValueFront(Measure _measure){
    Node *n = new Node();   
    n->measure = _measure;       
    n->next = head;         
                            
    head = n; 
    
    currentSize++;              
}
/*
    Adds a value to the end of the list - if the list is full, removes the 
    oldest element first to create space
*/
void LinkedList::addValueEnd(Measure _measure)
{
    if(head == NULL)
    {
        Node *aux = new Node();
        aux->measure = _measure;
        aux->next = NULL;
        head = aux;   
    }
    else
    {
        while(currentSize >= sizeLimit)
        {
            popValueFRONT();                
        }
        
        Node *n = head;
        while(n->next != NULL)
        {
            n = n->next;
        }
        Node *aux = new Node();
        aux->measure = _measure;
        aux->next = NULL;
        n->next = aux;
    }
    currentSize++;
}
Measure LinkedList::popValueFRONT()
{
    Node *n = head;
    Measure _measure = n->measure;

    head = head->next;
    delete n;
    
    currentSize--;
    
    return _measure;
}
void LinkedList::ListAll()
{
    Node *n = head;
    int i = 0;
    while(n->next != NULL)
    {
        i++;
        char *ptr = n->measure.date.ToString();
        printf("%i. %s : T: %f | H: %f | P: %f |\r\n",i,ptr ,n->measure.temperature, n->measure.humidity, n->measure.pressure);
        n = n->next;   
    }   
}
void LinkedList::ListX(int x)
{
    Node *n = head;
    int i = 0;
    while(n->next != NULL && i < x)
    {
        char *ptr = n->measure.date.ToString();
        printf("%i. %s T: %f | H: %f | P: %f |\r\n",i,ptr , n->measure.temperature, n->measure.humidity, n->measure.pressure);   
        i++;
        n = n->next; 
    }   
}
void LinkedList::DeleteAll()
{
    while(head->next != NULL)
    {
           Node *n = head;
           head = head -> next;
           
           delete n;
           
           currentSize = 0;
    }   
}
void LinkedList::DeleteX(int x)
{
    int i = 0;
    while(head->next != NULL && i < x)
    {
        Node *n = head;
        head = head->next;
        
        delete n;
        
        i++;
    }   
    currentSize -= x;
}
int LinkedList::GetSize()
{
    return currentSize;   
}