Sajmon

GenericStack&Queue

Mar 7th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. /**
  7.  * @Author: Sajmon
  8.  * @date: 7.3.2012
  9.  **/
  10.  
  11. namespace GenIADT
  12. {
  13.     interface IADT {
  14.         Boolean IsEmpty();
  15.         Boolean IsFull();
  16.         void Clear();
  17.     }
  18.  
  19.     interface IGenStack<T> : IADT {
  20.         void Push(T objekt);
  21.         T Pop();
  22.         T Top();
  23.         String State();
  24.     }
  25.  
  26.     interface IGenQueue<T> : IADT {
  27.         void Add(T objekt);
  28.         T Get();
  29.         String State();
  30.    
  31.     }
  32.  
  33.     public class GenericStack<T> : IGenStack<T> {
  34.  
  35.         private String[] objectStatus = {"Empty", "Partially", "Full"};
  36.         private const int SIZE = 20;
  37.         int index = 0;
  38.         T[] genericStack = new T[SIZE];
  39.  
  40.  
  41.         public void Push(T objekt) {
  42.  
  43.             if (!IsFull())
  44.             {
  45.                 this.genericStack[index] = objekt;
  46.                 index++;
  47.                 Console.WriteLine("Item \"{0}\" was added to genericStack ", objekt);
  48.             }
  49.             else {
  50.                 throw new ApplicationException("Stack overflow.");
  51.             }
  52.         }
  53.  
  54.         public T Pop() {
  55.  
  56.             if (!IsEmpty())
  57.             {
  58.                 T temp = this.genericStack[index - 1];
  59.                 this.genericStack[index - 1] = default(T);
  60.                 index--;
  61.                 return temp;
  62.             }
  63.             else {
  64.                 throw new ApplicationException("Stack underflow");
  65.             }
  66.         }
  67.  
  68.         public T Top() {
  69.             if (!IsEmpty())
  70.             {
  71.                 return this.genericStack[index - 1];
  72.             }
  73.             else {
  74.                 throw new ApplicationException("Stack underflow");
  75.             }
  76.         }
  77.  
  78.         public Boolean IsEmpty() {
  79.  
  80.             return this.index == 0;
  81.         }
  82.  
  83.         public Boolean IsFull()
  84.         {
  85.             return this.index == SIZE - 1;
  86.         }
  87.  
  88.         public void Clear() {
  89.  
  90.             this.genericStack = null;
  91.             this.index = 0;
  92.             this.genericStack = new T[SIZE];
  93.             Console.WriteLine("GenericStack was deleted and created again.");
  94.         }
  95.  
  96.         public String State() {
  97.             if (index == 0) {
  98.                 return objectStatus[0];
  99.             }
  100.             else if (index < SIZE - 1)
  101.             {
  102.                 return objectStatus[1];
  103.             }
  104.             else {
  105.                 return objectStatus[2];
  106.             }
  107.         }
  108.     }
  109.  
  110.     public class GenericQueue<T> : IGenQueue<T> {
  111.  
  112.         private String[] objectStatus = {"Empty", "Partially", "Full"};
  113.         private const int SIZE = 20;
  114.         int index = 0;
  115.         T[] genericQueue = new T[SIZE];
  116.  
  117.         public void Add(T objekt) {
  118.             if (!IsFull())
  119.             {
  120.                 this.genericQueue[index] = objekt;
  121.                 this.index++;
  122.                 Console.WriteLine("Item {0} was added to queue.", objekt);
  123.             }
  124.             else {
  125.                 throw new ApplicationException("Queue overflow");
  126.             }
  127.         }
  128.  
  129.         public T Get() {
  130.             if (!IsEmpty())
  131.             {
  132.                 T temp = this.genericQueue[0]; // do premennej temp vlozim prvok, ktory odoberam
  133.                 int x = 0;
  134.                 while (x < index - 1) {
  135.                     this.genericQueue[x] = this.genericQueue[x + 1]; // posuniem prvky o jeden dolava
  136.                 }
  137.                 this.genericQueue[index - 1] = default(T); // posledny prvok zmazem
  138.                 this.index--; // znizim index o 1
  139.  
  140.                 return temp;
  141.             }
  142.             else {
  143.                 throw new ApplicationException("Queue underflow");
  144.             }
  145.            
  146.         }
  147.  
  148.         public Boolean IsEmpty() {
  149.             return this.index == 0;
  150.         }
  151.  
  152.         public Boolean IsFull() {
  153.             return this.index == SIZE - 1;
  154.         }
  155.  
  156.         public void Clear() {
  157.             this.genericQueue = null;
  158.             this.index = 0;
  159.             this.genericQueue = new T[SIZE];
  160.             Console.WriteLine("Queue was deleted and created again.");
  161.         }
  162.  
  163.         public String State() {
  164.             if (index == 0)
  165.             {
  166.                 return objectStatus[0];
  167.             }
  168.             else if (index < SIZE - 1 && index > 0)
  169.             {
  170.                 return objectStatus[1];
  171.             }
  172.             else
  173.             {
  174.                 return objectStatus[2];
  175.             }
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment