SlideShare a Scribd company logo
File name: a2.cpp
Task
For this assignment, you are required to either A) modify your linked list code to implement a
queue or B) modify the provided stack code to implement a queue. Your program should
implement a C++ queue class with the following member functions:
- Void enq(int)
- Void deq()
- Void front()
- Bool isEmpty()
- Void printq()
Your program will take in a command file called “cmd.txt” which will instruct your program
what operations to run
File Format
<0 or 1 arguments>
Commands
- 1 enq
- 2 deq
- 3 front
- 4 isEmpty
- 5 printq
Example File
1 1
5
1 2
5
1 3
5
1 4
5
3
2
5
2
5
3
Expectations
You should not use any already pre implemented code for your queue such as a standard library
object
Your code should be well formatted with proper spacing and proper naming
Your code should have well named variables. No a’s b’s or c’s as names unless it is for
something like a loop counter
Your code should have the same output formatting you see below
Your code MUST follow the formatting you see below
Your file MUST be named a2.cpp
My example file does not contain all possible test cases it is up to you to determine any other
special cases to test your program against.
Example Output
//LinkedList class
#include
#include
#include
#include
#include
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList(); //constructor
void prepend(int num);
void append(int num);
void removeFront();
void removeBack();
void remove(int num);
int search(int num);
void printList();
};
//constructor
LinkedList::LinkedList()
{
head=NULL;
tail=NULL;
}
//prepend() method
void LinkedList::prepend(int num)
{
//allocate space for new node
Node *node=new Node;
//populate node
node->data=num;
node->next= NULL;
//attach new node to start of list
//if linked list empty
if(head==NULL)
{
head=node;
tail=node;
}
else //linked list not empty
{
node->next=head;
head=node;
}
}
//append() method
void LinkedList::append(int num)
{
//allocate space for new node
Node *node=new Node;
//populate node
node->data=num;
node->next= NULL;
//attach new node to end of list
//if linked list empty
if(head==NULL)
{
head=node;
tail=node;
}
else //linked list not empty
{
tail->next=node;
tail=node;
}
}
//removeFront() method
void LinkedList::removeFront()
{
//if linked list empty, then underflow
if(head==NULL)
{
cerr<<"Underflow"<next;
//delete node
cout<<"Removing "<data<<" from the list"<next!=NULL)
{
/*if(curr==tail)
break;*/
prev=curr;
curr=curr->next;
}
prev->next=NULL;
tail=prev;
//delete node
cout<<"Removing "<data<<" from the list"<data=num)
break;
prev=curr;
curr=curr->next;
}
//preserve node
Node *temp=curr;
//detach node from linked list
prev->next=curr->next;
//delete node
cout<<"Removing "<data<<" from the list"<data=num)
{
found=1; //num found
break;
}
curr=curr->next;
}
return found;
}
//printList() method
void LinkedList::printList()
{
//traverse linked list
Node *curr=head;
while(curr!=NULL)
{
cout<data<<"->";
curr=curr->next;
}
}
---------------------------
main() method
------------------------------
//read data from file
string line;
while(getline(infile,line))
{
stringstream ss(line);
int op; //operation to be performed
ss>>op;
int num;
switch(op)
{
case 1:
ss>>num;
cout<<"Prepending "<>num;
cout<<"Appending "<>num;
if(ll.search(num)==1)
cout<<"Found "<>num;
ll.remove(num);
break;
}
}
infile.close();
return 0;
} Back K-- Front 1 Front Back 2 1 Front Back 3 2 1 K-- Front Back 4 3 2 1 Front Front 1 De q
1. Front Back 2 4 3 2 K- Deq Back 4 3 K-- Front Front 3 Queue is not empty Back 4 3 Front
Back 4 3 Front
Solution
#include
#include
#include
#include
#include
using namespace std;
struct Node
{
int data;
Node *next;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList(); //constructor
void prepend(int num);
void append(int num);
void removeFront();
void removeBack();
void remove(int num);
int search(int num);
void printList();
};
//constructor
LinkedList::LinkedList()
{
head=NULL;
tail=NULL;
}
//prepend() method
void LinkedList::prepend(int num)
{
//allocate space for new node
Node *node=new Node;
//populate node
node->data=num;
node->next= NULL;
//attach new node to start of list
//if linked list empty
if(head==NULL)
{
head=node;
tail=node;
}
else //linked list not empty
{
node->next=head;
head=node;
}
}
//append() method
void LinkedList::append(int num)
{
//allocate space for new node
Node *node=new Node;
//populate node
node->data=num;
node->next= NULL;
//attach new node to end of list
//if linked list empty
if(head==NULL)
{
head=node;
tail=node;
}
else //linked list not empty
{
tail->next=node;
tail=node;
}
}
//removeFront() method
void LinkedList::removeFront()
{
//if linked list empty, then underflow
if(head==NULL)
{
cerr<<"Underflow"<next;
//delete node
cout<<"Removing "<data<<" from the list"<next!=NULL)
{
/*if(curr==tail)
break;*/
prev=curr;
curr=curr->next;
}
prev->next=NULL;
tail=prev;
//delete node
cout<<"Removing "<data<<" from the list"<data=num)
break;
prev=curr;
curr=curr->next;
}
//preserve node
Node *temp=curr;
//detach node from linked list
prev->next=curr->next;
//delete node
cout<<"Removing "<data<<" from the list"<data=num)
{
found=1; //num found
break;
}
curr=curr->next;
}
return found;
}
//printList() method
void LinkedList::printList()
{
//traverse linked list
Node *curr=head;
while(curr!=NULL)
{
cout<data<<"->";
curr=curr->next;
}
}
---------------------------
main() method
------------------------------
//read data from file
string line;
while(getline(infile,line))
{
stringstream ss(line);
int op; //operation to be performed
ss>>op;
int num;
switch(op)
{
case 1:
ss>>num;
cout<<"Prepending "<>num;
cout<<"Appending "<>num;
if(ll.search(num)==1)
cout<<"Found "<>num;
ll.remove(num);
break;
}
}
infile.close();
return 0;
}

More Related Content

Similar to File name a2.cppTaskFor this assignment, you are required to ei.pdf

Background Circular Linked List A circular linked list is .pdf
Background Circular Linked List A circular linked list is .pdfBackground Circular Linked List A circular linked list is .pdf
Background Circular Linked List A circular linked list is .pdf
aaseletronics2013
 
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
arjunhassan8
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
^^^ Discuss about Header Node And also write a program for unorder.pdf
^^^ Discuss about Header Node  And also write a program for unorder.pdf^^^ Discuss about Header Node  And also write a program for unorder.pdf
^^^ Discuss about Header Node And also write a program for unorder.pdf
arihantmum
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
adityastores21
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdfUsing the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
udit652068
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
kavithaarp
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
Qestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdfQestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdf
arihantmobileselepun
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 

Similar to File name a2.cppTaskFor this assignment, you are required to ei.pdf (20)

Background Circular Linked List A circular linked list is .pdf
Background Circular Linked List A circular linked list is .pdfBackground Circular Linked List A circular linked list is .pdf
Background Circular Linked List A circular linked list is .pdf
 
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptxData Structures and Agorithm: DS 05 Doubly Linked List.pptx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
 
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf^^^Q2. Discuss about Header Node    And also write a program fo.pdf
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
 
Discuss about Header Node And also write a program for unordered si.pdf
Discuss about Header Node  And also write a program for unordered si.pdfDiscuss about Header Node  And also write a program for unordered si.pdf
Discuss about Header Node And also write a program for unordered si.pdf
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docxC++ please put everthing after you answer it- thanks Complete the stub.docx
C++ please put everthing after you answer it- thanks Complete the stub.docx
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
^^^ Discuss about Header Node And also write a program for unorder.pdf
^^^ Discuss about Header Node  And also write a program for unorder.pdf^^^ Discuss about Header Node  And also write a program for unorder.pdf
^^^ Discuss about Header Node And also write a program for unorder.pdf
 
maincpp Build and procees a sorted linked list of Patie.pdf
maincpp   Build and procees a sorted linked list of Patie.pdfmaincpp   Build and procees a sorted linked list of Patie.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
I need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdfI need to implment a function that can reverse a single linked list..pdf
I need to implment a function that can reverse a single linked list..pdf
 
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdfUsing the header(dlist) and mainline file (dlistapp) belowYou are .pdf
Using the header(dlist) and mainline file (dlistapp) belowYou are .pdf
 
Write a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdfWrite a program in C that does the followinga) Builds a simple li.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
Qestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdfQestion Please add pre-condition, post-conditions and descriptions .pdf
Qestion Please add pre-condition, post-conditions and descriptions .pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 

