1

So I recently learned of Doubly Linked List on how it can go forward as well as backward, and I had this idea of trying to simulate an elevator on how it can move up and down. I wrote this code and I had it built with no errors. When I ran it, it got stuck on Input your destination:
This is my expected output:
This is an Elevator Simulator
Input a number of floors to construct for the elevator:
The following floors have been created:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Input your destination:
The elevator is going up.....
Elevator is now at Floor.....2
Elevator is now at Floor.....3
Elevator is now at Floor.....4
Elevator is now at Floor.....5
Input your destination:
The elevator is going down.....
Elevator is now at Floor.....4
Elevator is now at Floor.....3
Elevator is now at Floor.....2
Elevator is now at Floor.....1
The elevator is currently at floor 1

#include <bits/stdc++.h>

using namespace std;

//using a Doubly Linked List
struct node {
    int data;
    node *next;
    node *prev;
};

node *head = NULL;
node *last = NULL;
node *current = NULL;

void constructNode(node *head, int totalNodes) {
    for(int i = 1; i <= totalNodes; i++){
        node *newNode = new node;
        last = head;
        newNode->data = i;
        newNode->next = NULL;

        if (head == NULL) {
            newNode->prev = NULL;
            head = newNode;
            return;
        }
        while (last->next != NULL){
            last = last->next;
        }
        last->next = newNode;
        newNode->prev = last;
    }
    current = head;
    return;
}

void locateCurrent(node *current){
    cout << "The elevator is currently at floor " << current->data << endl;
}

void moveElevator(int destination){
    if(current->data <= destination && destination <= last->data){
        cout << "The elevator is going up....." << endl;
        while(current->data < destination){
            current = current->next;
            cout << "Elevator is now at Floor....." << current->data << endl; 
        }
        cout << "You have arrived at your destination." << endl;
    }
    else if(current->data >= destination && destination >= head->data){
        cout << "The elevator is going down....." << endl;
        while(current->data > destination){
            current = current->prev;
            cout << "Elevator is now at Floor....." << current->data << endl; 
        }
        current = current->prev;
        cout << "You have arrived at your destination." << endl;
    }
}

void displayNodes(node *head){
    node *temp = head;
    while (temp != NULL){
        cout << temp->data << ", ";
        temp = temp->next; 
    }
}

int main() {
    cout << "This is an Elevator Simulator " << endl;

    cout << "Input a number of floors to construct for the elevator: " << endl;
    constructNode(head, 10);

    cout << "The following floors have been created: " << endl;
    displayNodes(head);

    cout << "Input your destination: " << endl;
    moveElevator(5);

    cout << "Input your destination: " << endl;
    moveElevator(1); 

    //I might need this function in the future but it also is not working right now
    locateCurrent(current);
    
    return 0;
}
  • "its not working out well for me" is not a problem description. What happens? Why is that wrong? – underscore_d Mar 13 '21 at 14:21
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). Combination of both is even worse – prapin Mar 13 '21 at 14:29
  • I am guessing your program crashes as soon as you try to move the elevator. The reason that happens is because you never put the elevator in the building after constructing it :-P To be more specific, your `current` pointer is always NULL and never set to an actual "floor". – Lev M. Mar 13 '21 at 14:33
  • A) You still did not edit what actually happens / what output you get. B) You still initialize `current` before there is anything to initialize it to, so it still always stays NULL. You must initialize `current` *after* you create the floors! "=" operator does not link variables! It will not make `current` track the value of `head` and update automatically! It will only copy the value from `head` to `current` at the moment it is called, so if `head == NULL` when you call `current = head` you get `current == NULL`. – Lev M. Mar 13 '21 at 15:18
  • @LevM. I have now moved the **current = head** after line 33, just after making the floors. But my output is still stuck at Input your destination: – Joshua Balles Mar 13 '21 at 16:21
  • You should fix your question, but in the meantime this works: https://onlinegdb.com/3C17y_20K – Jack Lilhammers Mar 13 '21 at 21:19
  • @JackLilhammers you're the mvp my dude/dudette. I have studied your code and compared it to mine and found where I went wrong. My curiosity for doubly linked list has now been quenched. For future references though, in which part(s) should I fix my question? – Joshua Balles Mar 14 '21 at 02:48
  • Well, you're essentially saying this is my code and this is the expected output, please make it work :) Albeit many ask way worse questions, it's still not the best way to ask for help. **The question has been closed for lack of debugging details. One thing you could do would be to add the faulty output.** – Jack Lilhammers Mar 14 '21 at 13:43
  • Also, the expected output was much more readable when formatted as a code segment. And so should the faulty output be too. They're both console output, therefore it's clearer if they're monospaced and have a different background than the rest of the question. Of course they must be separated from your actual code. – Jack Lilhammers Mar 14 '21 at 13:48

0 Answers0