Sunday, April 18, 2021

Data Structure Lab - Program- 3

 Program 3- 

Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array.


Warm-up Exercise 


Stacks Push and POP Operation


Solution ) 


#include<stdio.h>

#include<stdlib.h>      /* for exit() */

#include<ctype.h>     /* for isdigit(char ) */

#include<string.h>

          #define SIZE 100

          /* declared here as global variable because stack[]

* is used by more than one fucntions */

char stack[SIZE];

int top = -1;

/* define push operation */

void push(char item)

{

if(top >= SIZE-1)

{

printf("\nStack Overflow.");

}

else

{

top = top+1;

stack[top] = item;

}

}


/* define pop operation */

char pop()

{

char item ;

if(top <0)

{

printf("stack under flow: invalid infix expression");

getchar();

/* underflow may occur for invalid expression */

/* where ( and ) are not matched */

exit(1);

}

else

{

item = stack[top];

top = top-1;

return(item);

}

}


/* define function that is used to determine whether any symbol is operator or not

(that is symbol is operand)

* this fucntion returns 1 if symbol is opreator else return 0 */

int is_operator(char symbol)

{

if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')

{

return 1;

}

else

{

return 0;

}

}


/* define fucntion that is used to assign precendence to operator.

* Here ^ denotes exponent operator.

* In this fucntion we assume that higher integer value

* means higher precendence */

int precedence(char symbol)

{

if(symbol == '^')/* exponent operator, highest precedence*/

{

return(3);

}

else if(symbol == '*' || symbol == '/')

{

return(2);

}

else if(symbol == '+' || symbol == '-')          /* lowest precedence */

{

return(1);

}

else

{

return(0);

}

}


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

{

int i, j;

char item;

char x;


push('(');                               /* push '(' onto stack */

strcat(infix_exp,")");                  /* add ')' to infix expression */


i=0;

j=0;

item=infix_exp[i];         /* initialize before loop*/


while(item != '\0')        /* run loop till end of infix expression */

{

if(item == '(')

{

push(item);

}

else if( isdigit(item) || isalpha(item))

{

postfix_exp[j] = item;              /* add operand symbol to postfix expr */

j++;

}

else if(is_operator(item) == 1)        /* means symbol is operator */

{

x=pop();

while(is_operator(x) == 1 && precedence(x)>= precedence(item))

{

postfix_exp[j] = x;                  /* so pop all higher precendence operator and */

j++;

x = pop();                       /* add them to postfix expresion */

}

push(x);

/* because just above while loop will terminate we have

oppped one extra item

for which condition fails and loop terminates, so that one*/


push(item);                 /* push current oprerator symbol onto stack */

}

else if(item == ')')         /* if current symbol is ')' then */

{

x = pop();                   /* pop and keep popping until */

while(x != '(')                /* '(' encounterd */

{

postfix_exp[j] = x;

j++;

x = pop();

}

}

else

{ /* if current symbol is neither operand not '(' nor ')' and nor

operator */

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}

i++;



item = infix_exp[i]; /* go to next symbol of infix expression */

} /* while loop ends here */

if(top>0)

{

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}

if(top>0)

{

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}



postfix_exp[j] = '\0'; /* add sentinel else puts() fucntion */

/* will print entire postfix[] array upto SIZE */

}


/* main function begins */

int main()

{

char infix[SIZE], postfix[SIZE];         /* declare infix string and postfix string */


/* why we asked the user to enter infix expression

* in parentheses ( )

* What changes are required in program to

* get rid of this restriction since it is not

* in algorithm

* */

printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n");

printf("\nEnter Infix expression : ");

gets(infix);

InfixToPostfix(infix,postfix);                   /* call to convert */

printf("Postfix Expression: ");

puts(postfix);                     /* print postfix expression */


return 0;

}



Data Structure Lab-Program-2

 Program -2 

 Write a C program that uses functions to perform the following: 

