Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Main Stack

This document defines a Stack class that implements a basic stack data structure using an array. The Stack class tracks the size of the stack, the underlying array, and indexes for the start and end of the stack. It provides Push and Pop methods to add and remove elements from the stack according to the LIFO principle, handling full and empty stack conditions. The Main method demonstrates creating and using a Stack object.

Uploaded by

Samrat Kaushik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Main Stack

This document defines a Stack class that implements a basic stack data structure using an array. The Stack class tracks the size of the stack, the underlying array, and indexes for the start and end of the stack. It provides Push and Pop methods to add and remove elements from the stack according to the LIFO principle, handling full and empty stack conditions. The Main method demonstrates creating and using a Stack object.

Uploaded by

Samrat Kaushik
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

using System; using System.Collections.Generic; using System.Linq; using System.

Text; using StackApp; namespace StackApp { public class Stack { private int _size = 0; private int[] stackArray = null; private int _start = 0; private int _end = -1; public int Size { get { return _size; } } public Stack(int size) { _size = size; stackArray = new int[size]; } public void Push(int element) { if (_end == _size - 1) Console.WriteLine("Stack full, Cannot insert."); else { _end++; stackArray[_end] = element; Console.WriteLine("Pushed: " + element); } }

public void Pop() { if (_start > _end) { Console.WriteLine("Stack is empty, Cannot pop."); } else { Console.WriteLine("Poped: " + stackArray[_start]); _start++; } } } } class Program { static void Main(string[] args) { Stack stack = new Stack(3); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(3); stack.Pop(); stack.Pop(); stack.Pop(); stack.Pop(); Console.ReadLine(); } }

You might also like