0% found this document useful (0 votes)
8 views

Peek Operation in Stack Using Array

Uploaded by

r3tvlucky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Peek Operation in Stack Using Array

Uploaded by

r3tvlucky
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Peek Operation in stack using array:

Peek Operation in Stack Using Arrays


Peeking into something means to quickly see what’s there at someplace.

In a similar way, it refers to looking for the element at a specific index in a


stack.

pushing an element into a stack needs you to first check if the stack is
not full, and then insert the element at the incremented value of the top.
And similarly, popping from a stack, needs you to first check if it is not
empty, and then you just decrease the value of the top by 1.
Peek operation requires the user to give a position to peek at as well.
Here, position refers to the distance of the current index from the top
element +1.
The index, mathematically, is (top -position+1).

So, before we return the element at the asked position, we’ll check if the
position asked is valid for the current stack. Index 0, 1 and 2 are valid for
the stack illustrated above, but index 4 or 5 or any other negative index
is invalid.
Note: peek(1) returns 12 here.

1. Create an integer function peek, and pass the reference to the stack,
and the position to peek in, as its parameters.
2. Inside the function, create an integer variable arrayInd which will store
the index of the array to be returned. This is just (top-position +1).
3. Before we return anything, we’ll check if the arrayInd is a valid index. If
it’s less than 0, it is invalid and we report an error. Otherwise,we just
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];

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 = 10;

sp->top = -1;

sp->arr = (int *) malloc(sp->size * sizeof(int));

printf("Stack has been created successfully\n");


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); // ---> Pushed 10 values

//push(sp, 46); // Stack Overflow since the size of the stack is 10

printf("After pushing, Full: %d\n", isFull(sp));

printf("After pushing, Empty: %d\n", isEmpty(sp));

printf("Popped %d from the stack\n", pop(sp)); // --> Last in first out!

printf("Popped %d from the stack\n", pop(sp)); // --> Last in first out!

printf("Popped %d from the stack\n", pop(sp)); // --> Last in first out!

// 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));

return 0;

Bold lines are added in last program. Rest of the code is same.
Output:
Stack has been created successfully

After pushing, Full: 1

After pushing, Empty: 0

Popped 6 from the stack

Popped 89 from the stack

Popped 46 from the stack

The value at position 1 is 57

The value at position 2 is 64

The value at position 3 is 3

The value at position 4 is 75

The value at position 5 is 99

The value at position 6 is 23

The value at position 7 is 1

You might also like