Sunday, September 9, 2018
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:
#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:
- $gcc -o main *.c
- $main
-
- Enter element to be added:
- 1
- rear = 0
- type -1 to exit
- 2
- rear = 1
- type -1 to exit
- 3
- rear = 2
- type -1 to exit
- 4
- rear = 3
- type -1 to exit
- 5
- rear = 4
- type -1 to exit
- 6
- rear = 5
- type -1 to exit
- 7
- rear = 6
- type -1 to exit
- -1
- The elements of the queue are
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 2
- 3
- 4
- 5
- 6
- 7
- 1 is deleted
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 0
- 3
- 4
- 5
- 6
- 7
- 2 is deleted
- type 1 to delete or 2 to exit
- The elements of the queue are
- 0
- 0
- 0
- 4
- 5
- 6
- 7
- 3 is deleted
- type 1 to delete or 2 to exit
- Deletion completed
1 view · Posted Just now
Friday, August 31, 2018
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:
#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
Thursday, August 23, 2018
Subscribe to:
Posts (Atom)
What is a superconductor?
A superconductor is a material that can conduct electricity with zero electrical resistance when it is cooled below a specific, very cold t...
-
Assuming you are working with a newly returned ResultSet whose cursor is pointing before the first row, an easier way to check this is to...
-
Acronym Full Form AJAX Asynchronous JavaScript and XML API Application Programming Interface APK Android Application Package ASP Activ...
-
Here are some links to online work sites https://www.upwork.com/ https://www.guru.com/ https://studio.envato.com/freelance-switch/ htt...

