Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace ConsoleApplication1
  6. {
  7.    public abstract class Stack
  8.     {
  9.         public Stack()
  10.         { }
  11.         /// <summary>
  12.         /// втолкнуть элемент item в стек
  13.         /// </summary>
  14.         /// <param name="item"></param>
  15.         public abstract void put(int item);
  16.         /// <summary>
  17.         /// удалить элемент в вершине стека
  18.         /// </summary>
  19.         public abstract void remove();
  20.         /// <summary>
  21.         /// прочитать элемент в вершине стека
  22.         /// </summary>
  23.         public abstract int item();
  24.         /// <summary>
  25.         /// определить, пуст ли стек
  26.         /// </summary>
  27.         /// <returns></returns>
  28.         public abstract bool IsEmpty();
  29.     }
  30.    public class Linkable
  31.     {
  32.         public Linkable()
  33.         { }
  34.         public int info;
  35.         public Linkable next;
  36.     }
  37.  
  38.     public class ListStack : Stack
  39.     {
  40.         public ListStack()
  41.         {
  42.             top = new Linkable();
  43.         }
  44.         Linkable top;
  45.         /// <summary>
  46.         /// втолкнуть элемент item в стек
  47.         /// </summary>
  48.         /// <param name="item"></param>
  49.         public override void put(int item)
  50.         {
  51.             Linkable newitem = new Linkable();
  52.             newitem.info = item;
  53.             newitem.next = top;
  54.             top = newitem;
  55.         }
  56.         /// <summary>
  57.         /// удалить элемент в вершине стека
  58.         /// </summary>
  59.         public override void remove()
  60.         {
  61.             top = top.next;
  62.         }
  63.         /// <summary>
  64.         /// прочитать элемент в вершине стека
  65.         /// </summary>
  66.         public override int item()
  67.         {
  68.             return (top.info);
  69.         }
  70.         /// <summary>
  71.         /// определить, пуст ли стек
  72.         /// </summary>
  73.         /// <returns></returns>
  74.         public override bool IsEmpty()
  75.         {
  76.             return (top.next == null);
  77.         }
  78.         public void TestStack()
  79.         {
  80.             ListStack stack = new ListStack();
  81.             stack.put(7); stack.put(9);
  82.             Console.WriteLine(stack.item());
  83.             stack.remove(); Console.WriteLine(stack.item());
  84.             stack.put(11); stack.put(13);
  85.             Console.WriteLine(stack.item());
  86.             stack.remove(); Console.WriteLine(stack.item());
  87.             if (!stack.IsEmpty()) stack.remove();
  88.             Console.WriteLine(stack.item());
  89.         }
  90.     }
  91.     class Program
  92.     {
  93.         static void Main(string[] args)
  94.         {
  95.             ListStack l = new ListStack();
  96.             l.TestStack();
  97.             Console.ReadKey();
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement