Knowledge Share
Linux Internals Networking C Programming Technical Posts
Wednesday 16 January 2019
Sunday 9 April 2017
How to increase Virtual Box Hard Disk Size
Here is what I've fixed the issue.
Initially I created Virtual Box of 15GB of space.
When I run into out-of-space issue. I did following.
Two steps involved to resize existing virtual disk space.
1) Using VBoxManage utility tool provided by Oracle VirtualBox ; Allocate enough space based on requirement. In my case I've increased from 15GB to 25GB.
I did this;
Checked if this present ;
"C:\Program Files\Oracle\VirtualBox\VBoxManage"
Then Run- CMD , to set environment variable
set PATH=%PATH%;C:\Program Files\Oracle\VirtualBoxGo to path where you have installed VirtualBox. My caseC:\Users\ssankar\VirtualBox VMs\ssankar-ubuntu>VBoxManage modifyhd ssankar-ubuntu.vdi --resize 250000%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% C:\Users\ssuriyanarayanan\VirtualBox VMs\ssankar-ubuntu>
Done Now If you open VirtualBox you will able to see "Virtual Box Size: 25GB " . Actual Size "14 GB".
Step:2: Now create partition from un-allocated space of 10GB. This can be achieved using tool call Gparted
For this step I've followed : http://derekmolloy.ie/resize-a-virtualbox-disk/#prettyPhoto
Tuesday 14 June 2016
Reverse Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void ReverseList (struct node **head)
{
struct node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
ReverseList(&head);
printf("Reversed List\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all: reverselist
reverselist: reverselist.o
${CC} ${CLAGS} -o $@ reverselist.o
clean:
-@rm *.o
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void ReverseList (struct node **head)
{
struct node *prev = NULL, *curr = *head, *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
ReverseList(&head);
printf("Reversed List\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all: reverselist
reverselist: reverselist.o
${CC} ${CLAGS} -o $@ reverselist.o
clean:
-@rm *.o
Swap linked list node without swapping data
swapnode.c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void swapNodes (struct node **head, int a, int b)
{
struct node *prev_x, *curr_x;
struct node *prev_y, *curr_y, *temp;
curr_x = *head;
while (curr_x && (curr_x->data != a)) {
prev_x = curr_x;
curr_x = curr_x->next;
}
curr_y = *head;
while (curr_y && (curr_y->data != b)) {
prev_y = curr_y;
curr_y = curr_y->next;
}
prev_x->next = curr_y;
temp = curr_y->next;
curr_y->next = curr_x->next;
prev_y->next = curr_x;
curr_x->next = temp;
return;
}
int main(int argc, char *argv[])
{
int i;
struct node *head = NULL;
if (argc <= 2) {
printf("usage: ./swapnode <a> <b> \r\n");
return 0;
}
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List\r\n");
PrintList(&head);
swapNodes(&head, atoi(argv[1]), atoi(argv[2]));
printf("After swap\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all : swapnode
swapnode: swapnode.o
${CC} ${CFLAGS} -o $@ swapnode.o
clean:
-@rm *.o
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void swapNodes (struct node **head, int a, int b)
{
struct node *prev_x, *curr_x;
struct node *prev_y, *curr_y, *temp;
curr_x = *head;
while (curr_x && (curr_x->data != a)) {
prev_x = curr_x;
curr_x = curr_x->next;
}
curr_y = *head;
while (curr_y && (curr_y->data != b)) {
prev_y = curr_y;
curr_y = curr_y->next;
}
prev_x->next = curr_y;
temp = curr_y->next;
curr_y->next = curr_x->next;
prev_y->next = curr_x;
curr_x->next = temp;
return;
}
int main(int argc, char *argv[])
{
int i;
struct node *head = NULL;
if (argc <= 2) {
printf("usage: ./swapnode <a> <b> \r\n");
return 0;
}
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List\r\n");
PrintList(&head);
swapNodes(&head, atoi(argv[1]), atoi(argv[2]));
printf("After swap\r\n");
PrintList(&head);
return 0;
}
CC = gcc
CFLAGS = -g -Wall
all : swapnode
swapnode: swapnode.o
${CC} ${CFLAGS} -o $@ swapnode.o
clean:
-@rm *.o
Wednesday 8 June 2016
Linked List FIFO
FIFO List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void InsertToFIFO (struct node **head, struct node **tail, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
*tail = newNode;
} else {
(*tail)->next = newNode;
*tail = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL, *tail = NULL;
for (i = 1; i <= 10; i++) {
InsertToFIFO(&head, &tail, i);
}
printf("FIFO List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: queue_list
queue_list: queue_list.o
${CC} ${CFLAGS} -o $@ queue_list.o
clean:
-@rm *.o
distclean: clean
-@rm queue_list
Output:
FIFO List
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void InsertToFIFO (struct node **head, struct node **tail, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
*tail = newNode;
} else {
(*tail)->next = newNode;
*tail = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL, *tail = NULL;
for (i = 1; i <= 10; i++) {
InsertToFIFO(&head, &tail, i);
}
printf("FIFO List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: queue_list
queue_list: queue_list.o
${CC} ${CFLAGS} -o $@ queue_list.o
clean:
-@rm *.o
distclean: clean
-@rm queue_list
Output:
FIFO List
1
2
3
4
5
6
7
8
9
10
Find the middle node in the linked list
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
/* traverse double the time
* return the first pointer
* basically divide by 2 algorithm
* **/
void FindMiddleNode (struct node **head, struct node **middle)
{
struct node *temp1, *temp2;
temp1 = temp2 = *head;
while (temp2 != NULL) {
temp1 = temp1->next;
temp2 = temp2->next->next;
}
*middle = temp1;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *middle = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
FindMiddleNode(&head, &middle);
printf("Middle Node %d \r\n", middle->data);
return 0;
}
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
/* traverse double the time
* return the first pointer
* basically divide by 2 algorithm
* **/
void FindMiddleNode (struct node **head, struct node **middle)
{
struct node *temp1, *temp2;
temp1 = temp2 = *head;
while (temp2 != NULL) {
temp1 = temp1->next;
temp2 = temp2->next->next;
}
*middle = temp1;
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *middle = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("List \r\n");
PrintList(&head);
FindMiddleNode(&head, &middle);
printf("Middle Node %d \r\n", middle->data);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: mergenode findAndRemoveLoop middlenode
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
findAndRemoveLoop: findAndRemoveLoop.o
${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o
middlenode: middlenode.o
${CC} ${CFLAGS} -o $@ middlenode.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
-@rm findAndRemoveLoop
-@rm middlenode
Output:
List
10
9
8
7
6
5
4
3
2
1
Middle Node 5
Find loop in the list and remove it
Following program to find the loop in the list and correct the loop
- Algorithm : loop through the list and return any node from the list
- Remove the loop :
Traverse the head and check if the looped node matches
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void CreateLoopedList (struct node **head)
{
struct node *temp = *head;
struct node *merge = *head;
while (temp->next != NULL) {
temp = temp->next;
}
printf ("Created Loop Between Node\r\n == %d -> %d == \r\n", temp->data, (merge->next->next->next)->data);
temp->next = merge->next->next->next;
return;
}
void FindLoopInList (struct node **head, struct node **loopNode)
{
struct node *slow = NULL, *fast = NULL;
slow = fast = *head;
/* alternatively use visit flag
* to find loop node
* */
while (slow && fast) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
/* return any node in the loop
* where slow and fast ptr are met
**/
*loopNode = slow;
break;
}
}
return;
}
void RemoveLoopFromList (struct node **head, struct node *loopNode)
{
struct node *temp1 = *head;
struct node *temp2 = NULL;
while (*head) {
temp2 = loopNode;
/* start from head move temp1 for each looped node not found case
* keep move temp2 , unless looping node found
* temp1 traverse from head
* temp2 loop from looped one side node
* any point of time temp1 == temp2 then break
* **/
while (temp2->next != loopNode && temp2->next != temp1) {
temp2 = temp2->next;
}
if (temp2->next == temp1) {
/* found the loop, make next to null
**/
temp2->next = NULL;
return;
}
temp1 = temp1->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *loopNode = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("Head List \r\n");
PrintList(&head);
CreateLoopedList(&head);
FindLoopInList(&head, &loopNode);
RemoveLoopFromList(&head, loopNode);
printf("After removing looped List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: mergenode findAndRemoveLoop
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
findAndRemoveLoop: findAndRemoveLoop.o
${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
-@rm findAndRemoveLoop
- Algorithm : loop through the list and return any node from the list
- Remove the loop :
Traverse the head and check if the looped node matches
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void AppendToList (struct node **head, int value)
{
struct node *newNode = NULL;
newNode = (struct node *) malloc(sizeof(struct node));
if (newNode == NULL)
return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
return;
}
void PrintList (struct node **head)
{
struct node *temp = *head;
while (temp) {
printf("%d\r\n", temp->data);
temp = temp->next;
}
return;
}
void CreateLoopedList (struct node **head)
{
struct node *temp = *head;
struct node *merge = *head;
while (temp->next != NULL) {
temp = temp->next;
}
printf ("Created Loop Between Node\r\n == %d -> %d == \r\n", temp->data, (merge->next->next->next)->data);
temp->next = merge->next->next->next;
return;
}
void FindLoopInList (struct node **head, struct node **loopNode)
{
struct node *slow = NULL, *fast = NULL;
slow = fast = *head;
/* alternatively use visit flag
* to find loop node
* */
while (slow && fast) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
/* return any node in the loop
* where slow and fast ptr are met
**/
*loopNode = slow;
break;
}
}
return;
}
void RemoveLoopFromList (struct node **head, struct node *loopNode)
{
struct node *temp1 = *head;
struct node *temp2 = NULL;
while (*head) {
temp2 = loopNode;
/* start from head move temp1 for each looped node not found case
* keep move temp2 , unless looping node found
* temp1 traverse from head
* temp2 loop from looped one side node
* any point of time temp1 == temp2 then break
* **/
while (temp2->next != loopNode && temp2->next != temp1) {
temp2 = temp2->next;
}
if (temp2->next == temp1) {
/* found the loop, make next to null
**/
temp2->next = NULL;
return;
}
temp1 = temp1->next;
}
return;
}
int main(void)
{
int i;
struct node *head = NULL;
struct node *loopNode = NULL;
for (i = 1; i <= 10; i++) {
AppendToList(&head, i);
}
printf("Head List \r\n");
PrintList(&head);
CreateLoopedList(&head);
FindLoopInList(&head, &loopNode);
RemoveLoopFromList(&head, loopNode);
printf("After removing looped List \r\n");
PrintList(&head);
return 0;
}
Makefile:
CC = gcc
CFLAGS = -g -Wall
all: mergenode findAndRemoveLoop
mergenode: mergenode.o
${CC} ${CFLAGS} -o $@ mergenode.o
findAndRemoveLoop: findAndRemoveLoop.o
${CC} ${CFLAGS} -o $@ findAndRemoveLoop.o
clean:
-@rm *.o
distclean: clean
-@rm mergenode
-@rm findAndRemoveLoop
Output:
Head List
10
9
8
7
6
5
4
3
2
1
Created Loop Between Node
== 1 -> 7 ==
After removing looped List
10
9
8
7
6
5
4
3
2
1
Subscribe to:
Posts (Atom)