-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstek.h
More file actions
115 lines (94 loc) · 1.8 KB
/
stek.h
File metadata and controls
115 lines (94 loc) · 1.8 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
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <iostream>
using std::cout;
using std::endl;
namespace Program
{
typedef unsigned int ui;
class stek
{
private:
int* arr;
int Max;
int index = 0;
public:
stek(int number)
{
Max = 1000;
if (number <= Max)
{
arr = new int[number];
Max = number;
}
else cout << "ïåðåïîëíåíèå ñòåêà!\n";
}
~stek()
{
delete[] arr;
};
bool Push(int n)
{
if (index == Max) return false;
else
{
arr[index] = n;
index++;
return true;
}
}
int Pop()
{
if (index < 0) return -1;
else return arr[--index];
}
bool Is_emty()
{
if (index <= 0) return true;
else return false;
}
bool Is_full()
{
if (index == Max) return true;
else return false;
}
};
struct Stek
{
int key;
Stek* next;
};
class Stek2
{
public:
Stek2();
~Stek2();
void Print()
{
Stek *print = next;
while (print)
{
cout << print->key << " -> ";
print = print->next;
}
cout << "NULL\n";
}
void Push(int d)
{
Stek *pv = new Stek; // îáú¤âë¤åì íîâóþ äèíàìè÷åñêóþ ïåðåìåííóþ òèïà stek
pv->key = d; // çàïèñûâàåì çíà÷åíèå, êîòîðîå ïîìåùàåòñ¤ â ñòåê
pv->next = next; // ñâ¤çûâàåì íîâûé ýëåìåíò ñòåêà ñ ïðåäûäóùèì
next = pv; // íîâûé ýëåìåíò ñòåêà ñòàíîâèòñ¤ åãî âåðøèíîé
}
int Pop()
{
int temp = next->key; // èçâëåêàåì â ïåðåìåííóþ temp çíà÷åíèå â âåðøèíå ñòåêà
Stek *pv = next; // çàïîìèíàåì óêàçàòåëü íà âåðøèíó ñòåêà, ÷òîáû çàòåì
// îñâîáîäèòü âûäåëåííóþ ïîä íåãî ïàì¤òü
next = next->next; // âåðøèíîé ñòàíîâèòñ¤ ïðåäøåñòâóþùèé top ýëåìåíò
delete pv; // îñâîáîæäàåì ïàì¤òü, òåì ñàìûì óäàëèëè âåðøèíó
return temp; // âîçâðàùàåì çíà÷åíèå, êîòîðîå áûëî â âåðøèíå
}
private:
Stek * next;
};
}