0% found this document useful (0 votes)
86 views35 pages

Dsa Assignment 1

The document discusses implementations of data structures and algorithms in C code. It includes code to implement a queue with enqueue and dequeue functions. It also includes code for a stack with push, pop and display functions. Finally, it provides code to evaluate postfix expressions and convert infix to postfix expressions using stacks. The code includes functions for operations on queues and stacks along with main functions to test and demonstrate the implementations.
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)
86 views35 pages

Dsa Assignment 1

The document discusses implementations of data structures and algorithms in C code. It includes code to implement a queue with enqueue and dequeue functions. It also includes code for a stack with push, pop and display functions. Finally, it provides code to evaluate postfix expressions and convert infix to postfix expressions using stacks. The code includes functions for operations on queues and stacks along with main functions to test and demonstrate the implementations.
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/ 35

DATA STRUCTURE AND ANALYS:

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:

1. Write a program in C to implement stack and operations on the it


(push, pop, top element, display elements).

2. Write a program in C to evaluate the postfix form of an algebraic


expression using stack. Get the input from the keyboard.

3. Write a program in C to convert infix form of an algebraic


expression into postfix form using stack. Get the input from the
keyboard.

1) /*C program to perform push, pop, display operations on stack*/

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 5

struct stack

int stk[MAXSIZE];

int top;

};

typedef struct stack ST;

ST s;
void push ()

int num;

if (s.top == (MAXSIZE - 1))

printf ("Stack is Full\n");

return;

else

printf ("\nEnter element to be pushed : ");

scanf ("%d", &num);

s.top = s.top + 1;

s.stk[s.top] = num;

return;

int pop ()

int num;

if (s.top == - 1)
{

printf ("Stack is Empty\n");

return (s.top);

else

num = s.stk[s.top];

printf ("poped element is = %d\n", s.stk[s.top]);

s.top = s.top - 1;

return(num);

void display ()

int i;

if (s.top == -1)

printf ("Stack is empty\n");

return;

else

{
printf ("\nStatus of elements in stack : \n");

for (i = s.top; i >= 0; i--)

printf ("%d\n", s.stk[i]);

int main ()

int ch;

s.top = -1;

printf ("\tSTACK OPERATIONS\n");

printf("----------------------------\n");

printf(" 1. PUSH\n");

printf(" 2. POP\n");

printf(" 3. DISPLAY\n");

printf(" 4. EXIT\n");

while(1)

printf("\nChoose operation : ");

scanf("%d", &ch);
switch (ch)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

default:

printf("Invalid operation \n");

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>

#define SIZE 100

char stack[SIZE];
int top = -1;

void push(char item)


{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}

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

int is_operator(char symbol)


{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}

void InfixToPostfix(char infix_exp[], char postfix_exp[])


{
int i, j;
char item;
char x;

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:

You might also like