Peek Operation in Stack Using Arrays
Peek Operation in Stack Using Arrays (With C Code & Explanation).
Peek Operation in Stack Using Arrays With C Code & Explanation
Now that we've completed the push and pop operations, we'll move on to the peek operation in stacks. Peeking into something literally means quickly checking what’s there at a certain place. Similarly, in stacks, it refers to looking at the element at a specific index.
As a reminder, when pushing an element into a stack, you must first check if the stack is not full and then insert the element at the incremented value of the top. For popping, you need to ensure the stack is not empty, then decrease the value of the top by 1.
The peek operation requires the user to provide a position to check. Here, the position refers to the distance from the top element, plus one. This can be visualized with some illustrations.
The index, mathematically, is (top - position + 1).
Before returning the element at the requested position, we check if the position is valid for the current stack. Indices 0, 1, and 2 are valid, while indices 4, 5, or any negative index are invalid.
Note: peek(1)
returns 12 here.
Now that we've covered the basics of the peek operation, we can write its code. Focus will mainly be on the peek operation, so you can reference the codes from the previous documents where we learned about push, pop, isFull, and isEmpty.
Understanding the Code Snippet 1:
-
Ensure you've copied everything from the previous documents. This is essential since we are focusing solely on the peek operation.
-
Create an integer function
peek
, passing the reference to the stack and the position to peek at as parameters. -
Inside the function, create an integer variable
arrayInd
to store the index of the array to be returned. This is calculated as(top - position + 1)
. -
Before returning anything, check if
arrayInd
is a valid index. If it’s less than 0, it is invalid, and an error should be reported. Otherwise, return the element at the index(top - position + 1)
.
int peek(struct stack* sp, int i){
int arrayInd = sp->top -i + 1;
if(arrayInd < 0){
printf("Not a valid position for the stack\n");
return -1;
}
else{
return sp->arr[arrayInd];
}
}
Code Snippet 1: Writing the peek function
Here is the whole source code:
#include<stdio.h>
#include<stdlib.h>
struct stack{
int size ;
int top;
int * arr;
};
int isEmpty(struct stack* ptr){
if(ptr->top == -1){
return 1;
}
else{
return 0;
}
}
int isFull(struct stack* ptr){
if(ptr->top == ptr->size - 1){
return 1;
}
else{
return 0;
}
}
void push(struct stack* ptr, int val){
if(isFull(ptr)){
printf("Stack Overflow! Cannot push %d to the stack\n", val);
}
else{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
int pop(struct stack* ptr){
if(isEmpty(ptr)){
printf("Stack Underflow! Cannot pop from the stack\n");
return -1;
}
else{
int val = ptr->arr[ptr->top];
ptr->top--;
return val;
}
}
int peek(struct stack* sp, int i){
int arrayInd = sp->top -i + 1;
if(arrayInd < 0){
printf("Not a valid position for the stack\n");
return -1;
}
else{
return sp->arr[arrayInd];
}
}
int main(){
struct stack *sp = (struct stack *) malloc(sizeof(struct stack));
sp->size = 50;
sp->top = -1;
sp->arr = (int *) malloc(sp->size * sizeof(int));
printf("Stack has been created successfully\n");
printf("Before pushing, Full: %d\n", isFull(sp));
printf("Before pushing, Empty: %d\n", isEmpty(sp));
return 0;
}
Code Snippet 2: Implementing the peek function
This is how we peek into a stack array. We’ll see how properly the functions work. First, we’ll push a few elements into the empty stack we created.
push(sp, 1);
push(sp, 23);
push(sp, 99);
push(sp, 75);
push(sp, 3);
push(sp, 64);
push(sp, 57);
push(sp, 46);
push(sp, 89);
push(sp, 6);
push(sp, 5);
push(sp, 75);
Code Snippet 3: Pushing a few elements in the stack
Now, we can peek into this stack array and print all the elements using a loop.
// Printing values from the stack
for (int j = 1; j <= sp->top + 1; j++)
{
printf("The value at position %d is %d\n", j, peek(sp, j));
}
Code Snippet 4: Calling the peek function
The output we received was:
The value at position 1 is 75
The value at position 2 is 5
The value at position 3 is 6
The value at position 4 is 89
The value at position 5 is 46
The value at position 6 is 57
The value at position 7 is 64
The value at position 8 is 3
The value at position 9 is 75
The value at position 10 is 99
The value at position 11 is 23
The value at position 12 is 1
PS D:\Code>
Figure 1: Output of the above program