Wednesday, December 5, 2012

Program covering basics of linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<iostream.h>
struct node
 {   int num;
      struct node *link;
 };
node *start , *save , *newptr , *ptr;
node * create_node ( int n )
{
      ptr  = ( node * )malloc(sizeof(struct node));
      ptr -> num = n;
      ptr -> link = NULL;
      return ptr;
}
void insert_node ( node *np , int n )
{
     node *tmp;
     if( start == NULL )
          start = np;
     else
     {    tmp = start;
          while (  tmp != NULL)
          { if ( tmp -> num <= n && ( tmp -> link -> num > n || tmp -> link == NULL))
                { np -> link = tmp -> link;
                   tmp -> link = np;
                   return;
                 }
            tmp = tmp -> link;
           }
      }
}
void del_beg()
{
         if ( start == NULL)
             printf("\nUnderflow");
        else
       {
             ptr = start;
             start = start -> link;
             free(ptr);
       }
}
void del (int inf)
{           ptr = start;
             node *tmp;
             while( ptr != NULL )
            {
                     if( ptr-> num == inf)
                    {        if ( ptr == start )
                              {    start = ptr ->link;
                                    free(ptr);
                                     return;
                              }
                             else
                             {      tmp = ptr;
                                     ptr = ptr->link;
                                     free(tmp);
                                     return;
                             }
                     }
                  else
                        ptr = ptr -> link;
              }
}
  void display ( node *np )
{
                 while (np != NULL )
                {     printf("%d" , np -> num );
                       printf("->>");
                       np = np -> link;
                 }
}
void main()
{
       clrscr();
       start = NULL ;
       int inf , ch1;
       char ch ;
       do
       {     printf("\n1.Insertion");
              printf("\n2.Deletion from begining");
              printf("\n3.Display");
              printf("\n4.Deletion of your choice");
              printf("\nEnter your choice:\t");
              scanf("%d" , &ch1 );
              switch(ch1)
             {    case 1:
                   printf("\nEnter the information of the new node:\t");
                   scanf("%d" , &inf );
                   printf("\nCreating new node!!! press Enter to continue");
                   getch();
                   newptr = create_node( inf ) ;
                   insert_node( newptr , inf );
                   display(start);
                   break;
                   case 2:
                   del_beg();
                    break;
                    case 3:
                    display(start);
                     break;
                    case 4:
                     printf("\nEnter the node to be delted:\t");
                     scanf( "%d" , inf );
                     del(inf);
                     break;
             }
              printf("\nDo you want to continue:\t");
              cin>>ch;
       }while ( ch == 'y' || ch == 'Y' );
       getch();
}

Structure solution


0Ans 10.3   Given is the structure declaration
                struct abc
                {              int x;
                                float y;
                                char z;
                }
                Following are the statements which declare and initialize the structure variable :
struct  a , b , c ; // Error The structure variable will not be declared as tag name abc  is         missing             
<!--[if !supportLists]-->II.                  <!--[endif]-->struct abc a , b , c ; //Correct Declaration
<!--[if !supportLists]-->III.                <!--[endif]-->abc  x , y , z ;   //Error the structure variable will not be declared as struct keyword is missing
<!--[if !supportLists]-->IV.                <!--[endif]-->struct abc a[] ;  //Error Array size is  missing
<!--[if !supportLists]-->V.                  <!--[endif]-->struct abc a = { }; // a is assigned a value which is never used
<!--[if !supportLists]-->VI.                <!--[endif]-->struct abc = b , { 1+2 , 3.0 , “xyz”}  //Error syntactically wrong
<!--[if !supportLists]-->VII.              <!--[endif]-->struct abc c = { 4 , 5 , 6 } //Error last member is a character it should be enclosed in single quotes
<!--[if !supportLists]-->VIII.            <!--[endif]-->struct abc a = 4 , 5.0 , “xyz” ; //Error Initialization values should be enclosed in curly braces
Ans 10.4   None of the given statements in the question  is  legal
Ans 10.5  For The given declaration
                struct item_bank
                {              int number;
                                Double cost;
                }
                Following are the correct statements
                (d)          struct item_bank items[10];
Ans 10.6   For the given declaration
                typedef struct abc
                {              char x;
                                int y ;
                                float z[10] ;
                }    ABC ;

