forked from sonumahajan/All_Program_helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLinkedList.cpp
More file actions
60 lines (43 loc) · 842 Bytes
/
StackLinkedList.cpp
File metadata and controls
60 lines (43 loc) · 842 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
#include<iostream>
using namespace std;
struct Node{ // From beginning O(1) for Insertion /deletion
int data; // From end O(n) for Insertion /deletion
Node *next;
};
Node *top = NULL;
void Push(int data){
Node *temp = new Node();
temp -> data = data;
temp -> next = top;
top = temp;
}
void Pop(){
if(top == NULL){
cout << "Stack is Empty" << endl;
}
Node *temp1 = top;
top = temp1 -> next;
free(temp1);
}
void Print(){
Node *temp = top;
while(temp != NULL){
cout << temp -> data << endl;
temp = temp -> next;
}
printf("\n");
}
int Top(){
return top->data;
}
int main(){
Push(6);
Push(7);
Push(5);
Push(4);
Print();
Pop();
Pop();
Print();
Top();
}