SlideShare a Scribd company logo
1 of 3
Download to read offline
Convert the stack into a queue in c++ and must have matching example testin code and output as
shown on next lines
Example Testing Code:
Stack S;
int tmp;
S.push(5);
S.peek(tmp);
cout << tmp << endl;
S.push(33);
S.push(1);
S.push(7);
S.peek(tmp);
cout << tmp << endl;
S.push(33);
S.push(12);
S.display( cout );
S.pop( );
S.display( cout );
S.push(14);
S.display( cout );
S.pop( );
S.pop( );
S.display( cout );
S.pop( );
S.pop( );
S.display( cout );
S.peek(tmp);
cout << tmp << endl;
Example Testing Output:
5
7
12 33 7 1 33 5
33 7 1 33 5
14 33 7 1 33 5
7 1 33 5
33 5
33
#include
#include
using namespace std;
// Stack implementation
class Stack {
private:
vector data;
public:
void push(int val) {
data.push_back(val);
}
void pop() {
if (!data.empty()) {
data.pop_back();
}
}
void peek(int& val) {
if (!data.empty()) {
val = data.back();
}
}
void display(ostream& os) {
for (int i = data.size() - 1; i >= 0; i--) {
os << data[i] << " ";
}
os << endl;
}
};
int main() {
Stack S;
int tmp;
S.push(5);
S.peek(tmp);
cout << tmp << endl;
S.push(33);
S.push(1);
S.push(7);
S.peek(tmp);
cout << tmp << endl;
S.push(33);
S.push(12);
S.display(cout);
S.pop();
S.display(cout);
S.push(14);
S.display(cout);
S.pop();
S.pop();
S.display(cout);
S.pop();
S.pop();
S.display(cout);
S.peek(tmp);
cout << tmp << endl;
return 0;
}

More Related Content

Similar to Convert the stack into a queue in c++ and must have matching example.pdf

5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
JawadTanvir
 

Similar to Convert the stack into a queue in c++ and must have matching example.pdf (20)

Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Verilog_Examples (1).pdf
Verilog_Examples (1).pdfVerilog_Examples (1).pdf
Verilog_Examples (1).pdf
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Stacks
StacksStacks
Stacks
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C language
C languageC language
C language
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++Formatted Console I/O Operations in C++
Formatted Console I/O Operations in C++
 
Phil Bartie QGIS PLPython
Phil Bartie QGIS PLPythonPhil Bartie QGIS PLPython
Phil Bartie QGIS PLPython
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
Oop1
Oop1Oop1
Oop1
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 

More from murtuzadahadwala3

Create an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdfCreate an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdf
murtuzadahadwala3
 
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdfCreate a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
murtuzadahadwala3
 
Create an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdfCreate an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdf
murtuzadahadwala3
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
murtuzadahadwala3
 
copyReverse.c code please do not change anything in the code bes.pdf
copyReverse.c code please do not change anything in the code bes.pdfcopyReverse.c code please do not change anything in the code bes.pdf
copyReverse.c code please do not change anything in the code bes.pdf
murtuzadahadwala3
 
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdfContabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
murtuzadahadwala3
 
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdfContaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
murtuzadahadwala3
 

More from murtuzadahadwala3 (20)

Create an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdfCreate an Executive Summary using the following report on investing .pdf
Create an Executive Summary using the following report on investing .pdf
 
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdfCreate a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
Create a ReportTopic Hudson Bay Mountain Estates (Smithers)The .pdf
 
Create an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdfCreate an Executive Summary using the following information for the .pdf
Create an Executive Summary using the following information for the .pdf
 
Create a python program that creates a database in MongoDB using API.pdf
Create a python program that creates a database in MongoDB using API.pdfCreate a python program that creates a database in MongoDB using API.pdf
Create a python program that creates a database in MongoDB using API.pdf
 
Create a Java application that uses card layout with four cards with.pdf
Create a Java application that uses card layout with four cards with.pdfCreate a Java application that uses card layout with four cards with.pdf
Create a Java application that uses card layout with four cards with.pdf
 
Create a Class Diagram for a Rectangle class that has one constructo.pdf
Create a Class Diagram for a Rectangle class that has one constructo.pdfCreate a Class Diagram for a Rectangle class that has one constructo.pdf
Create a Class Diagram for a Rectangle class that has one constructo.pdf
 
Crane Company began the month of June with 1,630 units in beginning .pdf
Crane Company began the month of June with 1,630 units in beginning .pdfCrane Company began the month of June with 1,630 units in beginning .pdf
Crane Company began the month of June with 1,630 units in beginning .pdf
 
