stack program in c

Stack has two ended in which one end the data is inserted called push and from other the data is removed called pop

  • STACK


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

int a[100],tos=-1,max=100;
void display()
{
 printf("\nvalues are:");
 for(int i=0;i<=tos;i++)
 {
  printf("\t %d ",a[i]);
 }
}

void push()
{
 int value;
 if(tos==max)
  printf("\nStack is full");
 else
 {
  tos++;
  printf("\nEnter value to add: ");
  scanf("%d",&value);
  a[tos] = value;
 }
}

void pop()
{
 if(tos==-1)
 {
  printf("\nSTack is already empty.");
 }
 else
 {
  a[tos]='\0';
  tos--;
 }
}

void peep()
{
 int m;
 printf("Enter the position to peep:");
 scanf("%d",&m);
 if(m>=1&&m<=tos)
  printf("\n The value at %d is %d",m,a[m-1]);
 else
  printf("\nEnter valid number");
}

void change()
{
 int m,value;
 printf("Enter position to change: ");
 scanf("%d",&m);
 if(m>=1&&m<=tos)
 {
  printf("Enter new value: ");
  scanf("%d",&value);
  a[m-1]=value;
 }
 else
  printf("\nEnter valid number");
}

main()
{
 int choice;
 while(1)
 {
  printf("\n1.PUSH\n2.POP\n3.PEEP\n4.CHANGE\n5.DISPLAY\n6.EXIT\n");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:
    push();
    break;
   case 2:
    pop();
    break;
   case 3:
    peep();
    break;
   case 4:
    change();
    break;
   case 5:
    display();
    break;
   case 6:
    exit(0);

   default:
    printf("Enter Valid Choice ");
    break;
  }
 }
}


happy coding😇

1 تعليقات

Hii

إرسال تعليق

Hii

أحدث أقدم