Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Stack {
- int* S;
- int top;
- };
- void push(Stack s, int &top, int wartosc) {
- top++;
- s.S[top] = wartosc;
- }
- int pop(Stack s,int &top) {
- int el;
- if (top > -1) {
- el = s.S[top];
- top--;
- } else {
- cout << "Blad" << endl;
- }
- return el;
- }
- void usun(Stack &s, int n) {
- int* t;
- t = new int[n];
- Stack tmp;
- tmp.S = t;
- int wartosc;
- tmp.top = -1;
- for (int i = 0; i < n-1; ++i) {
- push(tmp,tmp.top,pop(s,s.top));
- }
- if (s.top < 0) {
- cout << "Stos nie ma tylu elementow!" << endl;
- } else {
- //
- cout << "Pomocniczy:"<<endl;
- for (int i = 0; i < n-1; i++) {
- cout << tmp.S[i] << " ";
- }
- cout << endl;
- // KONIEC
- pop(s,s.top);
- for (int i = 0; i <= n; i++) {
- push(s,s.top,pop(tmp,tmp.top));
- }
- }
- }
- int main()
- {
- Stack sztos;
- int m;
- cout << "Podaj rozmiar stosu: ";
- cin >> m;
- int* tab;
- tab = new int[m];
- sztos.S = tab;
- sztos.top = -1;
- push(sztos,sztos.top,5);
- push(sztos,sztos.top,4);
- push(sztos,sztos.top,2);
- push(sztos,sztos.top,6);
- push(sztos,sztos.top,7);
- push(sztos,sztos.top,3);
- push(sztos,sztos.top,0);
- push(sztos,sztos.top,11);
- push(sztos,sztos.top,8);
- //TESTY;
- cout << "SZtosik przed usunieciem: " << endl;
- for(int i = 0; i < m; i++) {
- cout << sztos.S[i] << " ";
- }
- cout << endl;
- // HEJ
- int x;
- cout << "Ktory element usunac?";
- cin >> x;
- usun(sztos,x);
- cout << "SZtosik po usunieciu: " << endl;
- for(int i = 0; i < m-1; i++) {
- cout << sztos.S[i] << " ";
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement