60 635
Assignments Done
97,8%
Successfully Done
In April 2018

Answer to Question #5779 in C++ for alaa abu al-saud

Question #5779
Implement a as stack and a queue using arrays.
Expert's answer
#include <iostream>
using namespace std;

const int SIZE = 10000;

struct Stack
{
int arr[SIZE];
int k;
Stack() : k(0) { }
void push(int a)
{
& if (k >= SIZE) return;
& arr[k++] = a;
}
int pop()
{
& if (k == 0)
& {
& cout << "Error! Stack is empty!\n";
& return 0;
& }
& k--;
& return arr[k];
}
};

struct Queue
{
int arr[SIZE];
int k;
Queue() : k(0) { }
void push(int a)
{
& if (k >= SIZE) return;
& for (int i = k; i >= 1; i--)
& {
& arr[i] = arr[i-1];
& }
& k++;
& arr[0] = a;
}
int pop()
{
& if (k == 0)
& {
& cout << "Error! Queue is empty!\n";
& return 0;
& }
& k--;
& return arr[k];
}
};

int main()
{
Stack s;
cout << "Stack: \n" ;
for (int i = 0; i < 10; i++)
{
& s.push(i+1);
}
for (int i = 0; i < 10; i++)
{
& cout << s.pop() << '\n' ;
}
Queue q;
cout << "Queue: \n";
for (int i = 0; i < 10; i++)
{
& q.push(i+1);
}
for (int i = 0; i < 10; i++)
{
& cout << q.pop() << '\n' ;
}

system("pause");
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions