Zadanie już prawie na ukończeniu, dodałem kolejkę oraz nowe metody które działają poprawnie ale gdy chce np. z pustego stosu przenieść element do kolejki powinno wypisać tylko "error: wrong command" u mnie dodatkowo wykonuje niechciane operacje a przecież jest if/else... Help!
#include<iostream>
using namespace std;
const int maxSize = 10;
struct Stack
{
int tab[maxSize];
int top;
};
Stack new_s()
{
Stack stack = Stack();
stack.top = 0;
return stack;
}
bool isEmpty(Stack& s){
return s.top == 0;
}
bool isFull(Stack& s)
{
return s.top >= maxSize;
}
void push(Stack& s , int x)
{
if (isFull(s))
{
cout << "error: stack is full" << endl;
}
else
{
s.tab[s.top++] = x;
}
}
void pop(Stack& s)
{
if (isEmpty(s))
cout << "error: stact is empty" << endl;
else
s.top--;
}
int top(Stack& s)
{
return s.tab[s.top - 1];
}
void print_s(Stack& s) {
if(!isEmpty(s))
{
for (int i = 0; i < s.top; i++)
{
cout<< s.tab[i]<<" ";
}
cout<<endl;
}
else
cout << "error: stact is empty" << endl;
}
void delete_s(Stack& s)
{
while(!isEmpty(s))
pop(s);
}
void stackToStack(Stack& s, Stack &k)
{
if(isFull(s) || isFull(k))
cout << "error: stack is full" << endl;
else
{
int temp = top(s);
pop(s);
push(k,temp);
}
}
struct Queue
{
int tab[maxSize];
int rear;
int front;
};
Queue new_q()
{
Queue queue = Queue();
queue.front=0;
queue.rear=-1;
return queue;
}
int front(Queue& q)
{
return q.tab[q.front];
}
bool isEmpty(Queue &q)
{
if((q.rear+1)%maxSize == q.front)
return true;
else
return false;
}
bool isFull(Queue &q)
{
if((q.rear+2)%maxSize == q.front)
return true;
else
return false;
}
void enqueue(Queue &q, int x)
{
if(!isFull(q))
{
++q.rear % maxSize;
q.tab[q.rear]=x;
}
else
cout << "error: queue is full" << endl;
}
void dequeue(Queue &q)
{
if(!isEmpty(q))
++q.front % maxSize;
else
cout << "error: queue is empty" << endl;
}
int first(Queue &q)
{
return q.tab[q.front];
}
void print_q(Queue &q)
{
if(!isEmpty(q))
{
int index = q.front;
while(index!=q.rear)
{
cout<<q.tab[index]<<" ";
++index %= maxSize;
}
cout<<q.tab[q.rear]<<endl;
}
else
cout << "empty" << endl;
}
void delete_q(Queue& q)
{
while(!isEmpty(q))
dequeue(q);
}
void stackToQueue(Stack& s, Queue& q)
{
if((isFull(q) || isFull(s)) && (!isFull(q) || !isFull(s)))
cout << "error: wrong command" << endl;
else
{
int temp = top(s);
pop(s);
enqueue(q,temp);
}
}
void queueToStack(Queue& q, Stack& s)
{
if((isFull(q) || isFull(s)) && (!isFull(q) || !isFull(s)))
cout << "error: wrong command" << endl;
else
{
int temp = front(q);
dequeue(q);
push(s,temp);
}
}
void queueToQueue(Queue& q, Queue& q2)
{
if((isFull(q) || isFull(q)) && (!isFull(q) || !isFull(q)))
cout << "error: wrong command" << endl;
else
{
int temp = front(q);
dequeue(q);
enqueue(q2,temp);
}
}
int main()
{
Queue q = new_q();
enqueue(q,5);
//enqueue(q,8);
Stack s = new_s();
//push(s,1);
//push(s,2);
print_q(q);
print_s(s);
queueToStack(q,s);
print_q(q);
print_s(s);
Queue q2 = new_q();
enqueue(q2,20);
queueToQueue(q,q2);
print_q(q2);
}