Saturday, 31 December 2011

Implementation of Stacks using Linked Lists


#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insert(NODEPTR start)
{
 NODEPTR temp;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
 {
  printf("Unsuccessfull\n");
  return;
 }
 temp->link=NULL;
 printf("Enter the data to be pushed\n");
 scanf("%d",&temp->data);
 temp->link=start;
 start=temp;
 return start;
}

NODEPTR del(NODEPTR start)
{
 NODEPTR temp;
 if(start==NULL)
 {
  printf("Stack is empty\n");
  return;
 }
 temp=start;
 start=start->link;
 printf("The element popped is %d\n",temp->data);
 free(temp);
 return start;
}

void dis(NODEPTR start)
{
 NODEPTR temp;
 if(start==NULL)
 {
   printf("Stack is empty\n");
   return;
 }
 printf("The contents of the stack are:\n");
 for(temp=start;temp!=NULL;temp=temp->link)
  printf("%d\n",temp->data);
 }

main()
{
 int ch;
 NODEPTR start=NULL;
 while(1)
 {
  printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
  scanf("%d",&ch);
  switch(ch)
 {
  case 1 : start=insert(start);
           break;
  case 2 : start=del(start);
            break;
  case 3 : dis(start);
           break;
  default: return;
 }
}
}

Reversing of a linked list


#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insert(NODEPTR start)
{
 NODEPTR temp;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
 {
 printf("Unsuccessful\n");
 return start;
 }
 printf("Enter the data\n");
 scanf("%d",&temp->data);
 temp->link=start;
 start=temp;
 return start;
}

NODEPTR rev(NODEPTR start)
{
 NODEPTR cur=start;
 NODEPTR prev=NULL;
 NODEPTR next;
 while(cur!=NULL)
 {
  next=cur->link;
  cur->link=prev;
  prev=cur;
  cur=next;
 }
 return prev;
}

NODEPTR disb(NODEPTR start)
{
 NODEPTR temp;
 if(start==NULL)
 {
  printf("List is empty\n");
  return;
 }
 printf("The contents of the list before reversing\n");
 for(temp=start;temp!=NULL;temp=temp->link)
  printf("%d\n",temp->data);
 return start;
}
NODEPTR disa(NODEPTR prev)
{
 NODEPTR temp;
 if(prev==NULL)
 {
  printf("list is empty\n");
  return;
 }
 printf("The contents of the list after reversing\n");
 for(temp=prev;temp!=NULL;temp=temp->link)
  printf("%d\n",temp->data);
 return prev;
}

main()
{
 int ch;
 NODEPTR start=NULL;
 NODEPTR prev=NULL;
 while(1)
 {
  printf("1.Insert into the list\n2.List before reversing\n3.Reverse the list\n4.List after reversing\n");
 scanf("%d",&ch);
 switch(ch)
 {
  case 1 : start=insert(start);break;
  case 2 : start=disb(start);break;
  case 3 : prev=rev(start);break;
  case 4 : prev=disa(prev);break;
  default : return;
 }
 }
}


Implementation of Queues using Linked lists



#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insert(NODEPTR start)
{
 NODEPTR temp;
 NODEPTR cur=start;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
 {
  printf("Memory allocation not possible\n");
  return start;
 }
 printf("Enter the data\n");
 scanf("%d",&temp->data);
 temp->link=NULL;
 if(start==NULL)
  return temp;
 while(cur->link!=NULL)
  cur=cur->link;
 cur->link=temp;
 return start;
}

NODEPTR del(NODEPTR start)
{
 NODEPTR cur=start;
 if(start==NULL)
 {
 printf("Queue is empty\n");
  return NULL;
 }
 start=start->link;
 printf("Element deleted is %d\n",cur->data);
 free(cur);
 return start;
}

NODEPTR dis(NODEPTR start)
{
 NODEPTR cur=start;
 if(start==NULL)
 {
  printf("Queue is empty\n");
  return;
 }
 for(cur=start;cur!=NULL;cur=cur->link)
  printf("%d\n",cur->data);
}

main()
{
 int ch;
 NODEPTR start=NULL;
 while(1)
 {
  printf("1.Insert 2.Delete 3.Display\n");
  scanf("%d",&ch);
  switch(ch)
 {
  case 1 : start=insert(start);
           break;
  case 2 : start=del(start);
            break;
  case 3 : dis(start);
           break;
  default : return;
 }
}
}


Merging of 2 ordered Linked Lists without creating new nodes


#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insert(NODEPTR start)
{
 NODEPTR temp,cur=start;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
 {
  printf("Memory allocation not possible\n");
  return start;
 }
 printf("Enter the data\n");
 scanf("%d",&temp->data);
 temp->link=NULL;
 if(start==NULL)
  return temp;
 while(cur->link!=NULL)
  cur=cur->link;
 cur->link=temp;
 return start;
}

NODEPTR sl(NODEPTR start1,NODEPTR start2)
{
 NODEPTR start,p,q;
 if(start1==NULL)
  start=start2;
 else if(start2==NULL)
  start=start1;
 else if(start2->data >=start1->data)
  start=start1;
 else
  start=start2;
 p=start1;
 q=start2;
 while(start1!=NULL && start2!=NULL)
 {
  if(start2->data >= start1->data)
  {
   p=p->link;
   if(p!=NULL && p->data < start2->data)
   {
      start1=p;
      continue;
   }
   start1->link=start2;
   start1=p;
  }
  else
  {
   q=q->link;
   if(q!=NULL && q->data < start1->data)
   {
     start2=q;
     continue;
   }
   start2->link=start1;
   start2=q;
  }
 }
 return start;
}

void display(NODEPTR start)
{
 NODEPTR cur=start;
 if(start==NULL)
 {
   printf("List is empty\n");
   return;
 }
 printf("The merged list is \n");
 for(cur=start;cur!=NULL;cur=cur->link)
  printf("%d\t",cur->data);
}

main()
{
 int ch;
 NODEPTR start,start1,start2;
 start=NULL;
 start2=NULL;
 start1=NULL;
 while(1)
 {
  printf("1.Enter into list 1\n2.Enter into list 2\n3.Merge the 2 lists\n4.Display\n");
  scanf("%d",&ch);
  switch(ch)
 {
  case 1 : start1=insert(start1);break;
  case 2 : start2=insert(start2);break;
  case 3 : start=sl(start1,start2);break;
  case 4 : display(start);break;
  default: return;
 }
}
}

Binary Trees : Creating Tree using Iterative Technique with Inorder,Preorder and Postorder Traversals


#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int info;
 struct node *left,*right;
}NODE,*NODEPTR;

NODEPTR getnode()
{
 NODEPTR temp=(NODEPTR)malloc(sizeof(NODE));
 temp->left=temp->right=NULL;
 return temp;
}

NODEPTR create(NODEPTR root,int item)
{
 NODEPTR temp,cur,prev;
 temp=getnode();
 temp->info=item;
 if(root==NULL)
  return temp;
 cur=root;
 prev=NULL;
 while(cur!=NULL)
 {
  prev=cur;
  if(cur->info > item)
   cur=cur->left;
  else
   cur=cur->right;
 }
 if(item < prev->info)
  prev->left=temp;
 else
  prev->right=temp;
 return root;
}

void inorder(NODEPTR root)
{
 if(root!=NULL)
 {
  inorder(root->left);
  printf("%d\t",root->info);
  inorder(root->right);
 }
}

void preorder(NODEPTR root)
{
 if(root!=NULL)
 {
  printf("%d\t",root->info);
  preorder(root->left);
  preorder(root->right);
 }
}

void postorder(NODEPTR root)
{
 if(root!=NULL)
 {
  postorder(root->left);
  postorder(root->right);
  printf("%d\t",root->info);
 }
}

main()
{
NODEPTR root=NULL;
int item,ch,cont=1;
printf("Enter elements and -1 to terminate\n");
 while(1)
{
 scanf("%d",&item);
 if(item==-1)break;
 root=create(root,item);
}
 while(cont==1)
 {
 printf("1.Inorder 2.Preorder 3.Postorder \n");
 scanf("%d",&ch);
 switch(ch)
 {
   case 1 : inorder(root);
            break;
   case 2 : preorder(root);break;
   case 3 : postorder(root);break;
   default : return;
}
 }
 }

Binary Trees : Creating, Deleting and Displaying


All thanks to my friends and Khanetkar Textbook, this code finally works somehow! :D




#include<stdio.h>
#include<malloc.h>

typedef struct node
{
int info;
struct node *left, *right;
}NODE,*NODEPTR;
int count=0;

insert(int x,NODEPTR *root)
{
if((*root)==NULL)
{
(*root)=malloc(sizeof(NODE));
(*root)->left=(*root)->right=NULL;
(*root)->info=x;
count++;
return;
}
if(x<(*root)->info)
insert(x,&((*root)->left));
else
insert(x,&((*root)->right));
}

void inorder(NODEPTR root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d ",root->info);
inorder(root->right);
}
}

NODEPTR search(NODEPTR root, int num, NODEPTR *parent,int *found)
{
NODEPTR q=root,x;
*found=0;
*parent=NULL;
if(count==0)return NULL;
while(q!=NULL)//binary searching
{
if(q->info==num)
{
*found=1;
printf("Search works\n");
return q;
}
if(q->info>num)
{
*parent=q;
q=q->left;
}
else
{
*parent=q;
q=q->right;
}
}
printf("Key not found\n");
return NULL;
}

delete(NODEPTR *root, int num)
{
int found;
NODEPTR parent, x,xsucc;
if(*root==NULL)
{
printf("Tree is empty\n");
return;
}
parent=NULL;
x=NULL;
x=search(*root,num,&parent,&found);
if(!found)
{
printf("Data not found\n");
return;
}
if(x==*root&&count==1)
{
*root=NULL;
count--;
return;
}
if(x==*root&&count==2)
{
if(x->left!=NULL)
*root=x->left;
else
*root=x->right;
count--;
return;
}
if(x!=*root&&count==2)
{
(*root)->left=NULL;
(*root)->right=NULL;
count--;
return;
}
if(x->left!=NULL&&x->right!=NULL)
{
printf("Reaches correct if\n");
parent=x;
xsucc=x->right;
printf("Finds correct xsucc\n");
while(xsucc->left!=NULL)
{
parent=xsucc;
xsucc=xsucc->left;
}
x->info=xsucc->info;
x=xsucc;
printf("Successful swap\n");
}
if(x->left==NULL&&x->right==NULL)
{
if(parent->right==x)
parent->right=NULL;
else
parent->left=NULL;
free(x);
count--;
return;
}
if(x->left==NULL&&x->right!=NULL)
{
if(x==*root)
{
*root=x->right;
free(x);
count--;
return;
}
if(parent->left==x)
parent->left=x->right;
else
parent->right=x->right;
printf("Enters correct sub if\n");
free(x);
count--;
return;
}
if(x->left!=NULL&&x->right==NULL)
{
if(x==*root)
{
*root=x->left;
free(x);
count--;
return;
}
parent->left=x->left;
free(x);
count--;
return;
}
}


