Exercise 5
Exercise 5
#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
scanf("%d",&n);
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
push();
break;
case 2:
{
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
void push ()
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
stack[top] = val;
void pop ()
if(top == -1)
printf("Underflow");
else
void show()
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
printf("Stack is empty");
}
2. C Program to Implement Stack using Linked List
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
int val;
};
void main ()
int choice=0;
printf("\n----------------------------------------------\n");
while(choice != 4)
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
scanf("%d",&choice);
switch(choice)
case 1:
{
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting....");
break;
default:
};
void push ()
{
int val;
if(ptr == NULL)
else
scanf("%d",&val);
if(head==NULL)
ptr->val = val;
head=ptr;
else
ptr->val = val;
ptr->next = head;
head=ptr;
printf("Item pushed");
}
}
void pop()
int item;
if (head == NULL)
printf("Underflow");
else
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
void display()
int i;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
}
else
while(ptr!=NULL)
printf("%d\n",ptr->val);
ptr = ptr->next;
#include <stdio.h>
#define size 10
int stack[size],top=-1;
void push(int);
int pop();
int isempty();
void main()
int a[size],i;
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=0;i<size;i++)
push(a[i]);
int ele;
ele=pop();
printf("%d\t",ele);
if(top==size-1)
printf("stack is overflow");
else
top=top+1;
stack[top]=ele;
int pop()
int ele;
if(top==-1)
printf("stack is underflow");
else
ele=stack[top];
top=top-1;
}
return ele;
int isempty()
if(top==-1)
return 1;
else
return 0;
#include <stdio.h>
#include <stdlib.h>
// Stack implementation
int stack[MAX_SIZE];
printf("Stack Overflow\n");
return;
top++;
stack[top] = item;
int pop()
{
if (top < 0)
printf("Stack Underflow\n");
return -1;
top--;
return item;
return 1;
return 0;
int i = 0;
push(num);
else if (is_operator(symbol))
operand2 = pop();
operand1 = pop();
switch(symbol)
push(result);
i++;
symbol = expression[i];
result = pop();
return result;
int main()
return 0;
#include<stdio.h>
int main()
char expression[50];
printf("\nEnter an expression");
scanf("%s", expression);
while(expression[i]!= '\0')
if(expression[i]=='(')
x++;
else if(expression[i]==')')
x--;
if(x<0)
break;
i++;
}
if(x==0)
printf("Expression is balanced");
else
printf("Expression is unbalanced");
return 0;
Output
Enter an expression(A+B+(C*D)
Expression is unbalanced