Question

In: Computer Science

Write a C function to implement operation of a stack using the following format: /** *...

Write a C function to implement operation of a stack using the following format:

/**
* function:
*       push
*
* expects:
*       pointer to the stack
*       pointer to the size
*       the value to push
*
* returns:
*     true when value has been pushed
*       false otherwise
*
* The push function push a value to the passed in stack
*/
bool push(int *stack, int *size, int max_size, int to_push)
{
   /**
   * TODO: finish implementing this
   */
     
}

Solutions

Expert Solution

Required function definition is given below:

bool push(int *stack, int *size, int max_size, int to_push)
{

    if(*size==max_size)             //  condition for array overflow
    {
        cout<<"STACK OVERFLOW\n";
        return false;                   // return false because element will not add
    }
    else
    {
        size++;                         //incrementing by one because element can be add.
        stack[*size-1]=to_push;         //pushing element into stack
        return true;                       // return true because element has been added
    }
}

If you have any doubt feel free to ask and if you like the answer please upvote it .

Thanks


Related Solutions

Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...
Using STL stack class, implement in C++ the following pseudocode functioncheckBraces(aString: string) that checks for balanced braces { } in a given string /arithmetic expressions. Write a main() program to test the function.// Checks the string aString to verify that braces match.// Returns true if aString contains matching braces, false otherwise.checkBraces(aString: string): boolean{aStack = a new empty stackbalancedSoFar = truei =0// Tracks character position in stringwhile (balancedSoFar and i < length of aString) {ch = character at position i in...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
C++ Part 1: Developing And Testing A Stack Template Write a template, Stack.h, to implement a...
C++ Part 1: Developing And Testing A Stack Template Write a template, Stack.h, to implement a LIFO stack. Here is the specification for its public interface: class Stack { ... Stack( ); // may have a defaulted parameter Stack(const Stack<V>&); // copy constructor ~Stack(); Stack<V>& operator=(const Stack<V>&); void push(const V&); const V& peek( ); void pop( ); int size( ) const; bool empty( ) const; void clear( ); }; If you use dynamic memory (and you surely will!) be sure...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT