Tuesday, September 29, 2020

Evaluator Postfix expression: 6 5 2 + 3 8 7 / * 2 $ 3 *

 https://www.free-online-calculator-use.com/postfix-evaluator.html#


6 5 2 + 3 8 7 /  * 2 $ 3 *

According to my calculations, the postfix expression 6 5 2 + 3 8 7 / * 2 ^ 3 * evaluates to 35.274.


$ is also known as Power (^)




Below is my attempt to show the color-coded, step-by-step process I used to evaluate the postfix expression using the stack method.

6 5 2 + 3 8 7 / * 2 $ 3 *

The first character scanned is "6", which is an operand, so push it to the stack.

6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "5", which is an operand, so push it to the stack.

5
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "2", which is an operand, so push it to the stack.

2
5
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "+", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the right operand and then pop 5 from the stack to make the left operand.

6
 5 + 2 = 7 
StackExpression

Next, push the result of 5 + 2 (7) to the stack.

7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "3", which is an operand, so push it to the stack.

3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "8", which is an operand, so push it to the stack.

8
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "7", which is an operand, so push it to the stack.

7
8
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "/", which is an operator, so pop its two operands from the stack. Pop 7 from the stack for the right operand and then pop 8 from the stack to make the left operand.

3
7
6
 8 / 7 = 1.143 
StackExpression

Next, push the result of 8 / 7 (1.143) to the stack.

1.143
3
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 1.143 from the stack for the right operand and then pop 3 from the stack to make the left operand.

7
6
 3 * 1.143 = 3.429 
StackExpression

Next, push the result of 3 * 1.143 (3.429) to the stack.

3.429
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "2", which is an operand, so push it to the stack.

2
3.429
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "$", which is an operator, so pop its two operands from the stack. Pop 2 from the stack for the right operand and then pop 3.429 from the stack to make the left operand.

7
6
 3.429 $ 2 = 11.758 
StackExpression

Next, push the result of 3.429 $ 2 (11.758) to the stack.

11.758
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "3", which is an operand, so push it to the stack.

3
11.758
7
6
  
StackExpression

6 5 2 + 3 8 7 / * 2 $ 3 *

The next character scanned is "*", which is an operator, so pop its two operands from the stack. Pop 3 from the stack for the right operand and then pop 11.758 from the stack to make the left operand.

7
6
 11.758 * 3 = 35.274 
StackExpression

Next, push the result of 11.758 * 3 (35.274) to the stack.

35.274
7
6
  
StackExpression

Since we are done scanning characters, the remaining element in the stack (35.274) becomes the result of the postfix evaluation.

Postfix notation: 6 5 2 + 3 8 7 / * 2 $ 3 *
Result: 35.274


Wednesday, September 2, 2020

C Program to Create Singly Linked List .using Node Structure


Program to Create Singly Linked List .
//Program Create Singly Linked List
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//------------------------------------------------------------

void creat()
{
char ch;
 do
 {
  struct node *new_node,*current;

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

  printf("nEnter the data : ");
  scanf("%d",&new_node->data);
  new_node->next=NULL;

  if(start==NULL)
  {
  start=new_node;
  current=new_node;
  }
  else
  {
  current->next=new_node;
  current=new_node;
  }

 printf("nDo you want to creat another : ");
 ch=getche();
 }while(ch!='n');
}
//------------------------------------------------------------------

void display()
{
struct node *new_node;
 printf("The Linked List : n");
 new_node=start;
 while(new_node!=NULL)
   {
   printf("%d--->",new_node->data);
   new_node=new_node->next;
   }
  printf("NULL");
}
//----------------------------------------------------
void main()
{
create();
display();
}
//----------------------------------------------------
Output :
Enter the data : 10
Do you want to creat another :  y
Enter the data : 20
Do you want to creat another : y

Enter the data : 30
Do you want to creat another : n

The Linked List :
10--->20--->30--->NULL

Delete Node from First Postion : Singly Linked List

Delete First Node from Singly Linked List

Program :
void del_beg()
{
struct node *temp;

temp = start;
start = start->next;

free(temp);
printf("nThe Element deleted Successfully ");
}

Attention :
Step 1 : Store Current Start in Another Temporary Pointer
temp = start;
Step 2 : Move Start Pointer One position Ahead
start = start->next;
Step 3 : Delete temp i.e Previous Starting Node as we have Updated Version of Start Pointer
free(temp);


Insert node at Last / End Position in Singly Linked List



Inserting node at start in the SLL (Steps):
  1. Create New Node
  2. Fill Data into “Data Field
  3. Make it’s “Pointer” or “Next Field” as NULL
  4. Node is to be inserted at Last Position so we need to traverse SLL upto Last Node.
  5. Make link between last node and newnode
void insert_at_end()
{
struct node *new_node,*current;

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

if(new_node == NULL)
   printf("nFailed to Allocate Memory");

 printf("nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;

 if(start==NULL)
 {
   start=new_node;
   current=new_node;
 }
 else
 {
   temp = start;
     while(temp->next!=NULL)
     {
     temp = temp->next;
     }
   temp->next = new_node;
 }
}

Diagram :
insert_last
Attention :
  1. If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
  1. If we have previously created First or starting node then “else part” will be executed to insert node at start
  2. Traverse Upto Last Node., So that temp can keep track of Last node
else
       {
       temp = start;
   while(temp->next!=NULL)
  {
  temp = temp->next;
  }
  1. Make Link between Newly Created node and Last node ( temp )
temp->next = new_node;

To pass Node Variable to Function Write it as –
void insert_at_end(struct node *temp)

Insert node at Start/First Position in Singly Linked List



Inserting node at start in the SLL (Steps):
  1. Create New Node
  2. Fill Data into “Data Field
  3. Make it’s “Pointer” or “Next Field” as NULL
  4. Attach This newly Created node to Start
  5. Make newnode as Starting node
void insert_at_beg()
{
struct node *new_node,*current;

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

 if(new_node == NULL)
    printf("nFailed to Allocate Memory");

 printf("nEnter the data : ");
 scanf("%d",&new_node->data);
 new_node->next=NULL;

   if(start==NULL)
   {
   start=new_node;
   current=new_node;
   }
   else
   {
   new_node->next=start;
   start=new_node;
   }
}

Diagram :
insert_start
Attention :
  1. If starting node is not available then “Start = NULL” then following part is executed
if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
  1. If we have previously created First or starting node then “else part” will be executed to insert node at start
else
       {
       new_node->next=start;
       start=new_node;
       }

Linked-List : Insert Node at Middle Position in Singly Linked List


void insert_mid()
{
    int pos,i;
    struct node *new_node,*current,*temp,*temp1;

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

    printf("nEnter the data : ");
    scanf("%d",&new_node->data);

    new_node->next=NULL;
    st :
    printf("nEnter the position : ");
    scanf("%d",&pos);

    if(pos>=(length()+1))
       {
       printf("nError : pos > length ");
       goto st;
       }

    if(start==NULL)
       {
       start=new_node;
       current=new_node;
       }
    else
       {
       temp = start;
             for(i=1;i< pos-1;i++)
             {
             temp = temp->next;
             }
       temp1=temp->next;
       temp->next = new_node;
       new_node->next=temp1;
       }
}

Explanation :
Step 1 : Get Current Position Of “temp” and “temp1” Pointer.
temp = start;
                for(i=1;i< pos-1;i++)
  {
  temp = temp->next;
  }
Step 2 :
temp1=temp->next;
Step 3 :
temp->next = new_node;
Step 4 :
new_node->next = temp1