Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Wednesday, 1 April 2015

C programme to check if a linked list is circular

// c program to check if a linked list is circular

struct Node {
    int data ;
    struct Node* next ;

};

typedef Node* Node ;

int isCircularLinkedList(Node head)
{
     Node slowPtr = fastPtr = head ;
    
     while (slowPtr && fastPtr )
     {
        slowPtr = slowPtr->next ;
        fastPtr = fastPtr -> next;
       
        if( ! fastPtr )
        {
            return 0 ;
        }
        else {
            fastPtr = fastPtr - > next ;
        }
       
        if( slowPtr = fastPtr )
            return 1 ;
       
     }
    
     return 0 ;
}

C : find middle element using two pointer technique

// c code to find middle element using two pointer technique

struct Node {
    int data ;
    struct Node* next ;

};

typedef Node* Node ;

Node middleNodeOfLinkedList(Node head)
{
    Node slowPtr = fastPtr = head ;
   
    if(head->next != NULL )
    {
        fastPtr = fastPtr->next->next;
    }
   
    while (fastPtr != NULL )
    {
        // check for more nodes  at the node pointed by  fastPtr
        if(fastPtr -> next != NULL)
        {
            //advance fastPtr
            fastPtr = fastPtr->next->next ;
        }
        else {
            return slowPtr;
        }
       
        //advance slow ptr
        slowPtr = slowPtr->next ;
       
   }
  
   return slowPtr;
  
  
}

C Reverse a linked List

// C programme to reverse a linked list

strcut  Node {
    int data ;
    struct Node* next ;
} ;

typedef struct Node* Node ;


//function to reverse a linked list

Node reverseLinkedList (Node head)
{
    Node ptr = NULL ;
    Node newHead  = NULL ;
   
    while ( head != NULL )
    {
        //save head
        ptr = head;
       
        //advance head
        head = head->next;
       
        //advance the value at newHead
        newHead->next = newHead ;
       
        //add the saved node at newHead of list
        newhead = ptr ;
    }
}

Tuesday, 31 March 2015

Operators C tid bits


1 :
    precedence of comparison operators (<=, >= and ==) is higher than assignment operator =. The result of a comparison operator is either 0 or 1 based on the comparison result

    2 :
     int i = 1, 2, 3;
     printf("%d", i);
     Compile time error : Comma acts as a separator here. The compiler creates an integer variable and initializes it with 1. The compiler fails to create integer variable 2 because 2 is not a valid identifier.
   
     3:
     int i = (1, 2, 3);
     printf("%d", i);
     Output : 3
     The bracket operator has higher precedence than assignment operator. The expression within bracket operator is evaluated from left to right but it is always the result of the last expression which gets assigned.
   
     4:
      int i;
    i = 1, 2, 3;
    printf("%d", i);  Output : 1
    Comma acts as an operator. The assignment operator has higher precedence than comma operator. So, the expression is considered as (i = 1), 2, 3 and 1 gets assigned to variable i.
  
    5 The control in the logical OR goes to the second expression only if the first expression results in FALSE
    6: . An expression doesn't get evaluated inside sizeof operator.
  
    7 :
        This is an another way to convert an alphabet to upper case by resetting its bit positioned at value 32 i.e. 5th bit from the LSB(assuming LSB bit at position 0).
      
        eg : a &= ~32 ; Outputs A  ;
    8:
        We can swap two variables without any extra variable using bitwise XOR operator '^'. Let X and Y be two variables to be swapped. Following steps swap X and Y.

          X = X ^ Y;
          Y = X ^ Y;
          X = X ^ Y ;
           
   

Monday, 30 March 2015

Tid-Bits C I/O



1 :
     char array[100];
    printf("%d" , scanf("%s" , arr));
    //input  : Jimmmy
    prints 1  as scan returns number of inputs successfully read
 2: 
    printf(" \"JIMMY %% N  %% JOLLY\"");
   
    output : "JIMMY % N % JOLLY"
   
    (\\\) backslash are used for escaping double quotes
   
 3: prototype of printf() in C :
              int printf(const char *format, ...);
 4:

    The stdout stream is buffered, so will only display what’s in the buffer after it reaches a newline (or when it’s told to)
    eg :
    printf(“\%\n”); prints : %
   
   
  5 :
    Use of backspace \b  and return  \r escape characters is terminal dependent :
        http://stackoverflow.com/questions/17236242/usage-of-b-and-r-in-c
       
   6:
        int main()
            {
               printf(1 + "Jjim");
               return 0;
            }
           
            prints : jim
          
           printf()  prototype as int printf( const char *format , ... ) ; The compiler adds 1 to base address of "Jjim" and sends "jim" as an arguemnts to
           printf() function defined in stdio.h header file .
    7:
            int main()
                {
                    printf("%c ", 5["jimmyjolly"]);
                    return 0;
                }
               
              prints : 'j'
       
          Compiler assumes   5["jimmyjolly"] as *(5 + "jimmyjolly") Adding 5 to base address of the string increments the pointer to point 'j'
                             Applying value-of opertaor gives 'j' as final result
            Same logic for  "jimmyjolly"[5] where  output is j
     
     
      8 :
             gets() can read a string with spaces but a normal scanf() with %s can not.
             gets() doesn't do any array bound testing and should not be used
             use fgets() instead :
                char *fgets(char *s, int n, FILE *stream);
                fgets read at most n-1 characters into the array s, stopping if a newline is encountered, the newline is included in the array, which is terminated by ‘\0'.
                fgets returns s, or NULL if end of file or error occurs.
     9 :
            #include <stdio.h>

            int main()
            {
                char str[50] = {0};
                scanf("%4s", str);
                printf(str);
                getchar();
                return 0;
            }
           
            input :  jimmy
            ouput : jimmy
           statment  scanf("%4s" , str ) ;  Read maximum 4 characters from console where str is a char array variable eg. char str[50];
          
          
        10 :
           
            printf(“%.*s”, n, s); in this statement value of pointer will move till nth character of s , printing n characters

            .* means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
        11 :
             printf function returns the number of characters successfully printed on the screen.
         12 :
            The return type of getchar() is int to accommodate EOF which indicates failure
          13 :
             for a CPU with memory mapped I/O ,  I/O protection is ensured by operating system routine (s)