Odwróc porzadek elementów na stosie korzystajac z:
• jednego dodatkowego stosu,
• jednej dodatkowej kolejki.
#include <iostream>
#include <cstdlib>
using namespace std;
struct elem {
int dane;
elem *nast;
};
void push(elem *&stos, int x) {
elem* e = new elem;
e->dane = x;
e->nast = stos;
stos = e;
}
int pop(elem *&stos) {
int w = stos->dane;
elem* d = stos;
stos = stos->nast;
delete d;
return w;
}
int topEl(elem* stos) {
return stos->dane;
}
int count(elem* stos) {
int iloscEl = 0;
while (stos != NULL) {
iloscEl++;
stos = stos->nast;
}
return iloscEl;
}
bool isEmpty(elem* stos) {
bool w = true;
if (count(stos) > 0) {
w = false;
}
return w;
}
void usun(elem* &stos) {
while (stos != NULL) {
pop(stos);
}
}
void stos_rev(elem* &stos){
elem* stos2=NULL;
while(!isEmpty(stos)){
push(stos2,pop(stos));
}
delete stos;
stos=stos2;
}
int main() {
elem* p = NULL;
system("Pause");
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
struct elem {
int dane;
elem *nast;
};
void add(elem *&pocz_kolejki, elem *&kon_kolejki, int x) {
elem* e = new elem;
e->dane = x;
e->nast = NULL;
if (pocz_kolejki == NULL) {
pocz_kolejki = e;
}
if (kon_kolejki != NULL) {
kon_kolejki->nast = e;
}
kon_kolejki = e;
}
int next(elem *&pocz_kolejki, elem *&kon_kolejki) {
int w = pocz_kolejki->dane;
elem* d = pocz_kolejki;
if (kon_kolejki == pocz_kolejki) {
pocz_kolejki = NULL;
kon_kolejki = NULL;
} else {
pocz_kolejki = pocz_kolejki->nast;
}
delete d;
return w;
}
bool isEmpty(elem* pocz_kolejki) {
bool w = false;
if (pocz_kolejki == NULL) {
w = true;
}
return w;
}
void push(elem *&stos, int x) {
elem* e = new elem;
e->dane = x;
e->nast = stos;
stos = e;
}
int pop(elem *&stos) {
int w = stos->dane;
elem* d = stos;
stos = stos->nast;
delete d;
return w;
}
int count(elem* stos) {
int iloscEl = 0;
while (stos != NULL) {
iloscEl++;
stos = stos->nast;
}
return iloscEl;
}
bool isEmptyS(elem* stos) {
bool w = true;
if (count(stos) > 0) {
w = false;
}
return w;
}
void stos_rev(elem* &stos) {
elem* p = NULL;
elem* k = NULL;
while (!isEmptyS(stos)) {
add(p, k, pop(stos));
}
while (!isEmpty(p)) {
push(stos, next(p, k));
}
delete p;
delete k;
}
int main() {
elem* s = NULL;
system("Pause");
return 0;
}
Offline