Skip to main content

STACK in C

IMPLEMENTATION OF STACK USING ARRAY

Here is Example of Source Code of Data Structure-1,

Question 1: Data_structure:

 Write a C program to implement STACK using an array. This program must include the following functions in it.


  • Write a function for ā€œPushā€ operation.  
  • Write a function for ā€œPopā€ operation. 
  • Write a function for ā€œDisplayā€ operation which prints the content of the STACK. 
 Basic Understanding: Push and Pop Operation:


Here is How to Implement:


#include<stdio.h>
int stack[100],choice,n,top,i,x;
void push();
void pop();
void display();
int main(){
top=-1;
printf("\nEnter the size of the stack,maximum 100");
scanf("%d",&n);
printf("\nChoose number for operation\n\t 1.PUSH \n\t 2.POP \n\t 3.DISPLAY\n\tEnter 4 to exit the function\n");
do
{
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exited");
break;
}
default:
{
printf("\nEnter valid choice");
}
}
}
while(choice !=4);
return 0;
}
void push(){
if(top>=n-1)
{
printf("\nSTACK is full");
}
else
{
printf("\nEnter element");
scanf("%d",&x);
top++;
stack[top]=x;
}
printf("Enter next choice:1/2/3/4");
}
void pop(){
if(top<=-1)
{
printf("\nstack is empty");
}
else
{
printf("\n the popped element is %d",stack[top]);
top--;
}
printf("Enter next choice 1/2/3/4");
}
void display(){
if(top>=0)
{
printf("\nelements");
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
printf("\npress next choice 1/2/3/4");
}
else
{
printf("\n STACK is empty");
}
}
view raw STACK_USING_C.c hosted with ā¤ by GitHub


                                                  








Comments

Post a Comment

Popular posts from this blog

Postfix Evaluation(Single or Multi Digit) using STACK in C

POSTFIX EVALUATION WITH SINGLE OR MULTI DIGIT: Students find difficulties in finding the correct code for Postfix evaluation using Stack which works for Single as well for multidigit also; So here is the Question Format Given::- Question_2; Data_Structure:  Write a C program to evaluate the following POSTFIX expression. Use STACK to solve this problem.  INPUT: 5 6 2 + * 12 4 / -  OUTPUT: 37 Basic Understanding: Here is the Correct code:

Combinatorics For Programmers and Coders

Hey! Computer Geeks! Here we will discuss two or more special numbers that made the way of coding easy, Introduction to Catalan Numbers: The Catalan numbers are a sequence of positive integers that appear in many counting problems in combinatorics. They count certain types of lattice paths, permutations, binary trees, and many other combinatorial objects. They satisfy a fundamental recurrence relation and have a closed-form formula in terms of binomial coefficients. The Catalan number Cn describes, among other things: The number of binary trees with n nodes, The number of ways in which parentheses can be placed in a sequence of n+ 1 numbers to be multiplied two at a time, The number of well-formed reverse Polish expressions with n operands and n + 1 operators, The number of paths in a grid from (0, 0) to (n, n), increasing just one coordinate by one at each step, without crossing the main diagonal,  The number of n-bit sequences that the number of 1s never exceeds the n...