Stosy i Kolejki

0

Witam,
Czy ktoś jest mi wstanie pomóc, przymierzam się do zrobienia zadania https://pl.spoj.com/problems/PROGC03/ do tej pory napisałem coś takiego. Moje pytanie jest następujące jak mogę utworzyć tablicę stosów?

#include<iostream>
using namespace std;
const int maxSize = 20;
struct Stack
{
    int tab[maxSize];
    int top;
};
void new_s(Stack& s, int x)
{
    
    s.top = -1;
}
bool isEmpty(Stack& s)
{
    if (s.top == -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool isFull(Stack& s)
{
    if (s.top == maxSize-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void push(Stack& s , int x)
{
    if (isFull(s) == true)
    {
        cout << "nie mozna dodac elementu do stosu";
    }
    else
    {
        s.tab[s.top+1] = x;
        s.top = s.top+1;
    }
}
void pop(Stack& s)
{
    if(!isEmpty(s))
    {
        s.top = s.top -1;
    }
    else
    {
        cout << "stos jest pusty" << endl;
    }
}
int top(Stack& s)
{
    return s.tab[s.top];
}
int main()
{
    
}
0

Chodzi Ci o Stack stack_tab[] = {Stack(), Stack(), Stack()};?

0
Stack *tabS = new Stack[10];

Myślałem nad tablicą dynamiczną ale za bardzo nie wiem jak to zaimplementować w kodzie. Żebym mógł stworzyć kilka Stosów każdy o innym indeksie i dodawać do nich jakieś wartości.

0

to może tak:

class Stack
{
    int tab[maxSize];
    int top;
   void pop();
   int top();
   void push(int x);
   //reszta metod
};
...
std::vector<Stack> stack_vec;
stack_vec.push_back(Stack());
stack_vec[0].push(12);
... 

0

Nie lepiej klasę zrobić, a nie pisać w C?:) Ale zakładając, że Masz poprawną logikę, to trzeba stworzyć nowy obiekt, a potem normalnie tablicę stosów. Czemu tam jest ograniczenie do dwudziestu elementów, jak w zadaniu jest dziesięć? Na początek tak (kompiluje się, przynajmniej:)):

#include<iostream>
using namespace std;
const int maxSize = 20;
struct Stack
{
    int tab[maxSize];
    int top;
};
Stack new_s()
{

    Stack stack = Stack();
    stack.top = 10;
    return stack;
}
bool isEmpty(Stack& s)
{
    if (s.top == -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool isFull(Stack& s)
{
    if (s.top == maxSize-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void push(Stack& s , int x)
{
    if (isFull(s) == true)
    {
        cout << "nie mozna dodac elementu do stosu";
    }
    else
    {
        s.tab[s.top+1] = x;
        s.top = s.top+1;
    }
}
void pop(Stack& s)
{
    if(!isEmpty(s))
    {
        s.top = s.top -1;
    }
    else
    {
        cout << "stos jest pusty" << endl;
    }
}
int top(Stack& s)
{
    return s.tab[s.top];
}
int main()
{
	Stack s = new_s();
	Stack st_arr [10];
}

0

Racja z tym ograniczeniem mój błąd, jak by teraz wyglądało użycie metody push w tak zaimplementowanej tablicy?

0

Trochę się pomyliłem;), zaczynamy od top = 0, poprawiłem też parę Twoich bugów:); Spróbuj to potestować i powalczyć z zadaniem.

#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";
    }
    else
    {
		s.tab[s.top++] = x;
    }
}
void pop(Stack& s)
{
    if (isEmpty(s))
		cout << "error: stact is empty";
	else
		s.top--;
}
int top(Stack& s)
{
    return s.tab[s.top - 1];
}

void print_s(Stack&s) {
	for (int i = 0; i < s.top; i++) {
		cout<< s.tab[i]<<" ";
		}
}
int main()
{
	Stack s = new_s();
	push(s, 1);
	push(s, 2);
	push(s, 3);
	print_s(s);
	pop(s);
	cout << "\n";
	print_s(s);
	Stack st_arr [10];
}
0

Stworzyłem metodę delete_s - usuwającą cały stack oraz stackToStack która ma przenieść element ze szczytu stosu numer s na stos numer k i tutaj coś się wywala ;(

#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: stact is full" << endl;
		else
        {
            int temp = top(s);
            pop(s);
            push(k, temp);
        }
}
int main()
{
    Stack st_arr[maxSize];
    Stack s = new_s();
    push(s, 1);
    push(s, 2);
    push(s, 3);
    print_s(s);
    pop(s);
    print_s(s);
    delete_s(s);
    print_s(s);
    Stack k = new_s();
    push(k,10);
    print_s(k);
    stackToStack(s,k);
    print_s(s);
    print_s(k);
}
0

delete jest OK. Przeniesienie: "Przeniesienie elementu ze szczytu stosu numer i na stos numer j:".

0

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);
}

1

A nie tak powinno być?

    if((isFull(q) || isEmpty(s))
        cout << "error: wrong command" << endl;
0

Tak jest, wszystko działa, teraz tylko kombinować jak to przepuścić przez sprawdzarkę. Wielkie dzięki za pomoc !!!

0

Finalna wersja jak by komuś zależało na gotowcu ;)

