Data Structures Saturday Lab Work
Data Structures Saturday Lab Work
Data Structures Saturday Lab Work
1)ARRAYS OPERATION
#include<stdio.h>
int A[10],n,i,val,o;
int main()
{
}
}
void dfront(int x){
n--;
for(int i=0;i<n;i++){
A[i]=A[i+1];
}
for(i=0;i<n;i++){
printf("%d ",A[i]);
}
}
}
2.ARRAY IMPLEMENTATION OF STACK
# include<stdio.h>
int S[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top = -1;
printf("Array Implementation using Stack:\n");
printf("Enter the size of stack:\n");
scanf("%d",&n);
printf("Operations that area available in Stack:\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
while(choice !=4)
{
printf("Enter the choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("EXIT");
break;
}
default:
{
printf("Invalid Choice!!");
}
}
}
return 0;
}
void push()
{
if(top>=n-1)
{
printf("Error !! \n Stack Overflow!!!");
}
else{
printf("Enter the value that is to be Pushed:\n");
scanf("%d",&x);
top++;
S[top]=x;
}
}
void pop()
{
if(top<=1){
printf("Error!!\n Stack Underflow!!");
}
else{
printf("The Popped Element is %d",S[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("The Elements in Stack:\n");
for(i=top;i>=0;i--)
printf("%d\n",S[i]);
printf("Enter Next Choice!!\n");
}
else{
printf("The Stack is Empty\n");
}
}
3.INFIX TO POSTFIX
Code:
# include<stdio.h>
char S[20];
int top=-1;
void push(char x)
{
S[++top]=x;
}
char pop()
{
if (top == -1)
return -1;
else
return S[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+'||x == '-')
return 1;
if(x =='*'||x=='/')
return 2;
}
int main()
{
char exp[20];
char *e,x;
printf("Infix to Postfix Expression:\n");
printf("Enter the Expession:\n");
scanf("%s",exp);
e=exp;
printf("Postfix Expresion:\n");
do
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e ==')')
{
while((x=pop())!='(')
printf("%c",x);
}
else{
while(priority(S[top])>=priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}while(*e!='\0');
while(top!=-1)
{
printf("%c",pop());
}
}
4.POSTFIX EVALUATION
# include<stdio.h>
int S[20];
int top = -1;
void push(int x)
{
S[++top]=x;
}
int pop(){
return S[top--];
}
int main()
{
char exp[20];
char *choice;
int a,b,c,number;
printf("Evaluation of Postfix Expression:\n");
printf("Enter the Expression:\n");
scanf("%s",exp);
choice=exp;
do{
if(isdigit(*choice)){
number=*choice-48;
push(number);
}
else{
a=pop();
b=pop();
switch(*choice)
{
case '+':
{
c=a+b;
break;
}
case '-':
{
c=b-a;
break;
}
case '*':
{
c=a*b;
break;
}
case '/':
{
c=b/a;
break;
}
}
push(c);
}
choice++;
}while (*choice!='\0');
printf("Result:%s=%d\n",exp,pop()) ;
return 0;
}