Count the number of occurrences of an item in a matrix. Create a Pyt.pdf
Count the number of occurrences of an item in a matrix. Create a Pyt.pdfCount the number of occurrences of an item in a matrix. Create a Pyt.pdf
Count the number of occurrences of an item in a matrix. Create a Pyt.pdf
 
could you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdfcould you draw uml diagram for this code from PIL import Image, Im.pdf
could you draw uml diagram for this code from PIL import Image, Im.pdf
 
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdfCOUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
COUNTRY IS INDIA1. Describe the legal environment of chosen countr.pdf
 
convert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdfconvert the following C code to Mips assembly with steps and comment.pdf
convert the following C code to Mips assembly with steps and comment.pdf
 
copyReverse.c code please do not change anything in the code bes.pdf
copyReverse.c code please do not change anything in the code bes.pdfcopyReverse.c code please do not change anything in the code bes.pdf
copyReverse.c code please do not change anything in the code bes.pdf
 
Contrast the location of a food distributor and a supermarket. (The .pdf
Contrast the location of a food distributor and a supermarket. (The .pdfContrast the location of a food distributor and a supermarket. (The .pdf
Contrast the location of a food distributor and a supermarket. (The .pdf
 
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdfContabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
Contabilidad del iPhone en Apple Inc. El 21 de octubre de 2008, Appl.pdf
 
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdfContaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
Contaminaci�n en la cadena log�stica de productos agr�colas a granel.pdf
 
Constructing Entity Relationship Diagram University workshop case st.pdf
Constructing Entity Relationship Diagram University workshop case st.pdfConstructing Entity Relationship Diagram University workshop case st.pdf
Constructing Entity Relationship Diagram University workshop case st.pdf
 
Consider the international strategy of a current entrepreneurial ve.pdf
Consider the international strategy of a current  entrepreneurial ve.pdfConsider the international strategy of a current  entrepreneurial ve.pdf
Consider the international strategy of a current entrepreneurial ve.pdf
 
Consider the network shown in the attached picture. Assume Distance .pdf
Consider the network shown in the attached picture. Assume Distance .pdfConsider the network shown in the attached picture. Assume Distance .pdf
Consider the network shown in the attached picture. Assume Distance .pdf
 
Consider the DE PBC Article. Which of the following sources of capit.pdf
Consider the DE PBC Article. Which of the following sources of capit.pdfConsider the DE PBC Article. Which of the following sources of capit.pdf
Consider the DE PBC Article. Which of the following sources of capit.pdf
 
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdfComputer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
Computer Programming Task- Assembly Language- STM32F3 DISCOVERY.pdf
 

Recently uploaded

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)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
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
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
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
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
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)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Convert the stack into a queue in c++ and must have matching example.pdf

  • 1. Convert the stack into a queue in c++ and must have matching example testin code and output as shown on next lines Example Testing Code: Stack S; int tmp; S.push(5); S.peek(tmp); cout << tmp << endl; S.push(33); S.push(1); S.push(7); S.peek(tmp); cout << tmp << endl; S.push(33); S.push(12); S.display( cout ); S.pop( ); S.display( cout ); S.push(14); S.display( cout ); S.pop( ); S.pop( ); S.display( cout ); S.pop( ); S.pop( ); S.display( cout ); S.peek(tmp); cout << tmp << endl; Example Testing Output: 5 7 12 33 7 1 33 5 33 7 1 33 5 14 33 7 1 33 5 7 1 33 5
  • 2. 33 5 33 #include #include using namespace std; // Stack implementation class Stack { private: vector data; public: void push(int val) { data.push_back(val); } void pop() { if (!data.empty()) { data.pop_back(); } } void peek(int& val) { if (!data.empty()) { val = data.back(); } } void display(ostream& os) { for (int i = data.size() - 1; i >= 0; i--) { os << data[i] << " "; } os << endl; } }; int main() { Stack S; int tmp; S.push(5);
  • 3. S.peek(tmp); cout << tmp << endl; S.push(33); S.push(1); S.push(7); S.peek(tmp); cout << tmp << endl; S.push(33); S.push(12); S.display(cout); S.pop(); S.display(cout); S.push(14); S.display(cout); S.pop(); S.pop(); S.display(cout); S.pop(); S.pop(); S.display(cout); S.peek(tmp); cout << tmp << endl; return 0; }