Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
54 changes: 0 additions & 54 deletions Exercise_1.cpp

This file was deleted.

70 changes: 70 additions & 0 deletions Exercise_1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class Stack {

// Time Complexity Except Push is O(1) , Push O(n)
// Space Complexity = O(n)
const int MAX = 1000;
int top;
int[] arr = new int[MAX]; // Maximum size of Stack

public bool isEmpty()
{
//Write your code here
return top == -1;
}

public Stack()
{
//Initialize constructor
top = -1;
}

public void Resize()
{
int capacity = arr.Length * 2;
int[] newarr = new int[capacity];
Array.Copy(arr, newarr, arr.Length);
arr = newarr;

}
public void push(int x)
{
if (top == arr.Length - 1)
{
Resize();
}
arr[++top] = x;

}

public int pop()
{
//If empty return 0 and print " Stack Underflow"
if (isEmpty())
{
Console.Write(" Stack Underflow");
return 0;
}
return arr[top--];
}

public int peek()
{
if (isEmpty())
{
return 0;
}
return arr[top];
}
}

// Driver code
class Program {
public static void Main(String[] args)
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
Console.Write(s.pop() + " Popped from stack");
}
}
46 changes: 0 additions & 46 deletions Exercise_1.java

This file was deleted.

35 changes: 0 additions & 35 deletions Exercise_1.js

This file was deleted.

24 changes: 0 additions & 24 deletions Exercise_1.py

This file was deleted.

52 changes: 0 additions & 52 deletions Exercise_2.cpp

This file was deleted.

75 changes: 75 additions & 0 deletions Exercise_2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

//Time Complexity - O(1)
//Space Complexity - O(n)
public class StackAsLinkedList
{

StackNode root;

private class StackNode
{
public int data;
public StackNode next;

public StackNode(int data)
{
//Constructor here
this.data = data;
next = null;
}
}


public bool isEmpty()
{
return root == null;
}

public void push(int data)
{
//Write code to push data to the stack.
StackNode st = new StackNode(data);
st.next = root;
root = st;
}

public int pop()
{
//If Stack Empty Return 0 and print "Stack Underflow"
//Write code to pop the topmost element of stack.
//Also return the popped element
if (isEmpty())
{
Console.WriteLine("Stack Underflow");
return 0;
}
int popped = root.data;
root = root.next;
return popped;
}

public int peek()
{
//Write code to just return the topmost element without removing it.
if (isEmpty())
{
Console.WriteLine("Stack Underflow");
return 0;
}
return root.data;
}

//Driver code
public static void Main(String[] args)
{
StackAsLinkedList sll = new StackAsLinkedList();

sll.push(10);
sll.push(20);
sll.push(30);

Console.Write(sll.pop() + " popped from stack");

Console.Write("Top element is " + sll.peek());
}
}
Loading