Question

In: Computer Science

Write an implementation of the ADT stack that uses a resizeable array to represent the stack...

Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array.

Use c++ to write this code.

Solutions

Expert Solution

template<class ItemType>

class ArrayStack

{

private:

static const int INIT_SIZE = 5; // initial size of the array

int arraySize; // current size of the array

// "Anytime the stack becomes full, double the size of the array"

int top; // Index to top of stack

int numEntries; // number of entries in the stack

ItemType* items; // resizable array of stack entries

public:

ArrayStack() : arraySize(INIT_SIZE), top(INIT_SIZE - 1), numEntries(0) // Default constructor

{

items = new ItemType[arraySize];

}

ArrayStack(const ArrayStack<ItemType>& rhs) : arraySize(rhs.arraySize), top(rhs.top), numEntries(rhs.numEntries) // copy constructor

{

items = new ItemType[arraySize];

for (int i = arraySize - numEntries; i < arraySize; ++i)

items[i] = rhs.items[i];

}

~ArrayStack() // destructor

{

delete[] items;

}

bool isEmpty() const;

bool push(const ItemType& newEntry);

bool pop();

bool isEmpty() const {

return numEntries == 0;

}

bool push(const ItemType& newEntry) {

if (numEntries == arraySize) {

ItemType* newItems = new ItemType[arraySize * 2];

int i;

for(i = 0; i < numEntries; i++) {

newItems[arraySize * 2 - 2 - i] = items[arraySize - 1 - i];

}

items = newItems;

arraySize *= 2;

top = arraySize - 1;

items[top] = newEntry;

numEntries++;

return true;

}

int i;

for (i = numEntries - 1; i >= 0; i--) {

items[arraySize - 2 - i] = items[arraySize - 1 - i];

}

items[top] = newEntry;

numEntries++;

return true;

}

bool pop() {

if(isEmpty())

return false;

int i;

numEntries--;

for (i = 0; i < numEntries; i++) {

items[arraySize - 1 - i] = items[arraySize - 2 - i];

}

return true;

}

ItemType peek() const {

assert((!isEmpty()) && (top == arraySize - 1));

return items[top];

};

int getNumEntries() const // returns the number of entries in the stack

{

return numEntries;

}

int getCapacity() const // returns the size of the resizable array

{

return arraySize;

}

};


Related Solutions

write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an implementation of the ADT stack that uses a resizeable array to represent the stack...
Write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the beginning of the array. Use c++ to write this code.
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j βˆ’βˆ’; if (i <= j)...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT