
сд4
.pdfhead = null; tail = null;
}
else
{
head = head.Next;
tail.Next = head; // Обновляем связь для цикличности
}
count--; return true;
}
public bool RemoveLast(out int element)
{
element = -1;
if (head == null) return false; // Список пуст
Node current = head;
Node previous = null;
while (current.Next != head)
{
previous = current; current = current.Next;
}
element = current.Value; if (previous == null)
{
head = null;
21
tail = null;
}
else
{
previous.Next = head; tail = previous;
}
count--; return true;
}
public int PeekFirst()
{
return head != null ? head.Value : -1;
}
public int PeekLast()
{
return tail != null ? tail.Value : -1;
}
}
// Класс для реализации стека public class Stack
{
private List list;
public Stack()
{
list = new List();
22
}
public void Push(int element)
{
list.Add(element);
}
public int Pop()
{
if (list.RemoveLast(out int element))
{
return element;
}
throw new InvalidOperationException("Стек пуст.");
}
public int Peek()
{
int top = list.PeekLast(); if (top == -1)
{
throw new InvalidOperationException("Стек пуст.");
}
return top;
}
public int Count()
{
return list.Count();
}
23
}
// Класс для реализации очереди public class Queue
{
private List list;
public Queue()
{
list = new List();
}
public void Enqueue(int element)
{
list.Add(element);
}
public int Dequeue()
{
if (list.RemoveFirst(out int element))
{
return element;
}
throw new InvalidOperationException("Очередь пуста.");
}
public int Peek()
{
int first = list.PeekFirst(); if (first == -1)
24
{
throw new InvalidOperationException("Очередь пуста.");
}
return first;
}
public int Count()
{
return list.Count();
}
}
public class Program
{
public static void Main(string[] args)
{
Stack stack = new Stack(); Queue queue = new Queue(); bool running = true;
while (running)
{
Console.WriteLine("\nМеню:");
Console.WriteLine("1. Добавить элемент в стек");
Console.WriteLine("2. Удалить элемент из стека");
Console.WriteLine("3. Показать верхний элемент стека");
Console.WriteLine("4. Показать количество элементов в
стеке");
Console.WriteLine("5. Добавить элемент в очередь");
25
Console.WriteLine("6. Удалить элемент из очереди"); Console.WriteLine("7. Показать первый элемент очереди");
Console.WriteLine("8. Показать количество элементов в
очереди");
Console.WriteLine("9. Выйти"); Console.Write("Выберите действие: "); string choice = Console.ReadLine();
switch (choice)
{
case "1":
Console.Write("Введите элемент для добавления в
стек: ");
int stackElementToAdd =
int.Parse(Console.ReadLine());
stack.Push(stackElementToAdd);
Console.WriteLine($"Элемент {stackElementToAdd} добавлен в стек.");
break;
case "2": try
{
int stackElementToRemove = stack.Pop();
Console.WriteLine($"Элемент {stackElementToRemove} удален из стека.");
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
26
break;
case "3": try
{
int stackTop = stack.Peek();
Console.WriteLine($"Верхний элемент стека:
{stackTop}");
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
break;
case "4":
Console.WriteLine($"Количество элементов в
стеке: {stack.Count()}");
break;
case "5":
Console.Write("Введите элемент для добавления в
очередь: ");
int queueElementToAdd =
int.Parse(Console.ReadLine());
queue.Enqueue(queueElementToAdd);
Console.WriteLine($"Элемент {queueElementToAdd} добавлен в очередь.");
break;
case "6":
27
try
{
int queueElementToRemove =
queue.Dequeue();
Console.WriteLine($"Элемент {queueElementToRemove} удален из очереди.");
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
break;
case "7": try
{
int queueFirst = queue.Peek();
Console.WriteLine($"Первый элемент
очереди: {queueFirst}");
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
break;
case "8":
Console.WriteLine($"Количество элементов в
очереди: {queue.Count()}");
break;
28
case "9":
running = false;
Console.WriteLine("Выход из программы."); break;
default:
Console.WriteLine("Неверный ввод, попробуйте
еще раз.");
break;
}
}
}
}
29