Top Operation in Stack
Top operation is used to return the top element of the stack.
Below is a sample program to show Pop operation in Stack.
#include <bits/stdc++.h>
using namespace std;
int topElement(stack<int>& s) { return s.top(); }
int main()
{
stack<int> s; // creating a stack of integers
s.push(1); // This pushes 1 to the stack top
cout << topElement(s)
<< endl; // Prints 1 since 1 is present at the
// stack top
s.push(2); // This pushes 2 to the stack top
cout << topElement(s)
<< endl; // Prints 2 since 2 is present at the
// stack top
s.push(3); // This pushes 3 to the stack top
cout << topElement(s)
<< endl; // Prints 3 since 3 is present at the
// stack top
}
import java.util.Stack;
public class StackExample {
public static int topElement(Stack<Integer> s) {
return s.peek();
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
// Pushing 1 to the stack top
s.push(1);
System.out.println(topElement(s)); // Prints 1
// Pushing 2 to the stack top
s.push(2);
System.out.println(topElement(s)); // Prints 2
// Pushing 3 to the stack top
s.push(3);
System.out.println(topElement(s)); // Prints 3
}
}
# Python code:
def topElement(s):
return s[-1]
s = [] # creating a stack of integers
s.append(1) # This pushes 1 to the stack top
print(topElement(s)) # Prints 1 since 1 is present at the stack top
s.append(2) # This pushes 2 to the stack top
print(topElement(s)) # Prints 2 since 2 is present at the stack top
s.append(3) # This pushes 3 to the stack top
print(topElement(s)) # Prints 3 since 3 is present at the stack top
# This code is contributed by Sakshi
using System;
using System.Collections.Generic;
class Program
{
static int TopElement(Stack<int> s)
{
return s.Peek();
}
static void Main()
{
Stack<int> s = new Stack<int>(); // creating a stack of integers
s.Push(1); // This pushes 1 to the stack top
Console.WriteLine(TopElement(s)); // Prints 1 since 1 is present at the stack top
s.Push(2); // This pushes 2 to the stack top
Console.WriteLine(TopElement(s)); // Prints 2 since 2 is present at the stack top
s.Push(3); // This pushes 3 to the stack top
Console.WriteLine(TopElement(s)); // Prints 3 since 3 is present at the stack top
}
}
function topElement(s) {
return s[s.length - 1];
}
// Main function
function main() {
let s = []; // Creating an array to act as a stack
s.push(1); // Pushing 1 to the stack
console.log(topElement(s)); // Prints 1 since 1 is at the top of the stack
s.push(2); // Pushing 2 to the stack
console.log(topElement(s)); // Prints 2 since 2 is at the top of the stack
s.push(3); // Pushing 3 to the stack
console.log(topElement(s)); // Prints 3 since 3 is at the top of the stack
}
// Calling the main function
main();
//THis code is contributed by Utkarsh
Output
1 2 3
Basic Operations in Stack Data Structure with Implementations
In order to make manipulations in a stack, there are certain operations provided to us for Stack, which include:
- push() to insert an element into the stack
- pop() to remove an element from the stack
- top() Returns the top element of the stack.
- isEmpty() returns true if the stack is empty else false.
- size() returns the size of the stack.
In this post, we will see how to perform these operations on Stack.
Contact Us