SlideShare a Scribd company logo
1 of 5
Download to read offline
Please implement Stack using Array (capacity 100)
- Use template to be able to handle any types of data
- In main function, create two stacks (an integer type stack and character type stack). Test your
stacks with functions including push (), pop(), top(), isEmpty(), and size()
- Make a header file and a source file separately
- code should have no compile error
template
class ArrayStack {
private: // member data
enum { CAPACITY = 1000 }; // default capacity of stack
int capacity; // actual length of stack array
Object* S; // the stack array
int t; // index of the top of the stack
public:
//constructor given max capacity
ArrayStack(int cap = CAPACITY) {
capacity = cap;
S = new Object[capacity];
t = -1;
}
int size() const // number of elements in the stack
{
return(t + 1);
}
bool isEmpty() const // is the stack empty?
{
return(t < 0);
}
// return the top of the stack
Object& top() throw(StackEmptyException) {
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return S[t];
}
// push object onto the stack
void push(const Object& elem) throw(StackFullException) {
if (size() == capacity)
throw StackFullException("Stack overflow");
S[++t] = elem;
}
// pop the stack
Object pop() throw(StackEmptyException) {
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return S[t--];
}
ArrayStack(const ArrayStack& st); //copy constructor
// assignment operator constructor
ArrayStack& operator=(const ArrayStack& st);
~ArrayStack() // destructor
{delete[] S; }
};
template // copy constructor
ArrayStack::
ArrayStack(const ArrayStack& st) {
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++){ // copy contents
S[i] = st.S[i];
}
}
template // assignment operator
ArrayStack& ArrayStack::
operator=(const ArrayStack& st) {
if (this != &st) { //avoid self copy (x=x)
delete[] S; // delete old contents
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++) { // copy contents
S[i] = st.S[i];
}
}
return *this;
}
Solution
template
class ArrayStack {
private: // member data
enum { CAPACITY = 1000 }; // default capacity of stack
int capacity; // actual length of stack array
Object* S; // the stack array
int t; // index of the top of the stack
public:
//constructor given max capacity
ArrayStack(int cap = CAPACITY) {
capacity = cap;
S = new Object[capacity];
t = -1;
}
int size() const // number of elements in the stack
{
return(t + 1);
}
bool isEmpty() const // is the stack empty?
{
return(t < 0);
}
// return the top of the stack
Object& top() throw(StackEmptyException) {
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return S[t];
}
// push object onto the stack
void push(const Object& elem) throw (StackFullException) {
if (size() == capacity)
throw StackFullException("Stack overflow");
S[++t] = elem;
}
// pop the stack
Object pop() throw (StackEmptyException) {
if (isEmpty())
throw StackEmptyException("Access to empty stack");
return S[t--];
}
}
ArrayStack(const ArrayStack&st); //copy constructor
// assignment operator constructor
ArrayStack&operator=(const ArrayStack&st);
~ArrayStack() // destructor
{delete[] S; }
}
};
template // copy constructor
ArrayStack::
ArrayStack(const ArrayStack&st) {
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++){ // copy contents
S[i] = st.S[i];
}
}
template // assignment operator
ArrayStack& ArrayStack::
operator=(const ArrayStack&st) {
if (this != &st) { //avoid self copy (x=x)
delete[] S; // delete old contents
capacity = st.capacity;
t = st.t;
S = new Object[capacity];
for (int i = 0; i <= t; i++) { // copy contents
S[i] = st.S[i];
}
}
return *this;
}
int main()
{
ArrayStack a = new ArrayStack(10); // Stack can have at max 10 element
try{<<
cout<<"Stack size:"<

More Related Content

Similar to Please implement Stack using Array (capacity 100)- Use template to.pdf

computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Lalkamal2
 
Write a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdfWrite a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdf
footworld1
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
steviesellars
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdf
badshetoms
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
afgt2012
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
gertrudebellgrove
 

Similar to Please implement Stack using Array (capacity 100)- Use template to.pdf (20)

computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Please review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdfPlease review my code (java)Someone helped me with it but i cannot.pdf
Please review my code (java)Someone helped me with it but i cannot.pdf
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
 
Write a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdfWrite a class ArrayList that represents an array of integers. Init.pdf
Write a class ArrayList that represents an array of integers. Init.pdf
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdf
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 
Lec2
Lec2Lec2
Lec2
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.pptMO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
 
#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
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Templates
TemplatesTemplates
Templates
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Write a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdfWrite a program that converts an infix expression into an equivalent.pdf
Write a program that converts an infix expression into an equivalent.pdf
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx#include String.hpp#include ..Functionsfunctions.hpp.docx
#include String.hpp#include ..Functionsfunctions.hpp.docx
 

More from fashionscollect

In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdfIn the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
fashionscollect
 
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdfIn 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
fashionscollect
 
how slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdfhow slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdf
fashionscollect
 
How did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdfHow did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdf
fashionscollect
 
Describe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdfDescribe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdf
fashionscollect
 
With regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdfWith regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdf
fashionscollect
 
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdfWeaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
fashionscollect
 

More from fashionscollect (20)

Interpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdfInterpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdf
 
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdfIn the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
In the exclusivity case (Mayer et al., p. 849-853), given the pr.pdf
 
In which sentence is the underlined group of words a prepositional ph.pdf
In which sentence is the underlined group of words a prepositional ph.pdfIn which sentence is the underlined group of words a prepositional ph.pdf
In which sentence is the underlined group of words a prepositional ph.pdf
 
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdfIn 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
In 1905, the French biologist Lucien Cuénot discovered an interestin.pdf
 
Identify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdfIdentify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdf
 
how slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdfhow slavery build the united states economySolutionChattel sla.pdf
how slavery build the united states economySolutionChattel sla.pdf
 
How did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdfHow did Woodrow Wilson impact public administrationSolutionWo.pdf
How did Woodrow Wilson impact public administrationSolutionWo.pdf
 
Describe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdfDescribe the three most well-known types of ethical decision making .pdf
Describe the three most well-known types of ethical decision making .pdf
 
Central banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdfCentral banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdf
 
Cell division in the apical meristems Cell division in the apical me.pdf
Cell division in the apical meristems  Cell division in the apical me.pdfCell division in the apical meristems  Cell division in the apical me.pdf
Cell division in the apical meristems Cell division in the apical me.pdf
 
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdfAsteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
Asteroids X, Y, and Z have equal mass of 4.0 kg each, They orbit aro.pdf
 
Biochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdfBiochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdf
 
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
4. Ammonia and calcium chlorate react to yield nitrogen gas, water, a.pdf
 
With regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdfWith regard to the SDLC and the Systems Engineering processThe SD.pdf
With regard to the SDLC and the Systems Engineering processThe SD.pdf
 
What is the conjugate acid of each of the following Brønsted–Lowry b.pdf
What is the conjugate acid of each of the following Brønsted–Lowry b.pdfWhat is the conjugate acid of each of the following Brønsted–Lowry b.pdf
What is the conjugate acid of each of the following Brønsted–Lowry b.pdf
 
What is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdfWhat is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdf
 
What is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdfWhat is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdf
 
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdfWhat are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
 
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdfWeaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
Weaver Company Comparative Balance Sheet December 31, 2015 and 2014 2.pdf
 
13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf
 

Recently uploaded

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
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
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
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 

Please implement Stack using Array (capacity 100)- Use template to.pdf

  • 1. Please implement Stack using Array (capacity 100) - Use template to be able to handle any types of data - In main function, create two stacks (an integer type stack and character type stack). Test your stacks with functions including push (), pop(), top(), isEmpty(), and size() - Make a header file and a source file separately - code should have no compile error template class ArrayStack { private: // member data enum { CAPACITY = 1000 }; // default capacity of stack int capacity; // actual length of stack array Object* S; // the stack array int t; // index of the top of the stack public: //constructor given max capacity ArrayStack(int cap = CAPACITY) { capacity = cap; S = new Object[capacity]; t = -1; } int size() const // number of elements in the stack { return(t + 1); } bool isEmpty() const // is the stack empty? { return(t < 0); } // return the top of the stack Object& top() throw(StackEmptyException) { if (isEmpty()) throw StackEmptyException("Access to empty stack"); return S[t]; } // push object onto the stack
  • 2. void push(const Object& elem) throw(StackFullException) { if (size() == capacity) throw StackFullException("Stack overflow"); S[++t] = elem; } // pop the stack Object pop() throw(StackEmptyException) { if (isEmpty()) throw StackEmptyException("Access to empty stack"); return S[t--]; } ArrayStack(const ArrayStack& st); //copy constructor // assignment operator constructor ArrayStack& operator=(const ArrayStack& st); ~ArrayStack() // destructor {delete[] S; } }; template // copy constructor ArrayStack:: ArrayStack(const ArrayStack& st) { capacity = st.capacity; t = st.t; S = new Object[capacity]; for (int i = 0; i <= t; i++){ // copy contents S[i] = st.S[i]; } } template // assignment operator ArrayStack& ArrayStack:: operator=(const ArrayStack& st) { if (this != &st) { //avoid self copy (x=x) delete[] S; // delete old contents capacity = st.capacity; t = st.t; S = new Object[capacity]; for (int i = 0; i <= t; i++) { // copy contents
  • 3. S[i] = st.S[i]; } } return *this; } Solution template class ArrayStack { private: // member data enum { CAPACITY = 1000 }; // default capacity of stack int capacity; // actual length of stack array Object* S; // the stack array int t; // index of the top of the stack public: //constructor given max capacity ArrayStack(int cap = CAPACITY) { capacity = cap; S = new Object[capacity]; t = -1; } int size() const // number of elements in the stack { return(t + 1); } bool isEmpty() const // is the stack empty? { return(t < 0); } // return the top of the stack Object& top() throw(StackEmptyException) { if (isEmpty()) throw StackEmptyException("Access to empty stack"); return S[t]; }
  • 4. // push object onto the stack void push(const Object& elem) throw (StackFullException) { if (size() == capacity) throw StackFullException("Stack overflow"); S[++t] = elem; } // pop the stack Object pop() throw (StackEmptyException) { if (isEmpty()) throw StackEmptyException("Access to empty stack"); return S[t--]; } } ArrayStack(const ArrayStack&st); //copy constructor // assignment operator constructor ArrayStack&operator=(const ArrayStack&st); ~ArrayStack() // destructor {delete[] S; } } }; template // copy constructor ArrayStack:: ArrayStack(const ArrayStack&st) { capacity = st.capacity; t = st.t; S = new Object[capacity]; for (int i = 0; i <= t; i++){ // copy contents S[i] = st.S[i]; } } template // assignment operator
  • 5. ArrayStack& ArrayStack:: operator=(const ArrayStack&st) { if (this != &st) { //avoid self copy (x=x) delete[] S; // delete old contents capacity = st.capacity; t = st.t; S = new Object[capacity]; for (int i = 0; i <= t; i++) { // copy contents S[i] = st.S[i]; } } return *this; } int main() { ArrayStack a = new ArrayStack(10); // Stack can have at max 10 element try{<< cout<<"Stack size:"<