refactoring

This commit is contained in:
Christian Bender 2019-02-14 15:51:36 +01:00
parent a080a2aa64
commit 49d82f1fde

View File

@ -1,82 +1,31 @@
//program for stack using array //program for stack using array
#include <stdio.h>
#include<stdio.h>
void push(); void push();
void pop(); void pop();
void peep(); void peek();
void update(); void update();
int a[100], top = -1;
int main() int main()
{ {
int n,a[100],top=0;
//function for pushing the element
void push()
{
printf("\nenter the value to insert");
scanf("%d",&n);
top=top+1;
a[top]=n;
}
//function for poping the element out
void pop()
{
if(top==0)
{
printf("\nstack is empty");
}
else
{
int item;
item=a[top];
top=top-1;
printf("\npoped item is %d ",item);
}
}
//function for peeping the element from top of the stack
void peep()
{
int i;
printf("\nenter the element position to view from top");
scanf("%d",&i);
if(top-i+1<0)
{
printf("\nunderflow condition");
}
else
{
int x;
x=a[top-i+1];
printf("\nthe %dth element from top is %d",i,x);
}
}
//function to update the element of stack
void update()
{
int i,n;
printf("\nenter the position to update");
scanf("%d",&i);
printf("\nenter the item to insert");
scanf("%d",&n);
if(top-i+1<0)
{
printf("\nunderflow condition");
}
else
{
a[top-i+1]=n;
}
}
int x; int x;
while(1) while (1)
{ {
printf("\n0.exit");
printf("\n1.push"); printf("\n1.push");
printf("\n2.pop"); printf("\n2.pop");
printf("\n3.peep"); printf("\n3.peek");
printf("\n4.update"); printf("\n4.update");
printf("\nenter your choice"); printf("\nenter your choice? ");
scanf("%d",&x); scanf("%d", &x);
switch(x) switch (x)
{ {
case 0:
return 0;
case 1: case 1:
push(); push();
break; break;
@ -84,7 +33,7 @@ int main()
pop(); pop();
break; break;
case 3: case 3:
peep(); peek();
break; break;
case 4: case 4:
update(); update();
@ -93,6 +42,58 @@ int main()
printf("\ninvalid choice"); printf("\ninvalid choice");
} }
} }
return(0); return (0);
} }
//function for pushing the element
void push()
{
int n = 0;
printf("\nenter the value to insert? ");
scanf("%d", &n);
top += 1;
a[top] = n;
}
//function for poping the element out
void pop()
{
if (top == -1)
{
printf("\nstack is empty");
}
else
{
int item;
item = a[top];
top -= 1;
printf("\npoped item is %d ", item);
}
}
//function for peeping the element from top of the stack
void peek()
{
if (top >= 0)
printf("\n the top element is %d", a[top]);
else
printf("\nstack is empty");
}
//function to update the element of stack
void update()
{
int i, n;
printf("\nenter the position to update? ");
scanf("%d", &i);
printf("\nenter the item to insert? ");
scanf("%d", &n);
if (top - i + 1 < 0)
{
printf("\nunderflow condition");
}
else
{
a[top - i + 1] = n;
}
}