СД Практика 4
.pdf
2.5 Методы для вывода первого элемента
Метод CheckFirstinStack() выводит верхний элемент в стеке без удаления.
Происходит проверка длины стека, если стек пустой, то выводится соответствующее сообщение, если нет, то выводятся данные из начального узла. Метод представлен на рисунке 2.7.
Рисунок 2.7 – Метод CheckFirstinStack()
Метод CheckFirstinQueue() выводит первый элемент в очереди без удаления. Происходит проверка длины очереди, если стек пустой, то выводится соответствующее сообщение, если нет, то выводятся данные из начального узла. Метод представлен на рисунке 2.8.
Рисунок 2.8 – Метод CheckFirstinQueue()
11
Результат работы программы представлен на рисунке 2.9.
Рисунок 2.9 – Результат
12
Заключение
Входе выполнения данной практической работы были реализованы стек
иочередь с помощью классов, а также типовые методы для работы с ними.
13
Приложение А
public class Node
{
public int Data; public Node Next;
public Node(int data)
{
Data = data;
}
}
public class Stack
{
public Node head; public int Length;
public int RemoveFromStack()
{
CheckFirstinStack(); Node point = head; head = head.Next; Length--;
return point.Data;
}
public void AddToStack(int element)
{
14
Node point = new Node(element); point.Next = head;
head = point; Length++;
}
public int StackLength()
{
return Length;
}
public int CheckFirstinStack()
{
if (Length == 0)
{
Console.WriteLine("В стеке нет элементов"); return 0;
}
return head.Data;
}
public void PrintStack()
{
Node current = head; while (current != null)
{
Console.Write(current.Data + " "); current = current.Next;
}
15
Console.WriteLine();
}
}
public class Queue
{
public Node head; public Node tail; public int Length;
public int RemoveFromQueue()
{
CheckFirstinQueue(); Node point = head; head = head.Next; Length--;
return point.Data;
}
public void AddToQueue(int element)
{
Node point = new Node(element); Node temp = tail;
tail = point; if (Length == 0)
{
head = tail;
}
else
16
{
temp.Next = tail;
}
Length++;
}
public int QueueLength()
{
return Length;
}
public int CheckFirstinQueue()
{
if (Length == 0)
{
Console.WriteLine("В очереди нет элементов"); return 0;
}
return head.Data;
}
public void PrintQueue()
{
Node current = head; while (current != null)
{
Console.Write(current.Data + " "); current = current.Next;
}
17
Console.WriteLine();
}
}
public class Program
{
public static void Main()
{
Stack stack = new Stack();
Queue queue = new Queue();
Console.WriteLine("Введите элементы через пробел:"); string spisok = Console.ReadLine();
int[] a = spisok.Split(' ').Select(x => int.Parse(x)).ToArray(); for (int i = 0; i < a.Length; i++)
{
stack.AddToStack(a[i]);
queue.AddToQueue(a[i]);
}
int lenghtS = stack.StackLength(); int lengthQ = queue.QueueLength();
Console.Write("Стек: "); stack.PrintStack();
Console.WriteLine($"Длина стека: {lenghtS}");
Console.Write("Очередь: "); queue.PrintQueue();
Console.WriteLine($"Длина очереди: {lengthQ}");
int firstS = stack.CheckFirstinStack();
18
int firstQ = queue.CheckFirstinQueue();
Console.WriteLine($"Первый элемент в стеке: {firstS}");
Console.WriteLine($"Первый элемент в очереди: {firstQ}");
int removedS = stack.RemoveFromStack(); int removedQ = queue.RemoveFromQueue();
Console.WriteLine($"Удален элемент в стеке: {removedS}");
Console.WriteLine($"Удален элемент в очереди: {removedQ}");
lenghtS = stack.StackLength(); lengthQ = queue.QueueLength();
Console.Write("Стек после удаления элемента: "); stack.PrintStack(); Console.WriteLine($"Длина стека: {lenghtS}");
Console.Write("Очередь после удаления элемента: "); queue.PrintQueue(); Console.WriteLine($"Длина очереди: {lengthQ}");
}
}
19
