-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStacklinkedlist.cpp
More file actions
99 lines (83 loc) · 1.83 KB
/
Stacklinkedlist.cpp
File metadata and controls
99 lines (83 loc) · 1.83 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
#include <iostream>
#include <chrono>
using namespace std::chrono;
using namespace std;
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* head;
public:
Stack() {
head = nullptr;
}
void Push(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = head;
head = newNode;
}
int Pop() {
if (head == nullptr) {
cout << "Stack is empty!" << endl;
return -1;
}
int popValue = head->data;
Node* temp = head;
head = head->next;
delete temp;
return popValue;
}
int StackTop() {
if (head == nullptr) {
cout << "Stack is empty!" << endl;
return -1;
}
return head->data;
}
bool isEmpty() {
return (head == nullptr);
}
void Display() {
if (head == nullptr) {
cout << "Stack is empty!" << endl;
return;
}
Node* temp = head;
cout << "Stack elements: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};
int main() {
Stack s;auto start = high_resolution_clock::now();
s.Push(8);
s.Push(10);
s.Push(5);
s.Push(11);
s.Push(15);
s.Push(23);
s.Push(6);
s.Push(18);
s.Push(20);
s.Push(17);
s.Display();
cout << "Popped elements: ";
for (int i=0;i<5;i++){
cout << s.Pop() << " ";
}
cout << endl;
s.Display();
s.Push(4);
s.Push(30);
s.Push(3);
s.Push(1);
s.Display();auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: "<< duration.count() << " microseconds" << endl;
}