-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackTut.cpp
More file actions
41 lines (35 loc) · 772 Bytes
/
StackTut.cpp
File metadata and controls
41 lines (35 loc) · 772 Bytes
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
#include <iostream>
using namespace std;
class Stack {
struct book {
struct book* next;
int val;
};
struct book *top;
public:
Stack(int num){
top = createNode(num);
}
struct node* createNode(int num){
struct book* newBook = new book;
book->val = num;
book->next = NULL;
return newBook;
}
void push(int num){
struct newNode = createNode(num);
newNode->next = top; // point to the top of the stack
top = newNode; // Update top
}
void pop(){
struct book* temp = top;
top = top->next; // Advance top
delete temp; // remove the old top
}
void peek() {
return top->val;
}
};
int main(){
return 0;
}