0% found this document useful (0 votes)
54 views

Stack Using Arrays

The document contains three sections detailing the implementation of stack data structures in C using arrays, linked lists, and evaluating postfix expressions. Each section includes the aim, required software, program code, sample input/output, and references. All programs successfully execute and demonstrate basic stack operations such as push, pop, and display.

Uploaded by

nagas2820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Stack Using Arrays

The document contains three sections detailing the implementation of stack data structures in C using arrays, linked lists, and evaluating postfix expressions. Each section includes the aim, required software, program code, sample input/output, and references. All programs successfully execute and demonstrate basic stack operations such as push, pop, and display.

Uploaded by

nagas2820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Stack Using Arrays

Stack Using Arrays


Exp.No:
Date:
1.Aim:
Implement a program in C to perform Stack using arrays.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
#define MAX 10
int s[MAX];
int top=-1;
main()
{

int ch; while(1)


{
printf("\n1.push \n2.pop \n 3.display \n 4.exit");
printf("enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("wrong choice");
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

Stack Using Linked List


Exp.No:
Date:
1.Aim:
Implement a program in C to perform Stack using Linked List.
2.Software Required:
TURBO C
3.Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL,*newnode,*tmp;
int main()
{
int ch;
while(1)
{
printf("\n1.push \n2.pop\n3.display \n4.exit");
printf("enter ur choice");
scanf("%d",&ch);
}
switch(ch)
{
case 1: push();
break;
case 2: pop();
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

int val2 = pop(stack);


switch (exp[i])
{
case '+':
push(stack, val2 + val1);
break;
case '-':
push(stack, val2 - val1);
break;
case '*':
push(stack, val2 * val1);
break;
case '/':
push(stack, val2 / val1);
break;
}
}
}
return pop(stack);
}
int main()
{
char exp[] = "231*+9-";
printf("postfix evaluation: %d", evaluatePostfix(exp));
return 0;
}
Stack Using Linked List

4.Sample Input and Output:


postfix evaluation: -4
5.Result:
Program Successfully Executed.
6.References:
https://www.geeksforgeeks.org/evaluation-of-postfix-expression/

You might also like