2017 universities Ranking in Pakistan

Here are overall top universities of Pakistan.Some universities are at the top place in engineering fields but others are at Applied science, BA and in IT programs

Binary Search in C++

Search found at index

Quick sorting program

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Tree traversal (preorder,inorder,postorder) using linklist (c++)

Tuesday 27 November 2018

Singly Linklist in C++ (insertion at satart,middle and end. Searching in linklist. Deletion of any node)


#include <iostream>

using namespace std;

struct node
{
    int data;
    struct node *next;
};

int main()

{
    node *head;
    node *temp;
    node *last;

    last=0;

    int choice;

   do

    {
        temp=new node();
        cout<<"Enter values in a node:"<<endl;
        cin>>temp->data;
        if(last==0)
        {
            head=last=temp;

        }

        else
        {
            last->next=temp;
            last=temp;
        }
        cout<<"Enter your choice:"<<endl;
        cin>>choice;
    }while(choice==1);

        last->next=0;

        temp=head;

        while(temp!=0)

        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }
        /************insertion at the start of the node***************************/
        do{
        temp=new node();
        cout<<"Enter value at the start of the node: "<<endl;
        cin>>temp->data;

        temp->next=head;

        head=temp;

        cout<<"Enter your choice: "<<endl;

        cin>>choice;
        }while(choice==1);

        temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;

        }


        /************** iNSERTION AT MIDDLE OF THE LINKLIST***********/

        node *temp2;
        int location=0;

        cout<<"\n Enter your location"<<endl;

        cin>>location;

        temp=new node();

        cout<<"\n Enter data in node: "<<endl;
        cin>>temp->data;

        temp2=head;


        while(location-1>0)

        {
            location--;
            temp2=temp2->next;
        }
        temp->next=temp2->next;
        temp2->next=temp;

        temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }

    /**************************Search traverse*************/

    int value;
    int count=1;
    int flag=0;
    cout<<"\n Enter value you want to search in LinkList: "<<endl;
    cin>>value;
    temp=head;
    while(temp!=0)
    {
        if(temp->data==value)
        {
            cout<<"Data found successfully at index "<<count<<endl;
            flag=1;
            break;
        }
        else
        {
            temp=temp->next;
            count++;
        }
    }

    if(flag==0)

    {
        cout<<"Data not found"<<endl;
    }
    /**************************deleting node******************/
    int b=0;
    int item;
    node *temp3;
    cout<<"which data do you want to delete?"<<endl;
    cin>>item;
    temp=head;
    while(temp->next->next!=0)
    {
        if(temp->next->data==item)
        {
            cout<<"Data found successfully"<<endl;
            b=1;
            break;
        }
        else
        {
            temp=temp->next;
            temp3=temp->next;
        }
    }

    if(b==0)

    {
        cout<<"Data not found"<<endl;
    }
    else
    {
        temp->next=temp->next->next;
        delete temp3;
    }

    temp=head;

        while(temp!=0)
        {
            cout<<temp->data<<" ";
            temp=temp->next;
        }


    return 0;

}

Monday 26 November 2018

Binary Search in C++



#include <iostream>
using namespace std;


void binarysearch(int arr[],int s,int size);
void binarysearch(int arr[],int s,int size)
{
    int lower=0,higher=size-1,m;
    while(lower<=higher)
    {
        m=(lower+higher)/2;
        if(s==arr[m])
        {
        cout<<"Search found at index"<<m<<endl;
        return;
        }
        else if(s>arr[m])
            lower=m+1;
        else
            higher=m-1;
    }
    cout<<"Search not found"<<endl;
}

/******************************************/

int main()
{
    int n[5],s,size;
    cout<<"Enter value in array of type int:"<<endl;
    for(int i=0;i<5;i++)
    {
     cin>>n[i];
    }
    cout<<"Enter number in Search: ";
    cin>>s;
    size=sizeof(n)/sizeof(n[0]);
    binarysearch(n,s,size);
    return 0;
}


Selection sorting Dynamically



Worst-Complexity: n ^ 2
Average-Complexity: n ^ 2
Best-Complexity: n ^ 2

Space-Complexity:  1

#include<iostream>
using namespace std;

/*********************sorting function********************/

void selection_Sort(int arr[], int n)
{
    int i, j, min;

    for (i = 0; i < n-1; i++)
    {
        min = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min])
            min = j;

          swap(arr[min],arr[i]);
    }

 /****************** Printing values*****************************/

void print_Array(int arr[], int size) 

    int i; 

    for (i=0; i < size; i++) 

        cout<<arr[i];

cout<<endl; 

/**********************Driver function*************************/

int main() 

int *arr;

int n;

cout<<"How many numbers your want to sort:"<<endl;

    cin>>n;

    arr=new int[n];

    cout<<"Enter the numbers:"<<endl;

for(int i=0;i<n;i++)

{

cin>>arr[i];

}    

selection_Sort(arr, n); 

    cout<<"After sorting array: \n"<<endl; 

    print_Array(arr, n); 

    return 0; 

Wednesday 14 November 2018

"Infix to Post-fix" conversion and "parenthesis check using "User define" stacks C++


