Saturday, October 1, 2016

Operations On Sorted Linklist :)

//Program To Perform Operations On A Sorted Linklist

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>  //for malloc function
struct node
{
    int data;
    struct node *next;

}*head;
void insert(int data) //for insertion
{ struct node *temp=(struct node *)malloc(sizeof(struct node));
struct node *save=(struct node *)malloc(sizeof(struct node));
struct node *ptr=(struct node *)malloc(sizeof(struct node));
temp->data=data;
if(head==NULL) //if it is first element
{
    head=temp;
    temp->next=NULL;
}
if(temp->data<head->data)//if value less than first element
{
    temp->next=head;
    head=temp;
 }
else{

ptr=head;
while(ptr!=NULL)
{
    if(temp->data<ptr->data)
    {
        break;
    }
    else
    {   save=ptr;
        ptr=ptr->next;
    }
}
save->next=temp;
temp->next=ptr;
}
}
void print() //for display
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
printf("\n");
}
}
 void delete(int data)
 {
  struct node *ptr=(struct node*)malloc(sizeof(struct node));
struct node *save=(struct node*)malloc(sizeof(struct node));
save=head;
ptr=head->next;
if(save->data==data)
{
 head=head->next;
 return;
 } 
 else{
  while(ptr!=NULL)
  {
   if(ptr->data==data)
   {
    save->next=ptr->next;
    return;
   }
   else
   {
    save=ptr;
    ptr=ptr->next;
   }}
   printf("ELEMENT NOT FOUND");
  
 }
 }
main()
{
int choice,num;
    head=NULL; //initially head is null
    while(1)  //for user interface
    { printf("\nOperations On Linklist\n");
         printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice);
              switch(choice)
              {
                   case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2:
                   printf("Enter Element To Delete");
                   scanf("%d",&num);//If user wishes to delete element from the queue
                        delete(num); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             print(); //calling display function
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }

Output:


Postfix Expression Evaluation For Multidigit :)

//Program to Evaluate Postfix Expression

#include<iostream>
#include<ctype.h>  //for isdigit function
using namespace std;
int stack[100];
int top=-1;
int value=0;    //Global Declarations
int pop()
{

    return (stack[top--]);
}
void push(int x)
{
    if(value==1)
    {
        int y;
        y=pop();
        stack[++top]=x+10*y;   //for more than one digit
    }
    else if(value==0)
    {
    stack[++top]=x;
    value=1;
}
}
main()
{
char c,postfix[100];
int i=0,y,x;
cout<<"\nEnter The Postfix Expression:";
cin.getline(postfix,100);
while((c=postfix[i++])!='\0')
{
    if(isdigit(c))
    push(c-'0');  //for converting datatype
    else if(c==',')
    {
        value=0;
    }
    else
    {
    x=pop();  //top element
    y=pop();  //next top element
    switch(c)
    {
        case'+':
            push(x+y);
         break;
         case '-':
             push(y-x);
             break;
        case '*':
        push(y*x);
        break;
        case '/':
        push(y/x);
        break;
        default:
        cout<<"\nInvalid Operator";    
    }
    }
    }
    cout<<"\n\nEntered Expression:"<<postfix;
cout<<"\n\nResult Of Evaluation:"<<stack[top];
}

Output:

 

Friday, September 30, 2016

Operations On Queue Implemented Using Linklist :)

Program to Operate Queue Which Is Implemented Using Linklist:


#include<stdio.h>
#include<stdlib.h>  //for malloc function
struct node         //node in a link list
{
    int data;
    struct node* next;
}*head;
void insert(int num)  //for insertion
{   struct node *ptr=malloc(sizeof(struct node));
    struct node *temp=malloc(sizeof(struct node));
    temp->data=num;
    temp->next=NULL;
    if(head==NULL)   //for first insertion
    {
        head=temp;
    }
    else
    {
    ptr=head;
    while(ptr->next!=NULL)   //for pointing to last node
    {
        ptr=ptr->next;
    }
    ptr->next=temp;         //saving temp as last node

}}
void delete()     // for deletion
{
    if(head==NULL)  //no node in queue
    {
        printf("Queue is Empty");
    }
    else{
        head=head->next;    }
}
void display()   //for display
{
    struct node * ptr=head;
    if(head==NULL)
    {
        printf("\nQueue Is Empty\n");
        return;
    }
    printf("\nElements Are:\n");
    while(ptr!=NULL)
    {
        printf("%d\n",ptr->data);
        ptr=ptr->next;   //for moving forward
    }
}
main()
{ int choice,num;
    head=NULL; //initially head is null
    while(1)  //for user interface
    { printf("\nOperations On Queue\n");
         printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice);
              switch(choice)
              {
                   case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2://If user wishes to delete element from the queue
                        delete(); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             display(); //calling display function
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }

Also see Program of Operation on Queue as Array

OUTPUT: 


Thursday, September 29, 2016

Binary To Decimal Conversion

Program To Convert Binary Number To Decimal:


#include<iostream>
#include<math.h> //for power function
using namespace std;
main()
{
    int binary,decimal=0,i=0,remainder;
    cout<<"\nEnter The Binary Number ";
    cin>>binary;
    while(binary!=0)
    {
        remainder=binary%10; // getting the last digit
        binary=binary/10;
        decimal +=remainder*pow(2,i);//finding 2^i*digit
        i++;
    }
    cout<<"Decimal number is "<<decimal;
}

Also See Program To Perform Operations On Queue

Output:

Wednesday, September 28, 2016

Operations on QUEUE :)

