We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Queue Class

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;
}
2
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
eavila
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

gusano79
Practically a Master Poster
675 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6

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.

eavila
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

gusano79
Practically a Master Poster
675 posts since May 2004
Reputation Points: 193
Solved Threads: 108
Skill Endorsements: 6

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0763 seconds using 2.72MB