-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
60 lines (58 loc) · 1.3 KB
/
stack.cpp
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
*/ Code Contributed by Mohd Mawan Ahmad */
#include<iostream>
using namespace std;
typedef struct node{
int data;
struct node* link;
}Node;
Node* getNewNode(int data){
Node* node = (Node*) malloc(sizeof(Node));
node->data = data;
node->link = NULL;
return node;
}
void insertInStack(Node** top , int value){
if(top == NULL){
(*top) = getNewNode(value);
}else {
Node* node = getNewNode(value);
node->link = (*top);
(*top) = node;
}
}
int pop(Node** top){
int ele = (*top)->data;
(*top) = (*top)->link;
return ele;
}
bool isEmpty(Node** top){
if((*top) == NULL) return true;
return false;
}
void display(Node** top){
cout << "Normal Order of Elements " << endl;
Node* temp = (*top);
while(temp != NULL){
cout << temp->data << " ";
temp = temp->link;
}
}
void reverseDisplay(Node** top){
if(isEmpty(top)) return;
int ele = pop(top);
reverseDisplay(top);
cout << ele << " ";
}
int main(){
Node* top = NULL;
int n;
cout << "Enter Elements " << endl;
cin >> n;
for(int i = 0 ; i < n ; i++){
int ele;
cin >> ele;
insertInStack(&top , ele);
}
display(&top);
reverseDisplay(&top);
}