Lab 3 19 BCE1096

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

CSE 2003 Data Structures and Algorithms

Practice Lab on Stack Applications and Queues


30th July 2020
BS YASHWWANTH
19BCE1096
Practice Question 1:

Write a program to convert the expression from infix to postfix form and evaluate the
same using stack data structure.

Example Input and output:

(5 + 6) * 9 +10 will be  5 6 + 9 * 10 +  109

#include<stdio.h>

#include<stdlib.h> #include<math.h> #include <string.h>

char infix_string[20], postfix_string[20]; int top;

int stack[20];

int pop();

int precedence(char symbol); int isEmpty();

void infix_to_postfix();

int check_space(char symbol); void push(long int symbol);

int main()

int count, length;

char temp;

top = -1;

printf("\nINPUT THE INFIX EXPRESSION : ");

scanf("%s", infix_string);

infix_to_postfix();

printf("\nEQUIVALENT POSTFIX EXPRESSION : %s\n", postfix_string); return 0;


}

/* Function to convert from infix to postfix */ void infix_to_postfix()

unsigned int count, temp = 0;

char next;

char symbol;

for(count = 0; count < strlen(infix_string); count++) {

symbol = infix_string[count]; if(!check_space(symbol))

switch(symbol)

// Scanning the input expression

case '(': push(symbol);

break;

case ')':

while((next = pop()) != '(')

postfix_string[temp++] = next;

break;

case '+':

case '-':

case '*':

case '/':

case '%':

case '^':

while(!isEmpty() && precedence(stack[top]) >= precedence(symbol)) push the higher one

postfix_string[temp++] = pop();
push(symbol);

break;

default:

postfix_string[temp++] = symbol;

while(!isEmpty())

postfix_string[temp++] = pop();

postfix_string[temp] = '\0';

/* Function to check precedence of operators */ int precedence(char symbol)

switch(symbol)

case '(': return 0; case '+':

// pop until '(' is encountered

// Check precedence and

case '-': return 1; case '*': case '/': case '%': return 2; case '^': return 3; default: return 0; }

int check_space(char symbol)

if(symbol == '\t' || symbol == ' ' ) {

return 1; }

else

{
return 0; }

void push(long int symbol) {

if(top > 20)

printf("Stack Overflow\n"); exit(1);

top = top + 1;

stack[top] = symbol; }

int isEmpty() {

if(top == -1) {

return 1; }

else

// Push the symbol and make it as TOP

return 0; }

int pop()

if(isEmpty())

printf("Stack is Empty\n"); exit(1);

return(stack[top--]); // Pop the symbol and decrement TOP }

2) Implement the Queue with following operations,

 void enqueue(int N) — Add N to the back of the queue.


 int dequeue() — Remove the item at the front and return it.
 boolean isEmpty() — Return true if the queue is empty.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
  
struct Queue {
    int front, rear, size;
    unsigned capacity;
    int* array;
};
  
struct Queue* createQueue(unsigned capacity)
{
    struct Queue* queue = (struct Queue*)malloc(
        sizeof(struct Queue));
    queue->capacity = capacity;
    queue->front = queue->size = 0;
  
    queue->rear = capacity - 1;
    queue->array = (int*)malloc(
        queue->capacity * sizeof(int));
    return queue;
}
  
int isFull(struct Queue* queue)
{
    return (queue->size == queue->capacity);
}
  
int isEmpty(struct Queue* queue)
{
    return (queue->size == 0);
}
  
void enqueue(struct Queue* queue, int item)
{
    if (isFull(queue))
        return;
    queue->rear = (queue->rear + 1)
                  % queue->capacity;
    queue->array[queue->rear] = item;
    queue->size = queue->size + 1;
    printf("%d enqueued to queue\n", item);
}
  
// Function to remove an item from queue.
int dequeue(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    int item = queue->array[queue->front];
    queue->front = (queue->front + 1)
                   % queue->capacity;
    queue->size = queue->size - 1;
    return item;
}
  
int front(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    return queue->array[queue->front];
}
  
int rear(struct Queue* queue)
{
    if (isEmpty(queue))
        return INT_MIN;
    return queue->array[queue->rear];
}
  
int main()
{
    struct Queue* queue = createQueue(1000);
  
    enqueue(queue, 10);
    enqueue(queue, 20);
    enqueue(queue, 30);
    enqueue(queue, 40);
  
    printf("%d dequeued from queue\n\n",
           dequeue(queue));
  
    printf("Front item is %d\n", front(queue));
    printf("Rear item is %d\n", rear(queue));
  
    return 0;
}

Practice Question 3:

Assume you have two queues QQ1 and QQ2. Write a function called match that will
count how many elements in the same position of the two queues are equal. The
queues may not be the same length. Do not modify the queues. Use the queue member
functions to access the queues.

Input Format:
Get the length of QQ1, followed by elements in QQ1.
Get the length of QQ2, followed by elements in QQ2.

Output Format:

Number of elements in the same position, followed by the elements that match

###Sample Input:

5, 3 4 5 6 9 //QQ1, number of elements followed by the elements in QQ1


7, 4 3 5 8 9 11 23 //QQ2, number of elements followed by the elements in QQ2

###Sample Output:
2, 5 and 9 // The number of elements followed by the elements that match.

#include<stdio.h>

void main(){

int QQ1[10],QQ2[10],i,l1,l2,min,count=0,ans[10],j=0,k; printf("enter the length of the first QQ1: ");


scanf("%d",&l1);

for(i=0;i<l1;i++){

scanf("%d",&QQ1[i]); }

printf("enter the length of the first QQ2: "); scanf("%d",&l2);

for(i=0;i<l2;i++){

scanf("%d",&QQ2[i]); }

min=l1<l2?l1:l2; for(i=0; i<=min;i++){

if(QQ1[i]==QQ2[i]){ count++;

ans[j]=QQ1[i];

j++; }

printf("%d,",count); for(k=0;k<j;k++){

printf("%d\t",ans[k]); }
}

You might also like