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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
| #include <iostream>
#include<time.h>
#include <locale.h>
using namespace std;
struct Queue
{
int data;
Queue *next;
} *head, *tail;
struct DoubleLinkedList
{
int data;
DoubleLinkedList *next;
DoubleLinkedList *previous;
} *front;
void push(Queue **, Queue **, int);
void pop(Queue **, Queue **);
void popAll(Queue **, Queue **);
void change1(Queue **, Queue **);
void output(Queue **);
void pushDLL(DoubleLinkedList **, int );
void popDLL(DoubleLinkedList **);
void outputDLL(DoubleLinkedList **);
void changeDLL(DoubleLinkedList **);
void popAllDLL(DoubleLinkedList **);
void push(Queue **head, Queue **tail, int data)
{
Queue *element = new Queue;
element->data = data;
element->next = NULL;
if (*head == NULL)
*head = *tail = element;
else
{
(*tail)->next = element;
*tail = element;
}
}
void pop(Queue **head, Queue **tail)
{
Queue *temp = *head;
*head = (*head)->next;
if (*head == NULL)
*tail = NULL;
if (temp != NULL)
delete temp;
}
void popAll(Queue **head, Queue **tail)
{
while (*head != NULL)
pop(head, tail);
}
void change1(Queue **head, Queue **tail)
{
int temp = (*head)->data;
(*head)->data = (*tail)->data;
(*tail)->data = temp;
}
void output(Queue **head)
{
Queue *temp = *head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void pushDLL(DoubleLinkedList **front, int data)
{
DoubleLinkedList *el = new DoubleLinkedList;
el->data = data;
el->next = NULL;
if (*front == NULL)
{
*front = el;
el->previous = NULL;
}
else
{
DoubleLinkedList *temp = *front;
while (temp->next != NULL)
temp = temp->next;
el->previous = temp;
temp->next = el;
}
}
void popDLL(DoubleLinkedList **front)
{
if (*front != NULL)
{
DoubleLinkedList *temp = *front;
while (temp->next != NULL)
temp = temp->next;
if (temp->previous != NULL)
temp->previous->next = NULL;
else
*front = NULL;
delete temp;
}
}
void outputDLL(DoubleLinkedList **front)
{
DoubleLinkedList *temp = *front;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void change1DLL(DoubleLinkedList **front)
{
DoubleLinkedList *temp = *front;
while (temp->next != NULL)
temp = temp->next;
int t = (*front)->data;
(*front)->data = temp->data;
temp->data = t;
}
void popAllDLL(DoubleLinkedList **front)
{
while (*front != NULL)
popDLL(front);
}
void main()
{
setlocale(NULL, "Russian");
cout << "Односвязный (двусвязный) список - 1(2): ";
int choose;
cin >> choose;
if (choose == 1)
{
int n;
cout << "Введите количество элементов в очереди: ";
cin >> n;
for (int i = 0; i < n; i++)
push(&head, &tail, rand() % 100 + 1);
cout << "Очередь: ";
output(&head);
//change1(&head, &tail);
cout << "Новая очередь: ";
output(&head);
popAll(&head, &tail);
}
else
{
int n;
cout << "Введите количество элементов в очереди: ";
cin >> n;
for (int i = 0; i < n; i++)
pushDLL(&front, rand() % 100 + 1);
cout << "DLL: ";
outputDLL(&front);
//change1DLL(&front);
cout << "Новая DLL: ";
outputDLL(&front);
popAllDLL(&front);
}
system ("pause");
} |