I am using a stack to reverse a given string. Each character of the string is pushed onto the stack, then they are popped back into a new string which is returned to main. But for some reason, the value of stack[top] in the stack class code never seems to change.
#include <iostream>
#include <string>
using namespace std;
const int size = 4;
class Stack{
private:
//char stack[size];
char* stack;
int top;
public:
Stack()
{
top = -1;
}
void Init_Size(int size)
{
stack = new char[size];
}
bool isFull()
{
if (top == (size - 1))
return true;
return false;
}
bool isEmpty()
{
if (top == -1)
return true;
return false;
}
void push(char c)
{
if (isFull())
{
cout << "STACK IS FULL" << endl;
return;
}
top++;
stack[top] == c;
cout << "PUSHED" << stack[top] << " ONTO STACK" << endl;
}
char pop()
{
if (isEmpty())
{
cout << "STACK IS EMPTY" << endl;
return '/'; //error
}
char temp = stack[top];
cout << stack[top] << endl;
top--;
cout << "POPPED" << temp << " OFF STACK" << endl;
return temp;
}
};
string ReverseString(string str)
{
Stack stack;
string result;
stack.Init_Size(str.length());
for (int i=0; i < str.length(); i++)
{
stack.push(str[i]);
}
for (int i=0; i < str.length(); i++)
{
result[i] = stack.pop();
}
return result;
}
int main()
{
string str;
cin >> str;
cout << ReverseString(str);
}
I tried using a normal array for the stack instead of a dynamic one, but no change, however, I did notice that the value of the stack array can be changed but only in the void Init_Size(int size) function and I'm not sure why.
I see a number of errors in your program:
size: You have a global constant size set to 4 which is used in your isFull function, independent of the local size parameter used in Init_Size. Make this a class variable instead.
Your push function has the line stack[top] == c with two = instead of one - thus comparing the value against stack instead of setting stack. Turn on warnings and you should have found this yourself
ReverseString assigns directly to result[i] without ever setting the size of result. You could have used the str variable instead, since that is just a copy.
Those three are the major errors that need fixing for your program to work, but there are several minor issues as well, like using namespace std; thereby making size ambigious, making unnecessary copies of strings, using new and c-style arrays etc.
You have a problem with creation and deletion of your arrays here in your program as well as a few small misshaps on the way.
Fix:
#include <iostream>
#include <string>
using namespace std;
class Stack{
private:
//char stack[size];
int size;
char* stack;
public:
Stack() : size(0), stack(NULL) {}
~Stack() {if (stack != NULL) delete stack;} //THIS IS NEEDED HERE
void Init_Size(int newSize)
{
if (stack != NULL) delete stack;
size = newSize;
stack = new char[newSize]; //CREATES DYNAMIC ARRAY WHICH YOU NEED TO REMEMBER TO REMOVE OTHERWISE MEMORY LEAK
}
void push(char c) //adds
{
//adding here should work in a way:
//a) create new dynamic array with different size
//b) copy all elements into new array
//c) delete array
if (stack == NULL) Init_Size(1);
char* newArray = new char[size+1];
for(int i = 0; i < size; i++)
{
newArray[i] = stack[i];
}
newArray[size] = c;
delete stack;
stack = newArray;
size++;
cout << "PUSHED " << stack[size-1] << " ONTO STACK" << endl;
}
char pop()
{
//removing here should work in a way:
//a) create new dynamic array with different size
//b) copy all elements into new array
//c) delete array
if (stack == NULL)
{
cout << "Stack empty" << endl;
return '\0';
}
char* newArray = new char[size-1];
for(int i = 0; i < size-1; i++)
{
newArray[i] = stack[i];
}
char temp = stack[size-1];
delete stack;
stack = newArray;
size--;
cout << "POPPED " << temp << " OFF STACK" << endl;
return temp;
}
};
string ReverseString(string str)
{
Stack stack;
string result;
stack.Init_Size(str.length());
for (int i=0; i < str.length(); i++)
{
stack.push(str[i]);
}
for (int i=0; i < str.length(); i++)
{
result[i] = stack.pop();
}
return result;
}
int main()
{
string str;
cin >> str;
cout << ReverseString(str);
}
Related
#include <iostream>
#include <string>
using namespace std;
#define Max 100000
class Stack {
private:
int top =-1;
char letters[Max];
public:
void setTop(int t) {
top = t;
}
int getTop() {
return top;
}
bool isEmptyStack() {
if (top == -1) {
return true;
}
else{ return false;
}
}
char push(char x,int s) {
if (top != s - 1){
top++;
x = letters[top];
return x;
}
}
char pop() {
if ((isEmptyStack())==false){
cout << "the deleted value is: " << l[top]<<endl;
top--;
return l[top];
}
}
};
void reverse(char letters[], char temp[], int size, Stack stack) {
int i=0;
for (i = 0; i < size; i++) {
stack.push(letters[i],size);
}
i = 0;
cout << temp<<endl;
while (stack.isEmptyStack() == false)
{
letters[-1] = stack.getTop();
stack.pop();
stack.push(letters[i],size);
i++;
}
/* for (int i = 0; i < size; i++) {
cout << temp[i];
}*/
}
int myStringLength(const char* letter)
{
for (int i = 0, c = 0; letter[i] != '\0'; i++, c++) {
if (letter[i] != '\0')
for (; letter[i] != '\0'; i++, c++)
if (letter[i] == '\0') break;
return c;
}
}
int main()
//initializes the main function
{
Stack stack;
string w;
std::cout << "Enter a Word: ";
getline(cin,w);
char* letters = &w[0];
// sets the character text array to set the number of characters equal to the size of the string
//calls the processData function
std::cout << letters<<endl;
int size = myStringLength(letters);
reverse(letters, letters, size, stack);
return 0;//returns the function at 0.
}
I set out to create a program that will check if a word is a palindrome(meaning it is spelled the same normally and if the word is reversed.) I am not yet at that point that is just the final objective. In my code, I have created a stack class because I wanted to feel the satisfaction of getting the same result using my own code. My problem is the stack is not reversing it is returning some weird characters that I don't have the keys on my keyboard to replicate.
The desired outcome should be word's reversed characters.
if the word is food the function should be returning doof. I have already compared the reversed stack to the original and printed the final statement. I fixed the char letters[];
If you're open to using a simple function instead of a Stack then you could use the following program since it is much more simple than your Stack version and hence less-error prone.
#include <iostream>
#include <string>
bool checkIfPalindroom(const std::string &str)
{
for(int i=0;i<(str.size()/2);i++)
{
if (str[i] != str[str.size() - i - 1])
{
return false;//if this if is satisfied even once, return false
}
}
return true;//if the control flow reaches here this will mean that no if was satisfied and hence return true
}
int main()
{
std::string myString = "Somearbitrarystring";
if(checkIfPalindroom(myString))//call the function
{
std::cout<<"The given string: "<<myString <<" is a palindrome"<<std::endl;
}
else
{
std::cout<<"The given string: "<<myString<<" is not a palindrome"<<std::endl;
}
return 0;
}
In one of my earlier lab exercises I implemented a simple stack program
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int count = 0; //stores the indexed number
string myStack[20]; //stores the data from file
string fileName;
fstream readFile;
string storeFile; //stores the data as a string
//function declarations/prototypes
void push(string aString);
string top();
string pop();
bool isEmpty();
int main(int argc, char *argv[])
{
fileName = "file.txt";
readFile.open(fileName); //attempts to read the file
while(readFile >> storeFile){
if(readFile.bad()) {
cerr << "File failed to read " << endl;
break; //loop terminates
} else {
push(storeFile); //pushes file to stack
}
}
readFile.close();
while(isEmpty() !=true) { //while file is not emptpy return the top stack.
cout << top() << endl;
pop();
}
return 0;
}
void push(string value) {
myStack[count] = value;
count++;
}
string pop() {
if(count < 0) {
return NULL;
} else {
count--; //decrement count for stack
return myStack[count]; //return top value
}
}
string top() { //returns the current element at the top of the stack
return myStack[count];
}
bool isEmpty() { //checks whether or not the stack is empty
if(count < 0) {
return true;
}
else return false;
}
I am now required to revisit this program, however, this time modify it so that If you attempt to push data onto a full stack you should create a new array of twice the current size, copy the data from the old stack to the new stack and then delete the old stack without using STL or classes. We are allowed to use dynamic memory
So if the input, for example, looked something like this with an array size of 2
push 1
push 1
push 1
push 1
push 1
pop
pop
push 1
Then the output would look like this
Stack doubled from 2 to 4.
Stack doubled from 4 to 8.
Stack contains 4 entries.
How would I implement this? Thankyou
Push attempt
int dataCount = 0;
const int SIZE = 5;
int capacity = 0;
int myStack[SIZE];
void push(int value)
{
if(SIZE == capacity) {
cout << "Doubling current size " << endl;
capacity = capacity * 2;
int* temp = new int[capacity];
copy_n(myStack, SIZE, temp);
std::swap(myStack, temp);
delete[] temp;
}
myStack[dataCount] = value;
dataCount++;
}
What you are asking for requires the stack array to be allocated in dynamic memory, eg:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int count = 0; //stores the indexed number
int capacity = 0; //stores the allocated size
string *myStack = NULL; //stores the data from file
//function declarations/prototypes
void push(const string &aString);
string top();
void pop();
bool isEmpty();
void cleanup();
int main(int argc, char *argv[])
{
string storeFile; //stores the data as a string
string fileName = "file.txt";
ifstream readFile(fileName); //attempts to read the file
while (readFile >> storeFile) {
push(storeFile); //pushes file to stack
}
if (readFile.bad()) {
cerr << "File failed to read " << endl;
}
readFile.close();
while (!isEmpty()) { //while file is not empty return the top stack.
cout << top() << endl;
pop();
}
cleanup();
return 0;
}
void push(const string &value) {
if (count == capacity) {
int newCapacity = (capacity == 0) ? 20 : (capacity * 2);
string *newStack = new string[newCapacity];
for(int i = 0; i < count; ++i) {
newStack[i] = myStack[i];
}
delete[] myStack;
myStack = newStack;
capacity = newCapacity;
}
myStack[count] = value;
++count;
}
string top() {
if (count <= 0) {
return string();
}
return myStack[count-1]; //return top value
}
void pop() {
if (count > 0) {
--count; //decrement count for stack
myStack[count] = "";
}
}
bool isEmpty() {
return (count < 1);
}
void cleanup() {
capacity = count = 0;
delete[] myStack;
myStack = NULL;
}
Create a class That has
-Array start pointer
-current size
-the current position of the stack pointer
a method that inflate the array , delete the previous one and copy the data of the previous
-have a method that push on the stack and one that pops on top of the stack
You can event handle shrinking the stack when pop is less than length of the stack /2
I have a class foo like this:
class foo
{
private:
int* a;
public:
foo()
{
a = new int[4];
cout << "a" << endl;
}
};
When I create new object named foo1 and then I debug, after the allocating line, it yields the result: a 0x005a4580 {-842150451}.
But when I replace all int-s by char-s in class definition, it yields an undesired result:
a 0x005694a0 "ÍÍÍÍýýýý\x6ŒÒ•\x5Ÿ"
that the size of a is now greater than 4.
I dont know what happened. Could you please give me an explanation?
Full code:
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String(char* data)
{
setSize(0);
while (*(data + size) != '\0')
size++;
this->data = new char[size];
//need to allocate memory for 'data' pointer because 'data' pointer is now on the stack and the data must be on the heap
memcpy(this->data, data, size * sizeof(char));
}
void operator=(String rhs)
{
if (this->data != NULL)
delete[] this->data, data = NULL;
this->data = new char[rhs.getSize()]; //allocate
memcpy(this->data, data, size * sizeof(char));
}
int getSize()
{
setSize(0);
while (*(data + size))
size++;
return size;
}
void setSize(int size)
{
this->size = size;
}
void display()
{
for (int i = 0; i < size; i++)
cout << *(data + i);
}
~String()
{
if (data != NULL)
delete[] data, data = NULL;
}
private:
char* data;
int size;
};
void main()
{
String a("abcd");
String b("1");
a.display();
cout << endl;
cout << b.getSize() << endl;
a = b;
cout << a.getSize() << endl;
system("pause");
}
Whatever you're using to look at a doesn't know how much you allocated. It just knows the type.
In the first version it sees int *, so it shows a single int.
In the second version it sees char *, so it assumes it's a C string and prints whatever is in memory up to the first '\0' byte.
I have the following stack data structure implementation in C++:
// file: Stack.h
#pragma once
#include <iostream>
#include <exception>
class CStack
{
private:
int counter;
int *data;
int currentmaxsize;
void adjust();
public:
void push(int value);
int pop();
int peek();
int getsize();
CStack();
~CStack();
};
// file: Stack.cpp
#include "Stack.h"
void CStack::adjust()
{
int *temp = new int[currentmaxsize];
for (int i = 0; i < counter; i++)
{
temp[i] = data[i];
}
delete data;
data = new int[currentmaxsize * 2];
for (int i = 0; i < counter; i++)
{
data[i] = temp[i];
}
delete temp;
currentmaxsize *= 2;
}
int CStack::getsize()
{
return counter;
}
void CStack::push(int value)
{
if (counter+1 == currentmaxsize)
{
adjust();
}
counter++;
data[counter] = value;
}
int CStack::peek()
{
return data[counter];
}
int CStack::pop()
{
if (counter > 0)
{
int ret = data[counter];
counter--;
return ret;
}
else if (counter == 0)
{
throw std::exception("cannot pop empty stack");
}
return 0xFFFFFFFF;
}
CStack::CStack()
{
data = new int[100];
currentmaxsize = 100;
counter = 0;
}
CStack::~CStack()
{
delete data;
}
This is a fairly standard stack implementation. The only thing that is different from the kind of stack you would see in most textbooks is the adjust() function, which reallocates the stack with a bigger size if the original boundary is reached.
I wrote the following driver for the data structure as well:
// file: driver.cpp
#include <iostream>
#include "Stack.h"
int main(int argc, char *argv[])
{
CStack stack;
for (int i = 0; i < 200; i++)
{
stack.push(i);
std::cout << "Pushed: " << i << std::endl;
//std::cout << "New stack size: " << stack.getsize() << std::endl;
}
int len = stack.getsize();
std::cout << "len = " << len << std::endl;
for (int i = 0; i < len; i++)
{
std::cout << "Popped: " << stack.pop() << std::endl;
//std::cout << "New stack size: " << stack.getsize() << std::endl;
}
return 0;
}
This works almost as I would expect it to, except this one value in the program output:
Popped: 100
Popped: 99
Popped: 7798895
Popped: 97
Popped: 96
It is always the value of the 98th element in the stack that has a bizarre value like this, and I don't know why it is - the adjust() function is being called when the stack hits 100 values, not 99, so I don't imagine it's a problem with the adjust function.
Your push and peek and probably other functions use counter as the index of the last element. But other parts of your code use counter as the number of elements so counter-1 would be the index of last. So data is lost during adjust
Select one design: The valid indexes are 0 through counter-1 inclusive or 0 though counter or 1 through counter (wasting position 0).
I only like the first of those choices but any one of them can work (your existing code is closest to being the third). Having different parts play by different rules doesn't work.
I see at least one bug. You are using delete after doing a new[].
new should be matched with delete and new[] should be matched with delete[]. Otherwise it's undefined behavior.
Also, you are doing unnecessary copying in your adjust function.
I am trying to create a dynamic string array in c++. When trying to display the contents of my dynamic string array to the console I receive this error:
Exception thrown at 0x0FD670B6 (msvcp140d.dll) in Assignment4.exe: 0xC0000005: Access violation reading location 0xDDDDDDDD.
Here is my code:
DynamicStringArray.h
#pragma once
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class DynamicStringArray
{
public:
DynamicStringArray();
DynamicStringArray(DynamicStringArray &array);
~DynamicStringArray();
int getSize();
void displayContents();
void addEntry(const string &addElement);
string getEntry(int index);
int deleteEntry(const string &deleteElement);
private:
string *dynamicArray;
int size;
};
DynamicStringArray.cpp
#include "stdafx.h"
#include "DynamicStringArray.h"
#include <string>
#include <iostream>
using namespace std;
DynamicStringArray::DynamicStringArray()
{
dynamicArray = NULL;
size = 0;
}
DynamicStringArray::DynamicStringArray(DynamicStringArray &array)
{
if (dynamicArray != NULL)
{
size = 0;
delete [] dynamicArray;
dynamicArray = NULL;
}
size = array.getSize();
dynamicArray = new string[size];
for (int i = 0; i < size; i++)
dynamicArray[i] = array.dynamicArray[i];
}
DynamicStringArray::~DynamicStringArray()
{
cout << "In destructor." << endl;
delete [] dynamicArray;
dynamicArray = NULL;
}
int DynamicStringArray::getSize()
{
return size;
}
void DynamicStringArray::displayContents()
{
if (size != 0)
for (int i = 0; i < size; i++)
cout << "Item-" << i << ": " << dynamicArray[i] << endl;
else
cout << "Array is empty." << endl;
}
void DynamicStringArray::addEntry(const string &addElement)
{
string *temp = new string[size + 1];
for (int i = 0; i < size; i++)
temp[i] = dynamicArray[i];
temp[size] = addElement;
size++;
delete [] dynamicArray;
dynamicArray = temp;
delete[] temp;
}
string DynamicStringArray::getEntry(int index)
{
if ((index >= 0) && (index < size))
{
return dynamicArray[index];
}
return NULL;
}
int DynamicStringArray::deleteEntry(const string &deleteElement)
{
if(size == 0)
{
return false;
}
for (int i = 0; i < size; i++)
{
if (dynamicArray[i] == deleteElement)
{
string *temp = new string[size - 1];
for (int x = 0; x < size - 1; ++x)
{
if (x < i)
temp[x] = dynamicArray[x];
else
temp[x] = dynamicArray[x + 1];
}
delete[] dynamicArray;
dynamicArray = temp;
delete[] temp;
--size;
return true;
}
}
return false;
}
main:
int main()
{
DynamicStringArray dsArray1;
cout << "dsArray1.displayContents():" << endl;
dsArray1.displayContents(); // Should indicate array is empty
cout << "Display dsArray1.getSize()= " << dsArray1.getSize() << endl;
dsArray1.addEntry("Entry-A");
dsArray1.displayContents();
dsArray1.addEntry("Entry-B");
dsArray1.displayContents();
dsArray1.addEntry("Entry-C");
dsArray1.displayContents();
return 0;
}
Can anyone tell me what I am doing wrong. How can i fix this problem?
Please note that all of this is already available by utilizing
std::vector<std::string>. The std::vector class is the dynamic array class that C++ provides, and there is little to no reason to make home-made versions of what is available to you.
Having said this, one glaring issue is that your copy constructor is incorrect. The dynamicArray is uninitialized, but you use it here:
if (dynamicArray != NULL)
There is no guarantee what value dynamicArray has. The fix is to remove this entire block of code in the copy constructor:
if (dynamicArray != NULL)
{
size = 0;
delete [] dynamicArray;
dynamicArray = NULL;
}
Since the copy constructor constructs a brand new object, there is no reason to "pretest" for a NULL pointer and thus do unnecessary work. Remember that the object did not exist, so there is nothing preliminary to do.
The second issue is that you're issuing a delete [] temp; call in the addEntry and deleteEntry functions. Remove these lines, as you are deallocating the memory that you've just assigned to dynamicArray.
The third issue is that you're missing the user-defined assignment operator. The assignment operator has the following signature, and you need to provide the implementation:
DynamicStringArray& operator=(const DynamicStringArray& );
Without this function, assigning a DynamicStringArray to another DynamicStringArray will cause memory leaks and double deallocation of memory when both objects go out of scope.
One implementation can use the copy / swap idiom:
#include <algorithm>
//...
DynamicStringArray& DynamicStringArray::operator=(const DynamicStringArray& rhs)
{
DynamicStringArray temp(rhs);
std::swap(temp.dynamicArray, dynamicArray);
std::swap(temp.size, size);
return *this;
}
Another issue is this:
string DynamicStringArray::getEntry(int index)
{
if ((index >= 0) && (index < size))
{
return dynamicArray[index];
}
return NULL; // <-- Undefined behavior if this is done
}
It is undefined behavior to assign a std::string object with NULL. Either return an empty string, or throw an exception if the index is out of bounds.
In conclusion, I highly recommend that you read up on the Rule of 3 when it comes to designing classes that must implement correct copy semantics.