The Wayback Machine - https://web.archive.org/web/20240930174906/https://www.geeksforgeeks.org/basic-operations-in-stack-data-structure-with-implementations/
Open In App

Basic Operations in Stack Data Structure with Implementations

Last Updated : 18 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Push Operation in Stack:

Push operation adds an item to the stack.

If the stack is full, then it is said to be an Overflow condition.

Below is a sample program to show Push operation in Stack.

C++
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 

    stack<int> s; // creating a stack of integers 

    s.push(1); // This pushes 1 to the stack top 

    s.push(2); // This pushes 2 to the stack top 

    s.push(3); // This pushes 3 to the stack top 

    s.push(4); // This pushes 4 to the stack top 

    s.push(5); // This pushes 5 to the stack top 

    // printing the stack 

    while (!s.empty()) { 
        cout << s.top() << " "; 
        s.pop(); 
    } 

    // The above loop prints "5 4 3 2 1" 
}
Java
import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>(); // Creating a stack of integers

        s.push(1); // This pushes 1 to the stack top
        s.push(2); // This pushes 2 to the stack top
        s.push(3); // This pushes 3 to the stack top
        s.push(4); // This pushes 4 to the stack top
        s.push(5); // This pushes 5 to the stack top

        // Printing the stack in reverse order
        while (!s.isEmpty()) {
            System.out.print(s.pop() + " ");
        }

        // The above loop prints "5 4 3 2 1"
    }
}
Python
# Python Code:
stack = []

stack.append(1) # This pushes 1 to the stack top

stack.append(2) # This pushes 2 to the stack top

stack.append(3) # This pushes 3 to the stack top

stack.append(4) # This pushes 4 to the stack top

stack.append(5) # This pushes 5 to the stack top

# printing the stack

while stack:
    print(stack[-1], end=" ")
    stack.pop()

# The above loop prints "5 4 3 2 1"

# This code is contributed by Sakshi
C#
using System;
using System.Collections.Generic;

class Program {
    static void Main()
    {
        Stack<int> s = new Stack<int>(); // Creating a stack
                                         // of integers

        s.Push(1); // Pushing 1 to the stack top
        s.Push(2); // Pushing 2 to the stack top
        s.Push(3); // Pushing 3 to the stack top
        s.Push(4); // Pushing 4 to the stack top
        s.Push(5); // Pushing 5 to the stack top

        // Printing the stack
        while (s.Count > 0) {
            Console.Write(
                s.Peek()
                + " "); // Peek() gets the top element
                        // without removing it
            s.Pop(); // Pop() removes the top element
        }

        // The above loop prints "5 4 3 2 1"
    }
}
JavaScript
class Stack {
    constructor() {
        this.stack = [];
    }

    push(value) {
        this.stack.push(value); // Pushes the value to the stack top
    }

    top() {
        return this.stack[this.stack.length - 1]; // Returns the element at the top of the stack
    }

    pop() {
        return this.stack.pop(); // Removes and returns the top element of the stack
    }

    isEmpty() {
        return this.stack.length === 0; // Checks if the stack is empty
    }
}

function main() {
    const s = new Stack(); // Creating a stack

    s.push(1); // Pushing 1 to the stack top
    s.push(2); // Pushing 2 to the stack top
    s.push(3); // Pushing 3 to the stack top
    s.push(4); // Pushing 4 to the stack top
    s.push(5); // Pushing 5 to the stack top

    // Printing the stack
    while (!s.isEmpty()) {
        console.log(s.top() + " "); // Outputting the top element
        s.pop(); // Removing the top element
    }
    // The above loop prints "5 4 3 2 1"
}

main(); // Calling the main function

Output
5 4 3 2 1 

Pop Operation in Stack:

Pop operation is used to remove an item from the stack.

The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

Below is a sample program to show Pop operation in Stack.

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    stack<int> s; // creating a stack of integers

    s.push(1); // This pushes 1 to the stack top
    s.push(2); // This pushes 2 to the stack top
    s.push(3); // This pushes 3 to the stack top
    s.push(4); // This pushes 4 to the stack top
    s.push(5); // This pushes 5 to the stack top

    // Now, let us remove elements from the stack using pop function
    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop(); // removes the top element from the stack
    }
}
Java
import java.util.Stack;

