Here is the Code:
class Program
{
static void Main(string[] args)
{
// This Solution is written by me and is not completely tested. Please comment if you found any alternative solutions or any enhancement to my solution.
stack s = new stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.Display();
s.pop();
s.Display();
s.push(8);
s.Display();
s.pop();
s.Display();
}
}
public class node
{
public int data { get; set; }
public node next { get; set; }
public node(int _data)
{
data = _data;
}
}
public class stack
{
public node top { get; set; }
public void push(int data)
{
node n = new node(data);
if (top == null)
{
top = n;
}
else
{
n.next = top;
top = n;
}
}
public int pop()
{
int data=0;
if (top == null)
{
return 0;
}
else
{
data= top.data;
top = top.next;
}
return data;
}
public void Display()
{
var temp = top;
Console.WriteLine("-------------");
while (temp != null)
{
Console.WriteLine(temp.data);
temp = temp.next;
}
Console.WriteLine("-------------");
}
}
Output:
-------------
5
4
3
2
1
-------------
-------------
4
3
2
1
-------------
-------------
8
4
3
2
1
-------------
-------------
4
3
2
1
-------------
Press any key to continue . . .
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment