Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /**
- * @Author: Sajmon
- * @date: 7.3.2012
- **/
- namespace GenIADT
- {
- interface IADT {
- Boolean IsEmpty();
- Boolean IsFull();
- void Clear();
- }
- interface IGenStack<T> : IADT {
- void Push(T objekt);
- T Pop();
- T Top();
- String State();
- }
- interface IGenQueue<T> : IADT {
- void Add(T objekt);
- T Get();
- String State();
- }
- public class GenericStack<T> : IGenStack<T> {
- private String[] objectStatus = {"Empty", "Partially", "Full"};
- private const int SIZE = 20;
- int index = 0;
- T[] genericStack = new T[SIZE];
- public void Push(T objekt) {
- if (!IsFull())
- {
- this.genericStack[index] = objekt;
- index++;
- Console.WriteLine("Item \"{0}\" was added to genericStack ", objekt);
- }
- else {
- throw new ApplicationException("Stack overflow.");
- }
- }
- public T Pop() {
- if (!IsEmpty())
- {
- T temp = this.genericStack[index - 1];
- this.genericStack[index - 1] = default(T);
- index--;
- return temp;
- }
- else {
- throw new ApplicationException("Stack underflow");
- }
- }
- public T Top() {
- if (!IsEmpty())
- {
- return this.genericStack[index - 1];
- }
- else {
- throw new ApplicationException("Stack underflow");
- }
- }
- public Boolean IsEmpty() {
- return this.index == 0;
- }
- public Boolean IsFull()
- {
- return this.index == SIZE - 1;
- }
- public void Clear() {
- this.genericStack = null;
- this.index = 0;
- this.genericStack = new T[SIZE];
- Console.WriteLine("GenericStack was deleted and created again.");
- }
- public String State() {
- if (index == 0) {
- return objectStatus[0];
- }
- else if (index < SIZE - 1)
- {
- return objectStatus[1];
- }
- else {
- return objectStatus[2];
- }
- }
- }
- public class GenericQueue<T> : IGenQueue<T> {
- private String[] objectStatus = {"Empty", "Partially", "Full"};
- private const int SIZE = 20;
- int index = 0;
- T[] genericQueue = new T[SIZE];
- public void Add(T objekt) {
- if (!IsFull())
- {
- this.genericQueue[index] = objekt;
- this.index++;
- Console.WriteLine("Item {0} was added to queue.", objekt);
- }
- else {
- throw new ApplicationException("Queue overflow");
- }
- }
- public T Get() {
- if (!IsEmpty())
- {
- T temp = this.genericQueue[0]; // do premennej temp vlozim prvok, ktory odoberam
- int x = 0;
- while (x < index - 1) {
- this.genericQueue[x] = this.genericQueue[x + 1]; // posuniem prvky o jeden dolava
- }
- this.genericQueue[index - 1] = default(T); // posledny prvok zmazem
- this.index--; // znizim index o 1
- return temp;
- }
- else {
- throw new ApplicationException("Queue underflow");
- }
- }
- public Boolean IsEmpty() {
- return this.index == 0;
- }
- public Boolean IsFull() {
- return this.index == SIZE - 1;
- }
- public void Clear() {
- this.genericQueue = null;
- this.index = 0;
- this.genericQueue = new T[SIZE];
- Console.WriteLine("Queue was deleted and created again.");
- }
- public String State() {
- if (index == 0)
- {
- return objectStatus[0];
- }
- else if (index < SIZE - 1 && index > 0)
- {
- return objectStatus[1];
- }
- else
- {
- return objectStatus[2];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment