You are on page 1of 4

Student Name: Mohammad Qassas

Subject: Data Structure


Professor: Hani Q. Al-Zoubi
Date: 3/1/2024

Homework: Stack class


#include <iostream>
#include <string>
#include <vector>
using namespace std;
class FullStack {
};
class EmptyStack {
};
template<class ItemType>
class StackType {
public:
StackType() {
top = -1;
}
bool IsEmpty() const {
return (top == -1);
}
bool IsFull() const {
return (top == MAX_ITEMS - 1);
}
void Push(ItemType newItem) {
if (IsFull())
throw FullStack();
top++;
items[top] = newItem;
}
void Pop() {
if (IsEmpty())
throw EmptyStack();
top--;
}
ItemType Top() {
if (IsEmpty())
throw EmptyStack();
return items[top];
}
private:
static const int MAX_ITEMS = 10;
int top;
ItemType items[MAX_ITEMS];
};
struct Student {
string name;
};
void DisplayStudentList(const StackType<Student>& studentStack) {
cout << "Student List:\n";
StackType<Student> tempStack = studentStack;

while (!tempStack.IsEmpty()) {
Student currentStudent = tempStack.Top();
cout << "Name: " << currentStudent.name << "\n";
tempStack.Pop();
}
cout << "----------------\n";
}
int main() {
StackType<Student> studentStack;
for (int i = 1; i <= 10; ++i) {
Student student;
student.name = "Student" + to_string(i);
studentStack.Push(student);
}
DisplayStudentList(studentStack);
cout << "Enter the number of students to delete: ";
int studentsToDelete;
cin >> studentsToDelete;

for (int i = 0; i < studentsToDelete; ++i) {


if (!studentStack.IsEmpty()) {
studentStack.Pop();
} else {
cout << "No more students to delete.\n";
break;
}
}
DisplayStudentList(studentStack);

return 0;
}

You might also like