public class Main {
    public static void main(String[] args)
    {
        Stack<Integer> s = new Stack<>(); // Creating a stack of integers

        s.push(1); // Pushing 1 to the stack top
        s.push(2); // Pushing 2 to the stack top
        s.push(3); // Pushing 3 to the stack top
        s.push(4); // Pushing 4 to the stack top
        s.push(5); // Pushing 5 to the stack top

        // Removing elements from the stack using the pop function
        while (!s.empty()) {
            System.out.print(s.peek() + " "); // Displaying the top element without removing it
            s.pop(); // Removes the top element from the stack
        }
    }
}
Python
stack = []

stack.append(1) # This pushes 1 to the stack top
stack.append(2) # This pushes 2 to the stack top
stack.append(3) # This pushes 3 to the stack top
stack.append(4) # This pushes 4 to the stack top
stack.append(5) # This pushes 5 to the stack top

# Now, let us remove elements from the stack using pop function
while stack:
    print(stack[-1], end=" ")
    stack.pop() # removes the top element from the stack
C#
using System;
using System.Collections.Generic;

class Program {
    static void Main()
    {
        // Creating a stack of integers
        Stack<int> s = new Stack<int>();

        // Pushing elements onto the stack
        s.Push(1); // This pushes 1 to the stack top
        s.Push(2); // This pushes 2 to the stack top
        s.Push(3); // This pushes 3 to the stack top
        s.Push(4); // This pushes 4 to the stack top
        s.Push(5); // This pushes 5 to the stack top

        // Removing elements from the stack using Pop function
        while (s.Count > 0) {
            Console.Write(s.Peek() + " "); // Displaying the top element without removing it
            s.Pop(); // Removes the top element from the stack
        }
    }
}
JavaScript
// Creating a stack
let stack = [];

// Pushing elements to the stack
stack.push(1); // This pushes 1 to the stack top
stack.push(2); // This pushes 2 to the stack top
stack.push(3); // This pushes 3 to the stack top
stack.push(4); // This pushes 4 to the stack top
stack.push(5); // This pushes 5 to the stack top

// Removing elements from the stack using pop function
while (stack.length > 0) {
    console.log(stack[stack.length - 1]); // Print the top element
    stack.pop(); // Removes the top element from the stack
}

Output
5 4 3 2 1 

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.

C++
#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 
}
Java
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
# 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
C#
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
    }
}
JavaScript
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

isEmpty Operation in Stack:

isEmpty operation is a boolean operation that is used to determine if the stack is empty or not.

This operation will return true if the stack is empty, else false.

Below is a sample program to show Pop operation in Stack.

C++
#include <bits/stdc++.h> 
using namespace std; 

bool isEmpty(stack<int>& s) 
{ 

    bool isStackEmpty 
        = s.empty(); // checking whether stack is empty or 
                    // not and storing it into isStackEmpty 
                    // variable 

    return isStackEmpty; // returning bool value stored in 
                        // isStackEmpty 
} 

int main() 
{ 

    stack<int> s; 

    // The if - else conditional statements below prints 
    // "Stack is empty." 
    if (isEmpty(s)) { 
        cout << "Stack is empty." << endl; 
    } 
    else { 
        cout << "Stack is not empty." << endl; 
    } 

    s.push(1); // Inserting value 1 to the stack top 

    // The if - else conditional statements below prints 
    // "Stack is not empty." 
    if (isEmpty(s)) { 
        cout << "Stack is empty." << endl; 
    } 
    else { 
        cout << "Stack is not empty." << endl; 
    } 
}
Java
import java.util.Stack;

public class Main {
    public static boolean isEmpty(Stack<Integer> s) {
        return s.empty();
    }

    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();

        if (isEmpty(s)) {
            System.out.println("Stack is empty.");
        } else {
            System.out.println("Stack is not empty.");
        }

        s.push(1);

        if (isEmpty(s)) {
            System.out.println("Stack is empty.");
        } else {
            System.out.println("Stack is not empty.");
        }
    }
}
Python
# Python Code:
def isEmpty(s):
    isStackEmpty = len(s) == 0 # checking whether stack is empty or 
                               # not and storing it into isStackEmpty variable
    return isStackEmpty # returning bool value stored in isStackEmpty

s = []

# The if - else conditional statements below prints "Stack is empty."
if isEmpty(s):
    print("Stack is empty.")
else:
    print("Stack is not empty.")

s.append(1) # Inserting value 1 to the stack top

# The if - else conditional statements below prints "Stack is not empty."
if isEmpty(s):
    print("Stack is empty.")
else:
    print("Stack is not empty.")
    
# This code is contributed by Sakshi
C#
using System;
using System.Collections.Generic;

