From 5ba8bc2c863b2df0de94d39648e959cf4cc0f90c Mon Sep 17 00:00:00 2001 From: GeorgianaK <52410645+GeorgianaK@users.noreply.github.com> Date: Tue, 25 May 2021 08:22:52 +0300 Subject: [PATCH] read violation resolved (#1496) --- .../circular_queue_using_linked_list.cpp | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/data_structures/circular_queue_using_linked_list.cpp b/data_structures/circular_queue_using_linked_list.cpp index 5d6484ce3..3d92b3b3e 100644 --- a/data_structures/circular_queue_using_linked_list.cpp +++ b/data_structures/circular_queue_using_linked_list.cpp @@ -2,34 +2,27 @@ struct node { int data; - struct node *next; + struct node* next; }; class Queue { - node *front; - node *rear; + node* front=nullptr; + node* rear=nullptr; - public: - Queue() { - front = NULL; - rear = NULL; - } +public: + Queue() = default; void createNode(int val) { - node *ptr; - node *nn; - nn = new node; - ptr = front; + auto* nn = new node; nn->data = val; - nn->next = NULL; + nn->next = nullptr; front = nn; rear = nn; } void enqueue(int val) { - if (front == NULL || rear == NULL) { + if (front == nullptr || rear == nullptr) { createNode(val); - } else { - node *ptr; - node *nn; - ptr = front; + } + else { + node* nn; nn = new node; nn->data = val; rear->next = nn; @@ -38,19 +31,23 @@ class Queue { } } void dequeue() { - node *n; + node* n; n = front; - front = front->next; - delete (n); + if (n) { + front = front->next; + delete n; + } } void traverse() { - node *ptr; + node* ptr; ptr = front; - do { - std::cout << ptr->data << " "; - ptr = ptr->next; - } while (ptr != rear->next); - std::cout << front->data << std::endl; + if (ptr) { + do { + std::cout << ptr->data << " "; + ptr = ptr->next; + } while (ptr != rear->next); + std::cout << front->data << std::endl; + } } }; int main(void) {