SlideShare a Scribd company logo
1 of 11
Download to read offline
#include
#include
using namespace std;
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
// Method to push an element x to stack1
void push1(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top1++;
arr[top1] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to push an element x to stack2
void push2(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top2--;
arr[top2] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to pop an element from first stack
int pop1()
{
if (top1 >= 0 )
{
int x = arr[top1];
top1--;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
// Method to pop an element from second stack
int pop2()
{
if (top2 < size)
{
int x = arr[top2];
top2++;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
};
/* Driver program to test twStacks class */
int main()
{
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is " << ts.pop1();
ts.push2(40);
cout << " Popped element from stack2 is " << ts.pop2();
return 0;
}
#include
#include
using namespace std;
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
// Method to push an element x to stack1
void push1(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top1++;
arr[top1] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to push an element x to stack2
void push2(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top2--;
arr[top2] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to pop an element from first stack
int pop1()
{
if (top1 >= 0 )
{
int x = arr[top1];
top1--;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
// Method to pop an element from second stack
int pop2()
{
if (top2 < size)
{
int x = arr[top2];
top2++;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
};
/* Driver program to test twStacks class */
int main()
{
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is " << ts.pop1();
ts.push2(40);
cout << " Popped element from stack2 is " << ts.pop2();
return 0;
}
Solution
#include
#include
using namespace std;
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
// Method to push an element x to stack1
void push1(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top1++;
arr[top1] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to push an element x to stack2
void push2(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top2--;
arr[top2] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to pop an element from first stack
int pop1()
{
if (top1 >= 0 )
{
int x = arr[top1];
top1--;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
// Method to pop an element from second stack
int pop2()
{
if (top2 < size)
{
int x = arr[top2];
top2++;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
};
/* Driver program to test twStacks class */
int main()
{
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is " << ts.pop1();
ts.push2(40);
cout << " Popped element from stack2 is " << ts.pop2();
return 0;
}
#include
#include
using namespace std;
class twoStacks
{
int *arr;
int size;
int top1, top2;
public:
twoStacks(int n) // constructor
{
size = n;
arr = new int[n];
top1 = -1;
top2 = size;
}
// Method to push an element x to stack1
void push1(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top1++;
arr[top1] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to push an element x to stack2
void push2(int x)
{
// There is at least one empty space for new element
if (top1 < top2 - 1)
{
top2--;
arr[top2] = x;
}
else
{
cout << "Stack Overflow";
exit(1);
}
}
// Method to pop an element from first stack
int pop1()
{
if (top1 >= 0 )
{
int x = arr[top1];
top1--;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
// Method to pop an element from second stack
int pop2()
{
if (top2 < size)
{
int x = arr[top2];
top2++;
return x;
}
else
{
cout << "Stack UnderFlow";
exit(1);
}
}
};
/* Driver program to test twStacks class */
int main()
{
twoStacks ts(5);
ts.push1(5);
ts.push2(10);
ts.push2(15);
ts.push1(11);
ts.push2(7);
cout << "Popped element from stack1 is " << ts.pop1();
ts.push2(40);
cout << " Popped element from stack2 is " << ts.pop2();
return 0;
}

More Related Content

Similar to #includeiostream#includestdlib.husing namespace std;class .pdf

New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
curwenmichaela
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
sreekanth3dce
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docxAdd functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
WilliamZnlMarshallc
 

Similar to #includeiostream#includestdlib.husing namespace std;class .pdf (20)

Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
Stack array
Stack arrayStack array
Stack array
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx#C programming Question 35Implement the functions required for the.docx
#C programming Question 35Implement the functions required for the.docx
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
 
Project of data structure
Project of data structureProject of data structure
Project of data structure
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
week-14x
week-14xweek-14x
week-14x
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
 
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docxAdd functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
Add functions push(int n- Deque &dq) and pop(Deque &dq)- Functions pus.docx
 

More from asif1401

(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
asif1401
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdfQuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
asif1401
 
import java.util.Scanner;public class Assignment4 {    public st.pdf
import java.util.Scanner;public class Assignment4 {    public st.pdfimport java.util.Scanner;public class Assignment4 {    public st.pdf
import java.util.Scanner;public class Assignment4 {    public st.pdf
asif1401
 

More from asif1401 (20)

the reaction of A to B since its Ea is the highes.pdf
                     the reaction of A to B since its Ea is the highes.pdf                     the reaction of A to B since its Ea is the highes.pdf
the reaction of A to B since its Ea is the highes.pdf
 
the one thing that comes to mind for me is about .pdf
                     the one thing that comes to mind for me is about .pdf                     the one thing that comes to mind for me is about .pdf
the one thing that comes to mind for me is about .pdf
 
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
(1) Acid rain occurs when the gases SO2 ,NOx react with water,oxygen.pdf
 
Iodine will replace both Cl and Br It will be SN2.pdf
                     Iodine will replace both Cl and Br It will be SN2.pdf                     Iodine will replace both Cl and Br It will be SN2.pdf
Iodine will replace both Cl and Br It will be SN2.pdf
 
The number of outcomes in the event A is 4 Yes event A is a s.pdf
 The number of outcomes in the event A is 4 Yes event A is a s.pdf The number of outcomes in the event A is 4 Yes event A is a s.pdf
The number of outcomes in the event A is 4 Yes event A is a s.pdf
 
There were very few planetary leftovers in this r.pdf
                     There were very few planetary leftovers in this r.pdf                     There were very few planetary leftovers in this r.pdf
There were very few planetary leftovers in this r.pdf
 
i have no idea. im just seeing if i get karma p.pdf
                     i have no idea. im just seeing if i get karma p.pdf                     i have no idea. im just seeing if i get karma p.pdf
i have no idea. im just seeing if i get karma p.pdf
 
Fe+3 solution is more acidic because it produces .pdf
                     Fe+3 solution is more acidic because it produces .pdf                     Fe+3 solution is more acidic because it produces .pdf
Fe+3 solution is more acidic because it produces .pdf
 
Copper chloride. Copper ions will form precipitat.pdf
                     Copper chloride. Copper ions will form precipitat.pdf                     Copper chloride. Copper ions will form precipitat.pdf
Copper chloride. Copper ions will form precipitat.pdf
 
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
   CCl4 molecule can give IR and Raman spectroscopy.   Selection r.pdf
 
User interface design processNavigation DesignInput DesignOu.pdf
User interface design processNavigation DesignInput DesignOu.pdfUser interface design processNavigation DesignInput DesignOu.pdf
User interface design processNavigation DesignInput DesignOu.pdf
 
True.it  is an example of an independent-measures designSolution.pdf
True.it  is an example of an independent-measures designSolution.pdfTrue.it  is an example of an independent-measures designSolution.pdf
True.it  is an example of an independent-measures designSolution.pdf
 
The main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdfThe main class of the tictoe game looks like.public class Main {.pdf
The main class of the tictoe game looks like.public class Main {.pdf
 
the inner electronsSolutionthe inner electrons.pdf
the inner electronsSolutionthe inner electrons.pdfthe inner electronsSolutionthe inner electrons.pdf
the inner electronsSolutionthe inner electrons.pdf
 
SolutionThe investees net income should be recorded as an increa.pdf
SolutionThe investees net income should be recorded as an increa.pdfSolutionThe investees net income should be recorded as an increa.pdf
SolutionThe investees net income should be recorded as an increa.pdf
 
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdfQuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
QuestionAnswer1TrueThe corpus callosum , otherwise called th.pdf
 
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdfNo. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
No. of Moles = Given MassMolecular Mass=3.0 x 101398= 3.06101.pdf
 
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdf
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdfLet the sample size be n ,Mean of the sample = 27 (same as pop.pdf
Let the sample size be n ,Mean of the sample = 27 (same as pop.pdf
 
JeffSolutionJeff.pdf
JeffSolutionJeff.pdfJeffSolutionJeff.pdf
JeffSolutionJeff.pdf
 
import java.util.Scanner;public class Assignment4 {    public st.pdf
import java.util.Scanner;public class Assignment4 {    public st.pdfimport java.util.Scanner;public class Assignment4 {    public st.pdf
import java.util.Scanner;public class Assignment4 {    public st.pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

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
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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
 
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
 
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.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
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...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
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...
 
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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
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Ữ Â...
 
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
 
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...
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

#includeiostream#includestdlib.husing namespace std;class .pdf

  • 1. #include #include using namespace std; class twoStacks { int *arr; int size; int top1, top2; public: twoStacks(int n) // constructor { size = n; arr = new int[n]; top1 = -1; top2 = size; } // Method to push an element x to stack1 void push1(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top1++; arr[top1] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to push an element x to stack2 void push2(int x) { // There is at least one empty space for new element
  • 2. if (top1 < top2 - 1) { top2--; arr[top2] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to pop an element from first stack int pop1() { if (top1 >= 0 ) { int x = arr[top1]; top1--; return x; } else { cout << "Stack UnderFlow"; exit(1); } } // Method to pop an element from second stack int pop2() { if (top2 < size) { int x = arr[top2]; top2++; return x; } else
  • 3. { cout << "Stack UnderFlow"; exit(1); } } }; /* Driver program to test twStacks class */ int main() { twoStacks ts(5); ts.push1(5); ts.push2(10); ts.push2(15); ts.push1(11); ts.push2(7); cout << "Popped element from stack1 is " << ts.pop1(); ts.push2(40); cout << " Popped element from stack2 is " << ts.pop2(); return 0; } #include #include using namespace std; class twoStacks { int *arr; int size; int top1, top2; public: twoStacks(int n) // constructor { size = n; arr = new int[n]; top1 = -1; top2 = size; }
  • 4. // Method to push an element x to stack1 void push1(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top1++; arr[top1] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to push an element x to stack2 void push2(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top2--; arr[top2] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to pop an element from first stack int pop1() { if (top1 >= 0 ) { int x = arr[top1];
  • 5. top1--; return x; } else { cout << "Stack UnderFlow"; exit(1); } } // Method to pop an element from second stack int pop2() { if (top2 < size) { int x = arr[top2]; top2++; return x; } else { cout << "Stack UnderFlow"; exit(1); } } }; /* Driver program to test twStacks class */ int main() { twoStacks ts(5); ts.push1(5); ts.push2(10); ts.push2(15); ts.push1(11); ts.push2(7); cout << "Popped element from stack1 is " << ts.pop1(); ts.push2(40);
  • 6. cout << " Popped element from stack2 is " << ts.pop2(); return 0; } Solution #include #include using namespace std; class twoStacks { int *arr; int size; int top1, top2; public: twoStacks(int n) // constructor { size = n; arr = new int[n]; top1 = -1; top2 = size; } // Method to push an element x to stack1 void push1(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top1++; arr[top1] = x; } else { cout << "Stack Overflow"; exit(1); }
  • 7. } // Method to push an element x to stack2 void push2(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top2--; arr[top2] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to pop an element from first stack int pop1() { if (top1 >= 0 ) { int x = arr[top1]; top1--; return x; } else { cout << "Stack UnderFlow"; exit(1); } } // Method to pop an element from second stack int pop2() { if (top2 < size) {
  • 8. int x = arr[top2]; top2++; return x; } else { cout << "Stack UnderFlow"; exit(1); } } }; /* Driver program to test twStacks class */ int main() { twoStacks ts(5); ts.push1(5); ts.push2(10); ts.push2(15); ts.push1(11); ts.push2(7); cout << "Popped element from stack1 is " << ts.pop1(); ts.push2(40); cout << " Popped element from stack2 is " << ts.pop2(); return 0; } #include #include using namespace std; class twoStacks { int *arr; int size; int top1, top2; public: twoStacks(int n) // constructor {
  • 9. size = n; arr = new int[n]; top1 = -1; top2 = size; } // Method to push an element x to stack1 void push1(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top1++; arr[top1] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to push an element x to stack2 void push2(int x) { // There is at least one empty space for new element if (top1 < top2 - 1) { top2--; arr[top2] = x; } else { cout << "Stack Overflow"; exit(1); } } // Method to pop an element from first stack
  • 10. int pop1() { if (top1 >= 0 ) { int x = arr[top1]; top1--; return x; } else { cout << "Stack UnderFlow"; exit(1); } } // Method to pop an element from second stack int pop2() { if (top2 < size) { int x = arr[top2]; top2++; return x; } else { cout << "Stack UnderFlow"; exit(1); } } }; /* Driver program to test twStacks class */ int main() { twoStacks ts(5); ts.push1(5); ts.push2(10);
  • 11. ts.push2(15); ts.push1(11); ts.push2(7); cout << "Popped element from stack1 is " << ts.pop1(); ts.push2(40); cout << " Popped element from stack2 is " << ts.pop2(); return 0; }