DATA STRUCTURES
AND ALGORITHMS
DR. FATMA ELGENDY
STACKS
• Stack is a linear data structure.
• It follows LIFO(Last In First Out) pattern for Input/output.
• The element inserted last will be removed first.
• The process to add an element into stack is called Push.
• The process to remove an element from stack is called Pop.
STACK<T> -BASIC OPERATIONS
• Push(T): add a new element on the top of the stack.
• Pop(): returns the highest element and removes it from the stack.
• Peek(): returns the highest element without removing it.
• Count: returns the count of elements in the stack.
• Clear(): retrieves all elements from the stack.
• Contains(T): check whether the stack contains the element.
• ToArray(): returns an array, containing all elements of the stack.
STACK IMPLEMENTATION
• In C# the standard implementation of the stack in .NET Framework is
System.Collections.Generics.Stack<T>.
• Example:
• Create a stack from an array
• The Pop() method returns the last element and removes it from a stack.
• The Peek() method returns the lastly added value from the stack but does not remove it.
• The Contains() method checks whether the specified element exists in a Stack
collection or not. It returns true if it exists, otherwise false.
STACK IMPLEMENTATION
USING LIST