int main()
{
NODEPTR root=NULL;
int elt=0,cont=1,num;
printf("Enter -1 to terminate\n");
while(elt!=-1)
{
scanf("%d",&elt);
if(elt!=-1)
insert(elt,&root);
}
printf("Before deletion\n");
inorder(root);
printf("\n");
while(cont==1)
{
printf("Enter element to be deleted\n");
scanf("%d",&num);
delete(&root,num);
printf("After deletion\n");
inorder(root);
printf("\nEnter 1 to keep deleting\n");
scanf("%d",&cont);
}
}

Insertion sort of strings in C


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void insert(char a[][25],int n)
{
 int i,j,k;
 char key[25];
 for(i=1;i<n;i++)
 {
  strcpy(key,a[i]);
  for(j=i-1;j>=0;j--)
  {
    if(strcmp(key,a[j])>=0)break;
     strcpy(a[j+1],a[j]);
  }
  strcpy(a[j+1],key);
  printf("After pass %d:\n",i);
  for(k=0;k<n;k++)
   printf("%s\n",a[k]);
 }
}

int main()
{
  int n,i;
  char a[25][25];
  printf("Enter the  number of elements to sort\n");
  scanf("%d",&n);
  printf("Enter the strings to be sorted\n");
  for(i=0;i<n;i++)
   scanf("%s",a[i]);
  insert(a,n);
  printf("After sorting\n");
  for(i=0;i<n;i++)
   printf("%s\n",a[i]);
}


Thanks to the senior who gave us the permission to copy their code :P

Right Threaded Tree in C



#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 int rthread;
 struct node *left,*right;
}NODE,*NODEPTR;

NODEPTR getnode()
{
 NODEPTR temp;
 temp=(NODEPTR)malloc(sizeof(NODE));
 temp->left=NULL;
 temp->rthread=1;
 return temp;
}

NODEPTR insucc(NODEPTR s)//to find the inorder successor
{
  NODEPTR  p=s->right;
  if(s->rthread==0)
   while(p->left!=NULL)
    p=p->left;
  return p;
}

void inorder(NODEPTR h)//function to traverse in in-order
{
  NODEPTR t=h;
  while(1)
  {
    t=insucc(t);
     if(t==h)
            return;
     printf("%d\n",t->data);
   }
}

NODEPTR create(NODEPTR head)
{
 NODEPTR y,t,q[100];
 int f=0,r=0,v;
 y=getnode();
 q[r++]=y;
 printf("Enter the root:");
 scanf("%d",&y->data);
 y->right=head;
 head->right=y;
 head->rthread=0;
 while(f!=r)
 {
  t=q[f++];
  printf("\nEnter the left child(%d) :",t->data);
  do
  {
   printf("Left child should be smaller\n");
   scanf("%d",&v);
   if(v==-1)break;
  }while(v>t->data);
  if(v!=-1)
  {
   y=getnode();
   q[r++]=y;
   t->left=y;
   y->right=t;
   y->data=v;
  }
  printf("Enter the right child(%d) :",t->data);
  do
  {
   printf("Right child should be  greater\n");
   scanf("%d",&v);
   if(v==-1)break;
  }while(v<t->data);
  if(v!=-1)
  {
    y=getnode();
    y->right=t->right;
    t->right=y;
    y->data=v;
    t->rthread=0;
    q[r++]=y;
  }
 }
 return head;
}

int main()
{
 NODEPTR head;
 head=getnode();
 head=create(head);
 printf("Inorder traversal : \n");
 inorder(head);
}



PS : A very long, inefficient code.. Try to understand at your own risk!

Tuesday, 18 October 2011

Student Linked List

C program to construct a singly linked list consisting of following instructions in each node : 1.Student ID(int) 2.Student name(string) 3.Semester(int) The operations supported are 1.Insertion operation at any position 2.deleting a node based on ID

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 char name[25];
 int id,sem;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insertp(NODEPTR start,int pos, int *count)
{
 NODEPTR temp,prev,cur;
 int i;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
  printf("Memory allocation not possible\n");
 printf("Enter the name, ID and semester of the student\n");
 scanf("%s%d%d",temp->name,&temp->id,&temp->sem);
 temp->link=NULL;
 for(cur=start;cur!=NULL;cur=cur->link)
  if(cur->id==temp->id)
  {
    printf("ID already exists\n");
    free(temp);
    return start;
   }
 (*count++);
 if(pos==1)
 {
   temp->link=start;
   start=temp;
   return start;
 }
 cur=start;
 for(i=1;i<pos;i++)
 {
   prev=cur;
   cur=cur->link;
  }
 prev->link=temp;
 temp->link=cur;
 return start;
}

NODEPTR deleteid(NODEPTR start, int did, int *count)
{
  NODEPTR prev,cur;
  for(cur=start;cur!=NULL;prev=cur,cur=cur->link)
  {
    if(cur->link==did)
      break;
  }
  if(cur==NULL)
  {
    printf("Record not found\n");
    return start;
  }
 if(cur==start)
  start=start->link;
 else
  prev->link=cur->link;
 free(cur);
 (*count)- -;
 return start;
}

int search (NODEPTR start, int sid)
{
  NODEPTR cur,t;
  int mid;
  for(cur=start;cur!=NULL;cur=cur->link)
    if(cur->id==sid)
      break;
  if(cur==NULL)
   printf("Record not found\n");
 else
 {
   while(1)
   {
     printf("Enter the ID of the student to be searched\n");
     scanf("%d",&nid);
     for(t=start;t!=NULL;t=t->link)
     {
       if(nid=t->id && (nid!= cur->id))
       {
         printf("ID already exists, enter again\n");
         break;
        }
     }
     if(t==NULL)
     {
       cur->id=nid;
       printf("Enter the name and semester\n");
       scanf("%s%d",cur->name,&cur->sem);
       return;
     }
    }//end of while
   }//end of else
 }//end of function

void display(NODEPTR start)
{
  NODEPTR cur;
  if(start==NULL)
   printf("The list is empty\n");
 else
 {
   printf("List content is shown below\n");
   for(cur=start;cur!=NULL;cur=cur->link)
     printf("%s\t%d\t%d\n",cur->name,cur->id,cur->sem);
  }
}

int main()
{
  NODEPTR start=NULL;
  int ch, id, pos, count=0;
  while(1)
  {
    display(start);
    printf("Enter your choice of operation 1.Insert 2.Delete 3.Search OTHER:Exit\n");
    scanf("%d",&ch);
    switch(ch)
    {
      case 1 : printf("Enter the position (1 to %d) to insert the node\n", count+1);
                   scanf("%d",&pos);
                   if(pos < 1 || pos>count+1)
                      printf("Invalid position\n");
                   else
                      start=insertp(start,pos,&count);
                  break;
     case 2 : printf("Enter the ID to be deleted\n");
                  scanf("%d",&id);
                  start=deleteid(start,id,&count);
                  break;
    case 3 : printf("Enter the ID to be searched\n");
                 scanf("%d",&id);
                 search(start,id);
                 break;
   default : return;
  }
 }
}


Saturday, 8 October 2011

Nokia Symbian Belle 600 700 701

First of all, I would like to say R.I.P Steve Jobs.Since Apple lost its Leader as well as Market (due to its iphone 4S), I think its time for us to check out other releases (although these released before iphone 4S).


Nokia has released 3 new Symbain Belle products- Nokia 600(loudest),700(smallest) and 701(brightest)
All have the latest Symbian Belle and a new technology called NFC- Near Field Technology.





Lets take a look at them


1.Nokia  600 - Its the loudest phone (106 Phons)- With built-in FM radio antenna for listening to radio without headphones and FM transmitter that makes it possible to broadcast music from your phone to any FM radio. Lowest price Rs. 12,999,when compared to 700 and 701. 1 GHz processor; 5 MP full focus camera with LED flash and HD video capture, and 2 GB of internal memory with ability to increase to 34 GB using a 32 GB microSD card.A powerful loudspeaker and the ability to also stream music wirelessly to NFC-enabled accessories.















2.Nokia 700 - Its the smallest smartphone of Nokia.Weighing 96gm and at 110 x 50.7 x 9.7 mm. 1Ghz processor, 3.2 inch AMOLED screen ClearBlack display, 2GB of internal memory (with the option of using a 32GB microSD card for a total of 34GB), HD video capture and 5MP full focus camera with LED flash. Obviously, it features the new single tap NFC pairing and sharing capabilities.The price is Rs. 18,099.





3.Nokia 701 - Its the slimmest and the brightest phone ever made, claims Nokia, based on a 3.5 inch ClearBlack display that makes it perfect for indoor and outdoor use. 1 GHz processor, 8 MP full focus camera with dual LED flash and 2X digital zoom, second front-facing camera and HD video capture. It comes with 8 GB internal memory and the possibility to increase to 40 GB by installing a 32 GB microSD card. The most catch feature of this phone is that it provides single-tap NFC pairing and sharing capabilities, allowing content to be shared and sound to be streamed wirelessly to headphones and NFC-enabled speakers.









I think this NFC is gonna work . Since it has been launched in India through entertainment , all teens are going to try their hands on this "single-tap" techno! 
FTW Nokia !! :D 




Check out this NDTV gadget guru video : http://www.ndtv.com/video/player/news/why-srk-likes-the-new-nokia-smartphones/212484

Sunday, 25 September 2011

Tower of Hanoi

#include<stdio.h>
#include<string.h>

int tower(int n, char s, char t, char d)
{
   int x,y;
   if(n<=0) return 0;
   x= tower(n-1,s,d,t);
   printf("move %d from %c to %c\n",n,s,d);
   y=tower(n-1,t,s,d);
   return(x+y+1);
 }

void main()
{
   int n;
   printf("Enter the number of discs\n");
   scanf("%d",&n);
   n= tower(n,'S', 'T','D');
   printf("There are %d moves\n",n);
   getch();
}


This is the code from the text Tanenbaum..a slight modification has been done.


