-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2AUGUST.cpp
More file actions
102 lines (79 loc) · 2.27 KB
/
2AUGUST.cpp
File metadata and controls
102 lines (79 loc) · 2.27 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <ctime>
using namespace std;
const int MAX_TRANSACTIONS = 3;
struct Transaction {
int transactionId;
double amount;
time_t time;
};
struct Stack {
Transaction transactions[MAX_TRANSACTIONS];
int top=-1;
bool isEmpty() {
return top == -1;
}
bool isFull() {
return top == MAX_TRANSACTIONS - 1;
}
void push(const Transaction trans) {
if (!isFull()) {
transactions[++top] = trans;
}
}
void pop() {
if (!isEmpty()) {
--top;
}
}
const Transaction& peek(){
if (!isEmpty()) {
return transactions[top];
}
}
};
struct Node {
Stack transactions;
Node* next;
};
void showTransactions(Node* head) {
Node* current = head;
int listNumber = 1;
while (current != NULL) {
cout << "List " << listNumber << " Transactions:" << endl;
for (int i = 0; i <= current->transactions.top; ++i) {
Transaction& trans = current->transactions.transactions[i];
cout << "Transaction ID: " << trans.transactionId << "\nAmount: " << trans.amount
<< "\nTime: " << ctime(&trans.time);
}
cout << endl;
current = current->next;
listNumber++;
}
}
int main() {
Node* head = NULL;
Node* tail = NULL;
auto addTransaction = [&](int transactionId, double amount, time_t time) {
Transaction newTransaction = {transactionId, amount, time};
if (tail == NULL || tail->transactions.isFull()) {
Node* newNode = new Node;
newNode->transactions.top = -1;
newNode->next = NULL;
if (tail == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
tail->transactions.push(newTransaction);
};
addTransaction(1, 100.0, time(NULL));
addTransaction(2, 200.0, time(NULL));
addTransaction(3, 300.0, time(NULL));
addTransaction(4, 400.0, time(NULL));
addTransaction(5, 500.0, time(NULL));
showTransactions(head);
}