1,105,263 Community Members

Queue Class

Member Avatar
eavila
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey, so as you can tell, this is my first attempt at writing a class. I put in a mock int main() in order to follow what exactly is going wrong. As far as I can tell, every time the struct in push is used, it always has the same address. The idea is 'first' is the first struct that's pushed onto the queue (using setValues) and 'last' is the most current value pushed onto the queue. I haven't really gotten to working with pop, but I don't need it to return a value, in case you're wondering. Really appreciate any and all help...
And sorry if my code offends ;)

//Postfix with Objects
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;

struct entry{
            bool operation;
            char op;
            int num;
            entry * next;
};
class Queue{
      public:
             entry first;
             entry * last;
             void setValues(entry);
             void pop();
             void push(entry);
             entry front();
             bool empty();
             };

void Queue::setValues(struct entry first1){
     first=first1;
     last=&first;
     }
  
void Queue::pop(){
     entry * temp1=new entry;
     temp1=last;
     int i=0;
     if (last==0){
                  cout<<"QUEUE EMPTY"<<endl;
                  return;
                  }
     if(last->next==0){
              last=0;
              return;
              }
              cout<<first.num<<" " <<first.next<<endl;
     entry returner;
     while (temp1->next!=0){
              temp1=temp1->next;
              i++;
     }
     i--;
     temp1=last;
     while (i!=0){
              temp1=temp1->next;
              i--;
     }
     returner.num=(temp1->num);
     returner.operation=(temp1->operation);
     returner.op=(temp1->op);
     returner.next=0;
     first=returner;
}
     
void Queue::push(struct entry x){
     x.next=last;
     last=&x;
}
     
entry Queue::front(){ 
     return first;
}
      
bool Queue::empty(){
     if(last==0)
           return true;
     else 
          return false;
}

int main() {
entry a = {false, 'a', 1, NULL};
entry b = {true, 'b', 2, NULL};
entry c = {false, 'c', 3, NULL};

Queue myQueue;
myQueue.setValues(a);
myQueue.push(b);
myQueue.push(c);

cout << myQueue.last->op << endl <<
myQueue.last->next->op << endl <<
myQueue.last->next->next->op << endl;
return 0;
}
Member Avatar
gusano79
Practically a Posting Shark
816 posts since May 2004
Reputation Points: 203 [?]
Q&As Helped to Solve: 131 [?]
Skill Endorsements: 10 [?]
 
0
 

I see that you're using pointers to entry in some places, and not others... change your code so it's consistent, i.e. use entry* everywhere in your Queue and related classes.

I tested this, and your code returns the following when it's all pointers:

c
b
a

Member Avatar
eavila
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ya, I ended up doing just that, and it seems to be working fine. Still kind of curious as to why my first implementation did't work, but whatever. Thanks for looking it over! If anyone wants the updated code with first as a pointer, let me know.

Member Avatar
gusano79
Practically a Posting Shark
816 posts since May 2004
Reputation Points: 203 [?]
Q&As Helped to Solve: 131 [?]
Skill Endorsements: 10 [?]
 
0
 

Ya, I ended up doing just that, and it seems to be working fine. Still kind of curious as to why my first implementation did't work, but whatever. Thanks for looking it over! If anyone wants the updated code with first as a pointer, let me know.

I didn't spend too much time diagnosing exactly where the problem was, but I'm 99% confident that it has to do with the fact that the non-pointers are getting passed by value, so you're making copies instead of referring to the original objects.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: