-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path12 ● Stack_using_Array.cs
111 lines (110 loc) · 2.66 KB
/
12 ● Stack_using_Array.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
namespace Stackarr
{
class stack
{
public int top;
public int[] arr;
public stack ()
{
top = -1;
}
public void get_array(int size)
{
while (size < 1)
{
Console.WriteLine("Enter Valid Size = ");
size = int.Parse(Console.ReadLine());
get_array(size);
}
arr = new int[size];
}
public bool is_empty()
{
return top < 0;
}
public bool is_full()
{
return top == arr.Length-1;
}
public void push(int n)
{
if(is_full())
Console.WriteLine("Stack Overflow!");
else
{
top++;
arr[top] = n;
}
}
public void pop()
{
if (is_empty())
Console.WriteLine("Stack Underflow!");
else
{
Console.WriteLine("Pop : "+arr[top]);
top--;
}
}
public void peek()
{
if (is_empty())
Console.WriteLine("Stack is empty!");
else
Console.WriteLine("the top element in the stack is " + arr[top]);
}
public int size()
{
if (is_empty())
{
Console.WriteLine("Stack is empty!");
return 0;
}
else
return top + 1;
}
public void display()
{
if (is_empty())
Console.WriteLine("Stack is empty!");
else
{
for(int i = top; i >=0;i--)
Console.Write(arr[i]+" ");
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
stack L = new stack();
Console.WriteLine("Enter the size of stack :");
int size = int.Parse(Console.ReadLine());
L.get_array(size);
if (L.is_empty())
{ Console.WriteLine("Empty Stack"); }
L.push(10);
L.push(11);
L.push(12);
L.push(13);
L.push(14);
L.push(15);
L.peek();
Console.WriteLine("size = " +L.size());
L.pop();
L.pop();
L.pop();
L.is_empty();
L.push(10);
L.push(11);
L.push(12);
L.push(10);
L.push(11);
L.push(12);
L.display();
}
}
}