More from infomalad

Operating systems have to balance the conflicting goals of convenien.pdf
Operating systems have to balance the conflicting goals of convenien.pdfOperating systems have to balance the conflicting goals of convenien.pdf
Operating systems have to balance the conflicting goals of convenien.pdf
infomalad
 
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdfPARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
infomalad
 
On SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdfOn SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdf
infomalad
 
List the problems that can be efficiently solved by Evolutionary P.pdf
List the problems that can be efficiently solved by Evolutionary P.pdfList the problems that can be efficiently solved by Evolutionary P.pdf
List the problems that can be efficiently solved by Evolutionary P.pdf
infomalad
 
I need to get a 910 in order to pass this and I have 40 mins from n.pdf
I need to get a 910 in order to pass this and I have 40 mins from n.pdfI need to get a 910 in order to pass this and I have 40 mins from n.pdf
I need to get a 910 in order to pass this and I have 40 mins from n.pdf
infomalad
 
How innovative offerings gain acceptance within market segmentsH.pdf
How innovative offerings gain acceptance within market segmentsH.pdfHow innovative offerings gain acceptance within market segmentsH.pdf
How innovative offerings gain acceptance within market segmentsH.pdf
infomalad
 
How does an organization socialize an individualHow ATM(automated.pdf
How does an organization socialize an individualHow ATM(automated.pdfHow does an organization socialize an individualHow ATM(automated.pdf
How does an organization socialize an individualHow ATM(automated.pdf
infomalad
 
Homework Develop a strategylies) to get a house. Consider possible h.pdf
Homework Develop a strategylies) to get a house. Consider possible h.pdfHomework Develop a strategylies) to get a house. Consider possible h.pdf
Homework Develop a strategylies) to get a house. Consider possible h.pdf
infomalad
 
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdf
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdfExplain in detail the DTE-DCE TransmissionSolutionData commun.pdf
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdf
infomalad
 
Find an example of a real project with a real project manager. The pr.pdf
Find an example of a real project with a real project manager. The pr.pdfFind an example of a real project with a real project manager. The pr.pdf
Find an example of a real project with a real project manager. The pr.pdf
infomalad
 
As a result of total quality management, organizational changes in t.pdf
As a result of total quality management, organizational changes in t.pdfAs a result of total quality management, organizational changes in t.pdf
As a result of total quality management, organizational changes in t.pdf
infomalad
 

More from infomalad (20)

Operating systems have to balance the conflicting goals of convenien.pdf
Operating systems have to balance the conflicting goals of convenien.pdfOperating systems have to balance the conflicting goals of convenien.pdf
Operating systems have to balance the conflicting goals of convenien.pdf
 
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdfPARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
PARTI PHILOSOPHY AND CONCEPTS FLEXIBLE BENEFITS SYSTEM IMPLEMENTATION.pdf
 
On SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdfOn SQL Managment studioThis lab is all about database normalizatio.pdf
On SQL Managment studioThis lab is all about database normalizatio.pdf
 
Maria, an experienced shipping clerk, can fill a certain order in 11.pdf
Maria, an experienced shipping clerk, can fill a certain order in 11.pdfMaria, an experienced shipping clerk, can fill a certain order in 11.pdf
Maria, an experienced shipping clerk, can fill a certain order in 11.pdf
 
List the problems that can be efficiently solved by Evolutionary P.pdf
List the problems that can be efficiently solved by Evolutionary P.pdfList the problems that can be efficiently solved by Evolutionary P.pdf
List the problems that can be efficiently solved by Evolutionary P.pdf
 
Implement the insertFirst member function of the LinkedList class . .pdf
Implement the insertFirst member function of the LinkedList class . .pdfImplement the insertFirst member function of the LinkedList class . .pdf
Implement the insertFirst member function of the LinkedList class . .pdf
 
I need to get a 910 in order to pass this and I have 40 mins from n.pdf
I need to get a 910 in order to pass this and I have 40 mins from n.pdfI need to get a 910 in order to pass this and I have 40 mins from n.pdf
I need to get a 910 in order to pass this and I have 40 mins from n.pdf
 
If related parties complete a qualified like-kind exchange, how long.pdf
If related parties complete a qualified like-kind exchange, how long.pdfIf related parties complete a qualified like-kind exchange, how long.pdf
If related parties complete a qualified like-kind exchange, how long.pdf
 
