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.
What is the difference between top and peek in stack?
4.2.
What is the peek() operation in the stack?
4.3.
Describe pop in the stack.
4.4.
What do you mean by stack?
4.5.
What is the application of stack? Give an example.
4.6.
What are the different types of stacks?
5.
Conclusion
Last Updated: Jul 2, 2024
Easy

Peek() Operation in Stack

Author Aditya Kumar
0 upvote

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 without using peek

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.

demonstration of peek

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 using peek

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

What is the difference between top and peek in stack?

In stack terminology, Top refers to the position of the last inserted element, accessible for reading or updating and Peek allows reading the top element without removing it from the stack, preserving the stack's state. It's used for inspection rather than modification.

What is the peek() operation in the stack?

This method is used to get the top element in the stack without removing it.

Describe pop in the stack.

Using Java's pop() function, one can remove an element from the stack.

What do you mean by stack?

A stack is a conceptual structure based on the last in, first out (LIFO) principle and composed of several homogenous pieces (LIFO).

What is the application of stack? Give an example.

Undo and redo is one of the applications of stack.

What are the different types of stacks?

Stacks are of two types register and memory.

Conclusion

In this article, we have discussed the peek operation in stack. We have discussed how the peek operation is useful to retrieve the data. If you want to enhance your knowledge in the stack and in other Data Structures, then you can check out our articles.

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.

Happy Coding!

Live masterclass