Zadanie ze SPOJ-a – kolejka przepełnia się

0

spoj https://www.spoj.com/WWSIASD/problems/ASD_4_2/ . Przy wpisaniu Enqueue12 wyskakuje komunikat queue is full poniżej kod.

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool isempty() {
  if (front == -1 && rear == -1)
    return true;
  else
    return false;
}
 
void enqueue(int value) {
  if (rear == SIZE - 1)
    cout << "Error: queue is full\n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}
 
void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";
  }
}
 
void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
  cout << " \n";
}
 
int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;
 
        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }
 
    return 0;
}

analizując swój kod stwierdziłem że warunek na queue is full nie ma sensu gdyż usuwa rzeczy z kolejki robiąc front++ oto modyfikacja

void enqueue(int value) {
  if (rear == SIZE)
    cout << "Error: queue is full\n";
  else {
    if (front == -1)
      front = 0;
    rear++;
    A[rear] = value;
    cout << "--->\n";
  }
}

dequeue

void dequeue() {
  if (isempty()){
    cout << "Error: queue is empty\n";
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front++;
     cout << A[front - 1] << "\n";

Display

void displayQueue() {
  if (isempty())
    cout << "Print: Queue is empty\n";
  else {
      cout << "Print:";
    for (int i = front; i <= rear; i++)
      cout <<" "<< A[i];
  }
else{
for(int j=rear;j<=front;j++)
cout<<" " << A[j];
}
  cout << " \n";
}
  }
}
2

const int SIZE = 10;

a wpiszujesz 12

czego oczekujesz?

po prostu debuguj kod

0

nie , nie , ja daję dokładnie takie samy liczby jak w przykładowym zadaniu na SPOJU link umieściłemw opisie , oczywiście pomiędzy dodawaniem elementów jest także ich usuwanie. Na spoju w momencie dodania elementu poleceniem Enqueue 12 12 wpisuje się w kolejkę , w moim programie po wpisaniu polecenia Enqueue 12 wyskakuje informacja enqueue is full rozumiesz ? Moje pytanie brzmi gdzie jest problem miałem już kilka odpowiedzi ale one są dla mnie w ciągu dalszym tajemnicą .

1

ok, ale czemu nie sprobujesz sam debugowac kodu? Mamy to zrobic za Ciebie?

1
  1. rear i front powinny przyjmować wartości tylko od 0 do SIZE - 1, wartość początkowa powinna być 0
  2. isempty to po prostu rear == front
  3. isfull to powinno być np rear == (front + 1) % SIZE
  4. podczas dodawania elementu front = (front + 1) % SIZE
  5. podczas usuwania elementu rear = (rear + 1) % SIZE
  6. SIZE lepiej ustawić na 11 bo w przypadku bufora cyklicznego, potrzebna jest nadmiarowa komórka, by dało się odróżnić pełny bufor od pustego.
0

fasadin ja debuguje kod i nie wiem w czym jest problem jakbym wiedział jak go rozwiązać to bym wam tylnej części ciała nie zwracał. Jeśli kogoś urazam swoimi pytaniami to z góry przepraszam.

0

Jak próbuje czytać ten kod, to nie mam pojęcia co autor miał na myśli.
Nie mam siły próbować ustalić co jest nie tak, lepiej pokazać jak może to wyglądać:

class Queue {
public:
    static constexpr size_t SIZE = 11;

    bool isEmpty() const
    {
        return head == tail;
    }

    bool isFull() const
    {
        return next(head) == tail;
    }

    bool enqueue(int x)
    {
        if (isFull())
            return false;
        tab[head] = x;
        head = next(head);
        return true;
    }

    bool dequeue(int& x)
    {
        if (isEmpty())
            return false;
        x = tab[tail];
        tail = next(tail);
        return true;
    }

    template <typename F>
    void forEach(F f) const
    {
        for (auto p = tail; p != head; p = next(p))
            f(tab[p]);
    }

private:
    static size_t next(size_t i)
    {
        if (i + 1 == SIZE)
            return 0;
        return i + 1;
    }

private:
    size_t head = 0;
    size_t tail = 0;
    std::array<int, SIZE> tab;
};
0

Cześć oto mój najnowszy , zmodyfikowany kod:Problem polega na tym że
Po wpisaniu Enqueue12 wszystko jest ok .
-po wpisaniu dequeue powinno się usuwać 10 , a wyskakuje zamiast 10 wyskakuje liczba 0.Potem przy wpisaniu** dequeue** wyskakuje prawidłowo liczba 12.
-Po wpisaniu słowa print zamiast queue is empty wyskakuje 0 (LINK DO SPOJA WYZEJ) co jest nie tak z tym kodzikem ? .

#include <iostream>
using namespace std;
#include <string>
const int SIZE = 10;
 
int A[SIZE];
int front = -1;
int rear = -1;
 
bool IsEmpty() {
  
    return (front == -1 && rear == -1); 
}
 
bool IsFull()
{
	return (rear+1)%SIZE == front ? true : false;
}

void enqueue(int x) {
  
	
	
	if (IsFull()){
    cout << "Error: queue is full\n";
	return;
  }

  if(IsEmpty())
  {
	  front = rear = 0; 
  }
  else {
    rear = (rear+1)%SIZE;
   
  }
  A[rear] = x;
   cout << "--->\n";
}
 

void dequeue() {
  if (IsEmpty()){
    cout << "Error: queue is empty\n";
	return;
    }
  else if (front == rear){
     cout << A[front] << "\n";
    front = -1;
    rear =  -1;
    }
  else{
    front = (front+1)%SIZE;
	cout << A[front - 1] << "\n";
	
  }
}

int Front(){

	if(front == -1){
		cout<<"Error: cannot return front from empty queue\n";
		return -1;
	}
	return A[front];
}
 

void displayQueue() 
{
  int count = (rear+SIZE-front)%SIZE + 1;
  

  for(int i = 0; i <count; i++)
  {
	  int index = (front+i) % SIZE;
	  cout<<A[index]<<" ";
  }
  cout<<"\n\n";
}
 
int main()
{
    string str;
    while (cin >> str) {
        int wlozyc;
 
        if (str == "Enqueue") {
            if (cin >> wlozyc) {
                enqueue(wlozyc);
            }
        } else if (str == "Dequeue") {
            dequeue();
        } else  if (str == "Print") {
            displayQueue();
        }
    }
 
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1