DSA2
DSA2
Exp. No :
2.1(a) Date :
Balancing Symbols
AIM:
PSEUDOCODE:
BEGIN
END
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
char s[SIZE];
int TOP=-1;
int push(char x)
{
s[++TOP]=x;
}
int pop()
{ s[--TOP]; }
char peak()
{
return s[TOP]; }
void check(char a[])
{
int i;
717823P3
23CSR205 Data Structures and Algorithms Laboratory
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='{'||a[i]=='('||a[i]=='[')
push(a[i]);
else
{
if((TOP!=1)&&(((a[i]=='}')&&(peak()=='{'))||((a[i]==')')&&(peak()=='('))||((a[i]==']')&&(peak(
)=='['))))
{
pop();
}
else
{
printf("false\n");
return;
}}}
if(TOP==-1)
printf("true\n");
else
printf("false\n");
}
int main()
{
char a[20];
int i,n;
printf("enter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",a);
check (a) }}
OUTPUT:
RESULT:
Thus the program was successfully executed and the output is verified.
717823P3
23CSR205 Data Structures and Algorithms Laboratory
Exp. No : 2.1 (b)
Date :
Infix to Postfix Expression Conversion
AIM:
PSEUDOCODE:
BEGIN
e = exp;
while(*e != '\0')
{
if(isalnum (*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
{
printf("%c",pop());
}
push(*e);
} e+
+;
}
while(top != -1)
{
printf("%c ",pop());
}
}
END
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
717823P3
23CSR205 Data Structures and Algorithms Laboratory
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}
int main()
{
char exp[100];
char *e, x;
int y;
scanf("%d",&y);
while(y--)
{
printf("\nEnter the expression :");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum (*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
{
printf("%c",pop());
}
push(*e);
} e+
+;
}
while(top != -1)
{
printf("%c ",pop());
}
}
return 0;
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
OUTPUT:
RESULT:
Thus the program was successfully executed and the output is verified.
717823P3
23CSR205 Data Structures and Algorithms Laboratory
Exp. No :
2.1(c) Date :
Postfix Expression Evaluation
AIM:
To find the highest streak maintained between two students using arrays.
PSEUDOCODE:
BEGIN
SOURCE CODE:
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int peak()
{
return stack[top];
}
int main()
{
717823P3
23CSR205 Data Structures and Algorithms Laboratory
char a[20];
int n1,n2,n3,i,t;
printf("enter the no.of times:");
scanf("%d",&t);
while(t--)
{
printf("Enter the expression : ");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
if(isdigit(a[i]))
{
push(a[i]-48);
}
else
{
n1 = peak();
pop();
n2 =peak();
pop();
switch(a[i])
{
case '+':
{
n3 = n1+n2;
push(n3);
break;
}
case '-':
{
n3 = n2-n1;
push(n3);
break;
}
case '*':
{
n3 = n1*n2;
push(n3);
break;
}
case '/':
{
n3 = n2/n1;
push(n3);
break;
}
default:
{
printf("invalid");
break;
}
}
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
}
printf("\nThe result ofexpression =%d\n",peak());
}
return 0;
}
OUTPUT:
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF FACULTY
RESULT:
Thus the program was successfully executed and the output is verified.
717823P3
23CSR205 Data Structures and Algorithms Laboratory
Exp. No :
2.2(a) Date :
Implementation of Linear Queue
AIM:
PSEUDOCODE:
BEGIN
void Enqueue(int element)
{
if(rear==size-1)
printf("Queue is full,overflow");
else
{
if(front==-1&rear==-1)
front=rear=0;
else
rear=rear+1;
queue[rear]=element;
}
}
int Dequeue()
{
int value;
if(front==-1)
printf("Queue is empty,underflow");
else
{
value=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
return value;
}
}
void Display()
{
int i;
if(front==-1)
printf("Queue is empty");
else
{
printf("Element in queue: ");
for(i=front;i<=rear;i++) printf("%d\
t",queue[i]);
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
printf("\n");
}
END
SOURCE CODE:
#include<stdio.h>
#define size 10
int queue[size],rear=-1,front=-1;
void Enqueue(int element)
{
if(rear==size-1)
printf("Queue is full,overflow");
else
{
if(front==-1&rear==-1)
front=rear=0;
else
rear=rear+1;
queue[rear]=element;
}
}
int Dequeue()
{
int value;
if(front==-1)
printf("Queue is empty,underflow");
else
{
value=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
return value;
}
}
void Display()
{
int i;
if(front==-1)
printf("Queue is empty");
else
{
printf("Element in queue: ");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
printf("\n");
}
void main()
{
int choice,ele,flg=1,value;
717823P3
23CSR205 Data Structures and Algorithms Laboratory
while (flg)
{
printf("1.Enqueue\t2.Dequeue\t3.Display\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the element to be inserted: ");
scanf("%d",&ele);
Enqueue(ele);break;
case 2:value=Dequeue();
printf("Deleted element: %d\n",value);break;
case 3:Display();break;
default:printf("Invalid input\n");
flg=0;break;
}
}
}
OUTPUT:
RESULT:
Thus the program was successfully executed and the output is verified.
717823P3
23CSR205 Data Structures and Algorithms Laboratory
Exp. No : 2.2(b)
Date :
Implementation of Circular Queue
AIM:
PSEUDOCODE:
BEGIN
void isfull(){
if((rear==size-1&&front==0)||(front==rear+1))
return 1;
else
return 0;
}
void isempty(){
if(front==-1)
return 1;
else
return 0;
}
void Enqueue(int element)
{
if(isfull())
printf("Queue is full,overflow");
else
{
if(front==-1&&rear==-1)
front=0;rear=0;
else if(rear==size-1)
rear=0;
else
rear=rear+1;
queue[rear]=element;
}
}
int Dequeue()
{
int value;
if(isempty())
printf("queue is empty,underflow");
else
{
value=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
else if(front==size-1)
front=0;
else
front=front+1;
}
return value;
}
void Display()
{
int i;
if(front==-1)
{
printf("Queue is empty\n");
}
else
{
printf("Element in Queue: ");
if(front<=rear)
{
for(i=front;i<=rear;i++) printf("%d\
t",queue[i]);
}
else
{
for(i=front;i<=size-1;i++)
{
printf("%d\t",queue[i]);
}
for(i=0;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}
printf("\n");
}
END
SOURCE CODE:
#include<stdio.h>
#define size 5
int queue[size],rear=-1,front=-1;
void isfull(){
if((rear==size-1&&front==0)||(front==rear+1))
return 1;
else
return 0;
}
void isempty(){
if(front==-1)
return 1;
else
return 0;
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
717823P3
23CSR205 Data Structures and Algorithms Laboratory
}
}
void main()
{
int choice,ele,flg=1,value;
while (flg)
{
printf("1.Enqueue\t2.Dequeue\t3.Display\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the element to be inserted: ");
scanf("%d",&ele);
Enqueue(ele);break;
case 2:value=Dequeue();
printf("Deleted element: %d\n",value);break;
case 3:Display();break;
default:printf("Invalid input\n");
flg=0;break;
}
}
}
OUTPUT:
RESULT:
717823P3
23CSR205 Data Structures and Algorithms Laboratory
Exp. No : 2.2(c)
Date : Print Strings in Random using Double Ended Queue
Thus the program was successfully executed and the output is verified.
AIM:
To print strings in random using double ended Queue using data structures in C.
PSEUDOCODE:
for(i=0;i<l;i++)
{
if(rear==size-1)
printf("Queue is full,overflow");
else
{
if(front==-1&rear==-1){
front=0;
rear=0;
}
else
rear=rear+1;
queue[rear]=ele[i];
}
}
}
void Display(char ele[])
{
int i,r=strlen(ele);
if(front==-1)
printf("Queue is
empty"); else
{
printf("Output string: ");
for(i=front;i<r;i++)
{
if(i==r-1)
printf("%c",ele[i]);
else
{
printf("%c",ele[i]);
printf("%c",ele[r-1]);
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
r=r-1;
}
printf("\n");
}
}
SOURCE CODE:
#include<stdio.h>
#include<string.h>
#define size 100
int queue[size],rear=-1,front=-1;
void Enqueue(char ele[])
{
int i,l=strlen(ele);
for(i=0;i<l;i++)
{
if(rear==size-1)
printf("Queue is full,overflow");
else
{
if(front==-1&rear==-1){
front=0;
rear=0;
}
else
rear=rear+1;
queue[rear]=ele[i];
}
}
}
void Display(char ele[])
{
int i,r=strlen(ele);
if(front==-1)
printf("Queue is
empty"); else
{
printf("Output string: ");
for(i=front;i<r;i++)
{
if(i==r-1)
printf("%c",ele[i]);
else
{
printf("%c",ele[i]);
printf("%c",ele[r-1]);
}
r=r-1;
}
printf("\n");
}
717823P3
23CSR205 Data Structures and Algorithms Laboratory
}
int main()
{
int flg;
char input[size];
printf("Enter number of times: ");
scanf("%d",&flg);
while(flg--)
{
printf("Enter the string: ");
scanf("%s",input);
Enqueue(input);
Display(input);
}
return 0;
}
OUTPUT:
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF FACULTY
RESULT:
Thus the program was successfully executed and the output is verified.
717823P3