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

Stack Using Array

This document describes how to implement a stack using an array in C programming. It defines functions for pushing, popping, and displaying items in the stack. The main function uses a switch statement to allow the user to select push, pop, or display and calls the corresponding functions. The push function inserts a new item into the stack array if there is space, pop removes and returns the top item if the stack is not empty, and display prints the contents of the stack from top to bottom.

Uploaded by

Abdul Samad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Stack Using Array

This document describes how to implement a stack using an array in C programming. It defines functions for pushing, popping, and displaying items in the stack. The main function uses a switch statement to allow the user to select push, pop, or display and calls the corresponding functions. The push function inserts a new item into the stack array if there is space, pop removes and returns the top item if the stack is not empty, and display prints the contents of the stack from top to bottom.

Uploaded by

Abdul Samad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

www.eazynotes.

com

Gursharan Singh Tatla

Page No. 1

STACK USING ARRAY


/****** Program to Implement Stack using Array ******/

#include <stdio.h> #define MAX 50 void push(); void pop(); void display(); int stack[MAX], top=-1, item; main() { int ch; do { printf("\n\n\n\n1.\tPush\n2.\tPop\n3.\tDisplay\n4.\tExit\n"); printf("\nEnter your choice: "); scanf("%d", &ch); switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("\n\nInvalid entry. Please try again...\n"); } } while(ch!=4); getch(); }

www.eazynotes.com

Gursharan Singh Tatla

Page No. 2

void push() { if(top == MAX-1) printf("\n\nStack is full."); else { printf("\n\nEnter ITEM: "); scanf("%d", &item); top++; stack[top] = item; printf("\n\nITEM inserted = %d", item); } } void pop() { if(top == -1) printf("\n\nStack is empty."); else { item = stack[top]; top--; printf("\n\nITEM deleted = %d", item); } } void display() { int i; if(top == -1) printf("\n\nStack is empty."); else { for(i=top; i>=0; i--) printf("\n%d", stack[i]); } }

You might also like