Archive | November, 2010

Post Incrementation and Pre Incrementation

See the below example to understand the difference between post incrementation and pre incrementation.

int

 

x = 10;int z;//First assigns value of x (10) to z and then x value incremented to 11.

z=x++;MessageBox.Show(“Post Incement of x=” + z);int y = 20;int a;

//First increments value of y to (21) and assigns 21 to as.a=++y; 

MessageBox.Show(“Pre incrementor Y=”+a);

Posted in C# FAQ7 Comments

How to execute standalone program without main() in Java?

We have a perception that every standalone program will have main() method when comes to languages C, C++ and Java. However, there is small difference in understanding.

The main() method is starting point in C, C++ languages but not in Java. In Java, we have a concept called Class Loading. We know that we pass class name as an argument when we run any java program. It means that we are telling the interpreter that class name we are passing must be loaded to start the process. So now we can sure that the starting point of java standalone program is class loading and not main() method. In other words, before reaching main() (which is in the class ), the interpreter must load the class.

Now we know where and how the java program starts its execution. Now we have to execute other code in the program without going to main() (you can also remove completely main() method).

There is a possibility called Static block. This block executes immediately once the class loaded into JVM. The block structure is as follows.

static {

}

This is a separate block which can be written apart of members and methods in a class. The code inside this block executes once the class loads in to JVM. So we can write any sort java code in this bound to our requirements.

However, once the execution of block ends, the JVM looks for the main() method. It does not matter whether you have main() defined or undefined in the program, we can terminate the process completely using System.exit() method. This must be called as a last line of the static block to stop the process intentionally without proceeding further to check for main() method.

class ABC

{

static {

System.out.println(“Standalone program execution without main()”);

System.exit(0);

}

}

> javac ABC.java

> java ABC

Standalone program execution without main()

This is how we can run a standalone program without using main() method.

Happy Learning – Java.

Posted in Java3 Comments

c training – comparing two linked lists, creating copy of a linked list, how to free the nodes of a linked list.

How to compare two linked lists? Write a C program to compare two linked lists.

Here is a simple C program to accomplish the same.

int compare_linked_lists(struct node *q, struct node *r)
{
static int flag;

if((q==NULL ) && (r==NULL))
{
flag=1;
}
else
{
if(q==NULL || r==NULL)
{
flag=0;
}
if(q->data!=r->data)
{
flag=0;
}
else
{
compare_linked_lists(q->link,r->link);
}
}
return(flag);
}

Another way is to do it on similar lines as strcmp() compares two strings, character by character (here each node is like a character).

How to create a copy of a linked list? Write a C program to create a copy of a linked list.

Check out this C program which creates an exact copy of a linked list.

copy_linked_lists(struct node *q, struct node **s)
{
if(q!=NULL)
{
*s=malloc(sizeof(struct node));
(*s)->data=q->data;
(*s)->link=NULL;
copy_linked_list(q->link, &((*s)->link));
}
}

Write a C program to free the nodes of a linked list.

Before looking at the answer, try writing a simple C program (with a for loop) to do this. Quite a few people get this wrong.

This is the wrong way to do it

struct list *listptr, *nextptr;
for(listptr = head; listptr != NULL; listptr = listptr->next)
{
free(listptr);
}

If you are thinking why the above piece of code is wrong, note that once you free the listptr node, you cannot do something like listptr = listptr->next!. Since listptr is already freed, using it to get listptr->next is illegal and can cause unpredictable results!

This is the right way to do it

struct list *listptr, *nextptr;
for(listptr = head; listptr != NULL; listptr = nextptr)
{
nextptr = listptr->next;
free(listptr);
}
head = NULL;

After doing this, make sure you also set the head pointer to NULL!

Posted in C Tutorials, Data Structures0 Comments

c training – implementing heterogeneous linked list

If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer. Void pointer is capable of storing pointer to any type as it is a generic pointer type.

Posted in C Tutorials, Data Structures0 Comments

c training – how to find middle node of linked list

How do you find the middle of a linked list? Write a C program to return the middle of a linked list?

This is one more popular interview question.
Here are a few C program snippets to give you an idea of the possible solutions.

Method1 (Uses one slow pointer and one fast pointer)

#include<stdio.h>
#include<ctype.h>

typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;

void add_node(struct node **head, int value);
void print_list(char *listName, struct node *head);
void getTheMiddle(mynode *head);

// The main function..
int main()
{
mynode *head;
head = (struct node *)NULL;

add_node(&head, 1);
add_node(&head, 10);
add_node(&head, 5);
add_node(&head, 70);
add_node(&head, 9);
add_node(&head, -99);
add_node(&head, 0);
add_node(&head, 555);
add_node(&head, 55);

print_list("myList", head);
getTheMiddle(head);

getch();
return(0);
}

// This function uses one slow and one fast
// pointer to get to the middle of the LL.
//
// The slow pointer is advanced only by one node
// and the fast pointer is advanced by two nodes!
void getTheMiddle(mynode *head)
{
mynode *p = head;
mynode *q = head;

if(q!=NULL)
{
while((q->next)!=NULL && (q->next->next)!=NULL)
{
p=(p!=(mynode *)NULL?p->next:(mynode *)NULL);
q=(q!=(mynode *)NULL?q->next:(mynode *)NULL);
q=(q!=(mynode *)NULL?q->next:(mynode *)NULL);
}
printf("The middle element is [%d]",p->value);
}
}

// Function to add a node
void add_node(struct node **head, int value)
{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));
temp->next=NULL;
temp->prev=NULL;

if(*head == NULL)
{
*head=temp;
temp->value=value;
}
else
{
for(cur=*head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
}
}

// Function to print the linked list...
void print_list(char *listName, struct node *head)
{
mynode *temp;

printf("\n[%s] -> ", listName);
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("[%d]->",temp->value);
}

printf("NULL\n");

}
Here p moves one step, where as q moves two steps, when q reaches end, p will be at the middle of the linked list.

Method2(Uses a counter)

#include<stdio.h>
#include<ctype.h>

typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;

void add_node(struct node **head, int value);
void print_list(char *listName, struct node *head);
mynode *getTheMiddle(mynode *head);

// The main function..
int main()
{
mynode *head, *middle;
head = (struct node *)NULL;
add_node(&head, 1);
add_node(&head, 10);
add_node(&head, 5);
add_node(&head, 70);
add_node(&head, 9);
add_node(&head, -99);
add_node(&head, 0);
add_node(&head, 555);
add_node(&head, 55);

print_list("myList", head);
middle = getTheMiddle(head);
printf("\nMiddle node -> [%d]\n\n", middle->value);

getch();
return(0);
}

// Function to get to the middle of the LL
mynode *getTheMiddle(mynode *head)
{
mynode *middle = (mynode *)NULL;
int i;
for(i=1; head!=(mynode *)NULL; head=head->next,i++)
{
if(i==1)
middle=head;
else if ((i%2)==1)
middle=middle->next;
}
return middle;
}
// Function to add a new node to the LL
void add_node(struct node **head, int value)
{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));
temp->next=NULL;
temp->prev=NULL;

if(*head == NULL)
{
*head=temp;
temp->value=value;
}
else
{
for(cur=*head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
}
}

// Function to print the LL
void print_list(char *listName, struct node *head)
{
mynode *temp;

printf("\n[%s] -> ", listName);
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("[%d]->",temp->value);
}

printf("NULL\n");

}

In a similar way, we can find the 1/3 th node of linked list by changing (i%2==1) to (i%3==1) and in the same way we can find nth node of list by changing (i%2==1) to (i%n==1) but make sure ur (n<=i).

Posted in C Tutorials, Data Structures0 Comments

c training – finding loop in linked list

How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
This is also one of the classic interview questions

There are multiple answers to this problem. Here are a few C programs to attack this problem.
Brute force method

Have a double loop, where you check the node pointed to by the outer loop, with every node of the inner loop.
typedef struct node

{
void *data;
struct node *next;
}mynode;
mynode * find_loop(NODE * head)
{
mynode *current = head;

while(current->next != NULL)
{
mynode *temp = head;
while(temp->next != NULL && temp != current)
{
if(current->next == temp)
{
printf("\nFound a loop.");
return current;
}
temp = temp->next;
}
current = current->next;
}
return NULL;
}

Visited flag
Have a visited flag in each node of the linked list. Flag it as visited when you reach the node. When you reach a node and the flag is already flagged as visited, then you know there is a loop in the linked list.

Fastest method
Have 2 pointers to start of the linked list. Increment one pointer by 1 node and the other by 2 nodes. If there’s a loop, the 2nd pointer will meet the 1st pointer somewhere. If it does, then you know there’s one.

Here is some code

p=head;
q=head->next;

while(p!=NULL && q!=NULL)
{
if(p==q)
{
//Loop detected!
exit(0);
}
p=p->next;
q=(q->next)?(q->next->next):q->next;
}
// No loop.

Posted in C Tutorials, Data Structures1 Comment

c training – reverse a linked list without using pointer

How do you reverse a linked list without using any C pointers?

One way is to reverse the data in the nodes without changing the pointers themselves. One can also create a new linked list which is the reverse of the original linked list. A simple C program can do that for you. Please note that you would still use the “next” pointer fields to traverse through the linked list (So in effect, you are using the pointers, but you are not changing them when reversing the linked list).

