-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-In-LinkedList.cpp
More file actions
72 lines (59 loc) · 1.98 KB
/
Stack-In-LinkedList.cpp
File metadata and controls
72 lines (59 loc) · 1.98 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
#include <iostream>
#include <stack>
#include<ctime>
using namespace std;
struct Transaction {
int transId;
int fromId;
double amount;
time_t time;
};
struct Node {
stack<Transaction> transactions;
Node* prev;
Node* next;
};
void showTransactions(Node* head) {
Node* current = head;
int listNumber = 1;
while (current != nullptr) {
cout << "**List " << listNumber << " Transactions:**" << endl;
stack<Transaction> transStack = current->transactions;
while (!transStack.empty()) {
Transaction trans = transStack.top();
cout << "TransID: " << trans.transId << "\nFromID: " << trans.fromId << "\nAmount: " << trans.amount
<< "\nTime: " << ctime(&trans.time)<<endl;
transStack.pop();
}
cout << endl;
current = current->next;
listNumber++;
}
}
int main() {
Node* head = nullptr;
Node* tail = nullptr;
auto addTransaction = [&](int transId, int fromId, double amount, time_t time) {
Transaction newTransaction = {transId, fromId, amount, time};
if (head == nullptr) {
head = new Node{stack<Transaction>(), nullptr, nullptr};
head->transactions.push(newTransaction);
tail = head;
} else {
if (tail->transactions.size() < 3) {
tail->transactions.push(newTransaction);
} else {
Node* newNode = new Node{stack<Transaction>(), tail, nullptr};
newNode->transactions.push(newTransaction);
tail->next = newNode;
tail = newNode;
}
}
};
addTransaction(1, 101, 100, time(nullptr));
addTransaction(2, 102, 200, time(nullptr));
addTransaction(3, 103, 300, time(nullptr));
addTransaction(4, 104, 400, time(nullptr));
addTransaction(5, 105, 500, time(nullptr));
showTransactions(head);
}