-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
71 lines (51 loc) · 1.04 KB
/
stack.cpp
File metadata and controls
71 lines (51 loc) · 1.04 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
#include <iostream>
#include <string>
using namespace std;
/** structure of stack using linked-list **/
class Node{
public:
string input;
Node* next = NULL;
Node(string data) {
input = data;
};
};
/** push in stack **/
void push(Node* &head, string data) {
Node* n = new Node(data);
Node* temp = head;
if(temp == NULL) {
head = n;
return;
}
head = n;
n->next = temp;
}
/** pop in stack **/
void pop(Node* &head) {
head = head->next;
}
/** head pointer **/
void peek(Node* head) {
cout <<head->input <<" ";
}
/** traversal of nodes **/
void traverse(Node* head) {
Node* temp = head;
while(temp != NULL) {
cout << temp->input << " ";
temp = temp->next;
}
}
/** main function **/
int main() {
Node* head = NULL;
push(head, "hello");
push(head, "how");
push(head, "are");
push(head, "you");
// peek(head);
pop(head);
traverse(head);
return 0;
}