#include<iostream>
using namespace std;
const int maxSizeS = 10;
const int maxSizeQ = 11;
struct Stack
{
    int tab[maxSizeS];
    int top;
};
void new_s(Stack& s)
{
    s.top = 0;
}
bool isEmpty(Stack& s){
    return s.top == 0;
}
bool isFull(Stack& s)
{
    return s.top >= maxSizeS;
}
bool push(Stack& s , int x)
{
    if (isFull(s))
        return false;
    s.tab[s.top++] = x;
    return true;
}

bool pop(Stack& s)
{
    if (isEmpty(s))
        return false;
    s.top--;
    return true;
}
int top(Stack& s)
{
    return s.tab[s.top - 1];
}

bool print_s(Stack& s)
{
	if (isEmpty(s))
		return false;
	for (int i = 0; i < s.top; i++)
		cout << s.tab[i] << " ";
		cout<<endl;
	return true;
}
void delete_s(Stack& s)
{
    new_s(s);
}
bool stackToStack(Stack& s, Stack &k)
{
    if(isEmpty(s) || isFull(k))
        return false;
    int temp = top(s);
    pop(s);
    push(k,temp);
    return true;
}
struct Queue
{
    int tab[maxSizeQ];
    int rear;
    int front;
};
void new_q(Queue& q)
{
    q.front=0;
    q.rear=-1;
}
int front(Queue& q)
{
    return q.tab[q.front];
}
bool isEmpty(Queue &q)
{
    if((q.rear+1)%maxSizeQ == q.front)
        return true;
        else
        return false;
}
bool isFull(Queue &q)
{
    if((q.rear+2)%maxSizeQ == q.front)
        return true;
        else
        return false;
}
bool enqueue(Queue &q, int x)
{
    if(isFull(q))
        return false;
    ++q.rear %= maxSizeQ;
    q.tab[q.rear]=x;
    return true;
}
bool dequeue(Queue &q)
{
    if(isEmpty(q))
        return false;
    ++q.front %= maxSizeQ;
    return true;
}
int first(Queue &q)
{
    return q.tab[q.front];
}
bool print_q(Queue &q)
{
    if(isEmpty(q))
        return false;
    int index = q.rear;
    while(index!=q.front)
    {
        cout<<q.tab[index]<<" ";
        --index += maxSizeQ;
        index %= maxSizeQ;
    }
    cout<<q.tab[q.front]<<endl;
    return true;
}
void delete_q(Queue& q)
{
	new_q(q);
}
bool stackToQueue(Stack& s, Queue& q)
{
    if(isEmpty(s) || isFull(q))
        return false;

        int temp = top(s);
        pop(s);
        enqueue(q,temp);
        return true;
}
bool queueToStack(Queue& q, Stack& s)
{
    if(isEmpty(q) || isFull(s))
        return false;
    int temp = front(q);
    dequeue(q);
    push(s,temp);
        return true;
}
bool queueToQueue(Queue& q, Queue& q2)
{
    if(isEmpty(q) || isFull(q2))
        return false;
    int temp = front(q);
    dequeue(q);
    enqueue(q2,temp);
    return true;
}
int main()
{
	Stack s_arr[maxSizeS];
	Queue q_arr[maxSizeS];
	string option;
	int i, j, e;
	while (cin >> option)
	{
		if (option == "new_s")
		{
			cin >> i;
			new_s(s_arr[i]);
		}
		else if (option == "push")
		{
			cin >> i >> e;
			if (!push(s_arr[i], e))
				cout << "error: stack is full" << endl;
		}
		else if (option == "pop")
		{
			cin >> i;
			if (!pop(s_arr[i]))
				cout << "error: stack is empty" << endl;
		}
		else if (option == "stack->stack")
		{
			cin >> i >> j;
			if (!stackToStack(s_arr[i], s_arr[j]))
				cout << "error wrong command" << endl;
		}
		else if (option == "delete_s")
		{
			cin >> i;
			delete_s(s_arr[i]);
		}
		else if (option == "print_s")
		{
			cin >> i;
			if (isEmpty(s_arr[i]))
                cout << "empty" << endl;
            else
                print_s(s_arr[i]);
		}
		else if (option == "new_q")
		{
			cin >> i;
			new_q(q_arr[i]);
		}
		else if (option == "enqueue")
		{
			cin >> i >> e;
			if (!enqueue(q_arr[i], e))
				cout << "error: queue is full" << endl;
		}
		else if (option == "dequeue")
		{
			cin >> i;
			if (!dequeue(q_arr[i]))
				cout << "error: queue is empty" << endl;
		}
		else if (option == "queue->queue")
		{
			cin >> i >> j;
			if (!queueToQueue(q_arr[i], q_arr[j]))
				cout << "error: wrong command" << endl;
		}
		else if (option == "delete_q")
		{
			cin >> i;
			delete_q(q_arr[i]);
		}
		else if (option == "print_q")
		{
			cin >> i;
			if (!print_q(q_arr[i]))
				cout << "empty" << endl;
		}
		else if (option == "stack->queue")
		{
			cin >> i >> j;
			if (!stackToQueue(s_arr[i], q_arr[j]))
				cout << "error: wrong command" << endl;
		}
		else if (option == "queue->stack")
		{
			cin >> i >> j;
			if (!queueToStack(q_arr[i], s_arr[j]))
				cout << "error: wrong command" << endl;
		}
	}
}

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