-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
83 lines (71 loc) · 1.45 KB
/
Stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include "ticket.h"
using namespace std;
class StackNode
{
public:
Ticket ticket;
StackNode *next;
StackNode(const Ticket &t)
: ticket(t), next(nullptr) {}
};
class Stack
{
private:
StackNode *top;
int size;
public:
Stack() : top(nullptr), size(0) {}
~Stack()
{
while (top != nullptr)
{
StackNode *temp = top;
top = top->next;
delete temp;
}
}
void push(const Ticket &ticket)
{
StackNode *newStackNode = new StackNode(ticket);
newStackNode->next = top;
top = newStackNode;
size++;
}
Ticket pop()
{
if (top == nullptr)
{
throw runtime_error("Stack is empty");
}
StackNode *temp = top;
Ticket ticket = temp->ticket;
top = top->next;
delete temp;
size--;
return ticket;
}
bool isEmpty() const
{
return top == nullptr;
}
int getSize() const
{
return size;
}
void display() const
{
StackNode *current = top;
while (current != nullptr)
{
cout << "Ticket ID: " << current->ticket.id
<< ", Customer: " << current->ticket.customerName
<< "\n";
current = current->next;
}
cout << "------------------------------------\n";
}
};
#endif