Thursday 30 December 2010

stack using Linked List in c++

One disadvantage of using an array to implement a stack is the wasted space-most of the time most of the array is unused. A more elegant and economical implementation of a stack uses a linked list, which is a data structure that links together individual data objects as if they were ``links'' in a ``chain'' of data.

//stack using linked list
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
template<class T>
class stack;//forward declaration, bcoa wea re usig stack class in node class
template<class T>
class node
{
 T data; //data field contains int value
 node<T> *link;//link field contain address of a node(next node)
 friend class stack<T>;//we can access data and linke from the next class stack
};
template<class T>
class stack
{
 private:
 node<T> *top;//top is a reference(pointer) to a node(topmost node)
 public:
 stack();
  ~stack();

 int isempty();


 void push(T x);
 T pop() ;
 T topmost();
 void display();
};
template <class T>
stack<T>::stack()
{
 top=NULL; //top points to first
}
template<class T>
int stack<T>::isempty()
 {
  return(top==NULL);//return true when stack is empty and false otherwise
 }
template<class T>
void stack<T>:: display()
 {
  node<T> *p;;//p is a pointer of type node
  if(isempty())
   cout<<"Stack is empty\n";
  else
  {
   cout<<"stack is as follows\n";
 //disp each node of stack until stack is exasted
 for(p=top;p!=NULL;p=p->link)
   {
  //display data and linke fields of each node
    cout<<p->data<<"\t"<<p->link<<endl;
   }
  }
 }//end display()
template<class T>
void stack<T>::push(T x)
 {
  node<T> *p;
  p=new node<T>;//create a node, p is pointer to it
  p->data=x;//store x in new node data field
  p->link=top;//attach new node p and top most node
  top=p;//change top pointer to new node
 }
template<class T>
T stack<T>:: pop()
 {
  T x;
  node<T> *p;
  if(isempty())//if stack is empty, deletion is not possble
   return(-1);
  p=top;//p points to top most node
  top=top->link;//move top to next ode so that top most node is logically delted
  x=p->data;//copy top nost node data to x
  delete p;//delate top most node physically
  return(x);//data of the deleted node
 }
template<class T>
int stack<T>::topmost()
 {
 if(isempty())//if stack is empty return-1
  return(-1);
 else
  return(top->data);//data of the top most node
 }
template<class T>
stack<T>:: ~stack()
 {
  node <T> *p;
  while(!isempty())//execute loop until stack is empty
  {
    p=top;
    top=top->link;
    delete p;
    //pop();//instead of the 2 statements

  }
 }

void menu()
 {
  cout<<"1.insertion\n";
  cout<<"2.deletion\n";
  cout<<"3.display\n";
  cout<<"4.topmost value of the stack\n";
  cout<<"5.exit\n";
 }

void main()
 {
  stack <int>a;
  int ch,x;
  menu();
  cout<<"enter ur choice\n";
  cin>>ch;
  while(ch<5)
  {
   switch(ch)
   {
    case 1:{
          cout<<"enter value to be inserted\n";
          cin>>x;
          if(x==-1)
            cout<<"invalid\n";
                   
         else
             a.push(x);
              break;
          }
   case 2: {
  x=a.pop();//delete top most node frm the stack
        if(x==-1)
         cout<<"stack is empty, del. is not possble\n";
        else
          cout<<"deleted value is "<<x<<endl;
        break;
       }
  case 3: {
           a.display();//display stack in the reverse order
           break;
          }
  case 4:{
         x=a.topmost();
         if(x==-1)
           cout<<"stack is emtpy\n";
         else
  cout<<"top most value is "<<x<<endl;
         break;
        }
       }//switch
   menu();
   cout<<"enter ur choice\n";
   cin>>ch;
  }
         

  cout<<"enter your choice\n";

}


/*
E:\rajendra\java\ds>javac Linked.java

E:\rajendra\java\ds>java Linked
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
1
enter value to be inserted
10
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
enter ur choice
1
enter value to be inserted
20
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
enter ur choice
1
enter value to be inserted
30
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
enter ur choice
1
enter value to be inserted
40
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
enter ur choice
3
stack is as follows
40      node@1b90b39
30      node@18fe7c3
20      node@b8df17
10      NULL
1.insertion
2. deletion
3.display
4.topmost value of the stack
5.exit
enter ur choice
5
enter your choice

E:\rajendra\java\ds>




*/

Saturday 27 November 2010

wap to read an 2d array and display

1) wap to read an 2d array and display */

#include<conio.h>
#include<stdio.h>
void main()
{
 int a[2][3];
 int i,j;
 printf("enter the elements in 2d array\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   scanf("%d",&a[i][j]);
  }
 }
 printf("2d array is\n");
 for(i=0;i<=2-1;i++)
 {
  for(j=0;j<=3-1;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
 }
 getch();
}

Arrays
c program examples

Sunday 10 October 2010

Disadvantages of separate chaining


Disadvantages of separate chaining:
Ø  It requires the implementation of separate data structures for chains and a code to manage it.
Ø  The main cost if chaining is the extra space required for the linked list.
Ø  For some languages creating new notes (for linked list) is expensive and slows down the system



Separate chaining

Advantages of separate chaining


Advantages   of   separate  chaining:
      Separate  chaining  has  several  advantages  over   open  addressing:
Ø  Collision  resolution   is  simple   and   efficient.
Ø  The    hash   table   can   hold  more  element  without   the  larger  performance  deterioration  of open  addressing
Ø  The  performance of chaining declines much more slowly than open addressing.
Ø  Deletion is easy-no special flag values are necessary.
Ø  Table size need not be a prime number.
Ø  The keys of the object to be hatched need not be unique.

Disadvantages of separate chaining:
Ø  It requires the implementation of separate data structures for chains and a code to manage it.
Ø  The main cost if chaining is the extra space required for the linked list.
Ø  For some languages creating new notes (for linked list) is expensive and slows down the system



Separate chaining

QUESTIONS on Dictionaries


QUESTIONS:

1.(a)Define  HashingHash Function  ,Collisions.
    (b)Explain  the different methods  that  are  used to  calculate   Hash Function.
2.Define  Collisions. Explain  the  different techniques  that  are  used  to  resolve  collsion.
3.What  is  Dictionaries  ,state  different  types of dictionaries ,operations  and  ADT(Operations  on   dictionary).
4.With  an  example  explain different types of  Hash Function  .
5.Perform  the  insertion operations   using  double  hashing  for  the  following  list:
      12,54,62,45,37,78,89,28,61,49.Where table size is 10.
6.(a)List  any  four to  five  advantages  and disadvantages of   separate  chaining.
   (b)Explain open  addressing  and  write  the  algorithm  to  insert  an  item  into  the  table.
7.(a)Explain  with  an example  about  linear  probing  ,quadratic  probing.
   (b)Provide  draw backs  of  separate chaining .
8.(a)Write  the  characteristics of  good  hashing   function.
   (b)Explain  separate  chaining  and  its  operations.
9.(a)Write  an   algorithm  of  insertion  and search on  double  hashing .
   (b)Applications  of  hash  tables.
FILL IN  THE  BLANKS:
1.Dictionaries  is  a  collection  of  pair of  keys  and  values.
2.Dictionaries  are  divided into  Ordered Dictionary  and  Unordered  Dictionary.
3.Accessing  randomly  pairs  of  keys and  values is  called   Unordered  Dictionary.
4.Accesssing   sequentially  pair  of  keys  and  values  is  called  Ordered Dictionary.
5.Operations  on   dictionary  are   insertion ,deletion and  searching.
6.The   Dictionaries  can  be   represented  as  a linear  list.
7.Two  methods   for   Linear  List  Representation  is  sorted  array  and   sorted  chain.
8.An   array   Data structures   is  used  to  implement  the  Dictionaries  is  called  as  sorted  array.
9.An   Linked list  data  structure   is  used  to  implement  the  Dictionaries  is  called   as  sorted  chain.
10.The  integer  returned  by  the  Hash Function  is  called  as   hash  key.
11.Simply  Hash Function is  calculated as  H(key)=key% tablesize
12.In   the  multiplicative  Hash Function  ,the   scientist  Donald  Knuth  use  constant  ‘A’, value  of  ‘A’ is 0.61803398987.
13.Ideally  if  no  of  Collisions  occurs   then  such  a  function  is called  perfect Hash Function.
15.The  Separate chaining  is  also  known  as  open hashing.
16.In   the  Open addressing  the  location   of the  hash  table  termed  as bucket.
17.The  function   of  Linear probing  is  h(x)=(h(x)+i)modn
18.One  major  problem  with  Linear probing  os  primary  clustering.
19.The    functions   of  Quadratic probing  is   h(x)=(h(x)+i2)modn.
20.The   function   of  double  hashing   is  H1(key)=key mod  table size  and   H2(key)=M-(key mod m).

CHOOSE  THE  CORRECT  ANSWERS:           
1. Mid  Square  method  is  calculated  by   [b]
(a)H(key)=key% table size
(b)(key)2=Result (mid part of result)
(c)H(key)=floor(p*(fractional part of key*A))
(d)H(key)=key/ table size
2.Division  Method  is  calculated  by [a]
(a)H(key)=key% table size
(b)H(key)=key/table size
(c)(key)2=Result (mid part of result)
(d)H(key)=floor(p*(fractional part of key*A))
3.Multiplicative   Hash   Functon  is  calculated by [d]
(a)H(key)=key% table size
(b)H(key)=key/table size
(c)(key)2=Result(mid part  of result)
(d)H(key)=floor(p*(fractional part  of key *A))
4.Find   Extraction  hash  function of   a  given  number   5798643  [a]
(a)5798
(b)14355
(c)99812
(d)none
  5.When  a  Hash Function  maps  two  different   keys  to  same  location  then  [b]