#include<stdio.h>
#include<conio.h>
#define MAX 20 //defining size of queue
void insert(int); //function declaration for insertion
int del();        //function declaration for deletion
void display();   //function declaration for displaying queue
int queue[MAX];   //queue array
int front=-1,rear=-1;     /*initialy setting both parameters to -1 position i.e out of the array*/
main()
{
      int choice,num; //variables that we require in the entire program
      while(1) //for repeatingly asking the user whether to continue the program?
      {
              printf("Enter your choice\n");
              printf("1. INSERT \n");
              printf("2. DELETE \n");
              printf("3. DISPLAY \n");
              printf("4. EXIT \n");
              scanf("%d",&choice); //getting the choice of the user
              switch(choice) //using switch statement to select the working of the entered(desired) choice
              {
              case 1: //If user wishes to insert element in the queue
                   printf("Enter number to be inserted in the queue: ");
                   scanf("%d",&num); //getting the element to be inserted from the user
                   insert(num); //calling insert function
                   break; //jumping out of switch statement
                   case 2://If user wishes to delete element from the queue
                        num=delete(); //calling delete function and saving the result in an int variable(num)
                        break;
                        case 3://If user wishes to display the queue
                             display(); //calling display function
                             printf("\n");
                             break;
                             case 4: //If user wishes to exit(stop continuing the program)
                                  exit(1); //end of while loop
                                  default ://If user enters a number other than 1 to 4
                                          printf("INVALID CHOICE \n");
                                          }
                                          }
                                          }
                                          //FUNCTION DEFINATION
      void insert (int x) //value of number to be inserted(num) gets copied in 'x' when function is called
      {
           if(rear==MAX-1) //rear is at the last position in queue array
           {
                          printf("Queue is full (OVERFLOW)\n");
                          return; //returning control back to calling function
                          }
                          if (front==-1)//only if we wish to insert 1st element in the queue
                          {
                                       front=0;//front is set to 1st position i.e queue[0] and here the first insertion will take place
                                       }
                                       rear=rear+1; //insertion take place at REAR
                                       queue[rear]=x;//element to be inserted is placed at the rear position of queue
                                       }//for eg: for 1st insertion: front is made 0 and rear is also made 0(-1+1)
                                       //and so the insertion will take place at queue[0]
                                       //for next insertion rear will be 1(0+1) and front will remain 0 as insertion occurs at REAR
        int delete () //for deleting element from queue
        {
            int x;
            if(front==-1 || front==rear+1) //front==-1 means front is yet not incremented or set to 0 for first insertion
                     //front==rear+1 : if for eg front=0,rear=2
                     //for first deletion front=1,rear=2;for second deletion front=2,rear=2;now if front=3(rear+1),there will be no element in the queue
                        {              
                        printf("Queue is empty (UNDERFLOW) \n");
                        }
                        x=queue[front];//saving value of element at FRONT(to be deleted) in a variable
                        front=front+1;//deletion occurs at FRONT,and so we increment it for each deletion
                        printf("%d has been deleted \n",x);
                        return x;
                        }
       void display() //for displaying queue
       {
            int i;
            if(front==-1 || front==rear+1) //checking if queue is empty
            {
                        printf("Queue is EMPTY \n");
                        }
                        //if queue is not empty:
                        for(i=front;i<=rear;i++)//start printing elements of queue from FRONT to REAR
                        {
                                printf("%d ",queue[i]);//displaying QUEUE
                                }
                                }
                             
                                               
                     
                                         

Cube Root Of A Number

Program To Calculate Cube root of a Given Number:


// program to calculate cuberoot of a number
#include<iostream>
#include<math.h>
using namespace std;
main()
{
float num,cuberoot;
cout<<"\nEnter Any Number To Find Cube Root ";
cin>>num;
cuberoot=pow(num,1.0/3.0);
cout<<"\nCuberoot Of Given Number "<<cuberoot;
}

Also See Program To Convert Decimal To Hexadecimal Click Here
OUTPUT:


Decimal To Hexadecimal Conversion

Program To Convert Decimal Number To Hexadecimal Number:


#include<iostream>
using namespace std;
main()
{
int num,hexa[50],i=0;
cout<<"\nEnter Any Decimal Number ";
cin>>num;
while(num>0)
{
hexa[i]=num%16;
num=num/16;
i++;
}
cout<<"\nHexadecimal Equivalent is ";
for(int j=i-1;j>=0;j--)
{
switch(hexa[j])
{
case 10:
cout<<"A";
break;
case 11:
   cout<<"B";
break;
case 12:
   cout<<"C";
break;
case 13:
   cout<<"D";
break;
case 14:
   cout<<"E";
break;
case 15:
   cout<<"F";
break;
default:
cout<<hexa[j];
}}
}
Also See Program To Convert Decimal TO Octal Click Here

Output: