top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Modify the class arrayStackType by adding the following function to it:

–2 votes
557 views

void pushGroup(int a[], int size); // this will push a group of elements stored in the array a with size size into the array stack.

NOTE1: don’t call any function; you need to implement everything from scratch.

NOTE2: this operation either completely succeeds or completely fails. In other words, you either push all the elements or none of them.

posted Dec 1, 2013 by As M Ob

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
What is arrayStackType you have not made it clear what it is....
it's class .. in this code

#include <iostream>
#include <cassert>

using namespace std;

class arrayStackType
{
public:
    arrayStackType(int = 100);
    void push(int);
    void pop();
    int top();
    bool isEmpty();
    bool isFull();
private:
    int maxSize, *stack, stackTop;
};

arrayStackType::arrayStackType(int size)
{
    if(size <= 0)
    {
        cout<< "Wrong size\n";
        maxSize = 100;
    }
    else
        maxSize = size;

    stackTop = 0;
    stack = new int [maxSize];
}

bool arrayStackType::isEmpty()
{
    return (stackTop == 0);
}

bool arrayStackType::isFull()
{
    return (stackTop == maxSize);
}

void arrayStackType::push(int item)
{
    if(isFull())
        cout<<"Full stack"<< endl;
    else
        stack[stackTop++] = item;
}

int arrayStackType::top()
{
    assert(!isEmpty());
   
    return stack[stackTop - 1];
}

void arrayStackType::pop()
{
    if(isEmpty())
        cout<<"Empty stack" << endl;
    else
        stackTop--;
}

int main()
{
    arrayStackType s1(30);

    for(int i = 1; i <= 20; i++)
        s1.push(i);

    cout << s1.top() << endl;

    return 0;
}

1 Answer

+2 votes
void arrayStackType::pushGroup(int a[], int size) {
    for (int i=0; i<size; i++) {
      if(isFull()) {
          cout<<"Full stack"<< endl;
          break;
      }
      else
          stack[stackTop++] = a[i];
    }
}
answer Dec 1, 2013 by Salil Agrawal
oh yah .. thanks so much
how can add the same function in Queue !!
if the code like this
#include <iostream>
#include <cassert>

using namespace std;

class arrayQueueType
{
public:
    arrayQueueType(int = 100);
    bool isEmpty();
    bool isFull();
    void addQueue(int);
    void deleteQueue();
    int front();
    int rear();
    void clearQueue();
private:
    int maxSize, length, queueFront, queueRear, *queue;
};

arrayQueueType::arrayQueueType(int size)
{
    if(size <= 0)
        maxSize = 100;
    else
        maxSize = size;

    length = 0;
    queue = new int [maxSize];
    queueFront = 0;
    queueRear = maxSize - 1;
}

bool arrayQueueType::isEmpty()
{
    return (length == 0);
}

bool arrayQueueType::isFull()
{
    return (length == maxSize);
}

void arrayQueueType::deleteQueue()
{
    if(isEmpty())
        cout<<"Empty Queue"<<endl;
    else
    {
        queueFront = (queueFront + 1) % maxSize;
        length--;
    }
}
void arrayQueueType::addQueue(int item)
{
    if(isFull())
        cout<<"Full Queue"<<endl;
    else
    {
        queueRear = (queueRear + 1) % maxSize;
        queue[queueRear] = item;
        length++;
    }

}

int arrayQueueType::front()
{
    assert(!isEmpty());
    return queue[queueFront];
}

int arrayQueueType::rear()
{
    assert(!isEmpty());
    return queue[queueRear];
}

void arrayQueueType::clearQueue()
{
    length = 0;
    queueFront = 0;
    queueRear = maxSize - 1;
}

int main()
{
    arrayQueueType q1(30);

    for(int i = 1; i <= 20; i++)
        q1.addQueue(i);
    cout<<q1.front() <<endl;
    cout<<q1.rear()<<endl;

    return 0;
}
...