0% found this document useful (0 votes)
6 views2 pages

C++ Stack Implementation Example

This document defines a StackType class that implements a stack data structure. The class contains methods for checking if the stack is empty or full, pushing and popping items onto/off of the stack, and accessing the top item. The main function demonstrates initializing a StackType object, pushing 20 items onto the stack, popping an item if full, checking if empty, and destroying the StackType object.

Uploaded by

Berkay Özerbay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

C++ Stack Implementation Example

This document defines a StackType class that implements a stack data structure. The class contains methods for checking if the stack is empty or full, pushing and popping items onto/off of the stack, and accessing the top item. The main function demonstrates initializing a StackType object, pushing 20 items onto the stack, popping an item if full, checking if empty, and destroying the StackType object.

Uploaded by

Berkay Özerbay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;


#define MAX_ITEMS 20;

class StackType
{
public:
StackType();
~StackType();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType newItem);
void Pop();
void MakeEmpty();
ItemType Top() const;
private:
int top;
ItemType items[MAX_ITEMS];
};

StackType::StackType()
{
top = -1; }
StackType::~StackType(){

StackType::MakeEmpty(){
top=-1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
void StackType::Push(ItemType newItem)
{
if (IsFull())
throw FullStack();
top++;
items[top] = newItem;
}
void StackType::Pop()
{
if(IsEmpty())
throw EmptyStack();
top--; }
ItemType StackType::Top() const
{
if (IsEmpty())
throw EmptyStack();
return items[top];
}

void main(){
StackType S;
int i;
ItemType myItem;
cin>>myItem;
S::StackType();
for(i=0;i<20;i++){
[Link](myItem);
}
if(IsFull())
[Link]();
if(IsEmpty())
[Link]();
S.~StackType();
}

You might also like