I'm trying to make a super basic circular linked list, but my program is seg-faulting when I try to enter the first number. Also, this assignment requires I only have a head node (no tail nodes)
#include "Queue.hpp"
#include <iostream>
int main()
{
Queue queue;
int choice=0;
int num;
do
{
std::cout << "Would you like to 1. Add a value to the back of the queue or 2. Exit?";
std::cin >> choice;
if (choice==1) //add value to back of queue
{
menu.message("Enter an integer:");
std::cin >> num;
queue.addBack(num);
if (choice==2)
{
menu.message("You've chosen to exit, goodbye!");
}
}while(choice!=2);
return 0;
}
#include "Queue.hpp"
#include <cstddef> //for NULL
#include <iostream>
/******************
*Queue
*Constructor for Queue
* ***************/
Queue::Queue()
{
head=NULL;
}
/******************
*~Queue
*Destructor for Queue - frees all memory of nodes in the queue
* ***************/
Queue::~Queue()
{
QueueNode* queue = head; //start at front
while (queue != NULL) //while still returning values
{
QueueNode* erase = queue;
queue = queue->next;
delete erase;
}
head=NULL;
}
/******************
*isEmpty
*checks if queue is empty. if so, returns true; otherwise, return false
* ***************/
bool Queue::isEmpty()
{
if (head==NULL)
{
returntrue;
}
else
{
returnfalse;
}
}
/******************
*addBack
*takes a user-inputted int, creates a QueueNode with user input int, appends it to the back of the list
* ***************/
void Queue::addBack(int v)
{
QueueNode* back;
back->val = v; //create new node with val
if(isEmpty())//if first in list
{
head=back;
head->next = head;//circular link to itself
}
else
{
QueueNode* oldback=(head->prev); //temp
(back->next) = (head->prev);//put new node at end
(back->prev)=oldback;
}
}
I'm trying really hard to make it haha. I'm referencing some examples in my textbook but they aren't exactly the same so I've been trying different things... I've only been coding for a few months so a lot of concepts are still unfamiliar. I'll fix that though and get back to you, thanks!