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

Stack Implementation

The document describes implementing a static stack using an array in C. It includes function prototypes and implementations for push, pop, peek, isEmpty and isFull operations on the stack. It also shows sample output of using the stack implementation.

Uploaded by

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

Stack Implementation

The document describes implementing a static stack using an array in C. It includes function prototypes and implementations for push, pop, peek, isEmpty and isFull operations on the stack. It also shows sample output of using the stack implementation.

Uploaded by

Aditya Kashyap
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Implementing Static Stack using an Array in C

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// N will be the capacity of the Static Stack


#define N 1000

// Initializing the top of the stack to be -1


int top = -1;

// Initializing the stack using an array


int stack[N];

// Function prototypes
void push(); // Push element to the top of the stack
int pop(); // Remove and return the top most element of the
stack
int peek(); // Return the top most element of the stack
bool isEmpty(); // Check if the stack is in Underflow state or
not
bool isFull(); // Check if the stack is in Overflow state or
not

int main(){
printf("STATIC ARRAY (Total Capacity: %d)\n", N);
int choice;

while(1){
printf("\nChoose any of the following options:\n");
printf(" 0: Exit 1: Push 2: Pop
3: Peek\n");
printf(" 4: Check if the stack is empty 5: Check if the
stack is full\n\n");
scanf("%d", &choice);

switch(choice){
case 0: exit(0);
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: isEmpty(); break;
case 5: isFull(); break;
default: printf("Please choose a correct option!");
}
}
return 0;
}

void push(){
// Checking overflow state
if(top == N-1)
printf("Overflow State: can't add more elements into the
stack\n");
else{
int x;
printf("Enter element to be pushed into the stack: ");
scanf("%d", &x);
top+=1;
stack[top] = x;
}
}

int pop(){
// Checking underflow state
if(top == -1)
printf("Underflow State: Stack already empty, can't remove
any element\n");
else{
int x = stack[top];
printf("Popping %d out of the stack\n", x);
top-=1;
return x;
}
return -1;
}

int peek(){
int x = stack[top];
printf("%d is the top most element of the stack\n", x);
return x;
}

bool isEmpty(){
if(top == -1){
printf("Stack is empty: Underflow State\n");
return true;
}
printf("Stack is not empty\n");
return false;
}

bool isFull(){
if(top == N-1){
printf("Stack is full: Overflow State\n");
return true;
}
printf("Stack is not full\n");
return false;
}
OUTPUT
STATIC ARRAY (Total Capacity: 1000)

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

1
Enter element to be pushed into the stack: 7

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

1
Enter element to be pushed into the stack: 6

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

2
Popping 6 out of the stack

Choose any of the following options:


0: Exit 1: Push 2: Pop 3: Peek
4: Check if the stack is empty 5: Check if the stack is full

Process returned 0 (0x0) execution time : 20.144 s


Press any key to continue.

Stack Program in C using Linked List


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

// Function prototypes
void push(); // Push element to the top of the stack
int pop(); // Remove and return the top most element of the stack
int peek(); // Return the top most element of the stack
bool isEmpty(); // Check if the stack is in Underflow state or not

struct node{
int val;
struct node *next;
};

struct node *head;

int main (){


int choice=0;
printf("DYNAMIC STACK");
while(1){
printf("\n\nChose any of the following options:\n");
printf("\n 0: Exit 1: Push 2: Pop 3: Peek\n 4: Check if stack is
empty\n");
scanf("%d",&choice);
switch(choice){
case 0: exit(0);
case 1: push(); break;
case 2: pop(); break;
case 3: peek(); break;
case 4: isEmpty(); break;
default: printf("Please choose a correct option!");
}
}
}

void push (){


int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be pushed: ");
scanf("%d", &val);

if(head == NULL){
ptr->val = val;
ptr->next = NULL;
head = ptr;
}
else{
ptr->val = val;
ptr->next = head;
head=ptr;
}
}

int pop(){
int item;
struct node *ptr;
if (head == NULL)
printf("Underflow State: can't remove any item");
else{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("%d is popped out of the stack", item);
return item;
}
return -1;
}

int peek(){
int x = head->val;
printf("%d is the top most element of the stack\n", x);
return x;
}

bool isEmpty(){
if(head == NULL){
printf("Stack is empty: Underflow State\n");
return true;
}
printf("Stack is not empty\n");
return false;
}

Output
DYNAMIC STACK

Chose any of the following options:

0: Exit 1: Push 2: Pop 3: Peek


4: Check if stack is empty

1
Enter the value to be pushed: 56

Chose any of the following options:

0: Exit 1: Push 2: Pop 3: Peek


4: Check if stack is empty

1
Enter the value to be pushed: 76

Chose any of the following options:

0: Exit 1: Push 2: Pop 3: Peek


4: Check if stack is empty

2
76 is popped out of the stack

Chose any of the following options:

0: Exit 1: Push 2: Pop 3: Peek


4: Check if stack is empty

Process returned 0 (0x0) execution time : 10.200 s


Press any key to continue

You might also like