Skip to content

Commit

Permalink
Fix iiitv#501: Add Stack [C#] (iiitv#502)
Browse files Browse the repository at this point in the history
* Added Stack [C#]

* fixed intendation

* forgot one whitespace...

* Added Main method

* An attempt to fix travis issues

* Removed Swap and Duplicate methods

* Edited pop method, added toString method and added prints

* Small change

* Change comments and fixed trailing whitespace
  • Loading branch information
franetheman authored and singhpratyush committed Oct 10, 2017
1 parent c261297 commit ffef329
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [AVL Tree](http://www.geeksforgeeks.org/avl-tree-set-1-insertion)| | |[:white_check_mark:](avl_tree/AvlTree.java) | | | | |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | | [:white_check_mark:](binary_search_tree/BinarySearchTree.java) | | | | |
| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | [:white_check_mark:](linked_list/LinkedList.cs) |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | [:white_check_mark:](stack/Stack.cs) |
| [Trie](https://en.wikipedia.org/wiki/Trie) | | [:white_check_mark:](trie/trie.cpp) | [:white_check_mark:](trie/Trie.java) | | | | | |


Expand Down
149 changes: 149 additions & 0 deletions stack/Stack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class MainClass
{
public static void Main()
{
Stack<string> test = new Stack<string>();

test.Push("123");
test.Push("test");
test.Push("rawr");
test.Push("Pancake");
test.Push("Yummy");
test.Push("321");
test.Push(":thinking:");

Console.WriteLine(test.ToString());

test.Rotate(4, true);

Console.WriteLine(test.ToString());

test.Rotate(5, false);

Console.WriteLine(test.ToString());

Console.WriteLine(test.Peek() + "\n");

while (!(test.IsEmpty()))
{
test.Pop();
Console.WriteLine(test.ToString());
}
}
}

class Stack<T>
{
private LinkedList<T> stack;

// Constructs an empty stack
public Stack()
{
stack = new LinkedList<T>();
}

// Adds element at the top of the stack
public void Push(T data)
{
stack.AddLast(data);
}

// Removes the element at the top of the stack
public T Pop()
{
if (IsEmpty())
return default(T);

T element = stack.Last();

stack.RemoveLast();

return element;
}

// Returns true if the stack is empty
public bool IsEmpty()
{
return (!stack.Any());
}


// Returns the last element of the stack
public T Peek()
{
if (IsEmpty())
return default(T);

return stack.Last();
}

/**
* gets the n top elements and rotates them
*
* if left == false then it will move the last element to behind the nth element(from the top)
*
* if left == true it will move the nth element(from the top) to the top
*/
public void Rotate(int n, bool left)
{
if (n <= 1)
return;

if (stack.Count < 2)
return;

n -= 1;

if (left)
{
LinkedListNode<T> node = stack.Last;

for (int i = 1; i <= n; ++i)
{
node = node.Previous;
}

T value = node.Value;

stack.Remove(value);

Push(value);
}
else
{
T last = stack.Last();

Pop();

LinkedListNode<T> node = stack.Last;

for (int i = 0; i < n; i++)
{
node = node.Previous;
}

stack.AddBefore(node.Next, last);
}
}

public override string ToString()
{
string s = "Stack: ";

foreach(T t in stack)
{
s += t.ToString() + " | ";
}

s += "\n";

return s;
}
}

0 comments on commit ffef329

Please sign in to comment.