Advertisement
m1okgoodyes

Stack LinkedList

Apr 2nd, 2022
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. namespace Stack_LinkedList
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             NodeStack<string> stack = new NodeStack<string>();
  12.             stack.Push("tom");
  13.             stack.Push("sam");
  14.             stack.Push("max");
  15.             stack.Push("kirill");
  16.             foreach(string s in stack)
  17.             {
  18.                 Console.WriteLine(s);
  19.             }
  20.             Console.WriteLine();
  21.             string header = stack.Peek();
  22.             Console.WriteLine($"верхушка стека: {header}");
  23.             Console.WriteLine();
  24.             header = stack.Pop();
  25.             foreach(string s in stack)
  26.             {
  27.                 Console.WriteLine(s);
  28.             }
  29.         }
  30.         public class Node<T>//создание узла
  31.         {
  32.             public Node(T data)
  33.             {
  34.                 Data = data;
  35.             }
  36.             public T Data { get; set; }
  37.             public Node<T> Next { get; set; }
  38.         }
  39.         public class NodeStack<T> : IEnumerable<T>
  40.         {
  41.             Node<T> head;
  42.             int count;
  43.             public bool IsEmpty
  44.             {
  45.                 get { return count == 0; }
  46.             }
  47.             public int Count
  48.             {
  49.                 get { return count; }
  50.             }
  51.  
  52.             public void Push(T item)
  53.             {
  54.                 //увеличиваем стек
  55.                 Node<T> node = new Node<T>(item);
  56.                 node.Next = head;//переустанавливаем верхушку стека
  57.                 head = node;
  58.                 count++;
  59.             }
  60.             public T Pop()
  61.             {
  62.                 if(IsEmpty)
  63.                 {
  64.                     throw new InvalidOperationException("стек пуст");
  65.                 }
  66.                 Node<T> temp = head;
  67.                 head = head.Next;//переустанавливаем верхушку стека
  68.                 count--;
  69.                 return temp.Data;
  70.             }
  71.             public T Peek()
  72.             {
  73.                 if(IsEmpty)
  74.                 {
  75.                     throw new InvalidOperationException("стек пуст");
  76.                 }
  77.                 return head.Data;
  78.             }
  79.             //создание интерфейса
  80.             IEnumerator IEnumerable.GetEnumerator()
  81.             {
  82.                 return ((IEnumerable)this).GetEnumerator();
  83.             }
  84.             IEnumerator<T> IEnumerable<T>.GetEnumerator()
  85.             {
  86.                 Node<T> current = head;
  87.                 while(current != null)
  88.                 {
  89.                     yield return current.Data;
  90.                     current = current.Next;
  91.                 }
  92.             }
  93.  
  94.         }
  95.  
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement