Etiket arşivi: stack examples in c

Decimal to Binary Conversion in C using Stack

Hi guys , today I will try to explain how we can convert decimal to binary in C programing language with using Stack.In addition I encoded some functions which are useful according to me.

NOTE : I am very thankful to my professor who is ‘Mehmet Ali Aksoy Tüysüz’ in order to he gave me Data Structures lecture.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct tagStack{
int* pData;               // Stores actual data
int sp;                      // Points to add/remove point
int capacity;             // Capacity of the array(Stack Data)
int item_count;         // Actually stored item count
}Stack;

#define SIZE 16 // I am gonna use this size to create stack for convert operation .

typedef int BOOL;  // Just to be exactly sure about Compiler includes BOOl type

#define TRUE 1
#define FALSE 0

/*

Right Now I will create stack.That’s why ı need to know how much space we need.So ı will get size like parameter of CreateStack Function.Then Function return the stack itself.

*/

Stack* CreateStack(int size)
{
Stack* pStack;

pStack = malloc(sizeof(Stack));

// You should check  the memory be taken or not ? 

if (NULL == pStack) {
fprintf(stderr, “Cannot allocate memory for Stack!!!\n”);
exit(EXIT_FAILURE);                                                                                           }
//After checking , you can take memory and create stack

pStack->pData = malloc(sizeof(int)* size);
if (NULL == pStack->pData) {
fprintf(stderr, “Cannot allocate memory for Stack array!!!\n”);
exit(EXIT_FAILURE);
}

pStack->capacity = size;
pStack->item_count = 0;
pStack->sp = 0;

return pStack;
}

/*

İf I take the space from memory , I should give it back.İf I would not do that,The segmentation fault might be created.

*/

void DestroyStack(Stack* pStack)
{
free(pStack->pData);
free(pStack);
}

/*

I encoded Full and Empty functions.They will allow me to know is there a space in stack or not.

*/

BOOL IsFull(Stack* pStack)
{
if (pStack->capacity == pStack->item_count)
return TRUE;
else
return FALSE;
}

BOOL IsEmpty(Stack* pStack)
{
if (0 == pStack->item_count)
return TRUE;
else
return FALSE;

}

/*

Push function has simple logic.I need a list to add data.So ı am taking list and data as a parameter of function . Return type is Bool.İf ı can add data to list, program will return true, other condition false will be the return value.

*/

BOOL Push(Stack* pStack, int toAdd)
{
if (IsFull(pStack))
return FALSE;
else {
pStack->pData[pStack->sp] = toAdd;
pStack->item_count++;
pStack->sp++;
return TRUE;
}
}

/*

Pop function simply extract the last value of stack.Thats why ı need list to remove data.I am taking it as parameter of function.After that if mission is completed , function return true , other case it will return false.

*/

int Pop(Stack* pStack)
{
int data, place;

place = pStack->sp – 1;
data = pStack->pData[place];
pStack->item_count -= 1;
pStack->sp–;
return data;
}

/*

Right now we get the major function ı wrote.I will summarize program quickly.İf we want to convert decimal to binary, we simply divide the decimal number in two and pushes remainder to stack  until division become zero.After that pop every value one by one and wrote them to int array.Last number is not being calculated so I pushed it the array firstly. 

*/

int* DecimaltoBinary(int number)
{

Stack* stack;
int *counter = malloc(sizeof(int)*SIZE ) ;
int i = 0;
int value = 0 ;
stack = CreateStack(SIZE);

while (number > 1) {
value = (number) % 2;
Push(stack, value);
number = number / 2;
}

counter[0] = number;
for (i = 1; IsEmpty(stack) == FALSE; i++){
counter[i] = Pop(stack);
}

return counter;

}

/*

This is print functions. Binary numbers basicly just includes “0” and “1”. So if the value in the string are not like them , it means we are end of the operation.Thats’s why ı am displaying numbers until values are different than “0” and “1”.

*/

void printBinary(int* binarynum){

int i = 0;
printf(“The binary number : \n”);
while (binarynum[i] == 0 || binarynum[i] == 1){
printf(“%d”, binarynum[i]);
i++;
}
printf(“\n\n”);
}

/*

To display all elemets of stack , ı am using IsEmpty function . As long As stack includes element, This function will display them one by one from Last to first direction .

*/

void PrintStack(Stack* stack) {

int value;
while (IsEmpty(stack) == FALSE)
{
value = Pop(stack);
printf(“%d”,value);
}
printf(“\n”);
}

/*

At first part, Program will convert decimal to binary until user press button which is different than “1”.At second part , Program creates stack with 10 size and initialize it 0 to 9 .After that it displays the elements.

*/

int main(int argc, char** argv)

{
Stack* stack;
int *binaryArray;
int decimalnumber;
int i = 1;

while (i == 1)
{

printf(“Please enter decimal number to convert binary : \n”);
scanf(“%d”, &decimalnumber);
binaryArray = DecimaltoBinary(decimalnumber);
printBinary(binaryArray);
printf(“———————————-\n”);
printf(“Press 1 for another convertion  or for not press 2..\n\n”);
scanf(“%d”,&i);
printf(“\n”);
}
printf(“———————————-\n”);
printf(“\nProgram creating 10 size Stack… \n”);
printf(“\nProgram initializing Stack 0 to 9 …\n”);
stack = CreateStack(10);

for (i = 0; i < 10; i++) {
Push(stack, i);
}

PrintStack(stack);

return EXIT_SUCCESS;

}

stackblog