Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

c++ - I made a queue management system and created a function to add two queues

Here's my code. I created the function to add two queues using the operation overloading. For some reson it always returns an empty

#include <iostream>
using namespace std; 

class Queue { 
    int size; 
    int* queue; 
    
    public:
    Queue() { 
        size = 0;
        queue = new int[100];
    }
    //adds an item to  the queue`enter code here`
    void add(int data) { 
        queue[size] = data; 
        size++;
    }
    //removes an item from the queue
    void remove() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        else { 
            for (int i = 0; i < size - 1; i++) { 
                queue[i] = queue[i + 1]; 
            } 
            size--; 
        } 
    } 
    // a function to display the queue
    void print() { 
        if (size == 0) { 
            cout << "Queue is empty"<<endl; 
            return; 
        } 
        for (int i = 0; i < size; i++) { 
            cout<<queue[i]<<" <- ";
        } 
        cout << endl;
    }

This is the function to add the queues. It is not returning an error. But whenever I tried to add the queues it always return an empty queue. What could be the problem

  Queue operator+(Queue &obj){
      Queue res;
      res.queue = *queue + obj.queue;
      return res;
  }
    
}; 

Here starts the main function.

 int main() { 
    Queue q1; 
    q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
    Queue q2;
    q2.add(3); q2.add(66); q2.add(128);  q2.add(5);
    Queue q3 = q1+q2;
    q3.print();

    return 0; 
} 
question from:https://stackoverflow.com/questions/65644361/i-made-a-queue-management-system-and-created-a-function-to-add-two-queues

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code should do something like this. This code isn't going to win any prizes for style, I'm just trying to show you what is required

Queue operator+(Queue &obj){
  Queue res;
  res.size = size + obj.size; // work out the new queues size
  res.queue = new int[100]; // allocate memory for new queue
  for (int i = 0; i < size; ++i) // copy from first queue to new queue
      res.queue[i] = queue[i];
  for (int i = 0; i < obj.size; ++i) // copy from second queue to new queue
      res.queue[size + i] = obj.queue[i];
  return res;
}

As I said there are all sorts of things wrong with this code, and with the other code you have written. But it will hopefully give you some idea of what you need to do.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...