Dsa Assignment 1
Dsa Assignment 1
Name: Devendhiran.D
Reg no: 21MID0218
Assignment-1
Class 1- assignment:
Class Two assignment:
i)Implementation of queue
ii) insertion and deletion of that queue
code
#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue ();
void show ();
int inp_arr [SIZE];
int Rear = - 1;
int Front = - 1;
main ()
{
int Ch;
while (1)
{
printf ("1. Enqueue Operation\n");
printf ("2. Dequeue Operation\n");
printf ("3. Display the Queue\n");
printf ("4. Exit\n");
printf ("Enter your choice of operations: ");
scanf ("%d", &ch);
switch (ch)
{
case 1:
enqueue ();
break;
case 2:
dequeue ();
break;
case 3:
show ();
break;
case 4:
default:
printf ("Incorrect choice \n");
}
}
}
void enqueue ()
{
int insert_item;
if (Rear == SIZE - 1)
printf ("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
printf ("Element to be inserted in the Queue\n: ");
scanf ("%d", &insert_item);
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}
void dequeue ()
{
if (Front == - 1 || Front > Rear)
{
printf ("Underflow \n");
return;
}
else
{
printf ("Element deleted from the Queue: %d\n", inp_arr [Front]);
Front = Front + 1;
}
}
void show ()
{
if (Front == - 1)
printf ("Empty Queue \n");
else
{
printf ("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf ("%d ", inp_arr[i]);
printf("\n");
}
}
Screen shot:
Output:
Class 3 assignment:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
struct stack
int stk[MAXSIZE];
int top;
};
ST s;
void push ()
int num;
return;
else
s.top = s.top + 1;
s.stk[s.top] = num;
return;
int pop ()
int num;
if (s.top == - 1)
{
return (s.top);
else
num = s.stk[s.top];
s.top = s.top - 1;
return(num);
void display ()
int i;
if (s.top == -1)
return;
else
{
printf ("\nStatus of elements in stack : \n");
int main ()
int ch;
s.top = -1;
printf("----------------------------\n");
printf(" 1. PUSH\n");
printf(" 2. POP\n");
printf(" 3. DISPLAY\n");
printf(" 4. EXIT\n");
while(1)
scanf("%d", &ch);
switch (ch)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
return 0;
}
Program screen shot:
Output:
2) /*C Program to Evaluate POSTFIX Expression Using Stack*/
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter a the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
Program screen shot:
Output:
3) #include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char stack[SIZE];
int top = -1;
char pop()
{
char item ;
if(top <0)
{
printf("stack under flow: invalid infix expression");
getchar();
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
postfix_exp[j] = '\0';
}
int main()
{
char infix[SIZE], postfix[SIZE];
printf("ASSUMPTION: The infix expression contains single letter variables and
single digit constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
Program screen shot:
Output: