How to implement priority queue in C++? - c++

I am implementing a train boarding system using priority queue. I have the working code but I need to make the following changes..
The priority levels will be : High, Medium and Low. So the passenger should input his/her name and the priority level. The train can have up to 30 passengers.
In the end I will sort the passengers accordingly... Here what I have so far my problem is taking strings as arguments instead of the integers I have now.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
#define High 1
#define Medium 2
#define Low 3
/*
* Node Declaration
*/
struct node
{
int priority;
int info;
struct node *link;
};
/*
* Class Priority Queue
*/
class Priority_Queue
{
private:
node *front;
public:
Priority_Queue()
{
front = NULL;
}
/*
* Insert into Priority Queue
*/
void insert(int item, int priority)
{
node *tmp, *q;
tmp = new node;
tmp->info = item;
tmp->priority = priority;
if (front == NULL || priority < front->priority)
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while (q->link != NULL && q->link->priority <= priority)
q = q->link;
tmp->link = q->link;
q->link = tmp;
}
}
/*
* Delete from Priority Queue
*/
void del()
{
node *tmp;
if (front == NULL)
cout << "Queue Underflow\n";
else
{
tmp = front;
cout << "Deleted item is: " << tmp->info << endl;
front = front->link;
free(tmp);
}
}
/*
* Print Priority Queue
*/
void display()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "Queue is empty\n";
else
{
cout << "Queue is :\n";
cout << "Priority Item\n";
while (ptr != NULL)
{
cout << ptr->priority << " " << ptr->info << endl;
ptr = ptr->link;
}
}
}
};
/*
* Main
*/
int main()
{
int choice, item, priority;
Priority_Queue pq;
do
{
cout << "1.Insert\n";
cout << "2.Delete\n";
cout << "3.Display\n";
cout << "4.Quit\n";
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
cin >> priority;
pq.insert(item, priority);
break;
case 2:
pq.del();
break;
case 3:
pq.display();
break;
case 4:
break;
default:
cout << "Wrong choice\n";
}
} while (choice != 4);
return 0;
}

I would avoid using string values for the queue and just translate plain-text priorities to the respective integer value, think
int ParsePriority(string plainTextPriority)
{
switch(plainTextPriority) {
case "High": return 1;
case "Medium": return 2;
case "Low": return 3;
}
throw "Unknown priority class";
}
You'll then be able to use the priority values like you did before.
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
do {
string plainTextPriority;
cin >> plainTextPriority;
try {
priority = ParsePriority(plainTextPriority)
}
catch {
priority = 0;
cout << "Could not parse the priority, please enter one of High, Medium, Low" << endl;
}
} while (priority == 0);
pq.insert(item, priority);
break;
I added the loop to take the case that a value that does not represent a valid priority has been entered into account.

