- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Implement Stack Operations Using Array using C
Program for implementing a stack using arrays.It involves various operations such as push,pop,stack empty,stack full and display.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;
}
void push(int item) {
st.top++;
st.s[st.top] = item;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("\nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]);
}
}
int main() {
int item, choice;
char ans;
st.top = -1;
printf("\n\tImplementation Of Stack");
do {
printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter The item to be pushed");
scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("\nEmpty stack!Underflow !!");
else {
item = pop();
printf("\nThe popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf("\nDo You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');
return 0;
}
Explanation of the C Porgram : Stack Using Array
Step 1 : Declare One Stack Structure
#define size 5
struct stack
{
int s[size];
int top;
}st;
We have created ‘stack’ structure.
We have array of elements having size ‘size’
To keep track of Topmost element we have declared top as structure member.
Step 2 : Push/Pop Operation
While pushing remember one thing in mind that we are incrementing the top and then adding element. and while removing or poping the element, we are firstly removing the element and then decrementing the top.
void push(int item) {
st.top++;
st.s[st.top] =item;
}
- Related Questions & Answers
- How to Solve Second Order Quadratic Equation Using C Program
- How to get a list of parameter names inside Python function?
- Write C Program to Calculate Addition of All Elements in Array
- How to Check Whether Number is Perfect Or Not using C
- Write C Program to Reversing an Array Elements in C Programming
- How to check the type of triangle using C
- How to reverse a given number using C
- How to make a chain of function decorators in Python?
- How to Calculate Area of Rectangle using C Program
- How to convert temperature from degree centigrade to Fahrenheit using C
- Write C program to find Exponent Power Series
- How to calculate sum of 5 subjects and find percentage Using C Program
- Write a C program using pointers to read in an array of integers and print its elements in reverse order.
- How to Implement Stack Operations Using Array using C
- How to Calculate Area of Right angle Triangle using C Language
- What is SQL ?
Advertisements
ads