Queue implemented with LinkedList error in pointer display function - c++

In this queue is implemented using Linked List, So the display function is showing the correct result but the SizeOf() is also implemented by the same logic and if called not showing the proper answer.
Why is this happening?
// A C program to demonstrate linked list based implementation of queue
#include <stdlib.h>
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
// A linked list (LL) node to store a queue entry
struct QNode
{
int key;
struct QNode *next;
};
// The queue, front stores the front node of LL and rear stores the last node of LL
struct Queue
{
struct QNode *front, *rear;
};
// A utility function to create a new linked list node.
struct QNode* newNode(int k)
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
temp->next = NULL;
return temp;
}
// A utility function to create an empty queue
struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
// The function to add a key k to q
void enQueue(struct Queue *q, int k)
{
// Create a new LL node
struct QNode *temp = newNode(k);
// If queue is empty, then new node is front and rear both
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}
// Function to remove a key from given queue q
struct QNode *deQueue(struct Queue *q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return NULL;
// Store previous front and move front one node ahead
struct QNode *temp = q->front;
q->front = q->front->next;
// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
return temp;
}
void Display(struct Queue *q)
{
if(q==NULL)
{
cout<<"No elements"<<endl;
return;
}
else{
while(q->front->next!=NULL)
{
cout<<q->front->key<<" ";
q->front=q->front->next;
}
cout<<q->front->key<<" ";
}
}
int SizeOf(struct Queue *q)
{
int count=0;
if(q==NULL)
{
cout<<"No elements"<<endl;
return 0;
}
else{
while(q->front->next!=NULL)
{
count++;
q->front=q->front->next;
}
count++;
}
return count;
}
// Driver Program to test anove functions
int main()
{
struct Queue *q = createQueue();
enQueue(q, 10);
enQueue(q, 20);
deQueue(q);
deQueue(q);
enQueue(q, 30);
enQueue(q, 40);
enQueue(q, 50);
enQueue(q, 40);
enQueue(q, 50);
struct QNode *n = deQueue(q);
if (n != NULL)
printf("Dequeued item is %d\n", n->key);
cout<<"The Queue is Displayed as follows:"<<endl;
Display(q);
cout<<"The Queue Size is as follows:"<<endl;
int no=SizeOf(q);
cout<<no<<endl;`enter code here`
return 0;
}
Output of Display() is 40 50 40 50 but output of SizeOf() is 1. What is the problem with that?

Related

Delete_at in a queue does not work as it should

so i want to implement a queue data structure in c++ and use some special methods like the delete_at() putting into consideration the constraints of the queue data structure, so I made it using the dequeue method and took all the data that are not equal to the index the user want to delete and stored in array in order to enqueue it all back in but without the index that the user want to delete, however nothing gets deleted , so here is the code :
#include <list>
using namespace std;
class Queue{
private:
struct node {
int data;
struct node *next;
};
struct node* front = NULL;
struct node* rear = NULL;
public:
void Enqueue(int d) {
struct node* tmp=new node;
if (rear == NULL) {
tmp->next = NULL;
tmp->data = d;
rear=tmp;
front = rear;
}
else {
rear->next = tmp;
tmp->data = d;
tmp->next = NULL;
rear = tmp;
}
}
int Dequeue() {
struct node* tmp = front;
int data=front->data;
if (front == NULL) {
return 0;
}
else{
if (tmp->next != NULL) {
tmp=front;
front = front->next;
delete tmp;
}
else {
tmp=front;
delete tmp;
front = NULL;
rear = NULL;
}
}
return data;
}
void Display() {
struct node* temp = front;
if (front == NULL) {
cout<<"Queue is empty"<<endl;
return;
}
while (temp != NULL) {
cout<<temp->data<<"\n";
temp = temp->next;
}
}
int Size() {
struct node* temp = front;
int cnt=0;
while (temp != NULL) {
cnt++;
temp = temp->next;
}
return cnt;
}
void Delete_at(int index){
int i=0;
int ar_size=Size();
int data_arr[ar_size-1];
if(index > Size()){
cout<<"\n"<<"Error: out of bounds !";
return;
}
while(i<ar_size){
if (i==(index-1)){
Dequeue();
}
else{
data_arr[i]=Dequeue();
}
i++;
}
i=0;
while(i<ar_size){
Enqueue(data_arr[i]);
i++;
}
}
};
int main() {
int i=0;
Queue q;
q.Enqueue(2);
q.Enqueue(6);
q.Enqueue(7);
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(4);
q.Delete_at(2);
q.Display();
return 0;
}
You have a two primary problems with your code generally, and Delete_at() cannot simply call Dequeue(). As a general note, your code contains duplicated expressions that can simply be consolidated. For example, Enqueue() can be written succinctly as:
void Enqueue(int d) {
struct node *tmp = new node;
tmp->next = nullptr;
tmp->data = d;
if (rear == nullptr) {
front = rear = tmp;
size = 1;
}
else {
rear->next = tmp;
rear = tmp;
size += 1;
}
}
Your Dequeue() function will segfault checking front->data BEFORE checking if front == nullptr. You must check before you dereference, e.g.
int Dequeue() {
struct node *tmp = front;
int data;
if (front == nullptr) { /* (must check before dereference (front->data) */
return 0;
}
data = front->data;
size -= 1;
if (tmp->next != nullptr) {
front = front->next;
}
else {
front = nullptr;
rear = nullptr;
}
delete tmp;
return data;
}
Your Delete_at() function must remove the node at a specific index. This requires that you maintain your ->next links throughout your list, updating the prev->next before the deleted node to point to the node after the one you are deleting. You do that by iterating with both the address of the node and a pointer to node. When you reach the index to remove, you simply replace what is currently at the address of that node with the next node and delete the current, see: Linus on Understanding Pointers
void Delete_at (size_t index) {
struct node *pnode = front, /* pointer to node */
**ppnode = &front; /* address of node */
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
while (index--) { /* loop index times */
ppnode = &pnode->next; /* address of next node */
pnode = pnode->next; /* pointer to next node */
}
*ppnode = pnode->next; /* replace struct at address with next */
delete pnode; /* delete removed node */
size -= 1;
}
Your Size() function simply reduces to a "getter" function:
size_t Size() {
return size;
}
Updating your example a bit and being mindful of Why is “using namespace std;” considered bad practice? your full code could now be:
#include <list>
#include <iostream>
class Queue{
private:
struct node {
int data;
struct node *next;
};
struct node *front = nullptr;
struct node *rear = nullptr;
size_t size;
public:
void Enqueue(int d) {
struct node *tmp = new node;
tmp->next = nullptr;
tmp->data = d;
if (rear == nullptr) {
front = rear = tmp;
size = 1;
}
else {
rear->next = tmp;
rear = tmp;
size += 1;
}
}
int Dequeue() {
struct node *tmp = front;
int data;
if (front == nullptr) { /* (must check before dereference (front->data) */
return 0;
}
data = front->data;
size -= 1;
if (tmp->next != nullptr) {
front = front->next;
}
else {
front = nullptr;
rear = nullptr;
}
delete tmp;
return data;
}
void Display() {
struct node *temp = front;
if (front == nullptr) {
std::cout << "Queue is empty" << '\n';
return;
}
while (temp != nullptr) {
std::cout << temp->data << '\n';
temp = temp->next;
}
}
size_t Size() {
return size;
}
void Delete_at (size_t index) {
struct node *pnode = front, /* pointer to node */
**ppnode = &front; /* address of node */
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
while (index--) { /* loop index times */
ppnode = &pnode->next; /* address of next node */
pnode = pnode->next; /* pointer to next node */
}
*ppnode = pnode->next; /* replace struct at address with next */
delete pnode; /* delete removed node */
size -= 1;
}
};
int main() {
Queue q;
q.Enqueue(2);
q.Enqueue(6);
q.Enqueue(7);
q.Enqueue(1);
q.Enqueue(2);
q.Enqueue(4);
q.Display();
std::cout << "\nq.Delete_at(2)\n\n";
q.Delete_at(2);
q.Display();
}
Example Use/Output
$ ./bin/queue_delete_at
2
6
7
1
2
4
q.Delete_at(2)
2
6
1
2
4
Look things over and let me know if you have further questions.
Edit With Additional Constraints From Comments
Per you comments, you have constraints of only being able to use Dequeue() and Enqueue() in Delete_at() and no pointers, etc... You can do that, but understand it will be horribly inefficient compared to simply removing the node at the index. You will essentially have to save (Dequeue()) your entire queue data in an allocated block of memory, omitting the index to remove. You will then need to iterate over all saved values calling Enqueue() to repopulated your list.
You can do that as:
void Delete_at (size_t index) {
if (index >= Size()) { /* validate with >= Size() */
std::cerr << '\n' << "Error: out of bounds !";
return;
}
size_t nelem = Size();
int *arr = new int [nelem],
n = 0;
for (size_t i = 0; i < nelem; i++) {
int tmp = Dequeue();
if (i != index && tmp)
arr[n++] = tmp;
}
for (size_t i = 0; i < (size_t)n; i++)
Enqueue (arr[i]);
delete[] arr;
}
(same output)
For a less readable more C++'ized presentation, you can replace the first loop with:
for (int i = 0, j = Dequeue(); j; i++, j = Dequeue())
if (static_cast<size_t>(i) != index)
arr[n++] = j;
It would be nice to have utilized at least a separate list pointer, so you could build the new list while simultaneously deleting the old, but your class/struct isn't setup to use additional pointers. So you are basically left with buffering all values except the index to remove and then recreating your queue.

How to keep track of layers when traversing a binary tree?

If I need to print out each elements of a binary tree constructed with the struct below. How could I keep track of which layer of elements I am printing?
struct for a binary tree node
For example:
any binary tree
Expected output:
layer 0: 12
layer -1: 28 19
layer -2: 94 32
layer -3: 65 18 72
Solution using queue based on GeeksForGeeks
#include <iostream>
#include <queue>
using namespace std;
// A Binary Tree Node
struct node
{
struct node *left;
int data;
struct node *right;
};
// Iterative method to do level order traversal
// line by line
void printLevelOrder(node *root)
{
// Base Case
if (root == NULL)
return;
// Create an empty queue for level order tarversal
queue<node *> q;
// Enqueue Root and initialize height
q.push(root);
int i = 0;
while (q.empty() == false)
{
cout << "layer " << i << ": ";
// nodeCount (queue size) indicates number
// of nodes at current lelvel.
int nodeCount = q.size();
// Dequeue all nodes of current level and
// Enqueue all nodes of next level
while (nodeCount > 0)
{
node *node = q.front();
cout << node->data << " ";
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
nodeCount--;
}
cout << endl;
--i;
}
}
// Utility function to create a new tree node
node *newNode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
// Driver program to test above functions
int main()
{
// Create binary tree
node *root = newNode(12);
root->left = newNode(28);
root->right = newNode(19);
root->left->left = newNode(94);
root->left->left->left = newNode(65);
root->left->left->right = newNode(18);
root->right->left = newNode(32);
root->right->left->right = newNode(72);
printLevelOrder(root);
return 0;
}
Solution using recursive function and helper function based on CrazyForCode:
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
void printLevel(node *, int);
int height(struct node *node);
/* Function to print level order traversal a tree*/
void printLevelOrder(struct node *root)
{
int h = height(root);
int i;
for (i = 1; i <= h; i++){
printf("layer %d: ",i*-1+1);
printLevel(root, i);
cout << endl;
}
}
/* Print nodes at a given level */
void printLevel(struct node *root, int level)
{
if (root == NULL)
return;
if (level == 1)
{
printf("%d ", root->data);
}
else if (level > 1)
{
printLevel(root->left, level - 1);
printLevel(root->right, level - 1);
}
}
/* Compute the "height" of a tree */
int height(struct node *node)
{
if (node == NULL)
return 0;
else
{
int lheight = height(node->left);
int rheight = height(node->right);
if (lheight > rheight)
return (lheight + 1);
else
return (rheight + 1);
}
}
node *newNode(int data)
{
node *temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
// Create binary tree
node *root = newNode(12);
root->left = newNode(28);
root->right = newNode(19);
root->left->left = newNode(94);
root->left->left->left = newNode(65);
root->left->left->right = newNode(18);
root->right->left = newNode(32);
root->right->left->right = newNode(72);
printLevelOrder(root);
return 0;
}

I am getting a breakpoint and i do not know why

I am trying to implement a priority Queue by using a linked list in c++. However, when I run the program it triggers a breakpoint within "priorityQLinkedList::dequeue()" method. Can someone tell why this is the case and give me suggestions on how to fix it?
Code:
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
struct DAT
{
int id;
char fullname[50];
double savings;
};
struct NODE
{
DAT data;
NODE *N;
NODE *P;
NODE(const int i, const char *f, const double s)
{
data.id = i;
strcpy_s(data.fullname, f);
data.savings = s;
N = NULL;
P = NULL;
}
};
class priorityQLinkedList
{
private:
NODE *front;
NODE *back;
public:
priorityQLinkedList() { front = NULL; back = NULL; }
~priorityQLinkedList() { destroyList(); }
void enqueue(NODE *);
NODE* dequeue();
void destroyList();
};
void priorityQLinkedList::enqueue(NODE *n)
{
if (front == NULL) {
front = n;
back = n;
}
else {
NODE *temp = front;
if (n->data.id > temp->data.id)
{
front->P = n;
n->N = front;
front = n;
}
else
{
//search for the posistion for the new node.
while (n->data.id < temp->data.id)
{
if (temp->N == NULL) {
break;
}
temp = temp->N;
}
//New node id's smallest then all others
if (temp->N == NULL && n->data.id < temp->data.id)
{
back->N = n;
n->P = back;
back = n;
}
//New node id's is in the medium range.
else {
temp->P->N = n;
n->P = temp->P;
n->N = temp;
temp->P = n;
}
}
}
}
NODE* priorityQLinkedList::dequeue()
{
NODE *temp;
//no nodes
if (back == NULL) {
return NULL;
}
//there is only one node
else if (back->P == NULL) {
NODE *temp2 = back;
temp = temp2;
front = NULL;
back = NULL;
delete temp2;
return temp;
}
//there are more than one node
else {
NODE *temp2 = back;
temp = temp2;
back = back->P;
back->N = NULL;
delete temp2;
return temp;
}
}
void priorityQLinkedList::destroyList()
{
while (front != NULL) {
NODE *temp = front;
front = front->N;
delete temp;
}
}
void disp(NODE *m) {
if (m == NULL) {
cout << "\nQueue is Empty!!!" << endl;
}
else {
cout << "\nID No. : " << m->data.id;
cout << "\nFull Name : " << m->data.fullname;
cout << "\nSalary : " << setprecision(15) << m->data.savings << endl;
}
}
int main() {
priorityQLinkedList *Queue = new priorityQLinkedList();
NODE No1(101, "Qasim Imtiaz", 567000.0000);
NODE No2(102, "Hamad Ahmed", 360200.0000);
NODE No3(103, "Fahad Ahmed", 726000.0000);
NODE No4(104, "Usmaan Arif", 689000.0000);
Queue->enqueue(&No4);
Queue->enqueue(&No3);
Queue->enqueue(&No1);
Queue->enqueue(&No2);
disp(Queue->dequeue());
disp(Queue->dequeue());
disp(Queue->dequeue());
disp(Queue->dequeue());
disp(Queue->dequeue());
delete Queue;
return 0;
}
One problem which stands out in your dequeue() method is that you are calling delete on a NODE pointer, and then attempting to return this deleted pointer to the caller. This could cause an error either in dequeue() itself, or certainly in the caller who thinks he is getting back a pointer to an actual live NODE object.
One potential fix would be to create a copy of the NODE being dequeued. You would still remove the target from your list, but the caller would then be returned a valid pointer, which he could free later.
NODE* priorityQLinkedList::dequeue()
{
NODE *temp;
// no nodes
if (back == NULL) {
return NULL;
}
NODE *temp2 = back;
temp = new NODE(temp2->data.id, temp2->data.fullname, temp2->data.savings);
// there is only one node
else if (back->P == NULL) {
front = NULL;
back = NULL;
delete temp2;
return temp;
}
// there are more than one node
else {
back = back->P;
back->N = NULL;
delete temp2;
return temp;
}
}
You're deleting pointers in dequeue that priorityQLinkedList does not own, so you don't know if it is safe to delete them.
In this case, they are not since the node pointers passed to enqueue are addresses of local, stacked based variables and have not been allocated by new. (There's also the already mentioned problem of deleting a pointer then returning it, which is Undefined Behavior.)
The fix for the code as shown is to remove the calls to delete in dequeue. However, if changes are made so that the nodes passed to enqueue are dynamically allocated, you'll need to add something to handle that.
1.First change strcpy_s to strcpy is struct NODE.
2.Instead of Delete(temp2) use temp2--.
//no nodes
if (back == NULL) {
return NULL;
}
//there is only one node
else if (back->P == NULL) {
NODE *temp2 = back;
temp = temp2;
front = NULL;
back = NULL;
temp2--;
return temp;
}
//there are more than one node
else {
NODE *temp2 = back;
temp = temp2;
back = back->P;
back->N = NULL;
temp2--;
return temp;
}
I hope this will resolve the problem.

Deque implementation based on doubly linked list not working

here is my code for deque implementation using doubly linked list. It is not working..can u get me some pointers on where i am going wrong. The pointers are not initialised and the code is getting stuck in the addqatend function.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include<time.h>
#include<math.h>
#include<ctype.h>
#include <malloc.h>
#include <Windows.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
void initqueue(struct queue *);
void addqatbeg(struct queue *,int);
void addqatend(struct queue *, int);
int delqatbeg(struct queue *);
int delqatend(struct queue *);
void delqueue(struct queue *);
int main()
{
struct queue a;
int i;
system("cls");
initqueue(&a);
addqatbeg(&a,11);
addqatbeg(&a,23);
addqatbeg(&a,-5);
addqatbeg(&a,45);
addqatend(&a,34);
addqatend(&a,78);
i = delqatbeg(&a);
if(i!=NULL)
printf("item deleted from front:%d",i);
i = delqatbeg(&a);
if(i!=NULL)
printf("item deleted from front:%d",i);
i = delqatend(&a);
if(i!=NULL)
printf("item deleted from end:%d",i);
i = delqatend(&a);
if(i!=NULL)
printf("item deleted from end:%d",i);
delqueue(&a);
system("pause");
return 0;
}
//initialise the queue
void initqueue(struct queue *q)
{
q->front = q->rear = NULL;
}
//add at the beginning of the queue
void addqatbeg(struct queue *q,int item)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
//temp->data = item;
//temp->link = NULL;
if(q->front == NULL)
{
q->rear = q->front = temp;
q->front->prev = NULL;
q->front->next = NULL;
q->rear->next = NULL;
q->rear->prev = NULL;
return;
}
q->front->data = item;
q->front->next = temp;
q->front = temp;
q->front->prev = q->front;
//q->rear->next = q->front->next;
//q->front->prev = q->rear;
}
//add at the end of the queue
void addqatend(struct queue *q, int item)
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
if(q->front == NULL)
{
q->rear = q->front = temp;
q->front->prev = NULL;
q->front->next = NULL;
return;
}
while(q->front->next!=NULL)
q->rear = q->front->next;
q->rear->data = item;
q->rear->next =temp;
q->rear = q->rear->next;
q->rear->prev = q->rear;
}
//delete at the beginning of the queue
int delqatbeg(struct queue *q)
{
struct node *temp;
int item;
if(q->front ==NULL)
{
printf("queue is empty:\n");
return NULL;
}
item = q->front->data;
temp = q->front;
q->front = q->front->next;
free(temp);
return item;
}
//delete at the end of the queue
int delqatend(struct queue *q)
{
struct node *temp;
int item;
if(q->rear == NULL)
{
printf("queue is empty\n");
return NULL;
}
item = q->rear->data;
temp = q->rear;
q->rear = q->rear->prev;
free(temp);
return item;
}
//free the nodes
void delqueue(struct queue *q)
{
struct node *temp;
if(q->front == NULL)
return;
while(q->front!=NULL)
{
temp = q->front;
q->front = q->front->next;
free(temp);
}
}
There are various issues here.
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string.h>
#include<time.h>
#include<math.h>
#include<ctype.h>
#include <malloc.h>
#include <Windows.h>
Ugh, that's a whole slew of includes. What language are you using? Make up your mind. <iostream> is a C++ header, the rest are C headers. What is <malloc.c>? Usually, malloc should be in <stdlib.h> or <windows.h>. For this example, you just need <stdlib.h> and <stdio.h>.
If I switch on compiler warnings, I get the warnings about comparing and assignig integers to pointers. In delqatend you should return plain 0 as error code, not NULL. likewise here:
i = delqatbeg(&a);
if (i != NULL)
printf("item deleted from front:%d", i);
The NULLshould be 0. (0 is a language representation of the null pointer as well as of null integers, but the macro NULL casts it to (void *), making it a pointer. here' i is an integer.) Also, please print a newline at the end of the string.
temp = (struct node *) malloc(sizeof(struct node));
if (temp == NULL)
printf("queue is full\n");
I like how the queue is full if your job is out of memory, but you shouldn't just print that but do domething else like aborting the process or returning an error code, otherwise you'll do something bad later on in the function to a NULL pointer.
Okay, now to your main problem. Let's look at your function addqatbeg. In the case where the queue is empty you insert the node correctly, although you essentially do the same assignment twice, because q->front and q->end are equal. But you don't assign the data to the new node.
The other case, where there is already a node in the queue, is a mess. You don't isert nodes by jiggling around their data, but by reornagising the queue structure through the pointers. Here's a better addqatbeg:
//add at the beginning of the queue
void addqatbeg(struct queue *q, int item)
{
struct node *temp;
// Create node and check for NULL
temp = (struct node *) malloc(sizeof(struct node));
if (temp == NULL) {
printf("queue is full\n");
return;
}
// Assign data
temp->data = item;
temp->prev = temp->next = NULL;
// Insert node
if (q->front == NULL) {
q->rear = q->front = temp;
} else {
temp->next = q->front;
q->front->prev = temp;
q->front = temp;
}
}
It should be easy to implement addqatend now. The functions addqatend and addqatbeg are totally analogous in your case, bacause you maintain forward and backward pointers. The while loop in your addqatend is superflous and introduces an endless loop: You check q->front, but never update it in your loop.
Look if these functions work for you. Repeating patterns suggest some refactoring in common functions, but try this out.
Addqatbeg:
//add at the beginning of the queue
void addqatbeg(struct queue *q,int item)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
if(q->front == NULL)
{
q->rear = q->front = temp;
q->front->data = item;
return;
}
q->front->next = temp;
q->front = q->front->next;
q->front->data = item;
}
Addqatend:
//add at the end of the queue
void addqatend(struct queue *q, int item)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp == NULL)
printf("queue is full\n");
if(q->rear == NULL)
{
q->rear = q->front = temp;
q->rear->data = item;
return;
}
q->rear->prev = temp;
q->rear = q->rear->prev;
q->rear->data = item;
}
Delqatbeg:
//delete at the beginning of the queue
int delqatbeg(struct queue *q)
{
struct node *temp;
int item;
if(q->front == NULL)
{
printf("queue is empty:\n");
return NULL;
}
item = q->front->data;
temp = q->front;
q->front = q->front->prev;
free(temp);
}
return item;
}
Delqatend:
//delete at the beginning of the queue
int delqatend(struct queue *q)
{
struct node *temp;
int item;
if(q->rear == NULL)
{
printf("queue is empty:\n");
return NULL;
}
item = q->rear->data;
temp = q->rear;
q->rear = q->rear->next;
free(temp);
return item;
}

