Question

In: Computer Science

#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...

#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.

Solutions

Expert Solution

#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.


Related Solutions

#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT