mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
read violation resolved (#1496)
This commit is contained in:
parent
a9d33871a9
commit
5ba8bc2c86
@ -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,20 +31,24 @@ class Queue {
|
||||
}
|
||||
}
|
||||
void dequeue() {
|
||||
node *n;
|
||||
node* n;
|
||||
n = front;
|
||||
if (n) {
|
||||
front = front->next;
|
||||
delete (n);
|
||||
delete n;
|
||||
}
|
||||
}
|
||||
void traverse() {
|
||||
node *ptr;
|
||||
node* ptr;
|
||||
ptr = front;
|
||||
if (ptr) {
|
||||
do {
|
||||
std::cout << ptr->data << " ";
|
||||
ptr = ptr->next;
|
||||
} while (ptr != rear->next);
|
||||
std::cout << front->data << std::endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
int main(void) {
|
||||
Queue q;
|
||||
|
Loading…
Reference in New Issue
Block a user