Mar 20, 2016 at 6:59am UTC
Hi, I am trying to use linked-list to print a sentence backwards.
I got my program to run, but I am not getting the right output (it's not printing backwards).
How can I fix this problem?
Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#ifndef STACKS_H
#define STACKS_H
#include<string>
#include<stack>
#include<iostream>
using namespace std;
struct nodeType
{
char info;
nodeType * link;
};
class linkedStackType
{
public :
void initializeStack();
bool isEmptyStack();
void push(const char & newItem);
char top();
void pop();
linkedStackType();
//linkedStackType(const linkedStackType & otherStack);
//~linkedStackType();
private :
nodeType * stackTop;
};
#endif // STACKS_H
//Stacks.cpp file
#include<stack>
#include<string>
#include "Stacks.h"
#include<iostream>
using namespace std;
//Empty Stack
bool linkedStackType::isEmptyStack()
{
return (stackTop == NULL);
}
//Default Constructor
linkedStackType::linkedStackType()
{
stackTop = NULL;
}
//Destroy Stack
void linkedStackType::initializeStack()
{
nodeType * temp;
while (stackTop != NULL)
{
temp = stackTop;
stackTop = stackTop->link;
delete temp;
}
}
void linkedStackType::push(const char & newElement)
{
nodeType *newNode; //pointer to create the new node
newNode = new nodeType; //create the node
newNode->info = newElement; //store newElement in the node
newNode->link = stackTop; //insert newNode before stackTop
stackTop = newNode; //set stackTop to point to the
}
char linkedStackType::top()
{
//assert(stackTop != NULL); //if stack is empty,
//terminate the program
return stackTop->info; //return the top element
}
void linkedStackType::pop()
{
nodeType *temp; //pointer to deallocate memory
if (stackTop != NULL)
{
temp = stackTop; //set temp to point to the top node
stackTop = stackTop->link; //advance stackTop to the
//next node
delete temp; //delete the top node
}
else
cout << "Cannot remove from an empty stack." << endl;
}
//mIn.cpp file
#include "Stacks.h"
#include<stack>
#include<string>
#include<iostream>
using namespace std;
int main()
{
char Words;
linkedStackType stack;
cout << "Please enter a sentence. \n" ;
cin.get(Words);
stack.push(Words);
while (!stack.isEmptyStack())
{
cout <<stack.top() << " " ;
stack.pop();
}
return 0;
}
Last edited on Mar 20, 2016 at 7:23am UTC
Mar 20, 2016 at 8:06am UTC
1 2 3 4
char Words;
linkedStackType stack;
cout << "Please enter a sentence. \n" ;
cin.get(Words);
You're telling the user to enter in a string but you're storing the string in a
char
.
Last edited on Mar 20, 2016 at 8:06am UTC
Mar 20, 2016 at 8:06am UTC
the problem is not in your stack, it is in the input, it only stores the first letter of input sentence to Words, therefore it only pushes the first letter of the sentence into the stack
EDIT: even if you do, you need to split the words in the sentence first before pushing it to the stack
Last edited on Mar 20, 2016 at 8:11am UTC