#include<stdio.h>

int tower(int n,char s,char d,char a)
{
 if(n==1)
 {
  printf("Move the disk %d from %c to %c\n",n,s,d);
  return;
 }
 tower(n-1,s,a,d);
 printf("Move the disk %d from %c to %c\n",n,s,d);
 tower(n-1,a,d,s);
}

main()
{
 int n;
 printf("Enter the number of discs:\n");
 scanf("%d",&n);
 printf("The moves are:\n");
 tower(n,'S','D','A');
}

Circular Queue using Arrays

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 5

void insert(int q[], int *f, int *r)
{
   if(*f == (*r+1)%SIZE)
      return;
   *r=(*r+1)%SIZE;
     printf("Enter the data\n");
     scanf("%d",&q[*r]);
     if(*f == -1)
        *f= 0;
}

void del(int *f, int *r)
{
   if(*f== -1)
      return;
   if(*f == *r)
     *f = *r = -1;
   else
     *f= (*f+1)% SIZE;
}

void display(int q[], int *f, int *r)
{
   int i;
   if(*f== -1)
       printf("circular queue is empty\n");
   else
   {
      for(i= *f; i!= *r; i=(i+1)%SIZE)
        printf("%d", q[i]);
      if(*f == (*r+1)%SIZE)
        printf("circular queue is full\n");
    }
}

void main()
{
  int ch, q[SIZE],f = -1,r= -1;
  while(1)
  {
    display(q,&f,&r);
    printf(" 1. Insert\n 2. Delete\n 3.quit\n");
    printf("Enter your choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
       case 1 : insert(q,&f,&r);break;
       case 2 : del(&f,&r);break;
       default : return;
     }
 }

This is my program which uses structures..a better way :)


#include<stdio.h>
#define size 3

struct queue
{
  int front,rear;
  int array[size];
}s;

int count=0,j;

int qfull()
{
  if(count == size)
    return 1;
  return 0;
}

int qempty()
{
 if(count == 0)
  return 1;
 return 0;
}

void insert_rear(int x)
{
 
   s.rear=(s.rear+1)%size;
   s.array[s.rear]=x;
   count++;
   return;
}


int del_front()
{
 int i;

 i=s.array[s.front];
 s.front=(s.front+1)%size;
 count--;
 printf("The element deleted is %d\n", i);

}

void display()
{
 int i,j=s.front;
 if(qempty())
  printf("QUEUE IS EMPTY\n");
 else
 {
   printf("The contents of the queue are:\n");
   for(i=0;i<count;i++)
    {
      printf("%d\n",s.array[j]);
      j=(j+1)%size;
    }
 }
}

main()
{
  s.rear= size-1;s.front=0;
  int ch,x,c;
  while(1)
  {
   printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
   printf("Enter your choice\n");
   scanf("%d",&ch);
   switch(ch)
   {
    case 1 : if(qfull())
             {
              printf("OVERFLOW IN QUEUE\n");
             }
             else
             {
              printf("Enter the element to be inserted\n");
              scanf("%d",&x);
              insert_rear(x);
             }
             break;
    case 2 :  if(qempty())
              {
               printf("UNDERFLOW\n");
              }
               else
             del_front();
             break;
    case 3 : display();
             break;
    default : return;
   }
  }
}


Infix to Postfix

Write a C pogram to convert an infix expression to postfix expression using the concept of stacks.


#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

#define SIZE 20

int pos;

struct stack
{
  int top;
  char items[SIZE];
};

void push(struct stack *s,char ch)
{
  if(s->top==(SIZE-1))
  return;
  s->items[++s->top]=ch;
}

char pop(struct stack *s)
{
  char y;
  if(s->top==-1)
  printf("Stack is empty\n");
  else
  {
     y=s->items[s->top];
     s->top--;
     return y;
    }
}

int prec(char stopn, char inopn)
{
  int ip=0, sp=0;
  switch(stopn)
  {
    case '+' :
    case '-'  :   sp=2 ; break;
    case '*' :
    case '/'  :   sp=4; break;
    case '$' :   sp=5; break;
    case '(' :    sp= -1; break;
  }

 switch (inopn)
 {
   case '+' :
   case '-' : ip=1;break;
   case '*' :
   case '/' : ip =3;break;
   case '$' : ip=6;break;
   case '(' : ip=9;break;
  case ')' : ip=0;break;
  default : ip=8; break;
 }

if(sp>ip) return 1;
else return 0;
} //end of int prec()

int empty(struct stack *s)
{
   if(s->top== -1)return 1;
   else return 0;
}

char stacktop(struct stack *s)
{
   if(empty(s))
   {
      printf("underflow in stack\n");
      return;
    }
   else
   return (s->items[s->top]);
}

void main()
{
  struct stack s;
  char infix[50], postfix[50],symb;
  int i,rank=0;
  s.top= -1;
  printf("Enter infix string\n");
  scanf("%s",infix);
  if(infix[strlen(infix)-1] == '*' || infix[strlen(infix)-1] == '+' || infix[strlen  (infix)-1]== '-' || infix[strlen(infix)-1] == '/')
  {
    printf("Invalid expression\n");
    return;
   }
  for(i=0,pos=0; ((symb=infix[i])!= '\0'); i++)
  {
     if(isalpha(symb))
     {
        postfix[pos++] = symb;
        rank++;
      }
     else
     {
        while(! empty(&s) && prec(stacktop(&s),symb))
        {
           postfix[pos++] = pop(&s);
           rank--;
           if(rank<1)
          {
           printf("Invalid expression\n");
           return ;
           }
        }//end of while
      if(symb != ')' || empty(&s))
      push(&s,symb);
      else
      pop(&s);
     }//end of else
   }//end of for loop
 
   while(!empty(&s))
    {
       postfix[pos++] = pop(&s);
       rank--;
       if(rank<1)
       printf("Invalid expression\n");
    }

    if(rank== 1)
    {
       postfix[pos] = '\0';
       printf("%s",postfix);
    }
     else
         printf("Invalid expression\n");
   
}//end of main

Wednesday, 21 September 2011

Matrix multiplication using two-dimensional arrays

To write a simple C program to read two matrices A(MxN) and B(PxQ) and compute the product A and B after checking compatibility for multiplication.Output the input matrices and the resultant matrix with suitable headings and format.

#include<stdio.h>
main()
{
  int a[100][100],b[100][100],c[100][100],m,n,p,q,i,j,k;
  printf("Enter the order of the matrix A and B\n");
  scanf("%d%d%d%d",&m,&n,&p,&q);
  if(n==p)
  {
    printf("Enter the elements of matrix A\n");
    for(i=0;i<m;i++)
    {
      for(j=0;j<n;j++)
      {
        scanf("%d",&a[i][j]);
      }
     }
     printf("Enter the elements of matrix B\n");
     for(i=0;i<p;i++)
     {
       for(j=0;j<q;j++)
       {
          scanf("%d",&b[i][j]);
       }
      }
     for(i=0;i<m;i++)
     {
       for(j=0;j<q;j++)
       {
          for(k=0;k<n;k++)
          {
             c[i][j] += a[i][k]+b[k][j];
           }
       }
      }
      printf("The original matrices are\n");
      for(i=0;i<m;i++)
      {
         for(j=0;j<n;j++)
         {
           printf("%d\t",a[i][j]);
          }
           printf("\n");
        }
        for(i=0;i<p;i++)
        {
           for(j=0;j<q;j++)
           {
             printf("%d\t",b[i][j]);
           }
            printf("\n");
         }
         printf("Resultant matrix is \n");
         for(i=0;i<m;i++)
         {
            for(j=0;j<q;j++)
            {
              printf("%d\t"c[i][j]);
             }
            printf("\n");
         }
     } //end of if statement
else
 printf("multiplication not possible since n is not equal to p\n");
}//end of program

Bubble sort in single dimension array

To write a C program to input N integers into a single dimension array and sort them in ascending order suing bubble sort technique.

Here's a diagramatic explanation :





C code :

#include<stdio.h>
main()
{
  int i,j,n,a[100],t;
  printf("Enter the size of the array\n");
  scanf("%d",&n);
  printf("Enter the elements of the array\n");
  for(i=0;i<n;i++)
   scanf("%d",&a[i]);
 printf("The unsorted array is :\n");
 for(i=0;i<n;i++)
  printf("%d\n",a[i]);

 for(i=0;i<n-1;i++) //Actual bubble sort begins
 {
   for(j=0;j<n-1-i;j++)
   {
      if(a[j]>a[j+1])
      {
         t=a[j];
         a[j]=a[j+1];
         a[j+1]=t;
       }
    }
 }
 printf("The sorted array is :\n");
 for(i=0;i<n;i++)
  printf("%d\n",a[i]);
}  

Binary Search in Single dimension array

This is for my juniors who are finding it difficult to code in labs because of horrible lab teachers+theory teachers :P I hope my juniors wont suffer like me!

 To write a C program to input N real integers in ascending order into single dimension array and to conduct a Binary Search for a given integer.

Here's how this algorithm works :




#include<stdio.h>
main(0
{
   int i,n,a[100],begin,end,mid,flag=-1,k;//Comparing with the above  pic, here min=begin,max=end
   printf("Enter the size of the array\n");
   scanf("%d",&n);
   printf("Enter the array in the increasing order\n");
   for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  printf("Enter the key number to be searched\n");
  scanf("%d",&k);
  begin=0;
  end=n-1;
  while(begin<=end)
  {
    mid=(begin+end)/2;
    if(k==a[mid])
    {
      flag=mid;
      break;
     }
   else if(k>a[mid])
     begin=mid+1;
   else
     end=mid-1;
  }
if(flag==-1)
 printf("FAILURE.Element not found in the array\n");
else
 printf("SUCCESS.The element is found in the array at location %d",flag);
}

Remember to enter the elements in ascending order! :)

Tuesday, 20 September 2011

TWO STACKS IN A SINGLE ARRAY IN C

Another important program which has been asked in many technical interviews is : WAP to implement two stacks in a single array so that either of the stacks become full only when the entire array is full.



Here's the C program :


#include<stdio.h>
#define SIZE 10
struct stack
{
 int top1,top2;
 int array[SIZE];
}s;

void push1(int x)
{
 if((s.top1)<(s.top2-1))
  s.array[++(s.top1)]=x;
 else
  printf("OVERFLOW IN STACK 1\n");
}

void push2(int x)
{
 if((s.top2)>(s.top1+1))
  s.array[--(s.top2)]=x;
 else
  printf("OVERFLOW IN STACK 2\n");
}

