Saturday, September 1, 2018

Queue : Array Implementation in C

#include <stdio.h>
#define QUEUE_LENGTH 50
struct queue
{
    int element[QUEUE_LENGTH];
    int front,rear,choice,x,y;
};
void traverse(struct queue q)
 {  printf("The elements of the queue are\n");
     for(int i=0;i<=q.rear;i++)
     {
         printf("%d\n",q.element[i]);
     }
 }
void add(struct queue *p,int y)
{
    if((*p).rear<QUEUE_LENGTH)
    {   (*p).rear +=1;
        (*p).element[(*p).rear]=y;
        printf("rear =  %d\n",(*p).rear);
        printf("type -1 to exit\n");
        scanf("%d",&y);
        if(y==(-1))
        {
            return;
        }
        add(p,y);
    }
    else
    {
        printf("Queue overflow\n");
    }
}
void delete(struct queue *p)
{
    int x=0,y=0;
    if((*p).front>(*p).rear)
    {
        printf("Queue empty\n");
    }
    else
    {
        x=(*p).element[(*p).front];
        (*p).element[(*p).front]=0;
        (*p).front=(*p).front+ 1;
    }
    printf("type 1 to delete or 2 to exit\n");
        scanf("%d",&y);
        if(y==(2))
        {
            return ;
        }
        if(y==(1))
        {
            traverse(*p);
            printf("%d is deleted\n",(*p).element[(*p).front]-1);
            delete(p);

        }
}
int main()
{   int choice=0,x=0;
    struct queue q;
    q.rear= -1,q.front=0;
        printf("\nEnter element to be added:\n");
        scanf("%d",&x);
        add(&q,x);
        traverse(q);
        delete(&q);
        printf("Deletion completed \n");
    return 0;
}

Output:

  1. $gcc -o main *.c
  2. $main
  3.  
  4. Enter element to be added:
  5. 1
  6. rear = 0
  7. type -1 to exit
  8. 2
  9. rear = 1
  10. type -1 to exit
  11. 3
  12. rear = 2
  13. type -1 to exit
  14. 4
  15. rear = 3
  16. type -1 to exit
  17. 5
  18. rear = 4
  19. type -1 to exit
  20. 6
  21. rear = 5
  22. type -1 to exit
  23. 7
  24. rear = 6
  25. type -1 to exit
  26. -1
  27. The elements of the queue are
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. type 1 to delete or 2 to exit
  36. The elements of the queue are
  37. 0
  38. 2
  39. 3
  40. 4
  41. 5
  42. 6
  43. 7
  44. 1 is deleted
  45. type 1 to delete or 2 to exit
  46. The elements of the queue are
  47. 0
  48. 0
  49. 3
  50. 4
  51. 5
  52. 6
  53. 7
  54. 2 is deleted
  55. type 1 to delete or 2 to exit
  56. The elements of the queue are
  57. 0
  58. 0
  59. 0
  60. 4
  61. 5
  62. 6
  63. 7
  64. 3 is deleted
  65. type 1 to delete or 2 to exit
  66. Deletion completed
 
 

Friday, August 31, 2018

Java Tutorial

C++ : ' string ' Data type

#include<iostream>
#include<string>
using namespace std;
int main(){
string arr[3][3] = {"Blue", "Red", "Orange", "night","ok","that","is","good","bye"};

for(int i=0;i<3;i++)
{

    for(int j=0;j<3;j++)
    {

        cout<< arr[i][j]<<" "<<i<<" "<<j<<endl;
       
    }

}
return 1;
}


Sunday, August 26, 2018

Flow of values of pointers in a Singly Linked-list as new nodes get added from front

#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node * next;
};
int insertNode(struct Node** head,int value){
    printf("Insert Node %d\n",value);
    struct Node* newNodePtr = (struct Node*)malloc(sizeof(struct Node));
    newNodePtr->value=value;
    newNodePtr->next=*head; // Now the 'next' of the NEW NODE holds what head held originally = NULL
    printf(" address in newNodePtr = %d = address OF the NEW NODE\n",newNodePtr);
    printf(" address in newNodePtr->next = %d \n",newNodePtr->next);
    printf(" address in *head = %d \n",*head);
    printf(" address in head = %d \n",head);
    *head=newNodePtr; // I am changing where head is POINTING TO, it now POINTS TO 'newNodePtr'
    // i.e. to the NEW NODDE (now holds the address of a POINTER pointing to the NEW NODE
    printf("Finally address OF head = %d \n",head);
    printf("Finally address in head = *head = %d = address OF the NEW NODE\n",*head);
    return 1;
}
void PrintList(struct Node* head){
        while(head){
        printf("value in nodes is %d \n",head->value);
        head = head->next;
        }
}
int main(){
    int a =10;
    int * p = &a; // 'p' points to 'a'
  struct Node * head = NULL;
   printf("In main, address IN 'head' = %d i.e. head POINTS TO NULL\n",head);
   printf("In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = %d \n",&head);
   printf("This address OF head '&head' = %d UNIQUELY IDENTIFIES this LIST and remains",&head);
   printf("UNCHANGED throughout\n");
   printf("In main, value at address of head '*(&head) = %d = address in 'head'\n",*(&head));
   printf("This means address IN 'head' == '*(&head)'\n");
   printf("NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER\n");
   printf("Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO");
   printf(" p = %d\n",p);
   printf(" &a = %d\n",&a);
   printf(" &p = %d\n",&p);
   printf(" *(&p) = %d\n",*(&p));
   printf(" *p = %d\n",*p);
   printf("(1) This means p == *(&p), where 'p' is any POINTER\n");
   printf("(2) This means if you print 'p' you will get '&a', because p=&a\n");
   printf("\nSee how the values of newNodePtr, newNodePtr->next\n");
   printf("addresses head holds(points to) changes, BUT  the address OF HEAD\n");
   printf("remains the SAME with each call to insertNode\n\n");
  int arr[5]={1,2,3,4,5};
    int i;
    for(i=0;i<5;i++){
     insertNode(&head,arr[i]);
    }
     PrintList(head);
    return 0;
}


Output:
$gcc -o main *.c
$main
In main, address IN 'head' = 0 i.e. head POINTS TO NULL
In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = -1625641784 
This address OF head '&head' = -1625641784 UNIQUELY IDENTIFIES this LIST and remainsUNCHANGED throughout
In main, value at address of head '*(&head) = 0 = address in 'head'
This means address IN 'head' == '*(&head)'
NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER
Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO p = -1625641768
 &a = -1625641768
 &p = -1625641776
 *(&p) = -1625641768
 *p = 10
(1) This means p == *(&p), where 'p' is any POINTER
(2) This means if you print 'p' you will get '&a', because p=&a

See how the values of newNodePtr, newNodePtr->next
addresses head holds(points to) changes, BUT  the address OF HEAD
remains the SAME with each call to insertNode

Insert Node 1
 address in newNodePtr = 10371104 = address OF the NEW NODE
 address in newNodePtr->next = 0 
 address in *head = 0 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371104 = address OF the NEW NODE
Insert Node 2
 address in newNodePtr = 10371136 = address OF the NEW NODE
 address in newNodePtr->next = 10371104 
 address in *head = 10371104 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371136 = address OF the NEW NODE
Insert Node 3
 address in newNodePtr = 10371168 = address OF the NEW NODE
 address in newNodePtr->next = 10371136 
 address in *head = 10371136 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371168 = address OF the NEW NODE
Insert Node 4
 address in newNodePtr = 10371200 = address OF the NEW NODE
 address in newNodePtr->next = 10371168 
 address in *head = 10371168 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371200 = address OF the NEW NODE
Insert Node 5
 address in newNodePtr = 10371232 = address OF the NEW NODE
 address in newNodePtr->next = 10371200 
 address in *head = 10371200 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371232 = address OF the NEW NODE
value in nodes is 5 
value in nodes is 4 
value in nodes is 3 
value in nodes is 2 
value in nodes is 1 

Ampere's circuital law

  Ampere's circuital law states that the line integral of a magnetic field around any closed loop is equal to the permeability of free s...