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

DOCX
Need help with writing the test cases for the following code in java-.docx
PDF
Implement the ADT stack by using an array stack to contain its entri.pdf
DOCX
@author Derek Harter @cwid 123 45 678 @class .docx
PDF
C program for array implementation of stack#include stdio.h.pdf
PDF
Were writing code for a project that dynamically allocates an arra.pdf
PDF
104332 sri vidhya eng notes
PDF
please tell me what lines do i alter to make this stack a queue. tel.pdf
PPT
computer notes - Data Structures - 8
Need help with writing the test cases for the following code in java-.docx
Implement the ADT stack by using an array stack to contain its entri.pdf
@author Derek Harter @cwid 123 45 678 @class .docx
C program for array implementation of stack#include stdio.h.pdf
Were writing code for a project that dynamically allocates an arra.pdf
104332 sri vidhya eng notes
please tell me what lines do i alter to make this stack a queue. tel.pdf
computer notes - Data Structures - 8

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

PPT
Stack Implementation
PDF
Please review my code (java)Someone helped me with it but i cannot.pdf
PDF
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
PDF
Write a class ArrayList that represents an array of integers. Init.pdf
PDF
STLStack.pdf
DOCX
Use a simple vector you created before to create two other more
PDF
C programming.   For this code I only need to add a function so th.pdf
PPT
Stack, queue and hashing
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
PPT
MO 2020 DS Stacks 1 AB.ppt
DOCX
#C programming Question 35Implement the functions required for the.docx
PPT
Java Generics
PPTX
Templates
PPT
01-intro_stacks.ppt
PPT
An Introduction to Stack Data Structures
PPT
introduction stacks in data structures and algorithms
PPTX
Stack and Queue
PDF
Write a program that converts an infix expression into an equivalent.pdf
PDF
LectureNotes-06-DSA
Stack Implementation
Please review my code (java)Someone helped me with it but i cannot.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Write a class ArrayList that represents an array of integers. Init.pdf
STLStack.pdf
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
Stack, queue and hashing
Array THE DATA STRUCTURE. ITS THE STRUCT
MO 2020 DS Stacks 1 AB.ppt
#C programming Question 35Implement the functions required for the.docx
Java Generics
Templates
01-intro_stacks.ppt
An Introduction to Stack Data Structures
introduction stacks in data structures and algorithms
Stack and Queue
Write a program that converts an infix expression into an equivalent.pdf
LectureNotes-06-DSA
Ad

More from fashionscollect (20)

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

Recently uploaded (20)

PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
Module on health assessment of CHN. pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Environmental Education MCQ BD2EE - Share Source.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
PDF
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
PPTX
Computer Architecture Input Output Memory.pptx
PDF
International_Financial_Reporting_Standa.pdf
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
B.Sc. DS Unit 2 Software Engineering.pptx
Unit 4 Computer Architecture Multicore Processor.pptx
My India Quiz Book_20210205121199924.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Module on health assessment of CHN. pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Environmental Education MCQ BD2EE - Share Source.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 2).pdf
BP 505 T. PHARMACEUTICAL JURISPRUDENCE (UNIT 1).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Mucosal Drug Delivery system_NDDS_BPHARMACY__SEM VII_PCI.pdf
Computer Architecture Input Output Memory.pptx
International_Financial_Reporting_Standa.pdf
Share_Module_2_Power_conflict_and_negotiation.pptx
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Introduction to pro and eukaryotes and differences.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)

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:"<