-
Notifications
You must be signed in to change notification settings - Fork 0
/
laba5
107 lines (99 loc) · 2.33 KB
/
laba5
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define N 5
struct List
{
int inf;
int next;
};
void init(struct List *head)
{
head[0].next = 0;
for (int i = 1; i < N; i++)
head[i].next = -1;
}
int check(struct List *head)
{
int cur = head[0].next;
if (cur == 0)
return (-1);
int counter = 0;
while (cur != 0 && cur != -1)
{
counter++;
cur = head[cur].next;
}
return (counter);
}
void look(struct List *head)
{
if (check(head) == -1)
std::cout << "List is empty" << std::endl;
else
{
int cur = head[0].next;
while (cur != 0 && cur != -1)
{
std::cout << "Inf is " << head[cur].inf << std::endl;
cur = head[cur].next;
}
}
}
int search(struct List *head, int elem)
{
if (check(head) == -1)
std::cout << "List is empty" << std::endl;
else
{
int cur = head[0].next;
while (cur != 0 && cur != -1)
{
if (head[cur].inf == elem)
return (cur);
cur = head[cur].next;
}
if (cur == 0 || cur == -1)
std::cout << "Cant find an element" << std::endl;
}
}
void add_after(struct List *head)
{
if (check(head) == N)
std::cout << "List is full" << std::endl;
else
{
std::cout << "Enter the number after which you want to add a new item" << std::endl;
int num = check_num();
int cur = head[0].next;
while (cur != 0 && cur != -1 && head[cur].inf != num)
cur = head[cur].next;
int nex = 1;
while (head[nex].next != -1)
nex = head[nex].next;
head[nex].next = head[cur].next;
head[cur].next = nex;
std::cout << "Enter the new item" << std::endl;
head[nex].inf = check_num();
}
}
void add_before(struct List *head)
{
if (check(head) == N)
std::cout << "List is full" << std::endl;
else
{
std::cout << "Enter the number before which you want to add a new item" << std::endl;
int num = check_num();
int j = head[0].next;
while (cur != -1 && cur != 0)
{
if (head[head[j].next].inf == num)
int cur = head[j].next;
j =
int main()
{
struct List array[N];
init(array);
return (0);
}