Практика 4(СД)
.pdfif (tail == null)
{
head = tail = newNode;
}
else
{
tail.Next = newNode; tail = newNode;
}
}
public int? Dequeue()
{
if (head == null) return null;
int value = head.Data; head = head.Next;
if (head == null) tail = null;
return value;
}
11
public int? Peek()
{
if (head == null) return null; return head.Data;
}
public int Count()
{
int count = 0;
Node current = head; while (current != null)
{
count++;
current = current.Next;
}
return count;
}
public override string ToString()
{
if (head == null)
12
return "(empty)";
Node current = head; string result = ""; while (current != null)
{
result += current.Data + " -> "; current = current.Next;
}
return result + "null";
}
}
class Program
{
static void Main()
{
Stack stack = new Stack();
Queue queue = new Queue();
while (true)
{
Console.WriteLine("Выберите структуру:\n");
13
Console.WriteLine("1. Стек");
Console.WriteLine("2. Очередь");
Console.WriteLine("0. Выход");
Console.Write("Ваш выбор: ");
string structureInput = Console.ReadLine();
if (!int.TryParse(structureInput, out int structureChoice))
{
Console.WriteLine("Ошибка ввода. Введите число."); continue;
}
if (structureChoice == 0)
{
Console.WriteLine("Выход из программы."); break;
}
switch (structureChoice)
{
case 1: StackMenu(stack);
14
break;
case 2: QueueMenu(queue); break;
default:
Console.WriteLine("Некорректный выбор структуры."); break;
}
}
}
static void StackMenu(Stack stack)
{
Console.WriteLine("Выберите действие:");
while (true)
{
Console.WriteLine("\nМеню стека:");
Console.WriteLine("1. Добавить в стек");
Console.WriteLine("2. Удалить из стека");
15
Console.WriteLine("3. Взять верхний элемент"); Console.WriteLine("4. Количество элементов"); Console.WriteLine("5. Показать стек"); Console.WriteLine("0. Возврат в главное меню");
Console.Write("Ваш выбор: ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int choice))
{
Console.WriteLine("Ошибка ввода. Введите число."); continue;
}
if (choice == 0) break;
switch (choice)
{
case 1:
Console.Write("Введите элемент для добавления: "); if (int.TryParse(Console.ReadLine(), out int pushElem))
16
{
stack.Push(pushElem); Console.WriteLine("Элемент добавлен в стек.");
}
else
{
Console.WriteLine("Ошибка ввода элемента.");
}
break;
case 2:
int? popped = stack.Pop(); if (popped.HasValue)
Console.WriteLine($"Удалён элемент: {popped.Value}"); else
Console.WriteLine("Стек пуст."); break;
case 3:
int? peeked = stack.Peek(); if (peeked.HasValue)
Console.WriteLine($"Верхний элемент: {peeked.Value}");
17
else
Console.WriteLine("Стек пуст."); break;
case 4:
Console.WriteLine($"Количество элементов в стеке: {stack.Count()}");
break;
case 5:
Console.WriteLine("Текущий стек:"); Console.WriteLine(stack.ToString()); break;
default:
Console.WriteLine("Недопустимый выбор."); break;
}
}
}
static void QueueMenu(Queue queue)
{
18
Console.WriteLine("Выберите действие:");
while (true)
{
Console.WriteLine("\nМеню очереди:"); Console.WriteLine("1. Добавить в очередь"); Console.WriteLine("2. Удалить из очереди"); Console.WriteLine("3. Взять первый элемент очереди"); Console.WriteLine("4. Количество элементов в очереди"); Console.WriteLine("5. Показать очередь"); Console.WriteLine("0. Возврат в главное меню");
Console.Write("Ваш выбор: ");
string input = Console.ReadLine();
if (!int.TryParse(input, out int choice))
{
Console.WriteLine("Ошибка ввода. Введите число."); continue;
}
if (choice == 0)
19
break;
switch (choice)
{
case 1:
Console.Write("Введите элемент для добавления: ");
if (int.TryParse(Console.ReadLine(), out int enqueuedElem))
{
queue.Enqueue(enqueuedElem); Console.WriteLine("Элемент добавлен в очередь.");
}
else
{
Console.WriteLine("Ошибка ввода элемента.");
}
break;
case 2:
int? dequeued = queue.Dequeue(); if (dequeued.HasValue)
Console.WriteLine($"Удалён элемент: {dequeued.Value}"); else
20
