using System;
public class Node {
public Node next;
public string value;
public Node(string value) {
this.value = value;
}
}
class Stack {
Node top;
Node bottom;
int length = 0;
static void Main() {
Console.WriteLine("Hello World!");
Stack myStack = new Stack();
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Push("Discord");
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Push("Udemy");
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Push("Google");
Console.WriteLine("myStack: " + myStack.ToString());
Console.WriteLine("myStack.Peek(): " + myStack.Peek());
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Pop();
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Pop();
Console.WriteLine("myStack: " + myStack.ToString());
myStack.Pop();
Console.WriteLine("myStack: " + myStack.ToString());
}
public void Push(string value) {
Node item = new Node(value);
if (this.length == 0) {
this.bottom = item;
}
if (this.top != null) {
item.next = this.top;
}
this.length++;
this.top = item;
}
public string Pop() {
if (this.top != null) {
string value = this.top.value;
this.top = this.top.next;
this.length--;
return value;
}
return null;
}
public string Peek() {
if (this.top != null) {
return this.top.value;
}
return null;
}
public string ToString() {
string s = "{";
Node current = this.top;
int count = 0;
while (current != null) {
if (count > 0) {
s += ", ";
}
count++;
s += "" + current.value;
current = current.next;
}
s += "}";
return s;
}
}