(a)overflow occurs
(b)Collisions occurs
(c)insertion occurs
(d)deletion occurs
6.When  the  hash  table  is  filled  if  there is a situation  to insert  a new  pair  in  the  same  hash  table then  [a]
(a)overflow occurs
(b)Collisions occurs
(c)insertion occurs
(d)both b&c
7.The  best  time  complexity  of  search  operation  is  [c]
(a)O(n)
(b)O(n log n)
(c)O(1)
(d)O(log n)
8The  worst  time  complexity  of  insertion operation  is [a]
(a)O(n)
(b)O(n log n)
(c)O(1)
(d)O(log n)
TRUE  (or)  FALSE:
1.Is  Collisions   occurs  for  the  given  values   23,13,21,14  and  the  given  table size  is  7 [T].
2.Is  the  digit  folding  is  a  Collision resolution techniques   [F].
3.Is  the  Extraction  is  a  Hash Function  [T].
MATCH  THE  FOLLOWING:
1.Hash Function                                    [a]          (a)h(x)=(h(x)+i)mod n
2 Mid  Square  method                             [b]          (b)h(x)=(h(x)+i2)mod n
3Multiplicative   Hash   Functon            [l]          (c)Collisions
 4 Linear probing                                   [a]         (d)pair(key,values)
5.Quadratic probing                              [b]         (e)table size  is  resolved                          
6.Rehashing                                            [e]        (f)O(1)
7.16,18,9,36  when  table  size  is          [c]         (g)table size  need not  be  prime  number. 
10  by   Division  Method
8.Advantages  of  separate  chaining     [g]        (h)O(n)
9.Best  time  complexity                        [f]         (i)(key)2=result(mid part of result)
10. Dictionaries                                         [d]        (j)H(key)=key%table size
                                                                            (k)pair(rows,columns)
                                                                            (l)H(key)=floor(p*(fractional part of  key  *A))


                                                      
         
      Dictionaries

Rehashing



Rehashing:  


        Rehashing   is  a technique  in which  the   table  size  is  resized  i.e  the  size  of  table  is  doubled  by  creating  a  new  table  .It  is  preferable   if  the  total   size   of  table  is  a  prime  number.
There  are  situations in which  rehashing  is  required:    
Ø  When  table  is  completely  full.
Ø  With  quadratic  probing  when  the   table   is  filled  half
Ø  When  insertion  fail  due  to  overflow.
In  such  situations  we  have   transfer  entries  from  the  old  table  to  the new  table    by  recombining   their  positions  using  suitable  hash  functions.
Example:
  Consider  the  elements  to  be  inserted  37,90,55,22,12,49  87.The  table  size  is  10.We  use  hash  function  H(key)=key  mod size
·         Insert  37,90,55,22,17  and 49  using    hash  function
     H(key)=key mod size
     H(37)=37%10=7
     H(90)=90%10=0
     H(55)=55%10=5
     H(22)=22%10=2
    H(17)=17%10=7(collision  solved by  linear probing)
    H(49) =49%10=9
     [0]         [1]          [2]         [3]         [4]          [5]         [6]          [7]           [8]       [9]
90

22


55

37
17
49

·         While   inserting  87  collision  occurs .By linear  probing  we  cant  insert this  element  in the  hash  table  because   this  table  is  almost full  and  if  we  try  to  insret  more  elements  collision  will  occur   and  eventually  further  will  fail .Hence  we  will  rehash  by  doubling  the table  size.The old  table  is10  then  we  should  double    this  size   for  the  new  table. It becomes 20 .But  20  is  not  a  prime  number we  will prefer  make  the  table  size  as 23 .And  the  new  hash function will be
         
          H(key)=key  mod 23
          H(37)=37%23=14
          H(90)=90%23=21
          H(55)=55%23=9
          H(22)=22%23=22
          H(17)=17%23=17(collision solved  by  linear probing)
          H(49)=49%23=3
          H(87)=87%23=18
·         Now   the   hash  table  is  sufficiently   large  to   accommodates  new  insertion
               [0] [1] [2] [3]   [4] [5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22]



49





55




37


17
87


90
22

Advantages:
Ø  This  technique  provides   the  programmer  a  flexibility  to  enlarge the  table  size  if  required.
Ø  Only  the  space  gets  doubled  with  simple   hash function which  avoids  the  occurrence  of  collision.