Write a C program to implement a stack using arrays
#include<stdio.h>
#define SIZE 5
int s[SIZE],top=-1;
void push();
void pop();
void display();
int main()
{
int ch;
for(;;)
{
printf("n1.Pushn2.Popn3.Shown4.Exit");
printf("n Enter your choice n");
scanf("%d",&ch);
switch(ch){
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
default: printf(" Invalidn”); exit(0);}
}
return 0;
}
void push()
{
int item;
if(top==SIZE-1)
{
printf(“STACK IS FULL”);
return;
}
printf(“enter an element to be inserted”);
scanf(“%d”,&item);
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s[top--]);
}
void display()
{
int i;
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Elements of the stack are:n”);
for(i=top;i>=0;i--)
printf(“%dn”,s[i]);
}
Write a C program to implement a stack using structures
#define MAX 10
struct stack
{ int a[SIZE]; int top; };
struct stack s;
s.top=-1;
void main()
{// same as previous code}
void pop()
{
if(s.top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s.a[s.top--]);
}
void push()
{
int item;
if(s.top==SIZE-1)
{ // same as previous push fush() }
printf(“Enter an item:”);
scanf(“%d”,&item);
s.top++;
s.a[s.top]=item;
}
void display()
{
int i;
if(s.top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Elements of the stack are:n”);
for(i=s.top;i>=0;i--)
printf(“%dn”,s.a[i]);
}
Write a C program to implement a stack using dynamic arrays
#include<stdio.h>
#include<stdlib.h>
int *s;
int stackSize=1;
int top=-1;
s=(int*)malloc(stackSize * sizeof(int));
void push()
{
if(top == stackSize-1)
{printf(“stack full allocate extra memory”);
stackSize++;
s=(int*)realloc(s,stackSize * sizeof(int));
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s[top--]);
printf(“stack size decreased”);
stackSize--;
s=(int*)realloc(s,stackSize * sizeof(int));
}
MAIN FUNCTION
REMAINS SAME

stack.pptx

  • 1.
    Write a Cprogram to implement a stack using arrays #include<stdio.h> #define SIZE 5 int s[SIZE],top=-1; void push(); void pop(); void display(); int main() { int ch; for(;;) { printf("n1.Pushn2.Popn3.Shown4.Exit"); printf("n Enter your choice n"); scanf("%d",&ch); switch(ch){ case 1: push(); break; case 2: pop(); break; case 3: display(); break; default: printf(" Invalidn”); exit(0);} } return 0; }
  • 2.
    void push() { int item; if(top==SIZE-1) { printf(“STACKIS FULL”); return; } printf(“enter an element to be inserted”); scanf(“%d”,&item); s[++top]=item; } void pop() { if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s[top--]); } void display() { int i; if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Elements of the stack are:n”); for(i=top;i>=0;i--) printf(“%dn”,s[i]); }
  • 3.
    Write a Cprogram to implement a stack using structures #define MAX 10 struct stack { int a[SIZE]; int top; }; struct stack s; s.top=-1; void main() {// same as previous code} void pop() { if(s.top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s.a[s.top--]); } void push() { int item; if(s.top==SIZE-1) { // same as previous push fush() } printf(“Enter an item:”); scanf(“%d”,&item); s.top++; s.a[s.top]=item; } void display() { int i; if(s.top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Elements of the stack are:n”); for(i=s.top;i>=0;i--) printf(“%dn”,s.a[i]); }
  • 4.
    Write a Cprogram to implement a stack using dynamic arrays #include<stdio.h> #include<stdlib.h> int *s; int stackSize=1; int top=-1; s=(int*)malloc(stackSize * sizeof(int)); void push() { if(top == stackSize-1) {printf(“stack full allocate extra memory”); stackSize++; s=(int*)realloc(s,stackSize * sizeof(int)); s[++top]=item; } void pop() { if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s[top--]); printf(“stack size decreased”); stackSize--; s=(int*)realloc(s,stackSize * sizeof(int)); } MAIN FUNCTION REMAINS SAME