-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackImplementation.cs
More file actions
116 lines (111 loc) · 2.5 KB
/
Copy pathStackImplementation.cs
File metadata and controls
116 lines (111 loc) · 2.5 KB
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
112
113
114
115
116
using System;
using System.Collections;
using System.Collections.Generic;
/*Stack - A stack is data structure in which items are added or
* removed in a last in first out .
* We can use example of plates in which the last
* added plate will be removed first.
* --- Stack two mehthod Push, Pop
* Push()--- > Push in stack means adding element to the stack
* Elements are added in the stack from one direction only. I ma calling tha t
* position as the top of the stack.
* Stack is somewaht of a restrictive data structure
* becasue it only allows insertion of
* element from the top of the stack.
*
* Pop--- Pop in stack means removeing an element from the stack
* item are added in the oreder 1 2 3
* then on calling pop then item 3 will be removed first then 2 and
* 1.
*
* Peek -- A peek operation returns the last added element in the staf
* on the top.
*
* Clear -- it will clear all the items pf the stack.
* Enumeratng among elemnt of the stack.
*/
namespace ConsoleApp13
{
internal class Stack
{
static readonly int MAX = 1000;
int top;
readonly int[] stack = new int[MAX];
public Stack()
{
top = -1;
}
public bool IsEmpty()
{
return (top < 0);
}
internal bool Push(int data)
{
if (top >= MAX)
{
Console.WriteLine("Stack is overflow");
return false;
}
else
{
stack[++top] = data;
return true;
}
}
internal int Pop()
{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return 0;
}
else
{
int value = stack[top--];
return value;
}
}
internal void Peek()
{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return;
}
else
Console.WriteLine("The topmost element of stak : " + stack[top]);
}
internal void PrintStack()
{
if (top < 0)
{
Console.WriteLine("Stack Underflow");
return;
}
else
{
Console.WriteLine("items in the stack are");
for (int i = top; i >= 0; i--)
{
Console.WriteLine(stack[i]);
}
}
}
}
class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Push(10);
stack.Push(20);
stack.Push(30);
stack.Push(40);
stack.Push(50);
stack.PrintStack();
stack.Peek();
Console.WriteLine("Item is poped form stack {0}", stack.Pop());
stack.PrintStack();
}
}
}