forked from itsyadavRajkumar/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackusingLinkedList
More file actions
52 lines (48 loc) · 836 Bytes
/
stackusingLinkedList
File metadata and controls
52 lines (48 loc) · 836 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
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(){
data = 0;
next = NULL;
}
Node(int x){
data = x;
next = NULL;
}
};
class stack{
private:
Node* head = NULL;
public:
void push(int x){
Node* temp = new Node(x);
if(head == NULL)
head = temp;
else
temp->next = head;
head = temp;
}
int pop(){
if(head == NULL)
return -1;
int top = head->data;
Node *temp = head;
head = head->next;
delete temp;
return top;
}
};
int main(){
stack s;
s.push(10);
s.push(20);
s.push(30);
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
return 0;
}