class Program
{
    // Function to check if a stack is empty
    static bool IsEmpty(Stack<int> s)
    {
        return s.Count == 0;
    }

    static void Main()
    {
        Stack<int> s = new Stack<int>();

        // Check if the stack is empty
        if (IsEmpty(s))
        {
            Console.WriteLine("Stack is empty.");
        }
        else
        {
            Console.WriteLine("Stack is not empty.");
        }

        // Push a value (1) onto the stack
        s.Push(1);

        // Check if the stack is empty after pushing a value
        if (IsEmpty(s))
        {
            Console.WriteLine("Stack is empty.");
        }
        else
        {
            Console.WriteLine("Stack is not empty.");
        }
    }
}
JavaScript
function isEmpty(stack) {
    // checking whether stack is empty or not
    return stack.length === 0;
}

function main() {
    const s = [];

    // The if - else conditional statements below prints "Stack is empty."
    if (isEmpty(s)) {
        console.log("Stack is empty.");
    } else {
        console.log("Stack is not empty.");
    }

    s.push(1); // Inserting value 1 to the stack top

    // The if - else conditional statements below prints "Stack is not empty."
    if (isEmpty(s)) {
        console.log("Stack is empty.");
    } else {
        console.log("Stack is not empty.");
    }
}

// Run the main function
main();
//This code is contributed by Monu.

Output
Stack is empty.
Stack is not empty.

size() Operation in Stack:

Size operation in Stack is used to return the count of elements that are present inside the stack. 

Below is a sample program to show Pop operation in Stack.

C++
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 

    stack<int> s; // creating a stack of integers 

    cout << s.size() 
        << endl; // Prints 0 since the stack is empty 

    s.push(1); // This pushes 1 to the stack top 
    s.push(2); // This pushes 2 to the stack top 
    cout << s.size() << endl; // Prints 2 since the stack 
                            // contains two elements 

    s.push(3); // This pushes 3 to the stack top 
    cout << s.size() << endl; // Prints 3 since the stack 
                            // contains three elements 
}
Java
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>(); // creating a stack of integers

        System.out.println(s.size()); // Prints 0 since the stack is empty

        s.push(1); // This pushes 1 to the stack top
        s.push(2); // This pushes 2 to the stack top
        System.out.println(s.size()); // Prints 2 since the stack contains two elements

        s.push(3); // This pushes 3 to the stack top
        System.out.println(s.size()); // Prints 3 since the stack contains three elements
    }
}
Python
# PYthon Code:
stack = [] # creating an empty list as a stack

print(len(stack)) # Prints 0 since the stack is empty

stack.append(1) # This appends 1 to the stack
stack.append(2) # This appends 2 to the stack
print(len(stack)) # Prints 2 since the stack contains two elements

stack.append(3) # This appends 3 to the stack
print(len(stack)) # Prints 3 since the stack contains three element

# This code is contributed by Sakshi
C#
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        Stack<int> s = new Stack<int>(); // creating a stack of integers 

        Console.WriteLine(s.Count); // Prints 0 since the stack is empty 

        s.Push(1); // This pushes 1 to the stack top 
        s.Push(2); // This pushes 2 to the stack top 
        Console.WriteLine(s.Count); // Prints 2 since the stack contains two elements 

        s.Push(3); // This pushes 3 to the stack top 
        Console.WriteLine(s.Count); // Prints 3 since the stack contains three elements 
    }
}
//This code is contribiuted by Kishan.
JavaScript
let stack = []; // Creating an array to simulate a stack

console.log(stack.length); // Prints 0 since the stack is empty

stack.push(1); // This pushes 1 to the stack top
stack.push(2); // This pushes 2 to the stack top
console.log(stack.length); // Prints 2 since the stack contains two elements

stack.push(3); // This pushes 3 to the stack top
console.log(stack.length); // Prints 3 since the stack contains three elements
//This code is contributed by Aman.

Output
0
2
3


Similar Reads

