-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracticep47_stack_3.cpp
More file actions
60 lines (50 loc) · 994 Bytes
/
practicep47_stack_3.cpp
File metadata and controls
60 lines (50 loc) · 994 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
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int data){
this->data=data;
this->next=NULL;
};
};
void push(Node* &head,int k){
Node* newNode=new Node(k);
if(head==NULL){
head=newNode;
return;
};
newNode->next=head;
head=newNode;
};
void pop(Node* &head){
Node* temp=head;
cout<<"Binary representation: ";
while(temp!=NULL){
cout<<temp->data;
head=temp->next;
delete temp; // Delete the current node (head)
temp=head; //making temp=head(the new head)
};
};
void binaryrep(int data,Node* &head){
if(data==0){
cout<<"No";
return;
};
while(data>0){
int k=data%2;
push(head,k);
data=data/2;
}
};
int main(){
Node* head=NULL;
Node* tail=NULL;
cout<<"Enter a number: ";
int data;
cin>>data;
binaryrep(data,head);
pop(head);
}