Can I get a C++ code and output for this program using classes instead of using struct. The following program implements a Last In First Out (LIFO) stack.
( I want to use class for function definitions too and if main need.)
#include <iostream>
using namespace std;
const int MAX = 100;
struct stack
{
int s[MAX]; // an array of integers
int top; // the index of the last number
};
void push(stack &, int);
void pop(stack&, int &);
int peek(stack);
void display(stack);
void main()
{
stack s;
s.top = -1;
push(s, 2); // add 2 into the stack
push(s, 5); // add 5 into the stack
push(s, 1); // add 1 into the stack
int num = peek(s); // look at the last number in
the stack
cout << "the top element is " << num
<< endl;
display(s); // output numbers of the stack from last to first
pop(s, num); // remove the last number from the stack
cout << "the remove element is " << num << endl;
system("pause");
}
void push(stack &sta, int number)
{
if (sta.top < MAX - 1)
{
sta.top++;
sta.s[sta.top] = number;
}
}
void pop(stack &sta, int &num)
{
if (sta.top > -1)
{
num = sta.s[sta.top];
sta.top--;
}
}
int peek(stack sta)
{
if (sta.top > -1)
return sta.s[sta.top];
}
void display(stack sta)
{
for (int i = sta.top; i > -1; i--)
{
cout << sta.s[i] << "
";
}
cout << endl;
}
Thanks!
The programming language C++ program using class is given below:
#include <iostream>
using namespace std;
const int MAX = 100;
class stack
{
//private data member
int s[MAX]; // an array of integers
int top; // the index of the last number
public:
//default constructor
//set the top value -1
stack()
{
top = -1;
}
void push(int number)
{
if (top < MAX - 1)
{
top++;
s[top] = number;
}
}
void pop(int &num)
{
if (top > -1)
{
num = s[top];
top--;
}
}
int peek()
{
if (top > -1)
return s[top];
}
void display()
{
for (int i = top; i > -1; i--)
{
cout << s[i] << " ";
}
cout << endl;
}
};
int main()
{
stack s;
//now data member top is private type
//but default constructor will set the top = -1
//s.top = -1;
s.push(2); // add 2 into the stack
s.push(5); // add 5 into the stack
s.push(1); // add 1 into the stack
int num = s.peek(); // look at the last number in the
stack
cout << "the top element is " << num << endl;
s.display(); // output numbers of the stack from last to first
s.pop(num); // remove the last number from the stack
cout << "the remove element is " << num << endl;
system("pause");
return 0;
}
OUTPUT:
Can I get a C++ code and output for this program using classes instead of using...
I need to modify my C++ code so it can get the min value of the stack code is as follows: #include <iostream> using namespace std; #define MAX_SIZE 100 class Stack { private: int S[MAX_SIZE]; int top; public: Stack() { top = -1; } void push(int x) { if (top == MAX_SIZE - 1) { cout << "Stack is Full." << endl; return; } S[++top] = x; } void pop() { if (top == -1) { cout << "Stack is...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD
COMMENTS:
stackARRAY:
#include<iostream>
#define SIZE 100
#define NO_ELEMENT -999999
using namespace std;
class Stack {
int arr[SIZE]; // array to store Stack elements
int top;
public:
Stack() {
top = -1;
}
void push(int); // push an element into Stack
int pop(); // pop the top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};
void Stack...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
Must be written in C++ Please
Lab 11 Due Date: April 25, 2019 Total Points: 15 points The purpose of this lab is to implement and test a static and dy namic stack Part 1: Static Stack In this part, you are going to design a stack of characters. Assume a simple static array implementation. Complete the code below as specified by the comments below const int MAX- 5 // define an alias for the element type class Stack private:...
I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...
stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...
c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data; // stack data, we assume integer for simplicity int top; // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) { // this...