СДлб4
.pdfПриложение А
(обязательное)
using System;
using System.Collections.Generic; using System.Collections;
using System.Xml.Serialization; using static sdlab4.Program;
namespace sdlab4
{
class Program
{
public class Node<T>
{
public int Element; public Node<int> Next; public Node(int element)
{
Element = element;
}
}
public class Stack<T>
{
public Node<int> head; public int count; public int Len()
{
return count;
}
public int Pop()
{
Check();
Node<int> point = head; head = head.Next; count--;
return point.Element;
11
}
public void Push(int element)
{
Node<int> point = new Node<int>(element); point.Next = head;
head = point; count++;
}
public int Check()
{
if (count == 0)
{
Console.WriteLine("Stack has no elements, return value 0"); return 0;
}
return head.Element;
}
}
public class Queue<T>
{
public Node<int> head; public Node<int> tail; public int count;
public int Len()
{
return count;
}
public int Delete()
{
Check();
Node<int> point = head; head = head.Next; count--;
return point.Element;
}
12
public void Add(int element)
{
Node<int> point = new Node<int>(element);
Node<int> temp = tail; tail = point; if (count == 0)
{
head = tail;
}
else
{
temp.Next = tail;
}
count++;
}
public int Check()
{
if (count == 0)
{
Console.WriteLine("Queue has no elements, returned value 0"); return 0;
}
return head.Element;
}
}
static void Main(string[] args)
{
int header; int count;
OneMore:
Console.WriteLine("1 - Stack, 2 - Queue"); switch (int.Parse(Console.ReadLine()))
{
case 1:
Stack<int> stack = new Stack<int>();
13
stack.Push(1);
stack.Push(22);
stack.Push(333);
stack.Push(44);
stack.Push(5);
stack.Push(66);
stack.Push(777);
stack.Push(88);
stack.Push(9);
header = stack.Check();
Console.WriteLine($"Top element of the stack : {header}");
count = stack.Len();
Console.WriteLine("The stack size is {0}", count);
header = stack.Check();
Console.WriteLine($"Top element of the stack : {header}");
header = stack.Pop();
Console.WriteLine("Removed element {0}", header);
header = stack.Check();
Console.WriteLine($"The stack size is: {header}");
count = stack.Len();
Console.WriteLine("The stack size is {0}", count);
break;
case 2:
Queue<int> line = new Queue<int>();
line.Add(111);
14
line.Add(22);
line.Add(3);
line.Add(44);
line.Add(555);
line.Add(66);
line.Add(7);
line.Add(88);
line.Add(999);
header = line.Check();
Console.WriteLine(" Queue start element : {0}", header);
header = line.Check();
Console.WriteLine("Queue start element: {0}", header);
count = line.Len();
Console.WriteLine("Queue length {0}", count);
header = line.Delete();
Console.WriteLine("The removed element of the queue is {0}", header);
header = line.Check();
Console.WriteLine("Queue start element: {0}", header);
count = line.Len();
Console.WriteLine("Queue length: {0}", count);
break;
}
goto OneMore;
}
}
}
15