-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1_queue_mas.cpp
115 lines (107 loc) · 2.12 KB
/
1_queue_mas.cpp
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
#include <iostream>
#include <cstdlib>
using namespace std;
int *getmem(int size)
{
int *newmem = new int[size];
return (newmem);
}
int *custom_realloc_push(int *queue, int size, int new_item)
{
int *newqueue = getmem(size);
for (int i = 0; i < size - 1; i++)
newqueue[i] = queue[i];
newqueue[size - 1] = new_item;
if (size != 1)
delete[] queue;
return (newqueue);
}
int *push_one(int *queue, int size)
{
char buffer[11] = "smth";
cout << "enter number" << endl;
cin.getline(buffer, 11);
queue = custom_realloc_push(queue, size, atoi(buffer));
return (queue);
}
int pop_one(int *queue, int size)
{
if (size == 0)
{
cout << "nothing to pop. 0 returned" << endl;
return (0);
}
int returned = queue[0];
for (int i = 0; i < size - 1; i++)
queue[i] = queue[i + 1];
queue[size - 1] = returned;
return (returned);
}
int *delete_one(int *queue, int size)
{
if (size <= 0)
return (queue);
int *newqueue = getmem(size);
for (int i = 0; i < size; i++)
newqueue[i] = queue[i + 1];
delete[] queue;
return (newqueue);
}
void show_queue(int *queue, int size)
{
for (int i = 0; i < size; i++)
cout << i << " - " << queue[i] << endl;
}
int main()
{
char buffer[11] = "smth";
int size = 0;
int *queue;
int flag = 0;
while (buffer[0] != 4 && (flag = flag + 1))
{
if (size < 1)
size = 0;
cout << "choose next operation or ctrl + D to end" << endl;
cout << "1 - push item" << endl;
cout << "2 - pop and operation plus for 2 elements in stack" << endl;
cout << "3 - delete one element" << endl;
cout << "4 - show current queue" << endl;
cin.getline(buffer, 11);
int choose = atoi(buffer);
switch(choose)
{
case 1:
{
queue = push_one(queue, ++size);
break;
}
case 2:
{
int a = pop_one(queue, size);
int b = pop_one(queue, size);
cout << "rezult is " << a + b << endl;
break;
}
case 3:
{
queue = delete_one(queue, --size);
break;
}
case 4:
{
show_queue(queue, size);
break;
}
default:
{
if (buffer[0] == 4)
break;
cout << "wrong operation. Retry" << endl;
break;
}
}
}
if (flag > 1)
delete[] queue;
}