forked from Dartpixel/Hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack using array.cpp
77 lines (70 loc) · 1.08 KB
/
Stack using array.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
typedef struct Node
{
int data;
struct Node* next;
} node;
node* top;
void push(int num)
{
node* temp;
temp=new Node();
temp->data=num;
temp->next=top;
top=temp;
}
void pop()
{
node* temp;
if (top==NULL){
cout<<"Stack Underflow\n";
return;
}
else{
temp=top;
top=top->next;
temp->next=NULL;
free(temp);
}
}
void display()
{
node* temp;
if (top==NULL){
cout<<"No element in Stack\n";
return;
}
else{
temp=top;
while (temp!=NULL){
if(temp->next!=NULL){
cout<<temp->data<<"-> ";
temp=temp->next;
}
else{
cout<<temp->data;
temp=temp->next;
}
}
}
}
int main()
{
push(10);
push(20);
push(30);
push(40);
display();
cout<<"\n";
pop();
pop();
push(50);
push(60);
push(70);
pop();
push(80);
display();
cout<<"\n";
return 0;
}