Table of contents
1.
Introduction
2.
What is Peek Operation in Stack?
2.1.
Without Peek Operation
2.1.1.
Implementation 
2.2.
C++
3.
Peek Operation Implementation
3.1.
Algorithm of the Peek Operation
3.2.
Implementation
3.3.
C++
4.
Frequently Asked Questions
4.1.
How does peek() work?
4.2.
What is peek() in queue?
4.3.
What is peek and PEEP?
5.
Conclusion
Last Updated: Mar 2, 2025
Easy

Peek() Operation in Stack

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The peek() operation is used with abstract data types like stacks and queues. This function return the value at the top or front of the collection while keeping the element from the collection in place.

In stack, we have two primary operations push and pop. For these operations, we have a lot of stack operations like isEmpty(), isFull(), etc. But the peek() operation in stack is one of the most important operations which is used to retrieve data. 

Peek operation in stack

In this article, we will discuss the peek operation in a stack in detail by implementing in programming languages. We will see how it works generally. Let’s discuss what is peek operation in stack.

What is Peek Operation in Stack?

Peek() is one the most important operations in the stack, which is used to get the top element of the stack without deleting that element. For example, if we have a class of students who are attending exam to get good marks from the teacher and the roll number 1 wants to know what is the last roll number student’s marks. We can apply the peek operation in the stack to get the last student’s marks. Here peek operation is used to get the last student’s marks easily. What if we do not have a peek operation, so this task becomes too difficult? Let’s understand this thing with an example.

Without Peek Operation

If we take the above example, we do not have any peek operation in stack, and we want to find the last student’s marks.

Implementation 

  • C++

C++

#include <iostream>


using namespace std;


int main()
{
   int numOfStudent;
   cout<<"Enter number of students"<<endl;
   cin>>numOfStudent;
   int arr[numOfStudent];
   cout<<"Marks of students"<<endl;
   int rollno=1;
   for(int i=0;i<numOfStudent;i++)
   {
       cout<<"Marks of roll no. "<<rollno<<endl;
       cin>>arr[i];
       ++rollno;
   }
   cout<<"Finding last student's marks...."<<endl;
   int len = sizeof(arr)/sizeof(arr[0]);
   int lastStudent=arr[len-1];
   for(int i=0;i<len;i++)
   {
       if(arr[i]==lastStudent)
       {
           cout<<arr[i]<<" is the last student's marks"<<endl;
       }
   }
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

output

The above implementation shows that the for loop is being used to get the last student’s marks. And the time complexity becomes O(n). But what if we use the peek operation in stack.

Peek Operation Implementation

In the above example, we can use the peek operation in stack to get the last student’s marks. Here is the implementation for the same.

Peek Operation Implementation

Algorithm of the Peek Operation

  1. Begin Peek Operation
  2. return stack[topElement];
  3. End Peek Operation

Implementation

  • C++

C++

#include<iostream>
#define MAX 10
using namespace std;
int stack[MAX], top = -1;
void push(int stack[], int marksOfStudent);
int peek(int stack[]);


// Push Function to insert marks
void push(int stack[], int marksOfStudent,int rollno)
{
   top++;
   cout<<"Marks of roll no. "<<rollno<<endl;
   cin>>marksOfStudent;
   stack[top] = marksOfStudent;
}


// Peek Function to show last student’s marks
int peek(int stack[])
{
   return (stack[top]);
}


int main()
{
   int numOfStudent;


   cout<<"Enter the number of students"<<endl;
   cin>>numOfStudent;
   cout<<"Marks of students"<<endl;
   int marksOfStudent;
   int rollno=1;
   while(numOfStudent>0){
       push(stack, marksOfStudent,rollno);
       --numOfStudent;
       ++rollno;
   }
   // Calling peek() to retrieve last student’s marks
int lastStudent = peek(stack);
   cout<<"Finding last student's marks...."<<endl;
   cout<<lastStudent<<" is the last student's marks"<<endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

output

By using the peek operation in stack we can easily find the marks of the last student with the time complexity of O(1).

Must Read Stack Operations

Frequently Asked Questions

How does peek() work?

The peek() method returns the top element of a data structure (like stack or queue) without removing it.

What is peek() in queue?

The peek() method in a queue retrieves the front element without deleting it, allowing access to the next item to be processed.

What is peek and PEEP?

Peek() returns the top element without removal, while PEEP is typically used in stack operations to inspect an element at a specific position without removal.

Conclusion

In this article, we explored the peek() operation in Stack, which plays a crucial role in accessing the top element without altering the data structure. Understanding the peek() method is essential for efficient stack manipulation, as it helps in various applications like expression evaluation, undo operations, and memory management.

Recommeded articles:

Live masterclass