lundi 22 juin 2020

Dynamically allocating memory to make a list (for school) C++

I'm trying to make a list but every time I send a random numbers to my function it returns a list with mostly the same number, I debugged it and it was working fine, but when I run it after compiling it don't. I want to make a list with 100 values and every value is a random number from 1 to 100. Here are the files I've made:

main.cpp

#include "main.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int randomN (int& randomNumber);

int main ()
{
    srand((unsigned) time(0));
    /*for (int index = 1; index < 10; index++) {
        int randomNumber;
        cout << randomN(randomNumber) << endl;
    }*/
    linked_list myList;
    for (int i = 1; i<= 100 ; i++){
        int randomNumber= randomN(randomNumber);
        myList.push_front(i,randomNumber);
    }
    cout << "First list:" << endl << myList << endl << endl;
    
    linked_list myList2;
    for (int i = 1; i<= 100 ; i++){
        int randomNumber= randomN(randomNumber);
        myList2.push_front(i,randomNumber);
    }
    cout << "Second list:" << endl << myList2 << endl << endl;

    //cin.get();

    return 0;
}

int randomN (int& randomNumber){
    randomNumber = (rand() % 100) + 1;
    return randomNumber;
}

fun.cpp

#include "main.h"
#include <ctime>
#include <cstdlib>

linked_list::linked_list(){
}
     // Copy constructor: A deep copy.  Copies the parameter node
    // as well as the chain of nodes it points to.
linked_list::linked_list(const linked_list& src){
    if (!src.isEmpty()){

        //Is onn the first possition of the list
        class linked_list::node_t::node_t *srcCurrent = src.head; 
        class linked_list::node_t::node_t *src_prev = srcCurrent->prev;
        class linked_list::node_t::node_t *src_next = srcCurrent->next;
        
        class linked_list::node_t::node_t *current = new linked_list::node_t(src.head->value);
        current->prev = nullptr;
        while(src_next != nullptr){
            
            if (srcCurrent->prev == nullptr){
                if(src_next){
                    class linked_list::node_t::node_t *newSrc = new class linked_list::node_t::node_t(src.head->value);
                    current->next = newSrc;
                    newSrc->prev = current;
                    src_prev = srcCurrent;
                    srcCurrent = srcCurrent->next;
                    src_next = src_next->next;
                }
                else
                    current->next = nullptr;
            }
            
            if(srcCurrent->next != nullptr && srcCurrent->prev != nullptr){
                current = current->next;
                current->value = srcCurrent->value;
                if(src_next->next == nullptr){
                    current->next = nullptr;
                    src_next = nullptr;
                    tail = current;
                    }
                else
                {
                    src_prev = srcCurrent;
                    srcCurrent = srcCurrent->next;
                    src_next = src_next->next;
                    class linked_list::node_t::node_t *newSrc = new class linked_list::node_t::node_t(src.head->value);
                    current->next = newSrc;
                    newSrc->prev = current;
                }
            }
        }
    }
}
    // cleans up storage this node points at
linked_list::~linked_list(){
    while(!isEmpty()) pop_back();
}

// inserting elements
void linked_list::insert(double value , size_t pos){}
// in front
void linked_list::push_front(int value, int random){
    //int8_t x = (int8_t) value;
    srand( time( NULL ) );
    //double k = ((rand() %100) + 1);
    auto nodePtr = new class linked_list::node_t::node_t(random);
    //struct linked_list::node_t* pointer = nullptr;
    if (value == 1){
        head = nodePtr;
        head->next = nullptr;
        head->prev = nullptr;
        // in the hole push front function I am using (head->prev) as a place to store
        // the previus node created
    }
    if (value == 100){
        tail = nodePtr;
        head->prev->next = tail;
        tail->prev = head->prev;
        tail->next = nullptr;
        head->prev = nullptr; // giving back head->prev it's original value
    }
    if(1 < value && value < 100){// I have a head and a temp
        if( head->next == nullptr){
            head ->next = nodePtr;
            nodePtr ->prev = head;
            head->prev = nodePtr;
        }
        // I have a head and a nodePtr, I want to make a new one
        else{
            head->prev->next = nodePtr;//problem here
            nodePtr->prev = head->prev;
            head->prev = nodePtr;
        }

    }
}

void linked_list::randomN(){
    srand( time( NULL ) );
}

// in back
void linked_list::push_back(double value){}
// deleting elements
void linked_list::pop_back(){// current problem
    while (tail->prev){
        class linked_list::node_t::node_t *oldTail = tail;
        //tail->prev->value = oldTail->value;
        tail = tail->prev;
        delete oldTail;
    }
    delete tail;
    head = nullptr;
    tail = nullptr;
}



