This is what im supposed to do:
Add the following operation to the class stackType:
void reverseStack(stackType &otherStack);
This operation copies the elements of a stack in reverse order onto
another stack.
Consider the following statements.
stackType stack1;
stackType stack2;
The statement
stack1.reverseStack(stack2);
copies the elements of stack1 onto stack2 in reverse order. That is, the
top element of stack1 is the bottom element of stack2, and so on.The
old contents of stack2 is destroyed and stack1 is unchanged.
And here is the code i have for it.
#include<iostream>
#include<stdlib.h>
using namespace std;
template<class Type>
class stackType
{
Type s[10];
int top, n;
public:
stackType()
{
top = -1;
n = 50;
}
stackType(int size)
{
top = -1;
n = size;
}
void push(Type elt)
{
if (top < n - 1)
s[++top] = elt;
else
cout << "\n\tstack is full.Can't insert " << elt << endl;
}
void pop()
{
if (top < 0)
cout << "\n\tstack is empty.\n";
else
cout << "\n\tPoped elt : " << s[top--];
}
void display() {
for (int i = 0; i <= top; i++) {
cout << s[i] << " ";
}
cout << endl;
}
void reverseStack(stackType<Type> &otherStack) {
//Destroy content of other stack by making top to -1
otherStack.top = -1;
//Iterate the current stack and push the elements to other stack
for (int i = top; i >= 0; i--) {
otherStack.push(s[i]);
}
}
};
int main()
{
stackType<int> stack1;
stack1.push(10);
stack1.push(20);
stack1.push(30);
cout << "Stack1 content: \n";
stack1.display();
stackType<int> stack2;
stack1.reverseStack(stack2);
cout << "Stack2 content: \n";
stack2.display();
return 0;
}
The code the book gave me was this:
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
virtual bool isEmptyStack() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const Type& newItem) = 0;
virtual Type top() const = 0;
virtual void pop() = 0;
};
template <class Type>
class stackType : public stackADT<Type>
{
private:
int maxStackSize;
int stackTop;
Type *list;
public:
void initializeStack()
{
stackTop = 0;
cout << "stackTop " << stackTop << endl;
}
void print()
{
for (int i = 0; i < stackTop; i++)
{
cout << list[i] << endl;
}
}
bool isEmptyStack() const
{
return(stackTop == 0);
}
bool isFullStack() const
{
return(stackTop == maxStackSize);
}
void push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
{
cout << "Cannot add to a full stack." << endl;
}
cout << "stacktop: " << stackTop << endl;
system("pause");
}
Type top() const
{
assert(stackTop != 0); //if stack is empty, terminate the program.
return list[stackTop - 1];
}
void pop()
{
if (!isEmptyStack())
stackTop--;
else
cout << "Cannot remove from an empty stack." << endl;
cout << "pop: " << stackTop << endl;
}
stackType(int stackSize = 100)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must be positive." <<
endl;
cout << "Creating an array of size 100." << endl;
maxStackSize = 100;
}
else
{
maxStackSize = stackSize;
cout << "maxStackSize " << maxStackSize << endl;
}
stackTop = 0;
list = new Type[maxStackSize];
}
stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
~stackType()
{
delete[] list;
}
const stackType<Type>& operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
{
copyStack(otherStack);
}
return *this;
}
bool operator==(const stackType<Type>& otherStack) const
{
if (this == &otherStack)
{
return true;
}
else
{
if (stackTop != otherStack.stackTop)
{
return false;
}
else
{
for (int i = 0; i < stackTop; i++)
{
if (list[i] != otherStack.list[i])
{
return false;
}
return true;
}
}
}
}
void copyStack(const stackType<Type>& otherStack)
{
delete[] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
//copy otherStack into this stack.
for (int j = 0; j < stackTop; j++)
{
list[j] = otherStack.list[j];
}
}
};
int main()
{
stackType<int> stack1(50);
stackType<int> stack2(50);
stack1.initializeStack();
stack1.push(23);
stack1.push(45);
stack1.push(38);
stack1.print();
stack2 = stack1;
if (stack1 == stack2)
cout << "stack1 and stack2 are identical" << endl;
else
cout << "stack1 and stack2 are not identical" << endl;
stack2.pop();
stack2.push(38);
cout << "**** After pop and push operations on stack2 ****" << endl;
if (stack1 == stack2)
cout << "stack1 and stack2 are identical" << endl;
else
cout << "stack1 and stack2 are not identical" << endl;
stack2.push(11);
cout << "**** After another push operation on stack2 ****" << endl;
if (stack1 == stack2)
cout << "stack1 and stack2 are identical" << endl;
else
cout << "stack1 and stack2 are not identical" << endl;
return 0;
}
enter image description here
You need to create a breakpoint at the end of your code (End of your main function).
Related
I want to print the values in the stack in the order they were given as input, but after compiling, the stack is 2 4 7 5. Could anyone please help me?
stacktype.h
#ifndef STACKTYPE_H_INCLUDED
#define STACKTYPE_H_INCLUDED
const int MAX_ITEMS = 5;
class FullStack
// Exception class thrown
// by Push when stack is full.
{};
class EmptyStack
// Exception class thrown
// by Pop and Top when stack is emtpy.
{};
template <class ItemType>
class StackType
{
public:
StackType();
bool IsFull();
bool IsEmpty();
void Push(ItemType);
void Pop();
ItemType Top();
private:
int top;
ItemType items[MAX_ITEMS];
};
#endif // STACKTYPE_H_INCLUDED
stacktype.cpp
#include "StackType.h"
template <class ItemType>
StackType<ItemType>::StackType()
{
top = -1;
}
template <class ItemType>
bool StackType<ItemType>::IsEmpty()
{
return (top == -1);
}
template <class ItemType>
bool StackType<ItemType>::IsFull()
{
return (top == MAX_ITEMS-1);
}
template <class ItemType>
void StackType<ItemType>::Push(ItemType newItem)
{
if( IsFull() ) throw FullStack();
top++;
items[top] = newItem;
}
template <class ItemType>
void StackType<ItemType>::Pop()
{
if( IsEmpty() ) throw EmptyStack();
top--;
}
template <class ItemType>
ItemType StackType<ItemType>::Top()
{
if (IsEmpty()) throw EmptyStack();
return items[top];
}
main.cpp
#include <iostream>
#include "StackType.h"
#include "StackType.cpp"
using namespace std;
int main()
{
StackType<int> mystack;
if (mystack.IsEmpty()) {
cout << "Stack is Empty" << endl;
}
else {
cout << "Stack is Not Empty" << endl;
}
int stc[]={5, 7, 4, 2};
for(int i=0; i<4; i++)
{
mystack.Push(stc[i]);
}
if (mystack.IsEmpty()) {
cout << "Stack is Empty" << endl;
}
else {
cout << "Stack is Not Empty" << endl;
}
if (mystack.IsFull()) {
cout << "Stack is Full" << endl;
}
else {
cout << "Stack is Not Full" << endl;
}
for(int i=0; i<4; i++)
{
cout << mystack.Top() << " ";
mystack.Pop();
}
cout << endl;
[After compilation the stack looks 2 4 7 5 but I want to print the stack like 5 7 4 2][1]}
[1]: https://i.stack.imgur.com/xyPXq.jpg
main.cpp
#include <iostream>
#include "StackType.h"
#include "StackType.cpp"
using namespace std;
int main()
{
StackType<int> mystack;
StackType<int> temp;
if (mystack.IsEmpty()) {
cout << "Stack is Empty" << endl;
}
else {
cout << "Stack is Not Empty" << endl;
}
int stc[]={5, 7, 4, 2};
for(int i=0; i<4; i++)
{
mystack.Push(stc[i]);
}
if (mystack.IsEmpty()) {
cout << "Stack is Empty" << endl;
}
else {
cout << "Stack is Not Empty" << endl;
}
if (mystack.IsFull()) {
cout << "Stack is Full" << endl;
}
else {
cout << "Stack is Not Full" << endl;
}
for(int i=0; i<4; i++)
{
temp.Push(mystack.Top());
mystack.Pop();
}
for(int i=0; i<4; i++)
{
cout << temp.Top() << " ";
mystack.Push(temp.Top());
temp.Pop();
}
cout << endl;
mystack.Push(3);
for(int i=0; i<5; i++)
{
temp.Push(mystack.Top());
mystack.Pop();
}
for(int i=0; i<5; i++)
{
cout << temp.Top() << " ";
mystack.Push(temp.Top());
temp.Pop();
}
cout<<endl;
if (mystack.IsFull()) {
cout << "Stack is Full" << endl;
}
else {
cout << "Stack is Not Full" << endl;
}
mystack.Pop();
mystack.Pop();
cout<<mystack.Top()<<" ";
}
#include <iostream>
using namespace std;
class Stack {
private:
int size;
public:
Stack(int n)
{
size = n;
}
int stack_arr[size], top = -1;
void push(int a)
{
if (top >= 4)
cout << "Stack is full" << endl;
else {
top++;
stack_arr[top] = a;
}
}
void pop()
{
if (top <= -1)
cout << "There is no element remaining in stack" << endl;
else {
cout << "The popped element is " << stack_arr[top] << endl;
top--;
}
}
void peek()
{
if (top < 0) {
cout << "Stack is Empty";
}
else {
int x = stack_arr[top];
cout << "The last element in the Stack is: ";
cout << x << endl;
}
}
int isempty()
{
if (top == -1)
cout << "Stack is Empty: ";
else
return false;
}
void display()
{
if (top >= 0) {
cout << "Stack elements are:";
for (int i = top; i >= 0; i--)
cout << stack_arr[i] << " ";
cout << endl;
}
else
cout << "Stack is empty";
}
};
int main()
{
Stack s(5);
s.push(10);
s.push(12);
s.push(14);
s.push(10);
s.push(12);
s.push(14);
s.peek();
s.display();
s.pop();
s.display();
}
I'm facing the issue while compilation
13:9: error: invalid use of non-static data member 'Stack::size'
14:15: error: from this location
In member function 'void Stack::push(int)':
21:7: error: 'stack_arr' was not declared in this scope
In member function 'void Stack::pop()':
28:40: error: 'stack_arr' was not declared in this scope
In member function 'void Stack::peek()':
38:17: error: 'stack_arr' was not declared in this scope
In member function 'void Stack::display()':
53:13: error: 'stack_arr' was not declared in this scope
can anyone help me for giving idea how i can get stack size from constructor,if im doing wrong approach
I made your code compile, you had to do two simple modifications:
private:
int size;
int* stack_arr;
int top = 0;
public:
Stack(int n)
{
size = n;
stack_arr = new int[size];
}
You forgot to define top, also to achieve what you tried with a dynamic array you can use new. I also fixed some issues that lead to random dygits while printing, you basically were going out of array scope:
void push(int a) {
if (top == size - 1) // in your code it was top<=4,
//suppose it was only for your specific test case
// your forgot that it has to be generic
cout << "Stack is full" << endl;
else {
top++;
stack_arr[top] = a;
}
}
void pop() {
if (top == -1)
cout << "There is no element remaining in stack" << endl;
else {
cout << "The popped element is " << stack_arr[top] << endl;
top--;
}
}
void peek()
{
if (top == -1) {
cout << "Stack is Empty";
}
else {
int x = stack_arr[top];
cout << "The last element in the Stack is: ";
cout << x << endl;
}
}
int isempty() {
if (top == -1)
cout << "Stack is Empty: ";
else
return false;
}
void display() {
if (top > -1) {
cout << "Stack elements are:";
for (int i = top; i >= 0; i--)
cout << stack_arr[i] << " ";
cout << endl;
}
else
cout << "Stack is empty";
}
In modern c++ there is a rule: If you have anything in your class using new, you should make a destructor. And if you create a destructor, you should create copy constructor, and copy assignment operator (Its called rule of 3). For bonus, in this case, at least in my opinion, I also created move constructor and move assignment operator ( likely you wont use then in your case, that's why I say its bonus. It's known as rule of 5).
//Destructor
~Stack()
{
delete [] stack_arr;
}
//copy Constructor
Stack(const Stack& s)
:
size(s.size),
stack_arr(new int[size]),
top(s.top)
{
for (int i = 0; i <= top; i++) stack_arr[i] = static_cast<int>(s.stack_arr[i]);
}
//copy assignment operator
Stack& operator=(const Stack& s)
{
if (stack_arr != nullptr) delete[] stack_arr;
size = s.size;
top = s.top;
stack_arr = new int[size];
for (int i = 0; i <= top; i++) stack_arr[i] = static_cast<int>(s.stack_arr[i]);
return *this;
}
//move constructor
Stack(Stack&& s) noexcept :
top(std::move(s.top)),
size(std::move(s.size)),
stack_arr(std::move(s.stack_arr))
{
s.stack_arr = nullptr;
}
//move assignment operator
Stack& operator=(Stack&& s)noexcept
{
if (this != &s)
{
if (stack_arr != nullptr) delete[] stack_arr;
size = std::move(s.size);
top = std::move(s.top);
stack_arr = std::move(s.stack_arr);
}
return *this;
}
You can do what std::stack does, and delegate to an existing container type.
#include <iostream>
#include <vector>
class Stack {
private:
std::vector<int> data;
public:
Stack(std::size_t n) : data(n)
{
}
void push(int a)
{
try {
data.push_back(a);
} catch (std::bad_alloc) {
std::cout << "stack is full" << std::endl;
}
}
void pop()
{
if (data.empty()) {
std::cout << "There is no element remaining in stack" << std::endl;
} else {
std::cout << "The popped element is " << data.back() << std::endl;
data.pop_back();
}
}
void peek()
{
if (data.empty()) {
std::cout << "Stack is Empty" << std::endl;
} else {
std::cout << "The last element in the Stack is: " << data.back() << std::endl;
}
}
bool isempty()
{
return data.empty();
}
void display()
{
if (!data.empty()) {
std::cout << "Stack elements are:";
for (int i : data) {
std::cout << i << " ";
}
std::cout << endl;
} else {
std::cout << "Stack is empty" << endl;
}
}
};
The issue is portrayed clearly in the display function.I wanted to display the array items in top, but I couldn't figure out a way to do so as it was constantly being incremented.
Instead of this...:
void CircularQueue :: Display() const {
for(int i = front; i < MAX; i++) {
cout << a[i] << endl;
}
}
**I want to display the circular queue using top:**
void CircularQueue :: Display() const {
for(int i = front; i < top; i++) {
cout << a[i] << endl;
}
}
In my main function, I enqueue'd 15 items, and as a result, top incremented 15 times. So obviously there would be 5 garbage values. How can I manipulate top so that the display function shows all 10 array values?
#include <iostream>
using namespace std;
#define MAX 10
class CircularQueue {
private:
int top;
int front;
public:
//assume that the max number of items in this circular queue is 10.
int a[MAX];
CircularQueue() {top = -1; front = -1;}
int enqueue(int x);
void dequeue();
void peekFront() const;
void peekBack() const;
void Display() const;
bool isEmpty();
};
//1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
//10,11,12,13,14,15,6,7,8,9
int CircularQueue :: enqueue(int x) {
//Problem: The array is actually growing in size.
++top;
a[top%10] = x;
int y = a[top%10];
cout << "Adding " << y << " to the queue." << endl;
if(top == 0) {
front = 0;
}
return y;
}
void CircularQueue :: dequeue() {
if(top < 0) {
cout << "The queue is empty." << endl;
} else {
int x = a[top];
cout << x << " will now be removed." << endl;
for(int i = 0; i <= top - 1; i++) {
a[i] = a[i+1];
}
top--;
x = a[top];
cout << "The last element of the queue is now: " << x << endl;
}
}
bool CircularQueue :: isEmpty() {
return top < 0;
}
void CircularQueue :: peekFront() const {
if(front < 0) {
cout << "The queue is empty." << endl;
} else {
int x = a[front];
cout << "The Front value is: " << x << endl;
}
}
void CircularQueue :: peekBack() const {
if(top < 0) {
cout << "The queue is empty." << endl;
} else {
int x;
x = a[top];
cout << "The back value is: " << a[top] << endl;
}
}
void CircularQueue :: Display() const {
for(int i = front; i < MAX; i++) {
cout << a[i] << endl;
}
}
int main() {
CircularQueue Aq;
Aq.enqueue(0);
Aq.enqueue(1);
Aq.enqueue(2);
Aq.enqueue(3);
Aq.enqueue(4);
Aq.enqueue(5);
Aq.enqueue(6);
Aq.enqueue(7);
Aq.enqueue(8);
Aq.enqueue(9); Aq.Display();
Aq.enqueue(10);
Aq.enqueue(11);
Aq.enqueue(12);
Aq.enqueue(13);
Aq.enqueue(14);
Aq.enqueue(15); Aq.Display();
return 0;
}
Expected output should be:
10,11,12,13,14,15,6,7,8,9
The array size should be 10 always. But when I keep enqueueing; The array size goes beyond 10.
I just have a few errors of the same type in my main program. My college professor is not answering my emails so I have to resort to asking you guys. In my main program I have several errors somewhat similar to this: "request for member which is of non-class type." Program01 is basically testing every function in ListType.h, OListType.h, and UListType.h to make sure everything works correctly. Any help you can provide in a timely fashion will be appreciated.
Here is ListType.h:
#ifndef LISTTYPE_H_INCLUDED
#define LISTTYPE_H_INCLUDED
#include <iostream>
class ListType {
public:
ListType(size_t=10);
ListType(const ListType&);
virtual ~ListType();
virtual bool insert(int)=0;
virtual bool eraseAll();
virtual bool erase(int)=0;
virtual bool find(int) const=0;
size_t size() const;
bool empty() const;
bool full() const;
friend std::ostream& operator << (std::ostream&, const ListType&);
const ListType& operator= (const ListType&);
protected:
int *items;
size_t capacity;
size_t count;
};
#endif // LISTTYPE_H_INCLUDED
Here is ListType.cpp:
#include "ListType.h"
ListType::ListType (size_t a) {
capacity = a;
count = 0;
items = new int [capacity];
}
ListType::ListType(const ListType& newlist) {
capacity = newlist.capacity;
count = newlist.count;
items = new int [capacity];
for (size_t i = 0; i < count; ++i)
items[i] = newlist.items[i];
}
ListType::~ListType() {
delete [] items;
}
bool ListType::eraseAll() {
count = 0;
return 0;
}
size_t ListType::size() const {
return (count);
}
bool ListType::empty() const {
return (count == 0);
}
bool ListType::full() const {
return (count == capacity);
}
std::ostream& operator << (std::ostream& out, const ListType& my_list) {
if (!my_list.empty()) {
for (size_t i = 0; i < my_list.count; ++i){
out << my_list.items[i] << ',';
}
}
return out;
}
const ListType& ListType::operator= (const ListType& rightObject) {
if (this != & rightObject) {
delete [] items;
capacity = rightObject.capacity;
count = rightObject.count;
items = new int[capacity];
for (size_t i = 0; i < count; ++i) {
items[i] = rightObject.items[i];
}
}
return *this;
}
Here is UListType.h:
#ifndef ULISTTYPE_H_INCLUDED
#define ULISTTYPE_H_INCLUDED
#include <iostream>
class UListType: public ListType {
public:
UListType(size_t=10);
bool insert(int);
bool erase(int);
bool find(int) const;
};
#endif // ULISTTYPE_H_INCLUDED
Here is UListType.cpp:
#include "ListType.h"
#include "UListType.h"
UListType::UListType (size_t c): ListType(c) {}
bool UListType::insert(int item) {
if (full()) {
int *newitems;
capacity *=2;
newitems = new int[capacity];
for (size_t i =0; i < count; ++i){
newitems[i] = items[i];
}
delete [] items;
items = newitems;
}
items[count++] = item;
return true;
}
bool UListType::erase(int item) {
bool result = false;
size_t i=0;
while ( i < count && items [i] != item) {
++i;
}
if (i < count) {
items[i] = items[-- count];
result = true;
}
return result;
}
bool UListType::find(int item) const {
size_t i = 0;
while (i < count && items [i] != item) {
++i;
}
return i < count;
}
Here is OListType.h:
#ifndef OLISTTYPE_H_INCLUDED
#define OLISTTYPE_H_INCLUDED
#include <iostream>
class OListType: public ListType {
public:
OListType(size_t=10);
bool insert(int);
bool erase(int);
bool find(int) const;
};
#endif // OLISTTYPE_H_INCLUDED
Here is OListType.cpp:
#include "ListType.h"
#include "OListType.h"
OListType::OListType(size_t c): ListType(c) {}
bool OListType::insert(int item) {
size_t i = count;
if (full()) {
int *newitems;
capacity *=2;
newitems = new int[capacity];
for(size_t j=0; j < count; ++j) {
newitems[j] = items[i];
}
delete [] items;
items = newitems;
}
while (i > 0 && items[i-1] > item){
items[count++] = item;
}
return true;
}
bool OListType::erase(int item) {
bool found=false;
size_t i=0, j= count-1, mid;
while (i <= j && !(found)){
mid = (i + j)/2;
if (item < items [mid])
j = mid - 1;
else if (item > items [mid])
i = mid + 1;
found = items [mid] == item;
}
if (found) {
for (i = mid; i < count - 1; ++i) {
items [i] = items [i +1];
}
--count;
}
return found;
}
bool OListType::find (int item) const {
bool found=false;
size_t i=0, j= count-1, mid;
while (i <= j && !(found)){
mid = (i + j)/2;
if (item < items [mid])
j = mid - 1;
else if (item > items [mid])
i = mid + 1;
found = items [mid] == item;
}
return found;
}
Here is Program01.cpp:
#include "ListType.h"
#include "UListType.h"
#include "OListType.h"
#include <iostream>
using namespace std;
int main() {
OListType list[5] = {165, 16, 118, 212, 104};
UListType ranlist[10] = {243, 300, 154, 153, 592, 124, 195, 217, 289, 405};
UListType UListAssignmentTest;
OListType OListAssignmentTest;
cout << "The Ordered List before operations:" << endl;
cout << list << endl << endl;
if(list.empty()) **<-- HERE BE THE ERROR**
cout << "The list is empty, therefore it is true.";
else
cout << "The list is full or partially full, therefore it is false";
cout << endl << endl;
if(list.full())
cout << "The list is full, therefore it is true.";
else
cout << "The list is partially full or empty, therefore it is false";
cout << endl << endl;
list.insert(25);
cout << endl << endl;
cout << "The Ordered list after Insert:" << endl;
cout << list << endl << endl;
list.find(25);
cout << endl << endl;
list.find(30);
cout << endl << endl;
list.erase(25);
cout << endl << endl;
cout << "The Ordered List after Erase:" << endl;
cout << list << endl << endl;
cout << "The Unordered List before operations:" << endl;
cout << ranlist << endl << endl;
if(ranlist.empty())
cout << "The list is empty, therefore it is true.";
else
cout << "The list is full or partially full, therefore it is false";
cout << endl << endl;
if(ranlist.full())
cout << "The list is full, therefore it is true.";
else
cout << "The list is partially full or empty, therefore it is false";
cout << endl << endl;
ranlist.insert(25);
cout << endl << endl;
cout << "The Unordered List after Insert:" << endl;
cout << ranlist << endl << endl;
ranlist.find(25);
cout << endl << endl;
ranlist.find(30);
cout << endl << endl;
ranlist.erase(25);
cout << endl << endl;
cout << "The Unordered List after Erase:" << endl;
cout << ranlist << endl << endl;
cout << "Testing Ordered List Assignment Operator" << endl;
OListAssignmentTest = list;
cout << OListAssignmentTest << endl << endl;
cout << "Testing Unordered List Assignment Operator" << endl;
UListAssignmentTest = ranlist;
cout << UListAssignmentTest << endl << endl
cout << "Testing Ordered List Copy Constructor" << endl;
OListType OListVariable = list;
cout << OListVariable << endl << endl;
cout << "Testing Unordered List Copy Constructor" << endl;
UListType UListVariable = ranlist;
cout << UListVariable << endl << endl;
cout << "Testing Erase All for OList" << endl;
list.eraseAll();
cout << "OList values now: " << list.empty() << endl << endl;
cout << "Testing Erase All for UList" << endl;
ranlist.eraseAll();
cout << endl << "UList values now: " << ranlist.empty() << endl;
return 0;
}
OListType list[5] = {165, 16, 118, 212, 104};
This line declares an array of 5 OListType types. This doesn't seem correct.
You want to declare one OLIstType and insert 5 values into it. If not, please clarify what that line is supposed to denote.
Here is probably what you are supposed to do:
OListType list;
list.insert(165);
list.insert(16); // etc...
I'm in the process of learning how to create Stacks and linked lists. The program that I'm writing right now focuses on a template stack class. Everything was going smoothly when I made a stack of int, but my program started crashing when I started implementing a stack of char. To be specific it started messing up when I tried to implement a pop action on the stack of char.
Can you guys please verify that I'm doing this correctly and also let me know what I'm doing wrong with char stack?
Here's my code:
#include<iostream>
using namespace std;
#ifndef STACK_H
#define STACK_H
//STACK CLASS
template<typename T>
class Stack
{
public:
Stack(int = 10);
~Stack(){ delete stackPtr;};
bool isEmpty() const
{ return top == -1; }
bool isFull() const
{ return top == size - 1; }
//push and pop
bool push(const T&);
bool pop(T&);
private:
int size;
int top;
T *stackPtr;
};
//CONSTRUCTOR
template<typename T>
Stack<T>::Stack(int newSize)
: top(-1), size(newSize),
stackPtr(new T[size]) //allocate array using ptr ********
{
//empty constructor
};
//PUSH VALUES ONTO STACK
template<typename T>
bool Stack<T>::push(const T &pushVal)
{
if(!isFull())
{
stackPtr[++top] = pushVal;
return true;
}
return false;
};
//POP VALUES OFF OF STACK
template<typename T>
bool Stack<T>::pop(T &popVal)
{
if(!isEmpty())
{
popVal = stackPtr[top--];
return true;
}
return false;
};
#endif
//DRIVER
int main()
{
//STACK OF INT
Stack<int> intStack(5);
int intValue = 1;
cout << "Pushing values onto intStack: " << endl;
while(intStack.push(intValue))
{
cout << intValue << ' ';
intValue++;
}
cout << "\nStack is full, cannot push..."
<< endl << endl;
cout << "Popping values off of intStack: " << endl;
while(intStack.pop(intValue))
cout << intValue << ' ';
cout << "\nStack is empty, cannot pop..."
<< endl;
//STACK OF CHAR
Stack<char> charStack(5);
string greeting = "hello";
int strSize = greeting.length();
cout << "\nPushing values onto charStack: " << endl;
for(int i = 0; i < strSize; i++)
{
charStack.push(greeting.at(i));
cout << greeting.at(i) << ' ';
}
cout << endl;
cout << "Popping values off of charStack: " << endl;
for(int i = (strSize - 1); i >= 0; i++) //PROBLEM OCCURS
{
charStack.pop(greeting.at(i));
cout << greeting.at(i) << ' ';
}
system("pause");
}
for(int i = (strSize - 1); i >= 0; **i--**) //PROBLEM not anymore
{
charStack.pop(greeting.at(i));
cout << greeting.at(i) << ' ';
}
Probably it's not the source of your particular problem, but you really should use
delete[] stackPtr
instead of delete stackPtr in your destructor. Wikipedia explains why