Практика 4 - Стек, очередь - СФ
.pdfПриложение А
(обязательное)
using System;
using System.Reflection.Metadata.Ecma335; using System.Xml.Linq;
using System.Collections.Generic; using System.Collections;
using System.Security.Cryptography;
namespace SystemOfAData4
{
class Program
{
public class Node<T>
{
public Node(T data)
{
Data = data;
}
public T Data { get; set; } public Node<T> Next { get; set; }
}
class Queue<T> : IEnumerable<T>
{
Node<T> head; Node<T> tail; int count;
public void Enqueue(T data)
{
Node<T> node = new Node<T>(data); Node<T> tempNode = tail;
tail = node;
if (count == 0) head = tail;
else
tempNode.Next = tail; count++;
}
public T Dequeue()
{
if (count == 0)
throw new InvalidOperationException("Очередь пуста"); T output = head.Data;
head = head.Next; count--;
return output;
}
public T First
{
get
{
if (IsEmpty)
throw new InvalidOperationException("Очередь пуста"); return head.Data;
}
}
public int Count { get { return count; } }
public bool IsEmpty { get { return count == 0; } } public int CountQ()
{
11
if (IsEmpty) throw new InvalidOperationException("Очередь пуста"); return count;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Node<T> current = head; while (current != null)
{
yield return current.Data; current = current.Next;
}
}
}
public class NodeStack<T> : IEnumerable<T>
{
Node<T> head; int count;
public bool IsEmpty
{
get { return count == 0; }
}
public int Count
{
get {return count;}
}
public void Push(T item)
{
Node<T> node = new Node<T>(item); node.Next = head;
head = node; count++;
}
public T Pop()
{
if (IsEmpty) throw new InvalidOperationException("Стек пуст"); Node<T> temp = head;
head = head.Next; count--;
return temp.Data;
}
public T Peek()
{
if (IsEmpty) throw new InvalidOperationException("Стек пуст"); return head.Data;
}
public int CountStack()
{
if (IsEmpty) throw new InvalidOperationException("Стек пуст"); return count;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Node<T> current = head; while (current != null)
{
yield return current.Data; current = current.Next;
12
}
}
}
static void Main(string[] args)
{
Console.WriteLine("Работа со стеком - 1, работа с очередью - 2."); int choose = int.Parse(Console.ReadLine());
if (choose == 1)
{
NodeStack<int> stack = new NodeStack<int>(); Console.Write("Количество элементов стека "); int n = int.Parse(Console.ReadLine());
int[] arr = new int[n]; Random rand = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rand.Next(100);
}
foreach (int d in arr) stack.Push(d); Console.WriteLine(); Console.WriteLine("Стек: ");
foreach (int d in arr) Console.Write(d + "\t"); Console.WriteLine();
Console.WriteLine("Удалённый элемент: " + stack.Pop()); Console.WriteLine();
Console.WriteLine("Верхний элемент: " + stack.Peek()); Console.WriteLine();
Console.WriteLine("Количесвто элементов: " + stack.CountStack()); Console.WriteLine();
}
if (choose == 2)
{
Queue<int> queue = new Queue<int>(); Console.Write("Количество элементов очереди "); int n = int.Parse(Console.ReadLine());
int[] arr = new int[n]; Random rand = new Random();
for (int i = 0; i < arr.Length; i++)
{
arr[i] = rand.Next(100);
}
foreach (int d in arr) queue.Enqueue(d); Console.WriteLine(); Console.WriteLine("Очередь: ");
foreach (int d in queue) Console.Write(d + "\t"); Console.WriteLine();
queue.Dequeue();
Console.WriteLine();
Console.WriteLine("Список с удаленным элементом: "); Console.WriteLine();
foreach (int d in queue) Console.Write(d + "\t"); Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Первый элемент: " + queue.First); Console.WriteLine("Количество элементов в очереди: " +
queue.CountQ());
}
else Main(args);
}
}
}
13
