SlideShare a Scribd company logo
1 of 11
Download to read offline
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
 
^^^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
 
Unit7 C
Unit7 CUnit7 C
Unit7 C
 

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

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

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;