SlideShare a Scribd company logo
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:"<
Ad

Recommended

@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter @cwid 123 45 678 @class .docx
adkinspaige22
 
Stack and its applications
Stack and its applications
Ahsan Mansiv
 
My lecture stack_queue_operation
My lecture stack_queue_operation
Senthil Kumar
 
Algo>Stacks
Algo>Stacks
Ain-ul-Moiz Khawaja
 
Lec 4 Stack of Data Structures & Algorithms
Lec 4 Stack of Data Structures & Algorithms
haseebanjum2611
 
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).pdf
afgt2012
 
Stacks.ppt
Stacks.ppt
Waf1231
 
Stacks.ppt
Stacks.ppt
SupriyaGhosh43
 
Stacks
Stacks
FarithaRiyaz
 
Stack
Stack
Ubaid Ullah
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
ashokadyes
 
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
RAtna29
 
Stack
Stack
Swarup Kumar Boro
 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
SIGMATAX1
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptx
VandanaBharti21
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
Stack and queue
Stack and queue
Katang Isip
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
The Stack in data structures .ppt
The Stack in data structures .ppt
donemoremaregere376
 
Stacks
Stacks
Ashish Sethi
 
#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf
asif1401
 
Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
Stacks
Stacks
sardorbek mamazhanov
 
Stacks
Stacks
Temperory mukesh
 
Computer notes - Josephus Problem
Computer notes - Josephus Problem
ecomputernotes
 
Stacks and queue
Stacks and queue
Amit Vats
 
Interpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdf
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.pdf
fashionscollect
 

More Related Content

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

Stacks
Stacks
FarithaRiyaz
 
Stack
Stack
Ubaid Ullah
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
ashokadyes
 
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
RAtna29
 
Stack
Stack
Swarup Kumar Boro
 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
SIGMATAX1
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptx
VandanaBharti21
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
Stack and queue
Stack and queue
Katang Isip
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
The Stack in data structures .ppt
The Stack in data structures .ppt
donemoremaregere376
 
Stacks
Stacks
Ashish Sethi
 
#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf
asif1401
 
Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
Stacks
Stacks
sardorbek mamazhanov
 
Stacks
Stacks
Temperory mukesh
 
Computer notes - Josephus Problem
Computer notes - Josephus Problem
ecomputernotes
 
Stacks and queue
Stacks and queue
Amit Vats
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
ashokadyes
 
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
12650891 (1).ppthhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
RAtna29
 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
SIGMATAX1
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8
ecomputernotes
 
Week2-stacks-queues.pptx
Week2-stacks-queues.pptx
VandanaBharti21
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
Ch03_stacks_and_queues.ppt
Ch03_stacks_and_queues.ppt
OliverKane3
 
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
#include -algorithm- #include -cstdlib- #include -iostream- #include -.pdf
BANSALANKIT1077
 
The Stack in data structures .ppt
The Stack in data structures .ppt
donemoremaregere376
 
#includeiostream#includestdlib.husing namespace std;class .pdf
#includeiostream#includestdlib.husing namespace std;class .pdf
asif1401
 
Data Structure Lecture 2
Data Structure Lecture 2
Teksify
 
Computer notes - Josephus Problem
Computer notes - Josephus Problem
ecomputernotes
 
Stacks and queue
Stacks and queue
Amit Vats
 

More from fashionscollect (20)

Interpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdf
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.pdf
fashionscollect
 
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.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.pdf
fashionscollect
 
Identify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdf
fashionscollect
 
how 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.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 .pdf
fashionscollect
 
Central banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdf
fashionscollect
 
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.pdf
fashionscollect
 
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.pdf
fashionscollect
 
Biochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdf
fashionscollect
 
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.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.pdf
fashionscollect
 
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.pdf
fashionscollect
 
What is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdf
fashionscollect
 
What is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdf
fashionscollect
 
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.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.pdf
fashionscollect
 
13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf
fashionscollect
 
Interpersonal tactics, motivation, planning and leadership style can.pdf
Interpersonal tactics, motivation, planning and leadership style can.pdf
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.pdf
fashionscollect
 
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.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.pdf
fashionscollect
 
Identify and explain the characteristics that distinguish government.pdf
Identify and explain the characteristics that distinguish government.pdf
fashionscollect
 
how 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.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 .pdf
fashionscollect
 
Central banks in advanced economies have gradually moved away from m.pdf
Central banks in advanced economies have gradually moved away from m.pdf
fashionscollect
 
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.pdf
fashionscollect
 
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.pdf
fashionscollect
 
Biochemistry-Explain the differences in the ease of lateral movem.pdf
Biochemistry-Explain the differences in the ease of lateral movem.pdf
fashionscollect
 
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.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.pdf
fashionscollect
 
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.pdf
fashionscollect
 
What is the importance of phosphoryl-transfer potential and what fac.pdf
What is the importance of phosphoryl-transfer potential and what fac.pdf
fashionscollect
 
What is emphysema What effect does emphysema have on breathing Wha.pdf
What is emphysema What effect does emphysema have on breathing Wha.pdf
fashionscollect
 
What are cyclinsand How do cyclins oscillateSolutionCyclines.pdf
What are cyclinsand How do cyclins oscillateSolutionCyclines.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.pdf
fashionscollect
 
13. The auditors report on Other Financial Information (OFI) accomp.pdf
13. The auditors report on Other Financial Information (OFI) accomp.pdf
fashionscollect
 
Ad

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDM & Mia eStudios
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
Communicable Diseases and National Health Programs – Unit 9 | B.Sc Nursing 5t...
Communicable Diseases and National Health Programs – Unit 9 | B.Sc Nursing 5t...
RAKESH SAJJAN
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
RAKESH SAJJAN
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
RAKESH SAJJAN
 
Health Care Planning and Organization of Health Care at Various Levels – Unit...
Health Care Planning and Organization of Health Care at Various Levels – Unit...
RAKESH SAJJAN
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDM & Mia eStudios
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
Communicable Diseases and National Health Programs – Unit 9 | B.Sc Nursing 5t...
Communicable Diseases and National Health Programs – Unit 9 | B.Sc Nursing 5t...
RAKESH SAJJAN
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
RAKESH SAJJAN
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
RAKESH SAJJAN
 
Health Care Planning and Organization of Health Care at Various Levels – Unit...
Health Care Planning and Organization of Health Care at Various Levels – Unit...
RAKESH SAJJAN
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Ad

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