Reputation: 53
This program should takes a postfix arithmetic expression then compiles the values of that expression.. Each time an integer is read, its gonna get pushed into the stack.. Otherwise, Two integers would be popped if +,-,* is read.
class Stack {
Node *head;
public:
Stack() {
head = NULL;
};
void push(int data);
int pop();
bool isEmpty();
void print();
};
void Stack::push(int data)
{
Node * temp = new Node(data);
temp->next = head;
head = temp;
delete temp;
}
int Stack::pop()
{
int x = head->data;
head = head->next;
return x;
}
bool Stack::isEmpty(){
return head == NULL;
}
void Stack::print(){
Node * temp = head;
while (temp != NULL){
cout << temp->data << " ";
temp = temp->next;
}
delete temp;
}
int main() {
Stack st;
char exp [] = "23+", c;
int i, a;
for (i = 0; exp[i] != '\0'; i++){
c = exp[i];
if (c == '+'&&!st.isEmpty()){
a = st.pop() + st.pop();
st.push(a);
}
else if (c == '-'&&!st.isEmpty()){
a = st.pop() - st.pop();
st.push(a);
}
else if (c == '/'&&!st.isEmpty()){
a = st.pop() / st.pop();
st.push(a);
}
else if (c == '*'&&!st.isEmpty()){
a = st.pop() * st.pop();
st.push(a);
}
else if (c == '0')
st.push(0);
else if (c == '1')
st.push(1);
else if (c == '2')
st.push(2);
else if (c == '3')
st.push(3);
else if (c == '4')
st.push(4);
else if (c == '5')
st.push(5);
else if (c == '6')
st.push(6);
else if (c == '7')
st.push(7);
else if (c == '8')
st.push(8);
else if (c == '9')
st.push(9);
cout << c << endl;
st.print();
}
cin >> a;
return 0;
}
When I call the print function in main, I get an infinite loop as an output.. I tried looking for the thing that's causing an infinite loop but I couldn't find it.
Upvotes: 1
Views: 300
Reputation: 206577
Problems I see:
Using delete
in push()
:
void Stack::push(int data)
{
Node * temp = new Node(data);
temp->next = head;
head = temp;
delete temp; // This needs to go.
}
Not using delete
in pop()
:
int Stack::pop()
{
// Problem 1.
// What if head is NULL?
int x = head->data;
// Problem 2
// The old value of head is gone. It's a memory leak.
head = head->next;
return x;
}
You need:
int Stack::pop()
{
if ( head != NULL )
{
int x = head->data;
Node * temp = head;
head = head->next;
delete temp;
return x;
}
else
{
// Figure out what you want to do if head is NULL
}
}
Using delete
in print()
.
void Stack::print(){
Node * temp = head;
while (temp != NULL){
cout << temp->data << " ";
temp = temp->next;
}
delete temp; // This needs to go.
}
Missing user defined destructor. You need to delete the objects in the object. Otherwise, you are leaking memory. Something along the lines of the code below should work.
Stack::~Stack()
{
while (head)
{
Node * temp = head;
head = head->next;
delete temp;
}
}
Upvotes: 3
Reputation: 8313
Here is suggestion for push
and pop
.
try to understand the logic.
void Stack::push(int data)
{
Node * temp = new Node(data);
temp->next=head;
head=temp;
//Do not delete temp; deleting temp will delete the new added Node
}
int Stack::pop()
{
Node* temp = Head;
int x=head->data;
head=head->next;
delete temp; //here you free the released memory.
return x;
}
Also, instead all the if/else, for each digit in your code, you can do as follows:
else if(c>=0 && c<=9){
st.push(c-'0');
}
Upvotes: 1