Basic Operations on Binary Tree with Implementations
The tree is a hierarchical Data Structure. A binary tree is a tree that has at most two children. The node which is on the left of the Binary Tree is called “Left-Child” and the node which is the right is called “Right-Child”. Also, the smaller tree or the subtree in the left of the root node is called the “Left sub-tree” and that is on the right i
15+ min read
Name some Queue implementations and compare them by efficiency of operations
A queue is a linear data structure in which insertion is done from one end called the rear end and deletion is done from another end called the front end. It follows FIFO (First In First Out) approach. The insertion in the queue is called enqueue operation and deletion in the queue is called dequeue operation. A queue can be implemented in two ways
10 min read
Basic Operations for Queue in Data Structure
Basic Operations on Queue: Some of the basic operations for Queue in Data Structure are: enqueue() - Insertion of elements to the queue.dequeue() - Removal of elements from the queue.peek() or front()- Acquires the data element available at the front node of the queue without deleting it.rear() - This operation returns the element at the rear end w
11 min read
Static Data Structure vs Dynamic Data Structure
Data structure is a way of storing and organizing data efficiently such that the required operations on them can be performed be efficient with respect to time as well as memory. Simply, Data Structure are used to reduce complexity (mostly the time complexity) of the code. Data structures can be two types : 1. Static Data Structure 2. Dynamic Data
4 min read
Stack Vs Heap Data Structure
What is Stack? A stack is a linear data structure where the last element entered exits first. The order of stack data structure might be LIFO, FILO: According to this technique, the piece that is in last will come out first. As an example, consider a stack of dishes stacked on top of each other. The plate we put last is on top, and because we take
3 min read
Implement Dynamic Multi Stack (K stacks) using only one Data Structure
In this article, we will see how to create a data structure that can handle multiple stacks with growable size. The data structure needs to handle three operations: push(x, stackNum) = pushes value x to the stack numbered stackNumpop(stackNum) = pop the top element from the stack numbered stackNumtop(stackNum) = shows the topmost element of the sta
15+ min read
Design and Implement Special Stack Data Structure | Added Space Optimized Version
Question: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack. All these operations of SpecialStack must be O(1). To implement SpecialStack, you should only use standard Stack data structure
15+ min read
Top 50 Problems on Stack Data Structure asked in SDE Interviews
A Stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack. To learn about Stack Data Structure in detail, please refer to the Tutorial on Stack Data Structure. Given below are the most frequently asked interview questions on Stack:Eas
3 min read
Stack Data Structure
A Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last. Table of Content What is Stack D
4 min read
A data structure for n elements and O(1) operations
Propose a data structure for the following: The data structure would hold elements from 0 to n-1. There is no order on the elements (no ascending/descending order requirement) The complexity of the operations should be as follows: Insertion of an element – O(1) Deletion of an element – O(1) Finding an element – O(1) We strongly recommend to minimiz
4 min read
Design an efficient data structure for given operations
To design an efficient data structure for a specific set of operations, it's important to consider the time and space complexity of different data structures and choose the one that is best suited for the specific requirements. For example, if you need to perform operations such as inserting elements, finding the minimum element, and deleting the m
15+ min read
Stack Permutations (Check if an array is stack permutation of other)
A stack permutation is a permutation of objects in the given input queue which is done by transferring elements from the input queue to the output queue with the help of a stack and the built-in push and pop functions. The rules are: Only dequeue from the input queue.Use inbuilt push, and pop functions in the single stack.Stack and input queue must
15+ min read
Reversing a Stack with the help of another empty Stack
Given a Stack consisting of N elements, the task is to reverse the Stack using an extra stack. Examples: Input: stack = {1, 2, 3, 4, 5} Output: 1 2 3 4 5 Explanation: Input Stack: 5 4 3 2 1 Reversed Stack: 1 2 3 4 5 Input: stack = {1, 3, 5, 4, 2} Output: 1 3 5 4 2 Approach 1: Follow the steps below to solve the problem: Initialize an empty stack.Po
8 min read
Sort a stack using a temporary stack
Given a stack of integers, sort it in ascending order using another temporary stack. Examples: Input : [34, 3, 31, 98, 92, 23] Output : [3, 23, 31, 34, 92, 98] Input : [3, 5, 1, 4, 2, 8] Output : [1, 2, 3, 4, 5, 8] Recommended PracticeSort a stackTry It! Algorithm: Create a temporary stack say tmpStack.While input stack is NOT empty do this: Pop an
6 min read
Find maximum in stack in O(1) without using additional stack in Python
The task is to design a stack which can get the maximum value in the stack in O(1) time without using an additional stack in Python. Examples: Input: Consider the following SpecialStack 16 –> TOP29151918When getMax() is called it should return 29, which is the maximum element in the current stack. If we do pop two times on stack, the stack becom
3 min read
Menu driven program in C++ to perform various basic operations on array
Prerequisite: Switch Statement in C/C++, Functions in C/C++, Loops in C and C++, C/C++ do-while loop with Examples Write a menu-driven program to perform below various basic operations in the array: Print all the even values in the array.Print all the odd values in the array.Sum & average of elements in the array.Find the maximum and minimum el
5 min read
Basic String Operations with Implementation
In this post, we will look into some of the basic String operations such as: Accessing characters by index in a string. Inserting character into a String. Modifying character in String Deletion of Character in String Concatenating strings (combining multiple strings into one). Finding the length of a string Comparing strings for equality or lexicog
15+ min read
Count the number of pop operations on stack to get each element of the array
Prerequisite: Stack, Hashing Given a stack of N numbers and an array of numbers. Count the number of pop operations required to get each element of the array. Once an element is popped then it's not pushed back again. Assume that all the elements from the array are present inside the stack initially. Examples: Input: N = 5, Stack: 6 4 3 2 1 , Array
6 min read
Time and Space Complexity analysis of Stack operations
What is Stack?Stack is a linear data structure that follows a particular order in which the elements are inserted and deleted. A stack follows the principle of last in first out(LIFO) which means that the last element inserted into the stack should be removed first. Consider an example of plates stacked over one another in the canteen. The plate wh
15+ min read
Design a stack with operations on middle element
How to implement a stack which will support the following operations in O(1) time complexity? 1) push() which adds an element to the top of stack. 2) pop() which removes an element from top of stack. 3) findMiddle() which will return middle element of the stack. 4) deleteMiddle() which will delete the middle element. Push and pop are standard stack
15+ min read
Is array a Data Type or Data Structure?
What is Data Type? In computer programming, a data type is a classification of data that determines the type of values that can be stored, manipulated, and processed. It tells the computer what kind of data a particular variable or constant can hold, and what operations can be performed on that data. Common data types include integers, floating-poi
8 min read
Data Structure Alignment : How data is arranged and accessed in Computer Memory?
Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment. Data alignment: Data alignment means putting the data in memory at an address equal to some multiple of the word size.
4 min read
Difference between data type and data structure
Data Type A data type is the most basic and the most common classification of data. It is this through which the compiler gets to know the form or the type of information that will be used throughout the code. So basically data type is a type of information transmitted between the programmer and the compiler where the programmer informs the compile
4 min read
Minimum operations required to Sort the Array using following operations
Given an array arr[] of size N. Make the array arr[] sorted in non-decreasing order in the minimum number of operations where you can choose any integer x and replace elements arr[i] == x equal to 0 for, 0 ? i ? N - 1. Examples: Input: n = 3, arr[] = {3, 3, 2}Output: 1Explanation: If you choose x = 3 then after one operation array will become {0, 0
10 min read
Minimum operations to reduce array elements to zero using prefix operations and updates
Given an array arr[] of length N. We can apply 2 types of operations on element of arr[]: Choose a prefix let say arr[0, i] (0 <= i < N) and decrement all elements of prefix by 1. This operation can only apply if all elements in the prefix > 0. Choose an element arr[i] (0 <= i < N) and update it to 0.The task is to output the minimum
8 min read
Career Path After Learning Basic Programming Languages & Data Structures
The career path after acquiring proficiency in basic programming languages and data structures is an important journey in the field of technology. After learning foundational skills, individuals gain entry into a world of opportunities, from excelling in coding competitions and securing internships to shaping efficient software applications and adv
4 min read
Data Structures | Stack | Question 1
Following is C like pseudo code of a function that takes a number as an argument, and uses a stack S to do processing. void fun(int n) { Stack S; // Say it creates an empty stack S while (n > 0) { // This line pushes the value of n%2 to stack S push(&S, n%2); n = n/2; } // Run while Stack S is not empty while (!isEmpty(&S)) printf("
1 min read
Data Structures | Stack | Question 2
Which one of the following is an application of Stack Data Structure? (A) Managing function calls (B) The stock span problem (C) Arithmetic expression evaluation (D) All of the above Answer: (D) Explanation: See http://en.wikipedia.org/wiki/Stack_(abstract_data_type)#Applications
1 min read
Data Structures | Stack | Question 3
Which of the following is true about linked list implementation of stack? (A) In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end. (B) In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning. (C) Both of t
1 min read
Data Structures | Stack | Question 4
Consider the following pseudocode that uses a stack C/C++ Code declare a stack of characters while ( there are more characters in the word to read ) { read a character push the character on the stack } while ( the stack is not empty ) { pop a character off the stack write the character to the screen } What is output for input \"geeksquiz\"? (A) gee
1 min read
Article Tags :
Practice Tags :