linked_list& linked_list::operator=(const linked_list& rhs){
    if (this == &rhs) return *this;
    memcpy(this, &rhs, sizeof(linked_list));
    return *this;
}

// accessing elements
//double linked_list::front() const{}
//double linked_list::back() const{}
//double linked_list::at(size_t pos) const{}
// removing elements void remove(size_t pos); // remove and access double pop_front(); double pop_back();
// informational size_t size() const; bool is_empty() const;
// output
void linked_list::print() const{}
void linked_list::print_reverse() const{}

// Returns true if the list is empty
bool linked_list::isEmpty() const {
    return (head == nullptr);
} 

main.h

#include "main.h"
#include <ctime>
#include <cstdlib>

linked_list::linked_list(){
}
     // Copy constructor: A deep copy.  Copies the parameter node
    // as well as the chain of nodes it points to.
linked_list::linked_list(const linked_list& src){
    if (!src.isEmpty()){

        //Is onn the first possition of the list
        class linked_list::node_t::node_t *srcCurrent = src.head; 
        class linked_list::node_t::node_t *src_prev = srcCurrent->prev;
        class linked_list::node_t::node_t *src_next = srcCurrent->next;
        
        class linked_list::node_t::node_t *current = new linked_list::node_t(src.head->value);
        current->prev = nullptr;
        while(src_next != nullptr){
            
            if (srcCurrent->prev == nullptr){
                if(src_next){
                    class linked_list::node_t::node_t *newSrc = new class linked_list::node_t::node_t(src.head->value);
                    current->next = newSrc;
                    newSrc->prev = current;
                    src_prev = srcCurrent;
                    srcCurrent = srcCurrent->next;
                    src_next = src_next->next;
                }
                else
                    current->next = nullptr;
            }
            
            if(srcCurrent->next != nullptr && srcCurrent->prev != nullptr){
                current = current->next;
                current->value = srcCurrent->value;
                if(src_next->next == nullptr){
                    current->next = nullptr;
                    src_next = nullptr;
                    tail = current;
                    }
                else
                {
                    src_prev = srcCurrent;
                    srcCurrent = srcCurrent->next;
                    src_next = src_next->next;
                    class linked_list::node_t::node_t *newSrc = new class linked_list::node_t::node_t(src.head->value);
                    current->next = newSrc;
                    newSrc->prev = current;
                }
            }
        }
    }
}
    // cleans up storage this node points at
linked_list::~linked_list(){
    while(!isEmpty()) pop_back();
}

// inserting elements
void linked_list::insert(double value , size_t pos){}
// in front
void linked_list::push_front(int value, int random){
    //int8_t x = (int8_t) value;
    srand( time( NULL ) );
    //double k = ((rand() %100) + 1);
    auto nodePtr = new class linked_list::node_t::node_t(random);
    //struct linked_list::node_t* pointer = nullptr;
    if (value == 1){
        head = nodePtr;
        head->next = nullptr;
        head->prev = nullptr;
        // in the hole push front function I am using (head->prev) as a place to store
        // the previus node created
    }
    if (value == 100){
        tail = nodePtr;
        head->prev->next = tail;
        tail->prev = head->prev;
        tail->next = nullptr;
        head->prev = nullptr; // giving back head->prev it's original value
    }
    if(1 < value && value < 100){// I have a head and a temp
        if( head->next == nullptr){
            head ->next = nodePtr;
            nodePtr ->prev = head;
            head->prev = nodePtr;
        }
        // I have a head and a nodePtr, I want to make a new one
        else{
            head->prev->next = nodePtr;//problem here
            nodePtr->prev = head->prev;
            head->prev = nodePtr;
        }

    }
}

void linked_list::randomN(){
    srand( time( NULL ) );
}

// in back
void linked_list::push_back(double value){}
// deleting elements
void linked_list::pop_back(){// current problem
    while (tail->prev){
        class linked_list::node_t::node_t *oldTail = tail;
        //tail->prev->value = oldTail->value;
        tail = tail->prev;
        delete oldTail;
    }
    delete tail;
    head = nullptr;
    tail = nullptr;
}



linked_list& linked_list::operator=(const linked_list& rhs){
    if (this == &rhs) return *this;
    memcpy(this, &rhs, sizeof(linked_list));
    return *this;
}

// accessing elements
//double linked_list::front() const{}
//double linked_list::back() const{}
//double linked_list::at(size_t pos) const{}
// removing elements void remove(size_t pos); // remove and access double pop_front(); double pop_back();
// informational size_t size() const; bool is_empty() const;
// output
void linked_list::print() const{}
void linked_list::print_reverse() const{}

// Returns true if the list is empty
bool linked_list::isEmpty() const {
    return (head == nullptr);
} 

Aucun commentaire:

Enregistrer un commentaire