In: Computer Science
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return stack; } // createStack /* ================= pushStack ================ This function pushes an item onto the stack. Pre stack is a pointer to the stack dataPtr pointer to data to be inserted Post Data inserted into stack Return true if successful false if underflow */ bool pushStack(STACK* stack, void* dataInPtr) { // Local Definitions STACK_NODE* newPtr; // Statements newPtr = (STACK_NODE*)malloc(sizeof(STACK_NODE)); if (!newPtr) return false; newPtr->dataPtr = dataInPtr; newPtr->link = stack->top; stack->top = newPtr; (stack->count)++; return true; } // pushStack /* =================== popStack ================== This function pops item on the top of the stack. Pre stack is pointer to a stack Post Returns pointer to user data if successful NULL if underflow */ void* popStack(STACK* stack) { // Local Definitions void* dataOutPtr; STACK_NODE* temp; // Statements if (stack->count == 0) dataOutPtr = NULL; else { temp = stack->top; dataOutPtr = stack->top->dataPtr; stack->top = stack->top->link; free(temp); (stack->count)--; } // else return dataOutPtr; } // popStack /* ================== stackTop ================= Retrieves data from the top of stack without changing the stack. Pre stack is a pointer to the stack Post Returns data pointer if successful null pointer if stack empty */ void* stackTop(STACK* stack) { // Statements if (stack->count == 0) return NULL; else return stack->top->dataPtr; } // stackTop /* ================= emptyStack ================ This function determines if a stack is empty. Pre stack is pointer to a stack Post returns 1 if empty; 0 if data in stack */ bool emptyStack(STACK* stack) { // Statements return (stack->count == 0); } // emptyStack /* ================== fullStack ================= This function determines if a stack is full. Full is defined as heap full. Pre stack is pointer to a stack head node Return true if heap full false if heap has room */ bool fullStack(STACK* stack) { // Local Definitions STACK_NODE* temp; // Statements if ((temp = (STACK_NODE*)malloc(sizeof(*(stack->top))))) { free(temp); return false; } // if // malloc failed return true; } // fullStack /* ================== stackCount ================= Returns number of elements in stack. Pre stack is a pointer to the stack Post count returned */ int stackCount(STACK* stack) { // Statements return stack->count; } // stackCount /* ================== destroyStack ================= This function releases all nodes to the heap. Pre A stack Post returns null pointer */ STACK* destroyStack(STACK* stack) { // Local Definitions STACK_NODE* temp; // Statements if (stack) { // Delete all nodes in stack while (stack->top != NULL) { // Delete data entry free(stack->top->dataPtr); temp = stack->top; stack->top = stack->top->link; free(temp); } // while // Stack now empty. Destroy stack head node. free(stack); } // if stack return NULL; } // destroyStack
Write a program that uses Stack to reverse a number.Please submit the source code in a single file(.c or .cpp).The program should contain appropriate comments. The stack.hheader file(stack ADT implementation)is provided as part of the assignment.
#include<iostream>
#include"stack.h"
using namespace std;
/* =============== reverse ==============
This algorithm reverses a number.
Pre n is a integer number
Post Returns reverse the number using STACK
*/
int reverse(int n){
STACK* stk = createStack();
int a[10];
int i=0;
while(n!=0)
{
a[i] = n%10;
pushStack(stk, a+i);
n = n / 10;
i++;
}
int num = 1;
while(!emptyStack(stk))
{
int *q = (int*)popStack(stk);
n = n + (*q) * num;
num = num * 10;
}
destroyStack(stk);
return n;
}
//main function
int main()
{
cout<<reverse(123);
return 0;
}
Output:
321
Solving your question and
helping you to well understand it is my focus. So if you face any
difficulties regarding this please let me know through the
comments. I will try my best to assist you. However if you are
satisfied with the answer please don't forget to give your
feedback. Your feedback is very precious to us, so don't give
negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid
plagiarism.
Thank you.