-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30_linked_list_stack2.cpp
72 lines (67 loc) · 1.35 KB
/
30_linked_list_stack2.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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
int isEmpty(Node* top)
{
if (top == NULL)
{
return 1;
}
return 0;
}
int isFull(Node *top)
{
Node *p = new Node;
if (p == NULL)
{
return 1;
}
return 0;
}
Node* push(Node* top , int x){
if(isFull(top)){
cout<<"Stack Overflow . "<<endl;
}
else{
Node* n = new Node;
n->data = x;
n->next = top;
top = n;
return top;
}
}
void linkedListTraversal(Node* ptr){
while(ptr != NULL){
cout<<"Element : "<<ptr->data<<endl;
ptr = ptr->next;
}
}
int pop(Node* tp){
if(isEmpty(tp)){
cout<<"Stack Underflow . "<<endl;
}
else{
Node* n = tp;
top = (tp)->next;
int x = n->data;
delete(n);
return x;
}
}
};
Node* top = NULL;
int main()
{
top = top->push(top , 78);
top = top->push(top , 7);
top = top->push(top , 8);
top->linkedListTraversal(top);
cout<<endl;
int element = top->pop(top);
cout<<"Popped element is "<<element<<endl;
top->linkedListTraversal(top);
return 0;
}