Following declarations are invalid
<!--[if !supportLists]-->(a)    <!--[endif]-->struct ABC v3;
Ans 10.8 Purpose of the following is:
<!--[if !supportLists]-->a.       <!--[endif]-->Template
<!--[if !supportLists]-->b.      <!--[endif]-->struct keyword : is used declare , define and initialize a structure variable
<!--[if !supportLists]-->c.       <!--[endif]-->typedef keyword  : is used to declare the alias name for the existing datatype
<!--[if !supportLists]-->d.      <!--[endif]-->sizeof operator :  is used to get the size of the variable which is passed as parameter to the function
<!--[if !supportLists]-->e.      <!--[endif]-->tag name : it’s the name given to a structure defined by us using struct keyword
Ans 10.9  struct                                              // Tag name for structure is missing
                {              int number ;
                                float price ;
                }                                                              //; is missing
Ans 10.10 Use of the following is as given below:
<!--[if !supportLists]-->a.       <!--[endif]-->union -  The concept of union has been partially borrowed from the concept of structure the major difference being the members of union share same memory location whereas the members of structure share different memory location. We use union when we want a group of variables which we are using at different time but want them under a common data type we use union
<!--[if !supportLists]-->b.      <!--[endif]-->Bit fields – There are several instances when data requires much less than 16 bits space. For this we use bit fields which is a set of adjacent bits whose size can be from 1 to 16 bits in length
<!--[if !supportLists]-->c.       <!--[endif]-->The size of operator – this operator is used for determining the size occupied by  the variable in the memory which is provided as operand to the size of operator.
Ans 10.12  For th following declarations and definition
                struct abc
                {              int a;
                                float b ;
                };
                struct xyz
                {              int x;
                                float y; 
                };
                abc         a1  , a2;
                xyz         x1 , x2;
a1 = x1;    //Error two structure variables cannot be copied
<!--[if !supportLists]-->b.      <!--[endif]-->abc.a1 = 10.75; //Error structure variable cannot be initialized in this way
<!--[if !supportLists]-->c.       <!--[endif]-->int m  = a + x ; //Error structure members cannot be accessed in this way
<!--[if !supportLists]-->d.      <!--[endif]-->int n = x1.x + 10 // Correct
<!--[if !supportLists]-->e.      <!--[endif]-->a1 = a2 //Error two structure variables can be  copied only member wise not directly
<!--[if !supportLists]-->f.        <!--[endif]-->if (a.a1 > x.x1)   // Error the member should come after the variable name.
<!--[if !supportLists]-->g.       <!--[endif]-->if( a1.a < x1.x)   //Correct
<!--[if !supportLists]-->h.      <!--[endif]-->if (x1 != x2 )    //Error two structure variables cannot be compared directly




Ans 10.15   Computer stores structures using the concept of “ word boundary”. A  word is the natural/native amount of data fetched frommemory on a single access by a CPU. Now storing the data at the boundary of the word the data can be fetched very fast. The size of the word boundary is machine dependent. Now suppose  that the word boundary of the given machine is 4 byte. Now a structure having three data members char ,  int  ,  float will be stored in the memory as shown below . Now we can easily understand that the empty space will be there  this empty byte in between the two data members is termed as slack byte.






Ans 10.16 The various points to be considered while implementing bit field are as follows:
          i. We cannot take address of a bit field variable. Therefore we cannot use scanf function for reading     them and neither we can use pointer variable for such type of structure.
ii.     Bit fields cannot be arrayed.
iii.    Bit fields should be assigned values that are within the range of the of their size.
Ans 10.19 
typedef struct product
                {              char name[10];
                                float price ;
                }              PRODUCT products[10] ;                               // Error Immediately after the alias name variable
                                                                                                                cannot be declared
//Correct mode
typedef struct product
                {              char name[10] ;
                                float price;
                }              PRODUCT;
PRODUCT products[10];



Ans 10.20
Output:
8                                                                              Explanation union will occupy the space according to largest member of the same
10                                                                           At a time only one member can use the memory location of the union
1.23                                                                        Same reason as above

Solution to Programing Exercise pointers

                                                    Solution to Programming Exercise Pointers
Guys i think the starting two programs  are quite easy and thus I am not providing the solutions to them
Ans 11.3

#include<stdio.h>
#include<conio.h>
int size;
void main()
{
clrscr();
void insert( int *ptr , int val);
int arr[10] , in , i ;
printf("\nEnter the size of the array:\t");
scanf("%d" , &size);
printf("\nEnter your sorted array:\t");
for( i = 0 ; i <  size ;  i++)
scanf("%d", &arr[i]);
printf("\nEnter the number you want to insert in the array:\t");
scanf("%d", &in);
insert(arr, in);
printf("\nThe array after insertion is:\t");
for ( i = 0 ; i < size ; i++ )
printf("%d " , arr[i] ) ;
getch();
}

void insert( int  *ptr , int val)
{ int pos , i ;
if ( val < *(ptr+0) )
pos = 0 ;
else
{ for( i = 0 ; i < size - 1 ; i++ )
{ if ( *(ptr+i) <= val && val < *(ptr+i+1) )
{ pos = i+1;
break;
}
}
if ( i == size-1 )
pos = size;
}
if ( size == 10)
printf("\nOverflow!");
for ( i = size ; i > pos ; i-- )
*(ptr + i) = *(ptr + (i - 1) ) ;
*(ptr + pos ) = val;
size++;
}

Ans 11.4

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int * add ( int *a[] , int *b[] );
int i , j , p[3][3] , q[3][3] , r[3][3];
printf("\nEnter a 3*3 Matrix:\n");
for( i = 0 ; i < 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
scanf("%d", p[i][j] ) ;
printf("\nEnter the second 3*3 Matrix;\n");
for( i =0 ; i < 3 ; i++)
for( j = 0 ; j < 3 ; j++ )
scanf( " %d " , q[i][j]  );
r = add( p , q );
printf("\nThe sum is:\t");
for ( i =0 ; i< 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
scanf( "%d" , r[i][j] );
getch();
}

int * add ( int *a[3] ,int *b[3] )
{
int  i , j , c[3][3] ;
for( i = 0 ; i < 3 ; i++ )
for( j = 0 ; j < 3 ; j++ )
c[i][j] = *( *(a + i ) + j )  + *( *(b + i ) + j );
       return c;
}

Ans 11.5

#include<stdio.h>
#include<conio.h>
void del ( char *ptr , char itm)
{
int i , pos , size;
for ( i = 0 , size = 0 ; * (ptr + i) != '\0' ; i++ , size++)
{
if( *(ptr + i ) == itm)
pos = i ;
}
for( i = pos ; i < size ; i++ )
*(ptr +i) = *(ptr+i+1);
*(ptr+size) = '\0';

}
void main()
{
clrscr();
char str[10] ,ch;
printf("\nEnter your string:\t");
gets(str);
printf("\nEnter the charcter:\t");
scanf("%c" , &ch);
del(str, ch ) ;
printf("\nYour string after deletion:\t%s", str);
getch();
}

Ans 11.6
#include<stdio.h>
#include<conio.h>
#include<iostream.h>

void main()
{
char * day_name( int no);
clrscr();
int n;
char * day;
printf("\nEnter the number of the day:\t");
scanf("%d" , &n ) ;
day = day_name( n );
printf("\nThe day is:\t%s" , day);
getch();
}
char * day_name ( int no )
{
static char * week_day[7] = { "Monday" , "Tuesday" , "Wednesday", "Thursday" , "Friday" , "Saturday", "Sunday"};
int i , pos = 0;
for ( i = 0 ; i < 7 ; i++ )
{
if( ( i+1 ) == no )
{ pos = i;
break ;
}
}
return week_day[pos];
}

Ans 11.7
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *name[5] ;
int i ;
void sort ( char  *str[] );
printf("\nEnter any five names:\t");
for ( i = 0 ; i < 5 ; i++ )
gets(name[i]);
sort ( name ) ;
printf("\nThe names in the alphabetical order are:\n");
for ( i = 4 ; i >= 0 ; i-- )
puts(name[i]);
getch();
}

void sort ( char *str[5] )
{
int i , j ;
char *tmp ;
for ( i = 0 ; i < 5 ; i++ )
{ for ( j = 0 ; j < (5-1) - i ; j++ )
{ if ( strcmp( str[i+1] , str[i] )  > 0 )
{ tmp = str[i];
str[i] = str[i+1];
str[i+1] = tmp;
}
}
}
}

Ans 11.9#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int a[5];
void reverse( int *p ) ;
printf("\nEnter the array:\t");
for (  i = 0 ; i <  5 ; i ++ )
sacnf("%d" , a[i] ) ;
reverse(a);
printf("\nThe reversed array is:\t");
for ( i = 0 i < 5 ; i++)
printf("%d" , a[i] );
getch();

}

void reverse ( int * p )
{
int i , tmp ;
for ( i = 0 , j = 4  ; i < 5/2 ; i++ ,  j-- )
{ tmp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = tmp;
}
}

Ans 11.10
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int a[5] , b[5] , i , res;
int cmp(int *p , int *q );
printf("\nEnter the array:\t");
for ( i = 0 ; i < 5 ; i++ )
scanf("%d" , &a[i] ) ;
printf("\nEnter the second array:\t");
for ( i = 0 ; i < 5 ; i++ )
scanf( "%d" , &b[i]);
if ( cmp( a , b) == 1)
printf("\nThey are identical");
else
printf("\nThey are not identical");
getch();
}

int cmp ( int *p , int *q)
{
int  i , flag = 1;
for ( i = 0 ; i < 5 ; i++ )
{ if( *(p+i) != *(q+i) )
{ flag = 0;
}
}
return flag;
}