1. Create a doubly-linked list of integers

2. Delete a given integer from the above doubly linked list. 

3. Display the contents of the above list after deletion


warmup Exercise- 

Click here to code Doubly Linked List

Create and traverse in doubly Linked List

Insert New node at different positions


Solution ) 


/*Doubly Linked List - Coded By Dr. M. Mann @ IIIT Sonepat */

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *prev;

    struct node *next;

}*head;

void creation(int n)

{

    head=(struct node *)malloc(sizeof(struct node));

    struct node *temp;

    struct node *newnode;

    printf("Enter data =");

    scanf("%d",&head->data);

    head->prev=NULL;

    head->next=NULL;

    temp=head;

    for(int z=2;z<=n;z++)

    {

       newnode=(struct node *)malloc(sizeof(struct node));

       printf("Enter data=");

       scanf("%d",&newnode->data);

       newnode->prev=temp;

       newnode->next=NULL;

       temp->next=newnode;

       temp=temp->next;

    }

}

void traverse()

{

    struct node *temp=head;

    while(temp)

    {

        printf("Entered data=%d\n",temp->data);

        temp=temp->next;

    }

}

void delete()

{

    int del;

    printf("Enter Which Node you want to delete=");

    scanf("%d",&del);

    struct node *temp=(struct node *)malloc(sizeof(struct node));

    temp=head;

    while(temp)

    {

        if(head->data==del)

        {

            head=temp->next;

            break;

        }

        else if(temp->next->data==del)

        {

            temp->next=temp->next->next;

            temp->next->next->prev=temp;

            break;

        }

        temp=temp->next;

    }

}

void main()

{

    int n;

    printf("Enter number of doubly list=");

    scanf("%d",&n);

    creation(n);

    traverse();

    delete();

    traverse();

}

Data Structure Lab-Program-1

 Dear Students 

It is advised to go through the Theory part in order to understand Lab Experiments, Here I am just giving your some revision exercise so that you will be ready for different code in DS Lab.


Go through the Structure Video discussed in C programming 

Video-1 



 Link for Other brush up Videos 



After this Study following post first one by one in sequence



Program-1 
 Write a C program that uses functions to perform the following: 
1. Create a singly linked list of integers. 
2. Delete a given integer from the above-linked list. 
3. Display the contents of the above list after deletion.

Solution 

/*
Code -Dr. Mukesh Mann @IIIT Sonepat
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *link;
}*head;
void createnode(int n)
{
 head=(struct node *)malloc(sizeof(struct node));
 int data;
 printf("Enter head data=");
 scanf("%d",&data);
 head->data=data;
 head->link=NULL;
 struct node *temp=(struct node *)malloc(sizeof(struct node));
 temp=head;
 struct node *newnode;
 for(int a=0;a<n-1;a++)
 {
  newnode=(struct node *)malloc(sizeof(struct node));
  printf("Enter node data=");
  scanf("%d",&data);
  newnode->data=data;
  newnode->link=NULL;
  temp->link=newnode;
  temp=temp->link;
 }
}
void traverse()
{
 struct node *temp=(struct node *)malloc(sizeof(struct node));
 temp=head;
 while(temp)
 {
  printf("Data you entered=%d\n",temp->data);
  temp=temp->link;
 }
}

void delete()
{
 struct node *temp=(struct node *)malloc(sizeof(struct node));
 temp=head;
 int del;
 printf("Enter data of the which you want to delete=");
 scanf("%d",&del);
 while(temp->data)
 {
     if(head->data==del)
     {
         head=temp->link;
         free(temp);
         break;
     }
     else if(temp->link->data==del)
     {
         temp->link=temp->link->link;
         break;
     }
      temp=temp->link;
 }
}
void main()
{
 int n;
 printf("Enter number of nodes in list=");
 scanf("%d",&n);
 createnode(n);
 traverse();
 delete();
 traverse();
}

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...