You are going to write a program that changes a decimal numb
You are going to write a program that changes a decimal number into a binary number. In order to do this, you will write a stack class. The stack class should be implemented using the node class and will used dynamic memory to create nodes – similar to the linked list. The data for the node will be an integer. The stack class should be generic. The code to use the stack and convert the number should be in the main program.
Algorithm for converting a decimal to a binary number. Taken from http://interactivepython.org/runestone/static/pythonds/BasicDS/ConvertingDecimalNumberstoBinaryNumbers.html.
But how can we easily convert integer values into binary numbers? The answer is an algorithm called “Divide by 2” that uses a stack to keep track of the digits for the binary result.
The Divide by 2 algorithm assumes that we start with an integer greater than 0. A simple iteration then continually divides the decimal number by 2 and keeps track of the remainder. The first division by 2 gives information as to whether the value is even or odd. An even value will have a remainder of 0. It will have the digit 0 in the ones place. An odd value will have a remainder of 1 and will have the digit 1 in the ones place. We think about building our binary number as a sequence of digits; the first remainder we compute will actually be the last digit in the sequence. As shown in Figure 5, we again see the reversal property that signals that a stack is likely to be the appropriate data structure for solving the problem.
Decimal-to-Binary Conversion
Specific information for class:
Stack class data
top – points to first Node in list
count – keeps track of number of Nodes in list
Stack class functions
Any needed constructors
Push – adds a Node to the top of the stack
Pop – Removes a Node from the top of the stack
Stack_top – Returns the item at the top of the stack without affecting the stack
Empty – returns true if stack is empty, false otherwise
Note: Remember that a stack is created in static (automatic memory) and a Node is in dynamic memory.
Requirements for main:
In the main program, you will ask the user for a decimal number. You will use the stack program that you wrote to change the decimal number into a binary number and print the result.
Sample:
Enter a number:
10
The binary number is : 1010
Solution
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
struct node
{
int data;
node *next;
}*top = NULL, *p = NULL, *np = NULL;
int x;
void push(int n)
{
np = new node;
np->data = n;
np->next = NULL;
if (top == NULL)
{
top = np;
}
else
{
np->next = top;
top = np;
}
}
int pop()
{
if (top == NULL)
{
cout<<\"underflow\ \";
}
else
{
p = top;
top = top->next;
x = p->data;
delete(p);
return(x);
}
}
int main()
{
int n, a;
cout<<\"enter the decimal number\ \";
cin>>n;
while (n > 0)
{
a = n % 2;
n = n / 2;
push(a);
}
p = top;
cout<<\"resultant binary no:\";
while(true)
{
if (top != NULL)
cout<<pop()<<\"\\t\";
else
break;
}
getch();
}