int pop1()
{
 if((s.top1)>=0 && (s.top1)<(s.top2))
  printf("The element popped out from stack1 is %d\n",s.array[(s.top1)--]);
 else
  printf("UNDERFLOW IN STACK 1\n");
}

int pop2()
{
 if((s.top2)<SIZE && (s.top2)>(s.top1))
  printf("The element popped out from stack 2 is %d\n",s.array[(s.top2)++]);
 else
  printf("UNDERFLOW IN STACK 2\n");
}

main()
{
 s.top1 = -1,s.top2 = 5;
 int ch,x;
 while(1)
 {
  printf("IMPLENTATION OF TWO STACKS IN SINGLE ARRAY\n");
  printf("Enter your choice of operation\n");
  printf("1.PUSH TO STACK 1\n2.PUSH TO STACK 2\n3.POP FROM STACK 1\n4.POP FROM STACK 2\n 5.EXIT\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: printf("Enter element to be pushed to stack 1\n");
          scanf("%d",&x);
          push1(x);
          break;
   case 2: printf("Enter element to be pushed to stack 2\n");
          scanf("%d",&x);
          push2(x);
          break;
   case 3: pop1();
          break;
   case 4: pop2();
          break;
   default: return;
  }
 }
}


IMPLEMENTATION OF STACKS USING ARRAYS IN C

Datastructures is an important subject in CS engineering.Implementation of stacks using arrays is a common concept.If you google "implementation of stack using arrays", you will find hundreds of programs explaining this. Most of the programs would have declared array globally, but finding the program which uses structures is difficult :P .So I thought of uploading my program.

Here's the program in C:


#include<stdio.h>
#define SIZE 100

struct stack
{
 int top;
 int array[SIZE];
}s;



void push(int x)
{
 if((s.top)==(SIZE-1))
 printf("STACK OVERFLOW\n");
 (s.array[++(s.top)])=x;
}

int pop(int x)
{
 if((s.top)==-1)
 printf("STACK UNDERFLOW\n");
 else
 printf("The element popped out of the stack is %d\n",s.array[(s.top)--]);
}

void display()
{
 int i;
 if((s.top)==-1)
  printf("STACK IS EMPTY\n");
 else
 {
  printf("The contents of the stack are:\n");
  for(i=s.top;i>=0;i--)
   printf("%d\n",s.array[i]);
 }
}

int main()
{
 int ch,x;
 s.top = -1;
 while(1)
 {
  printf("Enter your choice of operation\n");
  printf("1.PUSH\n2.POP\n3.DISPLAY THE STACK\n4.EXIT\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1: printf("Enter the element to be pushed\n");
             scanf("%d",&x);
             push(x);
             break;
   case 2: pop(x);
              break;
   case 3: display();
              break;
   default:return;
         
  }
 }
}

Sunday, 28 August 2011

Tablets On The Rise!

Today's ideas are tomorrow's technology.Ideas have never stopped and hence everyday we see hundreds of new technology flooding into the market.Now, its the time of TABLETS to rule the market, rather I can say iPads are ruling the market. The joke in the Silicon Valley is that there is no tablet market, only an iPad market.Yes..Yes..you heard it right.All thanks to Steve Jobs who revolutionized the world of PC's , music and phones by introducing iMac,Mac book pro,Mac book Air, iPods,iPhones and the latest with iPads.


