Практики(Вариант №7) / Практическая работа №4
.pdf}
// проверить, пуст ли стек |
||
public bool IsEmpty() |
||
{ |
return top == null; |
|
} |
||
|
||
// очистить стек public bool Clear()
{top = null; count = 0; return true;
|
} |
|
|
|
// вывести содержимое стека |
||
|
public void Print() |
||
|
{ |
Node current = top; |
|
|
|
||
|
|
Console.Write("Стек (сверху вниз): "); |
|
|
|
while (current != null) |
|
|
|
{ |
Console.Write(current.Value + " "); |
|
|
|
|
|
|
} |
current = current.Next; |
|
|
|
|
|
} |
Console.WriteLine(); |
|
} |
|
|
|
|
|
|
|
public class MyQueue |
|||
{private Node head; private Node tail; private int count; public MyQueue()
{head = null; tail = null; count = 0;
}
// добавить элемент в очередь |
||
public bool Enqueue(int element) |
||
{ |
Node newNode = new Node(element); |
|
|
||
|
if (head == null) |
|
|
{ |
head = newNode; |
|
|
|
|
} |
tail = newNode; |
|
|
|
|
else |
|
|
{ |
tail.Next = newNode; |
|
|
|
|
|
tail = newNode; |
|
} |
|
|
count++; |
|
} |
return true; |
|
|
|
|
// удалить и вернуть первый элемент очереди |
|
public int Dequeue() |
|
{ |
if (head == null) |
|
|
|
throw new InvalidOperationException("Очередь пуста"); |
|
int value = head.Value; |
11
head = head.Next;
if (head == null) tail = null;
count--;
} return value;
// вернуть первый |
элемент без удаления |
|
public int Peek() |
|
|
{ |
if (head == null) |
|
|
||
|
throw new |
InvalidOperationException("Очередь пуста"); |
} |
return head.Value; |
|
|
|
|
// количество элементов в очереди |
||
public int Size |
||
{ |
get { return count; } |
|
} |
||
|
||
// проверить, пуста ли очередь |
||
public bool IsEmpty() |
||
{ |
return head == null; |
|
} |
||
|
||
// очистить очередь |
|
public bool Clear() |
|
{ |
head = null; |
|
|
|
tail = null; |
|
count = 0; |
} |
return true; |
|
|
// вывести содержимое очереди public void Print()
{Node current = head;
Console.Write("Очередь (с начала к концу): "); while (current != null)
{
Console.Write(current.Value + " "); } current = current.Next;
}} Console.WriteLine();
class Program
{static void Main()
{Console.WriteLine("=== Демонстрация работы стека ==="); DemonstrateStack();
Console.WriteLine("\n=== Демонстрация работы очереди ==="); } DemonstrateQueue();
static void DemonstrateStack()
{MyStack stack = new MyStack();
Console.WriteLine("Добавляем элементы в стек: 10, 20, 30");
12
stack.Push(10);
stack.Push(20);
stack.Push(30);
Console.WriteLine($"Количество элементов: {stack.Size}"); Console.WriteLine($"Верхний элемент (без удаления): {stack.Peek()}"); stack.Print();
Console.WriteLine("Удаляем элементы из стека:"); while (!stack.IsEmpty())
{Console.WriteLine($"Удален: {stack.Pop()}"); stack.Print();
|
|
} |
|
|
} |
Console.WriteLine($"Количество элементов после удаления: {stack.Size}"); |
|
|
|
|
|
|
static void DemonstrateQueue() |
||
|
{ |
MyQueue queue = new MyQueue(); |
|
|
|
||
|
|
Console.WriteLine("Добавляем элементы в очередь: 10, 20, 30"); |
|
|
|
queue.Enqueue(10); |
|
|
|
queue.Enqueue(20); |
|
|
|
queue.Enqueue(30); |
|
|
|
Console.WriteLine($"Количество элементов: {queue.Size}"); |
|
|
|
Console.WriteLine($"Первый элемент (без удаления): {queue.Peek()}"); |
|
|
|
queue.Print(); |
|
|
|
Console.WriteLine("Удаляем элементы из очереди:"); |
|
|
|
while (!queue.IsEmpty()) |
|
|
|
{ |
Console.WriteLine($"Удален: {queue.Dequeue()}"); |
|
|
|
|
|
|
} |
queue.Print(); |
|
|
|
|
|
} |
Console.WriteLine($"Количество элементов после удаления: {queue.Size}"); |
|
} |
|
|
|
|
|
|
|
13
