Circular Linked List

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)

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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;
}


Queue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef QUEUE_HPP
#define QUEUE_HPP

class Queue

{
	struct QueueNode
	{
		QueueNode* next;
		QueueNode* prev;
		int val;	
	};
	
	private:
		QueueNode* head;
	public:
		Queue();
		~Queue();
		bool isEmpty();
		void addBack(int val);

};
#endif 


Queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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)
	{
		return true;
	}
	else
	{
		return false;
	}
}
/******************
 *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;
	}
}	
1
2
	QueueNode* back;
	back->val = v;	//create new node with val 

Your back pointer is uninitialized, so dereferencing it is illegal. Did you make this code?
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!
That fixed it! Thank you! :)
Topic archived. No new replies allowed.