Posted in C Tutorials, Data Structures0 Comments

c training – generic linked list

How to declare a structure of a linked list?

The right way of declaring a structure for a linked list in a C program is

struct node {
int value;
struct node *next;
};
typedef struct node *mynode;
Note that the following are not correct

typedef struct {
int value;
mynode next;
} *mynode;

The typedef is not defined at the point where the “next” field is declared.
struct node {
int value;
struct node next;
};
typedef struct node mynode;

You can only have pointer to structures, not the structure itself as its recursive!

Write a C program to implement a Generic Linked List.

Here is a C program which implements a generic linked list. This is also one of the very popular interview questions thrown around. The crux of the solution is to use the void C pointer to make it generic. Also notice how we use function pointers to pass the address of different functions to print the different generic data.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct list {
void *data;
struct list *next;
} List;

struct check {
int i;
char c;
double d;
} chk[] = { { 1, 'a', 1.1 },
{ 2, 'b', 2.2 },
{ 3, 'c', 3.3 } };

void insert(List **, void *, unsigned int);
void print(List *, void (*)(void *));
void printstr(void *);
void printint(void *);
void printchar(void *);
void printcomp(void *);

List *list1, *list2, *list3, *list4;

int main(void)
{
char c[]    = { 'a', 'b', 'c', 'd' };
int i[]     = { 1, 2, 3, 4 };
char *str[] = { "hello1", "hello2", "hello3", "hello4" };

list1 = list2 = list3 = list4 = NULL;

insert(&list1, &c[0], sizeof(char));
insert(&list1, &c[1], sizeof(char));
insert(&list1, &c[2], sizeof(char));
insert(&list1, &c[3], sizeof(char));

insert(&list2, &i[0], sizeof(int));
insert(&list2, &i[1], sizeof(int));
insert(&list2, &i[2], sizeof(int));
insert(&list2, &i[3], sizeof(int));

insert(&list3, str[0], strlen(str[0])+1);
insert(&list3, str[1], strlen(str[0])+1);
insert(&list3, str[2], strlen(str[0])+1);
insert(&list3, str[3], strlen(str[0])+1);

insert(&list4, &chk[0], sizeof chk[0]);
insert(&list4, &chk[1], sizeof chk[1]);
insert(&list4, &chk[2], sizeof chk[2]);

printf("Printing characters:");
print(list1, printchar);
printf(" : done\n\n");

printf("Printing integers:");
print(list2, printint);
printf(" : done\n\n");

printf("Printing strings:");
print(list3, printstr);
printf(" : done\n\n");

printf("Printing composite:");
print(list4, printcomp);
printf(" : done\n");

return 0;
}

void insert(List **p, void *data, unsigned int n)
{
List *temp;
int i;

/* Error check is ignored */
temp = malloc(sizeof(List));
temp->data = malloc(n);
for (i = 0; i < n; i++)
*(char *)(temp->data + i) = *(char *)(data + i);
temp->next = *p;
*p = temp;
}

void print(List *p, void (*f)(void *))
{
while (p)
{
(*f)(p->data);
p = p->next;
}
}

void printstr(void *str)
{
printf(" \"%s\"", (char *)str);
}

void printint(void *n)
{
printf(" %d", *(int *)n);
}

void printchar(void *c)
{
printf(" %c", *(char *)c);
}

void printcomp(void *comp)
{
struct check temp = *(struct check *)comp;
printf(" '%d:%c:%f", temp.i, temp.c, temp.d);
}

Posted in C Tutorials, Data Structures0 Comments

WebGardening and Webforming

Web Gardening Image

WebGardening

Usually all the application developed by a company will be assigned to a single worker process in the IIS.This is not called as the WebGardening.Second example is not in the WebGarden environment.

But IIS adminsitrator having a chance of assigning all the availble application to more than one worker process. This is called the WebGardening.First Figure shows you WebGarden environment.

Posted in ASP.NET FAQ9 Comments

Adding Custom config section to web.config

public class TechPalleCustomConfig : ConfigurationSection
{
[ConfigurationProperty("BestTraining", DefaultValue="Palle Technologies.", IsRequired=false)]
public string BestTraining
{
get
{
return this["BestTraining"] as string;
}
}

[ConfigurationProperty("Address", IsRequired=true)]
public string Address
{
get
{
return (string) this["Address"];
}
}
}

Note : If you want to see this config section in your web.config file you have to create an object for the class “TechPalleCustomConfig”. After creating the object you will see custom config section in the web.config.

Note: This is not the tested code, Check your self for the correctness of the code.

Posted in ASP.NET FAQ1 Comment