Stack Using Arrays
Stack Using Arrays
}
}
}
push()
{
int val;
printf("enter the value");
scanf("%d",&val);
if(top==MAX-1)
{
printf("stack is OVER flow");
}
else
{
top++; s[top]=val;
}
}
pop()
{
int val;
if(top==-1)
{
printf("under flow");
}
else
{
val=s[top]; top--;
printf("%d",val);
}
}
Stack Using Arrays
display()
{
int i;
for(i=top;i>=0;i--)
{
printf("%d",s[i]);
}
}
4.Sample Input and Output:
1.push
2.pop
3.display
4.exitenter ur choice1
enter the value45
1.push
2.pop
3.display
4.exitenter ur choice3
45
1.push
2.pop
3.display
4.exit
enter ur choice:4
5.Result:
Program Successfully Executed.
6.References:
https://www.geeksforgeeks.org/introduction-to-stack-data-structure-and-algorithm-
tutorials/
Stack Using Linked List
break;
case 3: display();
break;
case 4: exit(0);
default:printf("wrong choice");
}
}
push()
{
int val;
printf("enter ur value");
scanf("%d",&val);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=top;
top=newnode;
}
pop()
{
tmp=top;
if(tmp==NULL)
{
printf("stack is underflow");
}
else
{
printf("%d",top->data);
top=top->next;
free(tmp);
}
Stack Using Linked List
}
display()
{
if(top==NULL)
{
printf("stack is underflow");
}
else
{
tmp=top;
while(tmp!=NULL)
{
printf("%d",tmp->data);
tmp=tmp->next;
}
}
}
4.Sample Input and Output:
1.push
2.pop
3.display
4.exitenter ur choice1
Enter ur value:45
1.push
2.pop
3.display
4.exitenter ur choice3
45
Stack Using Linked List
1.push
2.pop
3.display
4.exitenter ur choice4
5.Result:
Program Successfully Executed.
6.References:
https://www.geeksforgeeks.org/implement-a-stack-using-singly-linked-list/
Stack Using Linked List
Postfix Expression
Exp.No:
Date:
1.Aim:
Implement a C program that uses stack operations to evaluate postfix Expression.
2.Software Required:
TURBO C
3.Program:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack
{
int top;
unsigned capacity; int* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack= (struct Stack*)malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array= (int*)malloc(stack->capacity * sizeof(int));
if (!stack->array)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
Stack Using Linked List
{
return stack->top == -1;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
int evaluatePostfix(char* exp)
{
struct Stack* stack = createStack(strlen(exp));
int i;
if (!stack)
return -1;
for (i = 0; exp[i]; ++i)
{
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
else
{
int val1 = pop(stack);
Stack Using Linked List