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

Data Structures Saturday Lab Work

The document contains 4 sections that describe different data structures and algorithms: 1. Arrays and operations on arrays like insertion and deletion at the front and back. 2. Implementation of a stack using an array that allows push, pop and display operations. 3. An algorithm to convert infix expressions to postfix notation using a stack. 4. Evaluation of a postfix expression using a stack to pop operands and operators to calculate the result.

Uploaded by

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

Data Structures Saturday Lab Work

The document contains 4 sections that describe different data structures and algorithms: 1. Arrays and operations on arrays like insertion and deletion at the front and back. 2. Implementation of a stack using an array that allows push, pop and display operations. 3. An algorithm to convert infix expressions to postfix notation using a stack. 4. Evaluation of a postfix expression using a stack to pop operands and operators to calculate the result.

Uploaded by

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

DATA STRUCTURES LAB MANUAL

1)ARRAYS OPERATION
#include<stdio.h>
int A[10],n,i,val,o;
int main()
{

printf("enter the no of elements:");


scanf("%d",&n);
printf("enter the elements:\n");
for(i=0;i<n;i++){
scanf("%d",&A[i]);
}
printf("which operation to be done:\n");
printf("1.insert at front\n");
printf("2.insert at back\n");
printf("3.delete at front\n");
printf("4.delete at back\n");
scanf("%d",&o);
if(o==1){
finsert();
}
else if(o==2){
binsert();
}
else if(o==3){
dfront();
}
else if(o==4){
dback();
}
}
void finsert(int x){
printf("enter the value to be inserted:");
scanf("%d",&val);
if(n==10){
printf("array is full !");
}
else{
for(i=n-1;i>=0;i--){
A[i+1]=A[i];
}
A[0]=val;
}
for(i=0;i<n+1;i++){
printf("%d ",A[i]);
}
}
void binsert(int x){
printf("enter the value to be inserted:");
scanf("%d",&val);
if(n==10){
printf("array is full !");
}
else{
n++;
A[n-1]=val;
for(i=0;i<n;i++){
printf("%d ",A[i]);}

}
}
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]);
}

void dback(int x){


if(n==-1){
printf("array is empty !\n");
}
else{
A[n-1]='\0';
n--;
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;
}

You might also like