Computer Scientists are Pretty Pessimistic

Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Thursday, 11 August 2016

Conversion of an Infix expression into Postfix expression


Algorithm : :
S <- empty stack
S <- ‘#’
j <- 0
i <- 0
Append '$' sign at last of the expression
while top(s) ≠ ‘$’ do
        Case:
           E[i] is an operand or number :
                      j <- j+1
                      P[j] <- E[i]
            E[i] = ‘(‘ :
                      S <= E[i]
            E[i] = ‘)’ :
                      x <= S
                      while x ≠ ‘(‘ do
                           j <- j+1
                           P[j] <- x
                           x <= S
             E[i] is an operator :
                       while prec(E[i]) ≤ prec(top(S)) do
                                 j <- j+1
                                 P[j] <=S
                                  S <= E[i]
        i <- i+1


Precedency Table
Character
Precedency
#
0
$
1
(
2
+ , -
3
*,  /
4
^
5

CODE :: 

#include <iostream>
#include <map>
#include <stack>
#include <cstdio>
#include <cstring>
using namespace std;
int prec(char c)
{
    map<char,int>mp;
    mp['#']=0;
    mp['$']=1;
    mp['(']=2;
    mp['+']=3;  mp['-']=3;
    mp['*']=4;  mp['/']=4;
    mp['^']=5;
    return mp[c];
}
int main()
{
    char E[10000],P[10000];
    stack<char>st;
    st.push('#');
    int j=0,i=0;
    gets(E);
    strcat(E,"$");
    while(st.top()!='$')
    {
        if(isalpha(E[i]))
        {
            P[j++] = E[i];
        }
        else if(E[i]=='(')
        {
            st.push(E[i]);
        }
        else if(E[i]==')')
        {
            char x = st.top(); st.pop();
            while(x!='(')
            {
                P[j++] = x;
                x = st.top(); st.pop();
            }
        }
        else
        {
            while(prec(E[i])<=prec(st.top()))
            {

                P[j++] = st.top(); st.pop();
            }
            st.push(E[i]);
        }
        i++;
    }
    for(int i=0;i<j;i++)
        cout<<P[i];
    cout<<endl;
}

Friday, 17 June 2016

Queue Implementation with link list

Algoritm ::

Q <= x  : // enqueue operation
                l ← getcell
                if l = nil then overflow
                else
                      info ( l ) ← x
                      link ( l ) ← nil
                      link (r ) ← l
                       r ← l
x <= Q : // dequeue operation
                t ← link ( f )
                if t = nil then underflow
                else
                      x ← info ( t )

                      link ( f ) ← link ( t )

CODE ::
#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
    int data;
    struct node *link;
}*t,*r,*f;
void enqueue(int x)
{
    node *l = (node*) malloc(sizeof(node));
    l->data = x;
    l->link = NULL;
    r->link = l;
    r=l;
}
int dequeue()
{
    t=f->link;
    if(t==NULL)
    {
        cout<<"Underflow"<<endl;
        exit(0);
    }
    int x = t->data;
    f->link = t->link;
    return x;
}
int main()
{
    node *head = (node*) malloc(sizeof(node));
    r=head;
    f=head;
    for(int i=0;i<5;i++)
    {
        int x= rand()%100;
        enqueue(x);
    }
    for(int i=0;i<5;i++)
    {
        cout<<dequeue()<<" ";
    }
}

Stack Implementation with link list

Algorithm ::

S <= x  : // push operation
             
             l ← getcell
             if l = nil then overflow
             else
                    info ( link ) ← x
                    
                    link ( l ) ← t

x <= S : // pop operation
             
             if t = nil then underflow
             else
                    x ← info ( t )

                    t ← link ( t )


CODE ::

#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
    int data;
    struct node *link;
}*t;
void push(int x)
{
    node *l = (node*) malloc(sizeof(node));
    l->data = x;
    l->link = t;
    t=l;
}
int pop()
{
    if(t==NULL)
    {
        cout<<"Underflow"<<endl;
        exit(0);
    }
    int x = t->data;
    t=t->link;
    return x;
}
int main()
{
    t=NULL;
    for(int i=0;i<5;i++)
    {
        int x= rand()%100;
        push(x);
    }
    for(int i=0;i<5;i++)
    {
        cout<<pop()<<" ";
    }
    return 0;
}

Radix Sort

CODE ::

#include <iostream>
#include <cstdlib>
#include <climits>
#include <queue>
using namespace std;
struct node {
    int data;
    struct node *link;
}*head;
void radixSort(int m, node *head)
{
    queue<int>qq[10];
    int i=0,temp;
    while(m>0)
    {
        node *ptr = head->link;
        while(ptr!=NULL)
        {
            temp = ptr->data;
            for(int j=0;j<i;j++)
                temp=temp/10;
            int ind = temp%10;
            qq[ind].push(ptr->data);
            ptr=ptr->link;
        }
        i++;
        m=m/10;
        ptr = head->link;
        for(int i=0;i<10;i++)
        {
            while(!qq[i].empty())
            {
                ptr->data = qq[i].front();
                qq[i].pop();
                ptr = ptr->link;
            }
        }
    }
}
int main()
{
    head = (node*) malloc(sizeof(node));
    head->link = NULL;
    node *temp = head;
    cout<<"Enter Number of elements: "<<endl;
    int n,max=INT_MIN;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        node *newnode = (node*) malloc(sizeof(node));
        newnode->data = rand()%1000;
        newnode->link =temp->link;
        temp->link = newnode;
        temp = newnode;
        max = max > newnode->data ? max : newnode->data;
    }
    radixSort(max,head);
    temp = head->link;
    while(temp!=NULL)
    {
        cout<<temp->data<<" ";
        temp=temp->link;
    }
    return 0;

}