How innovative offerings gain acceptance within market segmentsH.pdf
How innovative offerings gain acceptance within market segmentsH.pdfHow innovative offerings gain acceptance within market segmentsH.pdf
How innovative offerings gain acceptance within market segmentsH.pdf
 
How does an organization socialize an individualHow ATM(automated.pdf
How does an organization socialize an individualHow ATM(automated.pdfHow does an organization socialize an individualHow ATM(automated.pdf
How does an organization socialize an individualHow ATM(automated.pdf
 
Homework Develop a strategylies) to get a house. Consider possible h.pdf
Homework Develop a strategylies) to get a house. Consider possible h.pdfHomework Develop a strategylies) to get a house. Consider possible h.pdf
Homework Develop a strategylies) to get a house. Consider possible h.pdf
 
Given a definition of a group Given a definition of a group.pdf
Given a definition of a group Given a definition of a group.pdfGiven a definition of a group Given a definition of a group.pdf
Given a definition of a group Given a definition of a group.pdf
 
Given the indexed collection of denumerable sets A_i with i N, prov.pdf
Given the indexed collection of denumerable sets A_i with i  N,  prov.pdfGiven the indexed collection of denumerable sets A_i with i  N,  prov.pdf
Given the indexed collection of denumerable sets A_i with i N, prov.pdf
 
give an example of a successful progect thats ben done by a virtual .pdf
give an example of a successful progect thats ben done by a virtual .pdfgive an example of a successful progect thats ben done by a virtual .pdf
give an example of a successful progect thats ben done by a virtual .pdf
 
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdf
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdfExplain in detail the DTE-DCE TransmissionSolutionData commun.pdf
Explain in detail the DTE-DCE TransmissionSolutionData commun.pdf
 
Find an example of a real project with a real project manager. The pr.pdf
Find an example of a real project with a real project manager. The pr.pdfFind an example of a real project with a real project manager. The pr.pdf
Find an example of a real project with a real project manager. The pr.pdf
 
Discuss the importance of security and what elements need to be addr.pdf
Discuss the importance of security and what elements need to be addr.pdfDiscuss the importance of security and what elements need to be addr.pdf
Discuss the importance of security and what elements need to be addr.pdf
 
Define financial engineering in terms of how to change the risk-retu.pdf
Define financial engineering in terms of how to change the risk-retu.pdfDefine financial engineering in terms of how to change the risk-retu.pdf
Define financial engineering in terms of how to change the risk-retu.pdf
 
As a result of total quality management, organizational changes in t.pdf
As a result of total quality management, organizational changes in t.pdfAs a result of total quality management, organizational changes in t.pdf
As a result of total quality management, organizational changes in t.pdf
 
Complete and balance the following redox equation. Show all work and.pdf
Complete and balance the following redox equation. Show all work and.pdfComplete and balance the following redox equation. Show all work and.pdf
Complete and balance the following redox equation. Show all work and.pdf
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdfINU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
INU_CAPSTONEDESIGN_비밀번호486_업로드용 발표자료.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 

