-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStos.cpp
More file actions
105 lines (96 loc) · 2.18 KB
/
Stos.cpp
File metadata and controls
105 lines (96 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by kukul on 23.10.2019.
//
#include "Stos.h"
#include <iostream>
//ZADANIE 3
//STOS: S Y E U Q T S A O N I E
//ZADANIE 4
// E A S Y -> E A * S * Y * *
//podstawowe operacje wykonywane na stosie
void Stos::push(elem* &stos, int x){
if(stos == NULL){
elem* el = new elem;
el->dane = x;
el->nast = NULL;
stos = el;
}else {
elem *el = new elem;
el->dane = x;
el->nast = stos;
stos = el;
}
}
int Stos::topEl(elem* stos) {
return stos->dane;
}
int Stos::pop(elem* &stos){
int top = stos->dane;
stos = stos->nast;
return top;
}
bool Stos::isEmpty(elem* stos){
return stos == nullptr;
}
//operacje dodatkowe wykonywane na stosie
void Stos::sortElementsWithAdditionalStack(elem* &stos) { // sortowanie za pomocą dodatkowego stosu
elem* addStack;
while (stos != nullptr){
int a = pop(stos);
while(addStack!= nullptr && (addStack->dane) > a){
push(stos, addStack->dane);
pop(addStack);
}
push(addStack, a);
}
stos = addStack;
}
void add(elem*& pocz_kol, elem*& kon_kol, int x)
{
if (pocz_kol)
{
kon_kol->nast = new elem{ x, NULL };
kon_kol = kon_kol->nast;
}
else
pocz_kol = kon_kol = new elem{ x, NULL };
}
int next(elem*& pocz_kol, elem*& kon_kol)
{
int wartDane = pocz_kol->dane;
elem* wart = pocz_kol;
pocz_kol = pocz_kol->nast;
delete wart;
return wartDane;
}
int firstEl(elem* pocz_kol, elem* kon_kol)
{
int wartDane = pocz_kol->dane;
return wartDane;
}
void addStack(elem*& stos1, int x)
{
Stos::push(stos1, x);
}
int nextStack(elem*& stos1, elem*& stos2)
{
if (Stos::isEmpty(stos2))
while (!Stos::isEmpty(stos1))
Stos::push(stos2, Stos::pop(stos1));
return Stos::pop(stos2);
}
void reverse_stack(elem*& stack1, elem*& stack2)
{
int x = 0;
elem* p = NULL;
while (p != stack1)
{
x = Stos::pop(stack1);
while (stack1 != p)
Stos::push(stack2, Stos::pop(stack1));
Stos::push(stack1, x);
p = stack1;
while (stack2 != NULL)
Stos::push(stack1, Stos::pop(stack2));
}
}