Its a symbol of status to own an iPad these days!(just like the chocolate BOURNVILLE ad..."you dont buy a bournville, you earn it"...here a slight change though.."you dont buy an iPad, you EARN it). It is stylish+ sleek + shiny+efficient and most important of all..COSTLY(Rs.46,900)!!! A common student like me who has got a basic phone set and who recharges it with not less than 50 bucks per month obviously cannot afford it :P According to the quarter-yearly report of Apple Inc.,iPad sales grew 183 percent with total revenue of $28.57 billion!! I think the US government should borrow money from Apple to overcome its financial crisis...Obama, please think over this soon :P


There's an alternative tablet for iPad...that is Reliance 3G Tablet,which is just Rs.12999.Google-lovers would love it since it has Android OS and a long battery life(well atleast last longs for a day).After Steve Jobs stepped down from the CEO post, all rival companies are on their toes, ready to launch their Tablets.There is a TABLET WAR going on(just like Star Wars :P)This tablet effect is real and very much different from where the business was few years back when notebooks and netbooks were selling like hot dogs!


There are many Tablets released and yet to be released shortly..lets take a look at them.

1.Reliance 3G Tablet



Specifications : 

  • 3G Network
  • Android 2.3 Gingerbread OS
  • 800 MHz Processor
  • 7 inch Multi-touch LCD Display
  • 2 Megapixel Rear Camera
  • VGA Front camera for video calls
  • Media player
  • Video Recording/Playback
  • 512 MB RAM
  • Upto 32GB Expandable External memory (4GB Card included)
  • Wi-Fi
  • GPS with Maps
  • Facebook, Twitter Apps
  • Android Market
  • Document on the Go, Navigation apps, Mobile TV
  • Li-Ion battery
  • Weight: 389 grams 

 All this in just Rs.12,999.Way to go!


2.Dell's Inspiron Duo : Laptop-cum-Tablet 




    

Specifications :


  • Genuine Windows 7 Home Premium
  • 10.1” inch (1366×768 pixels) Touch Display Screen
  • Dual Core Intel Atom N550 Processor
  • 2GB DDR3 RAM
  • 250GB SATA Hard Disk Drive
  • Wireless 802.11n b/g/n
  • Bluetooth
  • Memory Card Reader
  • USB 2.0, Audio-Out Port
  • Gigabit Ethernet LAN
  • 1.3 Megapixel Webcam with Microphone
  • Battery Life up to 4 Hours
  • Duo Stage User Interface for Quick access
  • Innovative flip-hinge design, you can switch from touch to type in seconds
  • 10″ inch HD multi-touch display
  • Flash Support
Price in India is around 30k to 35k. This is surely in my wishlist!! :P

3.Samsung Galaxy Tab 10.1 

The Pulse reader app is one of the few tablet-optimized apps in the Android Market. It's a winner.  Not a fan of the plastic backing.

Specifications :




  • Operating System: Android v3.1 (Honeycomb)
  • Processor: 1GHz NVIDIA Tegra 2
  • OS Feature: Multi-tasking
  • Display Screen Size: 10.1 inches
  • Display Resolution: 1280 × 800 WXGA
  • Network band: Quad band EDGE/GPRS/HSPA +21
  • Processor Type: Dual Core
  • Camera Size: 3.0 mega pixels
  • Camera Feature: LED Flash , HD video recording
  • Front Camera Size: 2.0 Mega Pixels
  • Full HD Playback @ 30fps
  • Video Format Support: mpeg4, xvid, divX, wmv,h.264,h.263,VP8
  • Audio Format support: MP3, AAC, AAC+, eAAC+,WMA
  • Speakers Feature: Surround Sound
  • Internet Connectivity: 3G HSPA+ , up to 21 Mbit/s, WiFi 802.11 (a/b/g/n) (Dual band support 2.4GHz/5GHz)
  • OS Feature: Multi-tasking
  • Display Feature: Multi touch screen, 4 way rotations
  • Display Type: TFT LCD
  • Sensors : Gyroscope, Compass , accelerometer , Ambient light sensor
  • Android Market for apps downloads
  • Data Transfer Connectivity: Bluetooth v3.0, USB 2.0
  • Wi-Fi 802.11 b/g/n (2.4GHz,5GHz)
  • Audio Connector: 3.5 mm headphone Jack
  • Google Talk Video Chat, Google Maps, etc
  • Assisted GPS , turn-by-turn navigation with Google maps
  • Mobile Office (Polaris): PPT, Word, Excel docs creating/editing
  • Memory: 1GB RAM
  • Internal Storage memory: 16/32/64 GB
  • Battery Model: 7000mAh
  • Video Play Backup: up to 9 hours
  • Audio Play Backup: up to 72 hours
  • Dimensions: 256.7 x 175.3 x 8.6 MM
  • Weight : 565 gram
Price in India is around 36k to 37k.



4.Sony Tablet S1 and S2 :

     SONY S1

   


Specifications of S1 :


  • Android 3.0 Honeycomb Operating System
  • NVIDIA Tegra 2 Processor
  • 9.4 inch Display Screen with 1280 x 800 pixels resolution
  • 512MB RAM
  • 32GB Storage
  • Rear and Front Facing Camera
  • Custom UI
  • Remote Control for Sony AV Products
  • Swift Web Browser
  • Cross connectivity
  • Wi-Fi
  • 3G/4G
  • DLNA


SONY S2


Specifications of S2 : 

  • Android 3.0 Honeycomb Operating System
  • NVIDIA Tegra 2 Processor
  • 5.5 inch Dual Screen with 1024 x 480 pixels resolution
  • Rear and Front Facing Camera
  • Custom UI Book Style
  • eReader and Custom Email App
  • Wi-Fi
  • 3G/4G
Prices are yet to be officially announced.


5.HP TouchPad - WebOS..although its production has been stopped now.

HP Touchpad  HP TouchPad Webos tablet


Specifications :


  • Qualcomm Snapdragon dual-CPU APQ8060 1.2-GHz processor
  • 9.7-inch (1024×768 pixels) XGA capacitive, multitouch screen
  • 16/32GB Internal Memory
  • Front 1.3 Megapixel Camera Video Talk
  • Wi-Fi 802.11b/g/n
  • A-GPS with Navigation Maps (only in 3G Model)
  • Bluetooth v2.1+A2DP
  • 3.5 mm headphone jack
  • Stereo speakers with Beats Audio System
  • Email: Gmail push, Yahoo!, POP3, IMAP and Microsoft Exchange servers
  • Light sensor, accelerometer, compass and gyroscope
  • Rechargeable 6,300 mAh battery
  • Micro-USB port for Charging and PC Sync
  • Dimensions: 190 mm x 242 mm x 13.7 mm
  • Weight: 740gm
        But WebOS lacked thousands of apps and surely couldnt compete with Android.After only 48 days on the market, the CEO of HP, Leo Apotheker, announced that HP was abandoning it because of poor sales .

Well..these are the few tablets that's been launched and there are more to come! 

Gone are those days when people said laptops are handy and efficient. Dell has lost its PC's sales drastically this year.But now , I guess all the PC and laptop makers should get ready to face the drop in laptops sales too :( because the world is heading towards a better, smarter,all-in-one technology...that's ...TABLETS!!