File name a2.cppTaskFor this assignment, you are required to ei.pdf

  • 1. File name: a2.cpp Task For this assignment, you are required to either A) modify your linked list code to implement a queue or B) modify the provided stack code to implement a queue. Your program should implement a C++ queue class with the following member functions: - Void enq(int) - Void deq() - Void front() - Bool isEmpty() - Void printq() Your program will take in a command file called “cmd.txt” which will instruct your program what operations to run File Format <0 or 1 arguments> Commands - 1 enq - 2 deq - 3 front - 4 isEmpty - 5 printq Example File 1 1 5 1 2 5 1 3 5 1 4 5 3 2 5 2 5 3
  • 2. Expectations You should not use any already pre implemented code for your queue such as a standard library object Your code should be well formatted with proper spacing and proper naming Your code should have well named variables. No a’s b’s or c’s as names unless it is for something like a loop counter Your code should have the same output formatting you see below Your code MUST follow the formatting you see below Your file MUST be named a2.cpp My example file does not contain all possible test cases it is up to you to determine any other special cases to test your program against. Example Output //LinkedList class #include #include #include #include #include using namespace std; struct Node { int data; Node *next; }; class LinkedList { private: Node *head; Node *tail; public: LinkedList(); //constructor void prepend(int num); void append(int num); void removeFront(); void removeBack(); void remove(int num);
  • 3. int search(int num); void printList(); }; //constructor LinkedList::LinkedList() { head=NULL; tail=NULL; } //prepend() method void LinkedList::prepend(int num) { //allocate space for new node Node *node=new Node; //populate node node->data=num; node->next= NULL; //attach new node to start of list //if linked list empty if(head==NULL) { head=node; tail=node; } else //linked list not empty { node->next=head; head=node; } } //append() method void LinkedList::append(int num) { //allocate space for new node
  • 4. Node *node=new Node; //populate node node->data=num; node->next= NULL; //attach new node to end of list //if linked list empty if(head==NULL) { head=node; tail=node; } else //linked list not empty { tail->next=node; tail=node; } } //removeFront() method void LinkedList::removeFront() { //if linked list empty, then underflow if(head==NULL) { cerr<<"Underflow"<next; //delete node cout<<"Removing "<data<<" from the list"<next!=NULL) { /*if(curr==tail) break;*/ prev=curr; curr=curr->next; } prev->next=NULL;
  • 5. tail=prev; //delete node cout<<"Removing "<data<<" from the list"<data=num) break; prev=curr; curr=curr->next; } //preserve node Node *temp=curr; //detach node from linked list prev->next=curr->next; //delete node cout<<"Removing "<data<<" from the list"<data=num) { found=1; //num found break; } curr=curr->next; } return found; } //printList() method void LinkedList::printList() { //traverse linked list Node *curr=head; while(curr!=NULL) { cout<data<<"->"; curr=curr->next; }
  • 6. } --------------------------- main() method ------------------------------ //read data from file string line; while(getline(infile,line)) { stringstream ss(line); int op; //operation to be performed ss>>op; int num; switch(op) { case 1: ss>>num; cout<<"Prepending "<>num; cout<<"Appending "<>num; if(ll.search(num)==1) cout<<"Found "<>num; ll.remove(num); break; } } infile.close(); return 0; } Back K-- Front 1 Front Back 2 1 Front Back 3 2 1 K-- Front Back 4 3 2 1 Front Front 1 De q 1. Front Back 2 4 3 2 K- Deq Back 4 3 K-- Front Front 3 Queue is not empty Back 4 3 Front Back 4 3 Front Solution #include #include #include #include
  • 7. #include using namespace std; struct Node { int data; Node *next; }; class LinkedList { private: Node *head; Node *tail; public: LinkedList(); //constructor void prepend(int num); void append(int num); void removeFront(); void removeBack(); void remove(int num); int search(int num); void printList(); }; //constructor LinkedList::LinkedList() { head=NULL; tail=NULL; } //prepend() method void LinkedList::prepend(int num) { //allocate space for new node Node *node=new Node; //populate node node->data=num;
  • 8. node->next= NULL; //attach new node to start of list //if linked list empty if(head==NULL) { head=node; tail=node; } else //linked list not empty { node->next=head; head=node; } } //append() method void LinkedList::append(int num) { //allocate space for new node Node *node=new Node; //populate node node->data=num; node->next= NULL; //attach new node to end of list //if linked list empty if(head==NULL) { head=node; tail=node; } else //linked list not empty { tail->next=node; tail=node;
  • 9. } } //removeFront() method void LinkedList::removeFront() { //if linked list empty, then underflow if(head==NULL) { cerr<<"Underflow"<next; //delete node cout<<"Removing "<data<<" from the list"<next!=NULL) { /*if(curr==tail) break;*/ prev=curr; curr=curr->next; } prev->next=NULL; tail=prev; //delete node cout<<"Removing "<data<<" from the list"<data=num) break; prev=curr; curr=curr->next; } //preserve node Node *temp=curr; //detach node from linked list prev->next=curr->next; //delete node cout<<"Removing "<data<<" from the list"<data=num)
  • 10. { found=1; //num found break; } curr=curr->next; } return found; } //printList() method void LinkedList::printList() { //traverse linked list Node *curr=head; while(curr!=NULL) { cout<data<<"->"; curr=curr->next; } } --------------------------- main() method ------------------------------ //read data from file string line; while(getline(infile,line)) { stringstream ss(line); int op; //operation to be performed ss>>op; int num; switch(op) { case 1: ss>>num; cout<<"Prepending "<>num;