Queue, front and rear not declared in scope

I'm trying to create a Queue program, but I keep getting errors that "front" and "rear" are not declared in this scope. Can anyone tell me what I'm doing wrong? Here is my code. I've comparing it to other code I've written, and I've declared them exactly the same way.
#include <iostream>
using namespace std;
class node{
public:
int data;
node *next;
node();
};
class que{
public:
node *front;
node *rear;
void enq(int a);
void deq();
void pq();
que();
};
que::que(){
front = NULL;
rear = NULL;
}
node::node(){
data = 0;
next = NULL;
}
void enq(int a){
node *temp;
temp = new node;
temp->data = a;
if(front == NULL && rear == NULL){
front = rear = temp;
}
else{
rear->next = temp;
rear = temp;
}
}
void deq(){
node *temp;
temp = front;
if(front == NULL)
return;
if(temp == rear)
front = rear = NULL;
else{
temp = temp->next;
}
delete temp;
}
void pq(){
node *curs;
curs = front;
if(front == NULL)
return;
while(1){
cout << curs->data;
if(curs->next == NULL)
break;
else
curs=curs->next;
}
}
int main(){
que *Q = new que;
return 0;
}
In your code, you start defining functions like:
void pq(){
But that's not part of the class, you have to say:
void deq::pq(){