Last active
December 1, 2019 13:50
-
-
Save 000407/5af4399d5e3b6855b7f4d4f86086c268 to your computer and use it in GitHub Desktop.
#1::Stacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface IStack { | |
| void Push(int value); | |
| int Pop(); | |
| int Peek(); | |
| bool IsFull(); | |
| bool IsEmpty(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Stack : IStack { | |
| private int[] array; | |
| private int top; | |
| public Stack(int size) { | |
| this.array = new int[size]; | |
| top = -1; | |
| } | |
| // Method implementations | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Stack stack1 = new Stack(3); //Initialization | |
| stack1.Push(10); | |
| stack1.ShowStack(); | |
| stack1.Push(30); | |
| stack1.ShowStack(); | |
| stack1.Push(20); | |
| stack1.ShowStack(); | |
| stack1.Push(50); | |
| stack1.ShowStack(); //Throws the SystemException |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void Push(int value) { | |
| if(this.IsFull()) { //Check if the stack is full | |
| throw new SystemException("Stack is already full!"); // Will not try to add new items | |
| } | |
| this.array[++top] = value; | |
| } | |
| public int Pop() { | |
| if(this.IsEmpty()) { // Check if the stack is empty | |
| throw new SystemException("Stack is empty!"); // Will not try to remove the top most item from the stack | |
| } | |
| return this.array[top--]; | |
| } | |
| public int Peek() { | |
| if(this.IsEmpty()) { // Check if the stack is empty | |
| throw new SystemException("Stack is empty!"); // Will not try to get the top most item from the stack | |
| } | |
| return this.array[top]; | |
| } | |
| public bool IsFull() { | |
| return (this.array.Length - 1) == this.top; | |
| } | |
| public bool IsEmpty() { | |
| return this.top == -1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| | | | |
| | | | |
| |10| | |
| |__| | |
| | | | |
| |30| | |
| |10| | |
| |__| | |
| |20| | |
| |30| | |
| |10| | |
| |__| | |
| Unhandled Exception: | |
| System.SystemException: Stack is already full! | |
| at Stack.Push (System.Int32 value) [0x0000b] in <6575c48c03074e1789a42989f3c57445>:0 | |
| at StackDemo.Main (System.String[] args) [0x00031] in <6575c48c03074e1789a42989f3c57445>:0 | |
| [ERROR] FATAL UNHANDLED EXCEPTION: System.SystemException: Stack is already full! | |
| at Stack.Push (System.Int32 value) [0x0000b] in <6575c48c03074e1789a42989f3c57445>:0 | |
| at StackDemo.Main (System.String[] args) [0x00031] in <6575c48c03074e1789a42989f3c57445>:0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment