Stack
Creation Of Stack
#include<stdio.h>
#include<conio.h>
#define capacity 3
int stack[capacity];
int top=-1;
void push(int );
int pop();
void traverse();
void peek();
int empty(int );
int full(int );
void main()
{
int a,b,c;
while(1)
{
printf("For push enter 1\n");
printf("for pop enter 2\n");
printf("for peek 3\n");
printf("for traverse enter 4\n");
printf("for quit enter 5\n");
scanf("%d",&a);
switch(a)
{
case 1:{
printf("enter Element To Push\n");
scanf("%d",&b);
push(b);
break;
}
case 2:{ if(empty(top))
printf("stack underflow\n");
else
{
c=pop();
printf("removed Element=%d\n",c);
}
break;
}
case 3: peek();
break;
case 4: traverse();
break;
case 5: exit(0);
}
}
}
void push(int b)
{
if(full(top))
printf("stack overflow\n");
else
{
top++;
stack[top]=b;
printf("pushed element =%d\n",stack[top]);
}
}
int pop()
{
if(empty(top))
printf("stack empty");
else
return (stack[top--]);
}
void peek()
{ if(empty(top))
printf(" stack empty\n");
else
printf("peek element =%d\n",stack[top]);
}
void traverse()
{ if(empty(top))
printf("stack empty\n");
else
{
int i;
for(i=0;i<=top;i++)
printf("%d\n",stack[i]);
}
}
int empty(int top)
{
if(top==-1)
return 1;
else
return 0;
}
int full(int top)
{
if(top==capacity-1)
return 1;
else
return 0;
}
#include<conio.h>
#define capacity 3
int stack[capacity];
int top=-1;
void push(int );
int pop();
void traverse();
void peek();
int empty(int );
int full(int );
void main()
{
int a,b,c;
while(1)
{
printf("For push enter 1\n");
printf("for pop enter 2\n");
printf("for peek 3\n");
printf("for traverse enter 4\n");
printf("for quit enter 5\n");
scanf("%d",&a);
switch(a)
{
case 1:{
printf("enter Element To Push\n");
scanf("%d",&b);
push(b);
break;
}
case 2:{ if(empty(top))
printf("stack underflow\n");
else
{
c=pop();
printf("removed Element=%d\n",c);
}
break;
}
case 3: peek();
break;
case 4: traverse();
break;
case 5: exit(0);
}
}
}
void push(int b)
{
if(full(top))
printf("stack overflow\n");
else
{
top++;
stack[top]=b;
printf("pushed element =%d\n",stack[top]);
}
}
int pop()
{
if(empty(top))
printf("stack empty");
else
return (stack[top--]);
}
void peek()
{ if(empty(top))
printf(" stack empty\n");
else
printf("peek element =%d\n",stack[top]);
}
void traverse()
{ if(empty(top))
printf("stack empty\n");
else
{
int i;
for(i=0;i<=top;i++)
printf("%d\n",stack[i]);
}
}
int empty(int top)
{
if(top==-1)
return 1;
else
return 0;
}
int full(int top)
{
if(top==capacity-1)
return 1;
else
return 0;
}
Comments
Post a Comment