📒
Notes
Cloud ComputingData Science/AIGame Development
  • Home
  • Big O
  • Data Structures & Algorithms
    • Data Structures
      • Array
      • Stack
      • Queue
      • Linked List
      • Binary Tree
    • Algorithms
      • Searching
      • Sorting
      • Graphs
        • Searching
        • Minimum Spanning Tree
        • Shortest Path Algorithms
      • String Algorithms
  • Object Oriented Programming
  • Languages
    • HTML/CSS
      • CSS
    • C++
    • C#
      • Types
      • Keywords
        • Modifiers
          • Access Modifiers
        • Method Parameters
      • Operators and Expressions
      • Collections
      • Constructors
      • Delegates
      • Indexers
      • Concepts
      • Features
        • LINQ
          • Operators
          • Working with Data
          • Methods
          • Resources
        • Asynchronous Programming
        • Reflection
    • Dart
    • GraphQL
    • JavaScript
      • Variable and Parameter
      • Built-in objects
        • Array
        • Built-in Functions
      • Functions
      • Classes
      • Prototype
      • Libraries
        • jQuery
        • React
          • Components
          • State and Lifecycle
          • Hooks
            • useState
            • useEffect
          • Resources
      • Testing Framework
      • Web APIs
    • Kotlin
      • Basics
    • Python
      • Basics
      • Data Structures
      • Functions
      • Resources
        • Flask
    • SQL
      • Basics
      • Operators
      • JOINs
      • Aggregations
      • Subqueries
      • Views
      • Functions
        • Window Functions
      • Stored Procedures
      • Performance Tuning
      • Extras
    • Resources
  • 🌐Web Frameworks
    • Angular
      • Templates
      • Directives
        • Attribute Directives
        • Structural Directives
    • ASP.NET
      • Fundamentals
        • Dependency Injection
        • Middleware
        • Session & State Management
      • Web apps
        • MVC
          • Controllers
            • Filters
          • Models
            • Model Binding
            • Model Validation
          • Views
            • Tag Helpers
            • View Components
          • Features
        • Client-side development
      • Web APIs
        • Controller-based APIs
        • Minimal APIs
        • OpenAPI
        • Content Negotiation
      • SignalR
      • Host and Deploy
        • IIS
      • Security
    • Django
      • The Request/Response Cycle
    • Terminologies
      • Web Server
        • Internet Information Services
    • Resources
  • 📱App Frameworks
    • Introduction
      • Resources
    • Xamarin
      • Lifecycle
      • Custom Renderers & Effects
      • Behaviors
      • Triggers
      • Gestures
      • Commands
      • Dependency Service in XF
      • Libraries
      • Showcase
    • .NET MAUI
      • Controls
      • Navigation
      • Storage Options
  • Multi-Platform Frameworks
    • .NET
      • .NET Framework
        • ADO.NET
        • WCF
      • Fundamentals
        • Logging
        • Testing
      • Advanced
        • Asynchronous Programming
        • Parallel Programming
        • Threading
        • Memory Management
          • Garbage Collection
    • Flutter
  • Object-Relational Mappers
    • Entity Framework
      • Application Models
      • Configuration
      • Setting Up
      • Advanced
  • Databases
    • Introduction
      • DBMS Architecture
      • Normalization
      • Database Transaction Models
    • Relational Databases
      • Microsoft SQL Server
        • Basics
        • Functions
        • Stored Procedures
        • Error Handling
        • Log Shipping
        • Querying and Manipulating JSON data
        • Statements
        • Topics
        • Extras
    • Non-Relational Databases
      • MongoDB
      • Redis
        • Data Structures
        • Introduction
        • Managing Database
  • Tools
    • Version Control
      • Git
        • Setup and Config
        • Basics
          • Sharing and Updating Projects
        • Resources
      • Perforce Helix
    • GitHub
    • Powershell
  • Software Development
    • Software Development Life Cycle
    • Software Design Patterns
      • GoF Design Patterns
      • Architectural Patterns
        • MVC
        • MVVM
        • N-tier Architecture
        • Onion Architecture
        • Data Transfer Objects
      • CQRS
    • Software Design Principles
      • S.O.L.I.D. Priniciple
  • System Design
    • Topics
      • Load Balancing
  • Topics
    • JWT
    • Caching
      • Static vs Dynamic Caching
    • OSI model
      • HTTP
    • Glossary
    • API
      • SOAP
      • REST
    • Microservices
    • WebHooks
    • Practice
    • Operating Systems
      • Windows
    • Architecture
  • 🔖Bookmarks
  • 🔗Resources
Powered by GitBook
On this page

Was this helpful?

  1. Data Structures & Algorithms
  2. Data Structures

Stack

Last In First Out (LIFO)

PreviousArrayNextQueue

Last updated 2 years ago

Was this helpful?

The last item to be inserted into a stack is the first one to be deleted from it.

  • Example: A stack of trays on a table, the tray at the top of the stack is the first item to be moved if you require a tray from that stack.

Stack : Example
  • Non-Generic Stack

using System;
using System.Collections;

public class NonGenericStackExample {
    public static void Main() {
	Console.WriteLine("Non-Generic Stack Example\n");
	var myStack = new Stack();
	myStack.Push("Hello,");
	myStack.Push("World!");
	Console.WriteLine("Items in the Simple Stack: ");
	foreach (var item in myStack) {
		Console.WriteLine("- " + item + " ");
	}
    }
}
  • Generic Stack

using System;
using System.Collections.Generic;

public class GenericStackExample {
    public static void Main() {
	Console.WriteLine("Generic Stack Example\n");
	Stack<string> gStack = new Stack<string>();
	gStack.Push("One");
	gStack.Push("Two");
	gStack.Push("Three");
	gStack.Push("Four");
	gStack.Push("Five");
	Console.WriteLine("Items in the Generic Stack: ");
	foreach (var item in gStack) {
	    Console.WriteLine("- " + item + " ");
	}

	Console.WriteLine("Count: " + gStack.Count);
	Console.WriteLine("Contains(): " + gStack.Contains("One"));
	Console.WriteLine("Peek(): " + gStack.Peek());
	Console.WriteLine("Pop(): " + gStack.Pop());
	foreach (var item in gStack) {
	    Console.WriteLine("- " + item + " ");
	}

	Console.WriteLine("Copy of gStack also reversing the order");
	Stack<string> cgStack = new Stack<string>(gStack.ToArray());
	foreach (var cItem in cgStack) {
	    Console.WriteLine("- " + cItem + " ");
	}

	Console.WriteLine("Copy of gStack");
	string[] array = new string[gStack.Count * 2];
	gStack.CopyTo(array, gStack.Count);
	Stack<string> cgStack2 = new Stack<string>(array);
	foreach (var cItem in cgStack2)	{
	    Console.WriteLine("- " + cItem + " ");
	}

	Console.Write("Clear():");
	cgStack.Clear();
    }
}

Applications

  • undo/redo functionality

  • word reversal

  • stack backward/forward on browsers

  • backtracking algorithms

  • bracket verification