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(){
Related
#include <iostream>
using namespace std;
class List {
public:
struct node {
int data;
node *next;
};
node* head = NULL;
node* tail = NULL;
node* temp = NULL;
node* prev = NULL;
public:
void addNum(int num) {
temp = new node;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
}
void PrintList() {
temp = head;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
void DelNum(int num) {
temp = head;
while (temp != NULL) {
if (temp->data == num) {
prev->next = temp->next;
free(temp);
}
temp = prev;
temp = temp->next;
}
}
};
int main() {
List list;
list.addNum(1);
list.addNum(2);
list.addNum(3);
list.addNum(4);
list.addNum(5);
list.addNum(6);
list.DelNum(3);
list.PrintList();
return 0;
}
What is wrong with my DelNum function? When I run the program nothing pops up. Doesn't matter what number I put in.
As mss pointed out the problem is in your DelNum() function where you assign temp = prev;. In your initialization you defined that node* prev = NULL; So, prev = NULL at the point when you assigned it to temp which caused segmentation fault when you try to use it like temp = temp->next;.
Two main problems are there in DelNum function:
first, when you are in while loop
, you should assign
prev = temp;
second, when you have found your target element, after deleting it you have to break out of the loop, which isn't done in your code
below is your corrected code( also correction of some other corner case in DelNum function ):
#include <iostream>
using namespace std;
class List {
public:
struct node {
int data;
node *next;
};
node* head = NULL;
node* tail = NULL;
node* temp = NULL;
node* prev = NULL;
public:
void addNum(int num) {
temp = new node;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
cout<<num<<" is added \n";
}
void PrintList() {
temp = head;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
void DelNum(int num) {
if(head==NULL)//empty
{
cout<<"empty linked list, can't be deleted\n";
return;
}
if(head->next==NULL)//means only one element is left
{
if(head->data==num)
{
node * fordelete=head;
head=NULL;
cout<<num<<"is deleted\n";
delete(fordelete);
}
else
{
cout<<"not found , can't be deleted\n";
}
return;
}
temp = head; // when more than one element are there
prev = temp;
while (temp != NULL) {
if (temp->data == num) {
prev->next = temp->next;
free(temp);
cout<<num<<" is deleted\n";
break;
}
prev= temp;
temp = temp->next;
}
if(temp==NULL)
{
cout<<"not found, can't be deleted\n";
}
}
};
int main() {
List list;
list.addNum(1);
list.addNum(2);
list.addNum(3);
list.addNum(4);
list.addNum(5);
list.addNum(6);
list.PrintList();
list.DelNum(3);
list.DelNum(7);
list.PrintList();
return 0;
}
I hope it will help you.
struct Node
{
int data;
Node *next;
Node *prev;
};
class DoublyLinkedList
{
ofstream cout3;
Node *head;
public:
DoublyLinkedList()
{
head = NULL;
cout3.open("task3.out");
}
void insert(int num)
{
Node *temp = new Node;
//To insert if there are no elements
if(head == NULL)
{
temp->prev = NULL;
temp->data = num;
temp->next = NULL;
head = temp;
}
//To insert if there are elements
else
{
temp->prev = NULL;
temp->data = num;
temp->next = head;
head->prev = temp;
head = temp;
}
cout3<<"inserted "<<num<<endl;
}
void dele(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"cannot delete "<<num<<endl;
//To delete first element
else if (temp->prev == NULL)
{
head = temp->next;
(temp->next)->prev == NULL;
delete temp;
cout3<<"deleted "<<num<<endl;
}
//To delete last element
else if (temp->next == NULL)
{
(temp->prev)->next = NULL;
cout3<<"deleted "<<num<<endl;
delete temp;
}
//To delete any other element
else
{
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
cout3<<"deleted "<<num<<endl;
delete temp;
}
}
void search(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"not found "<<num<<endl;
else
cout3<<"found "<<num<<endl;
}
void display()
{
Node *temp = head;
while(temp != NULL)
{
cout3<<temp->data<<" ";
temp = temp->next;
}
cout3<<endl;
}
};
My implementation of Doubly Linked List.
I only insert at the beginning and delete the first occurrence of the number.
However if I want to delete the first element then it prints "deleted number" but when i display the number is still there.
Problem seems to be in my delete function but I cannot find what it is
See this line: (temp->next)->prev == NULL;
You wrote == instead of = , this seems to be the problem.
You dont show how you print the value but im guessing you move backward untill null value before you start..
Just expend the code to test it, it will give out the warning. Fix it, then the program will function as expected.
$ g++ test.cpp
test.cpp:66:30: warning: equality comparison result unused
[-Wunused-comparison]
(temp->next)->prev == NULL;
~~~~~~~~~~~~~~~~~~~^~~~~~~
test.cpp:66:30: note: use '=' to turn this equality comparison into an
assignment
(temp->next)->prev == NULL;
^~
=
1 warning generated.
test.cpp
#include <iostream>
#include <fstream>
struct Node
{
int data;
Node *next;
Node *prev;
};
class DoublyLinkedList
{
std::ofstream cout3;
Node *head;
public:
DoublyLinkedList()
{
head = NULL;
cout3.open("task3.out");
}
void insert(int num)
{
Node *temp = new Node;
//To insert if there are no elements
if(head == NULL)
{
temp->prev = NULL;
temp->data = num;
temp->next = NULL;
head = temp;
}
//To insert if there are elements
else
{
temp->prev = NULL;
temp->data = num;
temp->next = head;
head->prev = temp;
head = temp;
}
cout3<<"inserted "<<num<<std::endl;
}
void dele(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"cannot delete "<<num<<std::endl;
//To delete first element
else if (temp->prev == NULL)
{
head = temp->next;
(temp->next)->prev == NULL;
delete temp;
cout3<<"deleted "<<num<<std::endl;
}
//To delete last element
else if (temp->next == NULL)
{
(temp->prev)->next = NULL;
cout3<<"deleted "<<num<<std::endl;
delete temp;
}
//To delete any other element
else
{
(temp->prev)->next = temp->next;
(temp->next)->prev = temp->prev;
cout3<<"deleted "<<num<<std::endl;
delete temp;
}
}
void search(int num)
{
Node *temp = head;
int found_num = 0;
while(temp != NULL)
{
if(temp->data == num)
{
found_num = 1;
break;
}
else
temp = temp->next;
}
if(found_num == 0)
cout3<<"not found "<<num<<std::endl;
else
cout3<<"found "<<num<<std::endl;
}
void display()
{
Node *temp = head;
while(temp != NULL)
{
cout3<<temp->data<<" ";
temp = temp->next;
}
cout3<<std::endl;
}
};
int main()
{
DoublyLinkedList list;
list.insert(3);
list.insert(4);
list.insert(5);
list.display();
list.dele(3);
list.display();
}
In below link there is a guide to free the memory. Delete function not work mostly you can free method.
https://www.geeksforgeeks.org/write-a-function-to-delete-a-linked-list/
I'm learning C++, I try to implement simple singly linked list but the delete node part fails. I could not comprehend why this basic delete_node part is failing. It seems prev->set_next line in delete_node method does not working correctly. I tried to debug it too but failed to spot the error.
using namespace std; //ignore it for simplicity
class Node {
int data;
Node *next;
public:
Node() {}
void set_data(int a_data)
{
data = a_data;
}
void set_next(Node *a_next)
{
next = a_next;
}
int get_data()
{
return data;
}
Node* get_next()
{
return next;
}
};
class List {
Node *head;
public:
List()
{
head = NULL;
}
void print_list();
void append_node(int data);
void delete_node(int data);
};
void List::print_list()
{
Node *temp = head;
if(temp == NULL)
{
cout << "empty" << endl;
return;
}
if(temp->get_next() == NULL)
{
cout << temp->get_data() << "--->";
cout << "NULL" << endl;
}
else
{
do
{
cout << temp->get_data() << "+++>";
temp = temp->get_next();
} while(temp != NULL);
cout << "NULL" << endl;
}
}
void List::append_node(int data)
{
Node *new_node = new Node();
new_node->set_data(data);
new_node->set_next(NULL);
Node *temp = head;
if(temp != NULL)
{
while(temp->get_next()!=NULL)
{
temp = temp->get_next();
}
temp->set_next(new_node);
}
else
{
head = new_node;
}
}
void List::delete_node(int data)
{
Node *temp = head;
if(temp == NULL)
{
return;
}
else
{
Node *prev = NULL;
do
{
prev = temp;
if(temp->get_data() == data)
{
prev->set_next(temp->get_next());
delete temp;
break;
}
temp = temp->get_next();
} while(temp!=NULL);
}
}
int main()
{
List list;
list.append_node(10);
list.append_node(20);
list.append_node(30);
list.append_node(40);
list.append_node(50);
list.append_node(60);
list.delete_node(30); //
list.print_list();
return 0;
}
valgrind gives me following error.
==22232== Invalid read of size 8
==22232== at 0x400D38: Node::get_next() (20_1.cpp:25)
==22232== by 0x400A5E: List::print_list() (20_1.cpp:62)
==22232== by 0x400C6C: main (20_1.cpp:127)
==22232== Address 0x5abdd28 is 8 bytes inside a block of size 16 free'd
==22232== at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22232== by 0x400BA8: List::delete_node(int) (20
Lets take a closer look at these lines from the List::delete_node function
prev = temp;
if(temp->get_data() == data)
{
prev->set_next(temp->get_next());
delete temp;
break;
}
The first one make prev point to the very same node that temp is pointing to. After this prev == temp is true.
So when you do
prev->set_next(temp->get_next());
it is the same as
temp->set_next(temp->get_next());
That is, you make temp->next point to temp->next which doesn't change it at all. You never unlink the node from the list, but you do delete it. That makes your printing of the list invalid, as you will dereference a deleted node.
As a simple solution, you could do something like this:
if (head->get_data() == data)
{
// Special case: Head node is the one we want to delete
Node* old_head = head;
// Make the head be the second node in the list, if any
head = head->get_next();
// Delete the old head
delete old_head;
}
else
{
// We know it's not the head node of the list, use the "next" to find it
for (Node* node = head; node->get_next() != 0; node = node->get_next())
{
if (node->get_next()->get_data() == data)
{
// It's the "next" node we want to remove
Node* old_next = node->get_next();
// Unlink the node
node->set_next(node->get_next()->get_next());
delete old_next;
break;
}
}
}
The problem is that at the beginning of your do / while loop pointers temp and prev point to the same Node. Hence, you re-point the node, and then delete it right away.
A better approach is to not use prev at all. Get next, see if its data matches the one being deleted. If it does, "bypass" and delete next. Otherwise, move on to the next node until you hit NULL:
void List::delete_node(int data) {
if(head == NULL) {
return;
}
if (head->get_data() == data) {
Node *toDelete = head;
head = head->get_next();
delete toDelete;
return;
}
Node *temp = head;
for ( ; ; ) {
Node *next = temp->get_next();
if (next == null) {
break;
}
if (next->get_data() == data) {
temp->set_next(next->get_next());
delete next;
break;
}
temp = temp->get_next();
}
}
The exact working solution is
void List::delete_node(int data)
{
Node *temp = head;
Node *prev = NULL;
//first check whether its a parent element or not
if(temp && temp->get_data() == data){
head = head->get_next();
delete temp;
}
else{
while (temp){
if (temp->get_data() == data){
if (prev)
prev->set_next(temp->get_next());
delete temp;
return;
}
prev = temp;
temp = temp->get_next();
}
}
}
This even works for deleting head node
I see a number of problems with your code.
Your Node constructors is not initializing any of the Node members.
Your List class is missing a destructor to free any allocated nodes.
Your print_list() and append_node() implementations are a little more verbose than they need to be.
But, most importantly, regarding your particular question, your list's delete_node() method is not managing its prev variable correctly. prev is always pointing at the current node that is being looked at, not at the previous node that was already looked at. So you are not actually updating your links correctly when removing a node. You are also not updating the list's head member if the node being removed is the head node.
Try something more like this instead:
class Node;
class List {
Node *head;
public:
List();
~List();
void print_list();
void append_node(int data);
void delete_node(int data);
};
class Node {
int data;
Node *next;
public:
Node(int a_data = 0, Node *a_next = NULL);
void set_data(int a_data);
void set_next(Node *a_next);
int get_data();
Node* get_next();
friend class List;
};
Node::Node(int a_data, Node *a_next)
: data(a_data), next(a_next)
{
}
void Node::set_data(int a_data)
{
data = a_data;
}
void Node::set_next(Node *a_next)
{
next = a_next;
}
int Node::get_data()
{
return data;
}
Node* Node::get_next()
{
return next;
}
List::List()
: head(NULL)
{
}
List::~List()
{
Node *temp = head;
while (temp)
{
Node *next = temp->get_next();
delete temp;
temp = next;
}
}
void List::print_list()
{
Node *temp = head;
if (!temp)
{
cout << "empty" << endl;
return;
}
do
{
cout << temp->get_data();
temp = temp->get_next();
if (!temp) break;
cout << "+++>";
}
while (true);
cout << "--->NULL" << endl;
}
void List::append_node(int data)
{
Node **temp = &head;
while (*temp) temp = &((*temp)->next);
*temp = new Node(data);
}
void List::delete_node(int data)
{
Node *temp = head;
Node *prev = NULL;
while (temp)
{
if (temp->get_data() == data)
{
if (prev)
prev->set_next(temp->get_next());
if (temp == head)
head = temp->get_next();
delete temp;
return;
}
prev = temp;
temp = temp->get_next();
}
}
int main()
{
List list;
list.append_node(10);
list.append_node(20);
list.append_node(30);
list.append_node(40);
list.append_node(50);
list.append_node(60);
list.delete_node(30); //
list.print_list();
return 0;
}
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.
I am trying to impliment Queue using linked list but it goes stops unexpectidly.
could not find why?
#include <iostream>
#include <string>
using namespace std;
Class Node for creating a node.
class Node
{
public:
int data;
Node *next;
};
Queue Class containing operations for Queue.
class Queue{
private:
Node* front = NULL;
Node* rear = NULL;
public:
void enQueue(int x){
Node* temp = NULL;
temp->data = x;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = NULL;
return;
}
rear->next = temp;
rear = temp;
}
void dequeue()
{
Node* temp = front;
if(front == NULL)
{
cout << "No list found." << endl;
return;
}
if(front == rear){
front = rear = NULL;
}
else{
front = front->next;
}
delete temp;
}
};
main function goes here
int main(){
Queue a;
a.enQueue(45);
a.dequeue();
a.dequeue();
}
void enQueue(int x){
Node* temp = NULL; //Node* temp = new Node;
temp->data = x; //BOOM
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = NULL; //What?
return;
}
rear->next = temp;
rear = temp;
}
You are assigning to an invalid address.
This will only stop the program from "stopping unexpectidly". There are still bugs though.
The first time you enqueue a node, you're dereferencing a null pointer
void enQueue(int x){
Node* temp = NULL;
temp->data = x; // Wrong
dereferencing a null pointer yields undefined behavior.
There is an error in your enqueue function. Make the following changes:
void enQueue(int x){
Node* temp = new Node();//you need to create this variable and not set it to NULL
temp->data = x;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
Now your program will behave as expected