#include<iostream>
using namespace std;
char stack[100]; //
char postfix[100];
char exp[100]; // mathematical expression entered by the user
int top=-1;
/**********************************************/
void push1(char newitem)
{
                if(top=='\0')
                {
                                cout<<"Stack overflow"<<endl;
                }
                else
                {
                                exp[++top]=newitem;
                }
}
/**********************************************/
bool ArePair(char opening,char closing)
{
                if(opening == '(' && closing == ')') return true;
                else if(opening == '{' && closing == '}') return true;
                else if(opening == '[' && closing == ']') return true;
                return false;
}
/**********************************************/


void pop1()
{
                if(top==-1)
                {
                cout<<"Cannot pop. The stack does not exist"<<endl;
                }
                else
                {
                                top--;
                }
}
/**********************************************/
int rtop()
{
    if(top!=-1)
    {
        return exp[top];
    }
    else
    return false;
}
/**********************************************/
bool isempty()
{
    if(top==-1)
    {
        return true;
    }
    else
    {
        return false;
    }

}
/**********************************************/
bool AreParanthesesBalanced(char exp[])       // This function will check that parantheses are balanaced or not. If it if balance it will return true.
{
                for(int i =0;exp[i]!= '\0';i++)
                {
                                if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
                                {
                                    push1(exp[i]);
                                }

                                else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
                                {
                                                if(isempty() || !ArePair(rtop(),exp[i]))
                                                {
                                                   return false;
                                                }

                                                else
                                                {
                                                    pop1();

                                                }
                                }
                }
                return isempty();
}
/**********************************************/
int precedence(int ch)
{
 if(ch=='(')
        return 0;
 else if(ch=='+'||ch=='-')
    return 1;
 else if(ch=='*'||ch=='/'||ch=='%')
    return 2;
   return -1;
}
/**********************************************/
char pop()
{
  if(top==-1)
  {
   cout<<"\n stack is Empty";
  }
  else
  {
    char ch=stack[top];
    top--;
    return (ch);
  }
}
/**********************************************/
void push(int ch)
{
  if(top!=sizeof(stack)-1)
  {
    top++;
   stack[top]=ch;
  }
  else
    cout<<"Stack Overflow"<<endl;
}
/************************************************/
bool IsOperator(char C)
{
                if(C == '+' || C == '-' || C == '*' || C == '/' || C== '$')
                                return true;

                return false;
}
/************************************************/

bool IsOperand(char C)
{
                if(C >= '0' && C <= '9') return true;
                if(C >= 'a' && C <= 'z') return true;
                if(C >= 'A' && C <= 'Z') return true;
                return false;
}
/**********************************************/
void postfixfun(char exp[])
{
    int k=0;
    int j=0;
       int p=0;
    while(exp[k]!='\0')
    {
/*------------------------------------------------------------*/
        if(IsOperand(exp[k])==true)
        {
         postfix[j]=exp[k];
            j++;
        }
/*----------------------------------------------------------------*/
       else if(exp[k]=='(')
        {
          push(exp[k]);
        }
/*-------------------------------------------------------------------*/
       else if(IsOperator(exp[k])==true)
        {
            while(precedence(stack[top])>=precedence(exp[k]))
            {
                postfix[j]=pop();
            j++;
            }
            push(exp[k]);
        }
/*-------------------------------------------------------------------*/
        else if(exp[k]=')')
        {
            while(stack[top]!='(')
            {
                postfix[j]=stack[top];
                j++;
                pop();
            }
            pop();
        }
        k++;
    }
   
    while(top!=-1)
    {
    postfix[j]=pop();
    j++;
    }
}
/************************************************/
int main()
{

    cout<<"Enter your mathematic expression:"<<endl;
    cin>>exp;
    int size;
    size=sizeof(exp);

    if(AreParanthesesBalanced(exp))
    {
        cout<<"Expression is balanced \n" <<endl;
            cout<<"Postfix Expression="<<endl;
            postfixfun(exp);
            for(int i=0;i<size;i++)
            {
                cout<<postfix[i];
            }
    }
    else
    {
        cout<<"Not balanced"<<endl;
    }


    return 0;

}

New Website Design? Contact Us at Quatro Link

Saturday 3 November 2018

A Program which uses linear searching? C++



Starts from the base index and compare it with the rest of the element of an array. If the serached index found, it returns 1 else it returns -1.


#include<iostream>

using namespace std;
/********************************************/
int linearSearch(int n[],int s,int size)
    {
        for(int i=0;i<size;i++)
        {
            if(n[i]==s)
            {
            return i;
            }
        }

        return -1;
    }

/*********************************************/
int main()
{
   int n[10];
   int s;
   int result;
   cout<<"Enter values in 'int' array"<<endl;
    for(int j=0;j<10;j++)
    {
    cin>>n[j];
    }
   cout<<"Enter number to search in array"<<endl;
   cin>>s;
   int size=sizeof(n)/sizeof(n[0]);
   result=linearSearch(n,s,size);

   if(result==-1)
   {
       cout<<"Value not found!"<<endl;
    }
    else
    {
    cout<<"value is on index"<<result<<endl;
    }
       return 0;

}