Here you can use mapping.
int main()
{
int choice, item;
string priority;
map<string,int>m;
m["High"]=1;
m["Low"]=3;
m["Medium"]=2;
Priority_Queue pq;
do
{
cout << "1.Insert\n";
cout << "2.Delete\n";
cout << "3.Display\n";
cout << "4.Quit\n";
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Input the item value to be added in the queue : ";
cin >> item;
cout << "Enter its priority : ";
cin >> priority;
pq.insert(item, m[priority]);//look change
//... rest of the code will same
and you need to include #include<map> also.
Thank you.

Related

C++ how to list all linked list data while also using loop to increment var which is for output coord position

#include <iostream>
#include <string.h>
#include <string>
#include <iomanip>
#include <cctype>
#include <ctype.h>
#include <fstream>
#include <cstdlib>
#include <windows.h>
using namespace std;
struct student{
string studentid,yearlevel,fullname,birthday,address,gender,course;
student *next;
};
void mainmenu(){
cout << " [I] Student Information System [I]" << endl;
cout << " | What do you want to do? |" << endl;
cout << " | |" << endl;
cout << " | 1. Add New Record |" << endl;
cout << " | 2. Search Record |" << endl;
cout << " | 3. Display All Records |" << endl;
cout << " | 4. Display Specific Record |" << endl;
cout << " | 5. Delete Record |" << endl;
cout << " | 6. Exit |" << endl;
cout << " ********************************" << endl;
cout << "\t\t\t\t\t Please type your selection:";
}
void border(){
cout <<"////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"<< endl;
}
void gotoxy(short x,short y){
COORD pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void printrecords(student *head){
bool x;
cout <<"\n\n";
//cout <<"Student-ID Fullname Gender Yearlevel Course Birthday Address \n";
system("CLS");
gotoxy(0,1);
cout<<"Student ID";
gotoxy(12,1);
cout<<"Full Name";
gotoxy(30,1);
cout<<"Gender";
gotoxy(39,1);
cout<<"Year Level";
gotoxy(51,1);
cout<<"Course";
gotoxy(61,1);
cout<<"Birthday";
gotoxy(71,1);
cout<<"Address\n\n";
while(head!=NULL){
for(int i=2;x != false;){
gotoxy(0,i);
cout<<head->studentid;
gotoxy(12,i);
cout<<head->fullname;
gotoxy(30,i);
cout<<head->gender;
gotoxy(39,i);
cout<<head->yearlevel;
gotoxy(51,i);
cout<<head->course;
gotoxy(61,i);
cout<<head->birthday;
gotoxy(71,i);
cout<<head->address;
head = head->next;
i++;
if (head = NULL){
x=false;
}
}
x = true;
cout <<"\n\n";
}
}
void addrecord(student **head){
//NEW NODE
student *newnode = new student;
cout <<"Enter Student ID: ";
cin >> newnode->studentid;
cout <<"\nEnter Fullname: ";
cin.ignore();
getline(cin,newnode->fullname);
cout <<"\nEnter Gender (M/F): ";
getline(cin,newnode->gender);
cout <<"\nEnter Yearlevel(1/2/3/4/5): ";
cin >> newnode->yearlevel;
cout <<"\nEnter Course (BS______): ";
cin.ignore();
getline(cin,newnode->course);
cout <<"\nEnter Birthday (MM/DD/YYYY): ";
getline(cin,newnode->birthday);
cout <<"\nEnter Address: ";
getline(cin,newnode->address);
newnode->next = NULL;
// CHECK IF LIST IS EMPTY
if(*head==NULL)
{
*head = newnode;
return;
}
//TRANSVERSING LIST
student *temp = *head;
while(temp->next!=NULL)
{
temp = temp->next;
}
//add the newnode at the end of the list
temp->next = newnode;
}
void searchrecord(string searchValue,student *head){
//TEMP NODE POINT TO HEAD
student* temp=head;
//DECLARE 2 VAR to: TRACK | SEARCH
int found = 0;
int i=0;
/*CHECK temp node if NULL else check node DATA with searchValue,
if found update and break;
else continue searching till temp node is not null */
if(temp != NULL) {
while(temp != NULL) {
i++;
if(temp->studentid == searchValue) {
found++;
break;
}
temp = temp->next;
}
if (found == 1) {
cout<<"\n "<<searchValue<<" Details:\n";
cout<<searchValue<<" is numbered "<<i<<"on the database.\n";
cout <<"Student-ID Fullname Gender Yearlevel Course Birthday Address \n";
while(temp!=NULL){
cout<<left<<setw(9)<<temp->studentid;
cout<<left<<setw(10)<<temp->fullname;
cout<<left<<setw(10)<<temp->gender;
cout<<left<<setw(10)<<temp->yearlevel;
cout<<left<<setw(10)<<temp->course;
cout<<left<<setw(10)<<temp->birthday;
cout<<left<<setw(10)<<temp->address<<endl;
temp = NULL;
cout<<"\n\n";
}
}
else {
cout<<searchValue<<" is not in the database.\n";
}
} else {
cout<<"Their is no data in the database yet...\n";
}
}
int main(){
int select;
string u,searchValue;
bool system=false;
student *head=NULL;
do{
border();
mainmenu();
cin >> select;
switch (select){
case 1: { //Add New Record
cout<<"Adding New Record\n\n";
addrecord(&head);
break;
}
case 2: { //Search Record
cout<<"Accessesing Database Records....\n";
cout<<"Input the Student-ID that you would want to search:\n";
cout<<"Student-ID: ";
cin>>searchValue;
searchrecord(searchValue,head);
break;
}
case 3: { //Display All Records
cout<<"Displaying All Records.....\n\n";
cout<<"Database Record(s)\n";
printrecords(head);
break;
}
case 4: { //Display Specific Record
cout<<"Ran case 4\n";
break;
}
case 5: { //Delete Record
cout<<"Ran case 5\n";
break;
}
case 6:{ //EXIT
cout<<"Ran case 6\n";
cout<<"Exiting Program Have a Good Day!";
system = true;
break;
}
default:{
cout <<"\nInvalid Input... \n Try Again...\n";
break;
}
} //end switch
}while(!system);
return 0;
} // end main
In printrecord I use gotoxy for the outputs position in cmd
when i input 1,1,1,1,1,1 and then second input 2,2,2,2,2,2 it only prints 1,1,1,1 and exits.....
I want the program to list all linked list data while also incrementing var i to position the output going down not overlapping eachother
input 1,1,1,1,1,1 input 2,2,2,2,2 input 3,3,3,3,3,3
I want the program to output all data and then exit. go back to main menu.

Queue implementation using Array in C++

I have implemented an integer type Queue using an array in C++ as:
This is not a circular queue so we don't have to bother for the empty space after filling it once and then deleting some values.
class IntQueue
{
private:
int *queueArray;
int queueSize;
int rear, front;
public:
IntQueue(int); // Constructor
~IntQueue(void);
void Enqueue(int);
int Dequeue(void);
bool isFull(void);
bool isEmpty(void);
void Display(void);
};
// Constructor
IntQueue::IntQueue(int size)
{
queueArray = new int[size];
queueSize = size;
rear = -1;
front = -1;
}
// Destructor
IntQueue::~IntQueue(void)
{
delete [] queueArray;
}
// Enqueue function
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
rear++;
queueArray[rear] = num;
}
}
// Dequeue function
int IntQueue::Dequeue()
{
int num;
if (isEmpty())
{
cout << "The Queue is empty.\n";
}
else
{
front++;
num = queueArray[front];
}
return num;
}
//isFull function
bool IntQueue::isFull(void)
{
bool status;
if (rear == queueSize - 1)
status = true;
else
status = false;
return status;
}
//isEmpty function
bool IntQueue::isEmpty(void)
{
bool status;
if (front == rear)
status = true;
else
status = false;
return status;
}
// Display function
void IntQueue::Display()
{
if(isEmpty())
{
cout << "The Queue is empty.\n";
}
for(int i = front++; i <= rear; i++)
{
cout << "queueArray[" << i << "] = " << queueArray[i] << endl;
}
}
// Main function
int main()
{
int option, queueCapacity, value;
cout << endl << "Enter size of queue: ";
cin >> queueCapacity;
IntQueue *queue = new IntQueue(queueCapacity);
Menu:
cout << endl << "Queue implementation using Array" << endl;
cout << "--------------------------------" << endl;
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display Queue" << endl;
cout << "4. Display Menu" << endl;
cout << "5. Exit" << endl;
cout << "--------------------------------" << endl;
YourOption:
cout << endl << "Your option: ";
cin >> option;
switch(option)
{
case 1:
// Enqueue
cout << "Enter value to enqueue: ";
cin >> value;
queue->Enqueue(value);
goto YourOption;
case 2:
// Dequeue
int data;
data = queue->Dequeue();
cout << data << " is dequeued!";
goto YourOption;
case 3:
// Display
queue->Display();
goto YourOption;
case 4:
goto Menu;
case 5:
exit(0);
default:
cout << "Invalid option!" << endl;
break;
}
delete queue;
return 0;
}
The variable rear is initially -1. I have checked isFull() function as if rear is equal to the size of Queue, then the queue is full, not otherwise. However, to check queue emptiness, I compared if rear == front, then empty, not otherwise. But after executing it, by opting 2 from the menu, it displays some random value sometimes and the message i.e., "The Queue is empty".
What is the problem in this example?
void enqueue(int x)
{
//cout << "Enter the DAta " << endl;
//cin >> x;
if (front==(rear+1)%size)
{
cout << "Queue Is overflow::" << endl;
return;
}
else if (rear==-1&&front==-1)
{
//cout << "enter the To EnQueue :" << endl;
front = rear = 0;
queue[rear] = x;
}
else if (rear==0 && front==0)
{
rear = rear + 1 % size;
queue[rear] = x;
}
else
{
rear++;
queue[rear] = x;
}
}
Your queue is working properly but you have need to check some variable values like in the Enqueue(int num) function you have forgot to update the variable front=0
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
rear++;
queueArray[rear] = num;
}
}
because at the initial time when you display the values front=-1.
So, add the front=0 in the enqueue function like this,
void IntQueue::Enqueue(int num)
{
if (isFull())
{
cout << "The Queue is full.\n";
}
else
{
front=0;
rear++;
queueArray[rear] = num;
}
}
Hope you got it. if you have any confusion comment it.

separating C++ code in to multiple files

I hope you are all doing well.
I have the below code which manages a printing queue. I need to separate it into the below files and I dont know how to this. Oddly i've never been asked to do this. Any help you can provide would be greatly appreciated. I am not sure what code goes in which file.
Files I need to separate my code into:
1. heap.h - declaration file for heap.
2. heap.cpp - implementation file for heap.
3. pqtype.h - declaration file for priority queue.
4. pqtype.cpp - implementation file for priority queue.
5. test.cpp - driver file.
My current Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct node
{
int priority;
int jNum;
string jName;
struct node *next;
};
class PriorityQueue
{
private:
node *front;
public:
PriorityQueue()
{
front = NULL;
}
void addJob(string item, int priority, int number)
{
node *temp, *q;
temp = new node;
temp->jName = item;
temp->priority = priority;
temp->jNum = number;
//whether queue is empty
if (front == NULL || priority < front->priority)
{
temp->next = front;
front = temp;
}
else
{
q = front;
while (q->next != NULL && q->next->priority <= priority)
q = q->next;
temp->next = q->next;
q->next = temp;
}
}
void printJob()
{
node *temp;
if (front == NULL)
cout << "There Are No Print Requests In The Queue.\n";
else
{
temp = front;
cout << "\nNow printing Request # " << temp->jNum << " " <<"For "<< temp->jName <<"\n"<< endl;
front = front->next;
free(temp);
}
}
void viewJob()
{
node *ptr;
ptr = front;
if (front == NULL)
cout << "There Are No Print Requests In The Queue\n\n";
else
{
while (ptr != NULL)
{
cout << "Job #: " << ptr->jNum << " " <<"For "<< ptr->jName <<"\n"<< endl;
ptr = ptr->next;
}
}
}
};
int main()
{
int choice, priority;
PriorityQueue pq;
char ch;
string jName;
int number = 0;
cout << "Printing Queue\n" << endl;
do
{
cout << "==============" << endl;
cout << "1. Add Job\n";
cout << "2. Print Job\n";
cout << "3. View Job\n";
cout << "4. Exit\n";
cout << "\nEnter Your Option Now : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Are You A Student (s or S), TA (t or T), Or Instructor (i or I)? ";
cin >> ch;
if (ch == 'i' || ch == 'I')
{
priority = 1;
jName = "Instructor";
cout << "\nJob Successfully Entered.\n\n";
}
else if (ch == 't' || ch == 'T')
{
priority = 2;
jName = "TA";
cout << "\nJob Successfully Entered.\n\n";
}
else if (ch == 's' || ch == 'S')
{
priority = 3;
jName = "Student";
cout << "\nJob Successfully Entered.\n\n";
}
number++;
pq.addJob(jName, priority, number);
break;
case 2:
pq.printJob();
break;
case 3:
pq.viewJob();
break;
case 4:
cout << "\nThank You For Using Printint Queue\nProgram Now Closing...\n\n";
break;
default:
cout << "\nInvalid Choice Selected! \n";
}
}
while (choice != 4);
system("PAUSE");
return 0;
}

How to create a doubly linked list of integers in ascending sorted order?

I'm having hard time to finish the code to get the following steps:
how to read in a file of positive integers (>0)?
how to create a doubly linked list of integers in ascending sorted order?
how to write a method to print the entire list in ascending or descending order (input parameter ā€˜Aā€™ or ā€˜Dā€™ tells direction to print)?
how notify the user with a message if the number to add is already in the list or if the number is not in the list if they to delete a number not in the list.
There's no error when i run the program, just trying to solve the 4 steps above.
Here's what I have done so far
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node
{
int number;
node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, int number);
void inser(node*&head, node*&last, int number);
void remove(node *&head, node *&last);
void showlist(node *current);
int numIntElement(ifstream&x);
int main() {
string filename;
cout << " Please enter the name of the file = ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
if (!myfile.is_open()) {
cout << endl << " failed to open file, check that it exists and you have access \n" << "\n" << endl;
}
else if (myfile.is_open())
{
ifstream x;
numIntElement(x);
cout << " File exists \n";
////
node * head = NULL;
node *last = NULL;
char choice;
int number;
do {
choice = menu();
switch (choice)
{
case '1':
cout << " Please enter number : ";
cin >> number;
inser(head, last, number);
break;
case '2':
remove(head, last);
case '3':
showlist(head);
break;
default:
break;
}
} while (choice != '4');
{
}
}
system("pause");
return 0;
}
int numIntElement(ifstream&x) {
int n = 0;
int m;
ifstream myfile("file.txt");
int countsingfie = 0;
while (!myfile.eof())
{
myfile >> n;
countsingfie += n;
if (countsingfie == 0) {
cout << "Error : file exist but no data " << endl;
}
cout << "total number " << countsingfie << "\n" << endl;
return countsingfie;
}
}
bool isEmpty(node *head) {
if (head == NULL) {
return true;
}
else
{
return false;
}
}
char menu() {
char choice;
cout << " Main Menu \n";
cout << " 1 . Add a number \n";
cout << " 2 . Delete a number from the list \n";
cout << " 3 . Show the list \n";
cout << " 4. Exit \n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, int number) {
node *temp = new node;
temp->number = number;
temp->next = NULL;
head = temp;
last = temp;
}
void inser(node*&head, node*&last, int number) {
if (isEmpty(head)>0){
insertAsFirstElement(head, last, number);
}
else if (isEmpty(head) < 0)
{
cout << " Please enter a number above 0 " << endl;
}
else
{
node *temp = new node;
temp->number = number;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void remove(node *&head, node *&last) {
if (isEmpty(head)) {
cout << " Sorry, The list is already empty \n";
}
else if (head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void showlist(node *current) {
if (isEmpty(current)) {
cout << " The list is empty \n";
}
else
{
cout << " The list contains: \n ";
while (current != NULL)
{
cout << current->number << endl;
current = current->next;
}
}
}

Error in my c++ code using linkedlist "Stop working" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
my code stop working in that test case, i think that the error in function Checktables but i'm not sure and i can't fix the error please help me to tun this code correctly.
image of a test case and the error
this is a cpp file with main .cpp
#include"Header.h"
string Name;
string namess;
customer::customer()
{
name = "";
gsize = status = 0;
next = NULL;
}
customer::customer(string name1, int gsize1, int status1)
{
name = name1;
gsize = gsize1;
status = status1;
next = NULL;
}
waitinglist::waitinglist()
{
chairnum =50 ;
totalcustomers = tables = 0;
head = tail = NULL;
}
waitinglist::waitinglist(int val)
{
chairnum = 50;
totalcustomers = 0;
tables = 0;
head = tail = NULL;
}
void waitinglist::change()
{
customer*temp ;
temp = head;
cout << "enter the name: ";
cin >> namess;
while (temp != NULL)
{
if (namess == temp->name)
{
if (temp->status==2)
{
temp->status=1;
cout << "done! " << endl ;
break ;
}
}
else if (namess != temp->name)
{
temp = temp->next;
}
}
if (temp == NULL)
{
cout << "can't found! " << endl;
}
}
void waitinglist::newcustomer()
{
customer*tmp = new customer;
cout << "enter the name: "; cin >> tmp->name;
customer*tmpo=new customer;
tmpo=head ;
while (tmpo != NULL)
{
if (tmp->name != tmpo->name)
{
tmpo = tmpo->next;
}
else if (tmp->name == tmpo->name)
{
cout<<"The Name already exist! " << endl ;
cout << "enter the name: "; cin >> tmp->name;
tmpo=head;
}
}
cout << "enter the group number: "; cin >> tmp->gsize;
cout << "enter the status: "; cin >> tmp->status;
if (head == NULL) // linkedlist is empty
{
head = tail = tmp;
totalcustomers++;
}
else
{
tail->next = tmp;
tail=tail->next;
totalcustomers++;
}
}
void waitinglist::checktables()
{
float c=5.00;
customer*temp=head;
customer*found;
cout<<"enter number of tables: ";
cin >> tables ;
while (tables>=1 && temp!=NULL)
{
int x;
float y;
y=((temp->gsize)/c);
x=(temp->gsize)/c;
if (tables<y)
{
temp=temp->next;
}
else if (tables>=y)
{
if (x==y)
{
tables=tables-x ; // Correct Table!
cout<<temp->name<<endl;
}
else if (x!=y)
{
tables=tables-(x+1);
cout<<temp->name<<endl;
}
found=temp ;
delete found; // Discard
break ;
}
}
}
void waitinglist::displayall()
{
customer *tmp;
tmp = head;
if (tmp == NULL)
{
cout << "Empty!";
}
while (tmp != NULL)
{
cout << "Name: " << tmp->name <<endl;
cout << "group number: " << tmp->gsize << endl;
tmp = tmp->next;
}
cout << endl;
}
void waitinglist::diplaycustomer()
{
customer*tmp;
tmp = head;
cout << "enter the name: ";
cin >> Name;
while (tmp != NULL)
{
if (Name == tmp->name)
{
cout << "the name : " << tmp->name << endl;
cout << "the group size = " << tmp->gsize << endl;
cout << "the status = " << tmp->status << endl;
break;
}
else if (Name != tmp->name)
{
tmp = tmp->next;
}
}
if (tmp == NULL)
{
cout << "can't found!" << endl;
}
}
int main()
{
int choice;
string name1 = "";
int gsize1 = 0;
int status1 = 0;
waitinglist mylist;
cout << "Note: 1 in status means the customer not here and 2 means the customer is here.\n";
cout << "Select your option.\n\n";
cout << "(1) Add a new Customer.\n";
cout << "(2) Display information based on Name.\n";
cout << "(3) List all Names.\n";
cout << "(4) to change the status. \n" ;
cout << "(5) Check tables by name. \n";
cout << "(6) quit. \n";
do
{
cout << "\n";
cout << "Enter your choice: --> ";
cin >> choice;
if (1 <= choice && choice <= 5)
{
switch (choice)
{
case 1:
mylist.newcustomer();
break;
case 2:
mylist.diplaycustomer();
break;
case 3:
mylist.displayall();
break;
case 4:
mylist.change() ;
break;
case 5 :
mylist.checktables();
break;
default:
cout << "Invalid choice. Enter again.\n\n";
break;
}
}
else if (choice>6)
{
cout << "Invalid choice. Enter again.\n\n";
break;
}
} while (choice != 6);
return 0;
}
and this is the header file .h
#include<iostream>
#include<string>
using namespace std;
class customer
{
public:
string name;
int gsize;
int status;
customer* next;
customer();
customer(string,int,int);
};
class waitinglist
{
public:
int tables; //number of occupied tables
int chairnum;
int totalcustomers;
customer*head,*tail;
waitinglist();
waitinglist(int);
void newcustomer();
void diplaycustomer();
void displayall();
void change () ;
void checktables();
};
One error is that your checktables function corrupts your linked list structure by calling delete on one of the nodes:
found = temp;
delete found; // Discard
What you've just done in those lines above is to have a linked list with a broken (invalid) link in it. Any functions that now traverses the list (like displaytables) will now hit the broken link, and things start to go haywire.
To delete a node from a linked list, you have to not just call delete, but adjust the link in waitinglist that used to point to that deleted node and have it point to the next node after the deleted one.
Think of it like a real chain -- if one of the links in the chain needs to be removed, you have to physically remove it, and hook the link before it to the next good link. You didn't do this step.
I won't write the code for that, but this is what you should have seen much earlier in the development of your program. Better yet would have been to write a singly-linked list class that adds and removes nodes correctly first. Test it, and then once it can add and remove nodes successfully without error, then use it in your larger program.