Sajmon

Example of Generics

May 9th, 2012
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 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. */
  9. namespace IGenerics
  10. {
  11.     interface IData {
  12.         bool IsEmpty();
  13.         bool IsFull();
  14.         void Clear();
  15.     }
  16.  
  17.     public class GenericStack<T> : IData {
  18.  
  19.         private T[] stack;
  20.         private int size;
  21.         private int index;
  22.  
  23.         public GenericStack(int size)
  24.         {
  25.             this.size = size;
  26.             this.index = 0;
  27.             this.stack = new T[size];
  28.         }
  29.  
  30.         public void Push(T Number)
  31.         {
  32.             if (!IsFull())
  33.             {
  34.                 stack[index] = Number;
  35.                 index++;
  36.             }
  37.             else {
  38.                 throw new ApplicationException("Stack overflow");
  39.             }
  40.         }
  41.  
  42.         public T Pop() {
  43.             if (!IsEmpty())
  44.             {
  45.                 T temp = stack[index];
  46.                 stack[index] = default(T);
  47.                 index--;
  48.                 return temp;
  49.             }
  50.             else
  51.             {
  52.                 throw new ApplicationException("Stack underflow");
  53.             }
  54.         }
  55.  
  56.         public T Top() {
  57.             if (!IsEmpty())
  58.             {
  59.                 return stack[size - 1];
  60.             }
  61.             else
  62.             {
  63.                 throw new ApplicationException("Stack underflow");
  64.             }
  65.         }
  66.  
  67.         public bool IsEmpty()
  68.         {
  69.             return index == 0;
  70.         }
  71.  
  72.         public bool IsFull()
  73.         {
  74.             return index == size - 1;
  75.         }
  76.  
  77.         public void Clear()
  78.         {
  79.             stack = null;
  80.             index = 0;
  81.             stack = new T[size];
  82.         }
  83.     }
  84.  
  85.     public class GenericQueue<T> : IData {
  86.  
  87.         private T[] queue;
  88.         private int size;
  89.         private int index;
  90.  
  91.         public GenericQueue(int size) {
  92.             this.size = size;
  93.             this.queue = new T[size];
  94.             this.index = 0;
  95.         }
  96.  
  97.         public void Add(T item) {
  98.             queue[index] = item;
  99.             index++;
  100.         }
  101.  
  102.         public T Get() {
  103.  
  104.             if (!IsEmpty())
  105.             {
  106.                 T temp = queue[0];
  107.                 int x = 0;
  108.                 while (x < size - 1)
  109.                 {
  110.                     queue[x] = queue[x + 1]; // 1,2,3,4,5
  111.                     x++;
  112.                 }
  113.                 queue[size - 1] = default(T);
  114.                 index--;
  115.                 return temp;
  116.             }
  117.             else
  118.             {
  119.                 throw new ApplicationException("Stack underflow");
  120.             }
  121.         }
  122.  
  123.         public bool IsEmpty()
  124.         {
  125.             return index == 0;
  126.         }
  127.  
  128.         public bool IsFull()
  129.         {
  130.             return index == size - 1;
  131.         }
  132.  
  133.         public void Clear()
  134.         {
  135.             queue = null;
  136.             index = 0;
  137.             queue = new T[size];
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment