-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist_based_stack.cpp
More file actions
79 lines (67 loc) · 1.02 KB
/
Copy pathlinkedlist_based_stack.cpp
File metadata and controls
79 lines (67 loc) · 1.02 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
#include <iostream>
using namespace std;
class Node {
public:
int x;
Node* next;
Node(int k){
x = k;
}
};
class Stack {
public:
Node* head;
int l;
Stack(){
head = NULL;
l = 0;
}
void push (int k){ cout << "cool" << endl;
Node* sub = new Node(k);
l++;
if(head == NULL){
head = sub;
sub->next = NULL;
return;
}
Node* temp = head;
sub->next = temp;
head = sub;
}
int top(){
cout << "the first element of the stack is: " << endl;
return head->x;
}
void pop(){
cout << "popped" << endl;
head = head->next;
l--;
}
int len(){
cout << "Length of stack is: " << endl;
return l;
}
void print(){
Node* temp = head;
cout << "the current stack is: " << endl;
while(temp!=NULL){
cout << temp->x << endl;
temp = temp->next;
}
}
};
int main(){
Stack s;
for(int i=0; i<5; i++){
s.push(i);
}
s.print();
s.pop();
cout << s.len() << endl;
s.print();
s.pop();
cout << s.len() << endl;
s.print();
cout << s.top() << endl;
return 0;
}