-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab 3_stack.cpp
120 lines (111 loc) · 1.63 KB
/
lab 3_stack.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
116
117
118
119
120
#pragma once
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <tchar.h>
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;
int number = 0;
class list
{
public:
int data;
int n;
list* next;
list(int w, int a)
{
data = w;
n = a;
next = 0;
number++;
}
};
list* head = 0;
list* prosmotr = 0;
void add(int a, list* b)
{
if ((a == 0) && (head == 0))
{
head = b;
return;
}
prosmotr = head;
while (prosmotr->n != (a - 1))
{
if (prosmotr->next == 0) prosmotr->next = new list(0, prosmotr->n + 1);
else prosmotr = prosmotr->next;
}
b->next = prosmotr->next;
prosmotr->next = b;
//b->n = prosmotr->n+1 ;
prosmotr = prosmotr->next;
while (prosmotr->next != 0)
{
prosmotr = prosmotr->next;
prosmotr->n++;
}
}
void get(int a)
{
prosmotr = head;
while (prosmotr->n != a)
{
prosmotr = prosmotr->next;
}
cout << prosmotr->data << endl;
}
void del(int a)
{
prosmotr = head;
if (a == 0)
{
head = prosmotr->next;
prosmotr = head;
prosmotr->n--;
}
else
{
while (prosmotr->n != a - 1)
{
prosmotr = prosmotr->next;
}
list* q;
q = prosmotr->next;
prosmotr->next = q->next;
}
while (prosmotr->next != 0)
{
prosmotr = prosmotr->next;
prosmotr->n--;
}
number--;
}
int main()
{
int count;
cin >> count;
string action;
int l, h;
for (int i = 0; i < count; i++)
{
cin >> action;
if (action == "add")
{
cin >> h;
list* sad;
sad = new list(h, number);
add(number - 1, sad);
}
if (action == "get")
{
//cin >> l;
get(number-1);
}
if (action == "del")
{
//cin >> l;
del(number-1);
}
}
return 0;
}