Wednesday, December 28, 2011

Linked Lists FAQ with Example solutions


  1.   How do you reverse a singly linked list? How do you reverse a doubly linked list?  Write a C program to do the same. 
  2. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it? 
  3. How do you sort a linked list? Write a C program to sort a linked list.
  4. How to declare a structure of a linked list?
  5. Write a C program to implement a Generic Linked List.
  6. How do you reverse a linked list without using any C pointers?
  7.  How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
  8.  How do you find the middle of a linked list? Write a C program to return the middle of a linked list
  9.  If you are using C language to implement the heterogeneous linked list, what pointer type will you use?
  10.  How to compare two linked lists? Write a C program to compare two linked lists.
  11. How to create a copy of a linked list? Write a C program to create a copy of a linked list.
  12. Write a C program to free the nodes of a linked list?
  13. Can we do a Binary search on a linked list?
  14.  Write a C program to return the nth node from the end of a linked list
  15. How would you find out if one of the pointers in a linked list is corrupted or not? Write a C program to do the same.
Here are a few C programs to reverse a singly linked list.
Method1 (Iterative)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals (not required, though).
mynode *head, *tail, *temp;
// Functions
void add(int value);
void iterative_reverse();
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
iterative_reverse();
//Print it again
print_list();
return(0);
}
// The reverse function
void iterative_reverse()
{
mynode *p, *q, *r;
if(head == (mynode *)0)
{
return;
}
p = head;
q = p->next;
p->next = (mynode *)0;
while (q != (mynode *)0)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head = p;
}
// Function to add new nodes to the linked list
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method2 (Recursive, without using any temporary variable)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals.
mynode *head, *tail, *temp;
// Functions
void add(int value);
mynode* reverse_recurse(mynode *root);
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
if(head != (mynode *)0)
{
temp = reverse_recurse(head);
temp->next = (mynode *)0;
}
//Print it again
print_list();
return(0);
}
// Reverse the linked list recursively
//
// This function uses the power of the stack to make this
// *magical* assignment
//
// node->next->next=node;
//
// :)
mynode* reverse_recurse(mynode *root)
{
if(root->next!=(mynode *)0)
{
reverse_recurse(root->next);
root->next->next=root;
return(root);
}
else
{
head=root;
}
}
// Function to add new nodes to the linked list.
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method3 (Recursive, but without ANY global variables. Slightly messy!)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Functions
void add(mynode **head, mynode **tail, int value);
mynode* reverse_recurse(mynode *current, mynode *next);
void print_list(mynode *);
int main()
{
mynode *head, *tail;
head=(mynode *)0;
// Construct the linked list.
add(&head, &tail, 1);
add(&head, &tail, 2);
add(&head, &tail, 3);
//Print it
print_list(head);
// Reverse it.
head = reverse_recurse(head, (mynode *)0);
//Print it again
print_list(head);
getch();
return(0);
}
// Reverse the linked list recursively
mynode* reverse_recurse(mynode *current, mynode *next)
{
mynode *ret;
if(current==(mynode *)0)
{
return((mynode *)0);
}
ret = (mynode *)0;
if (current->next != (mynode *)0)
{
ret = reverse_recurse(current->next, current);
}
else
{
ret = current;
}
current->next = next;
return ret;
}
// Function to add new nodes to the linked list.
// Takes pointers to pointers to maintain the
// *actual* head and tail pointers (which are local to main()).
void add(mynode **head, mynode **tail, int value)
{
mynode *temp1, *temp2;
temp1 = (mynode *) malloc(sizeof(struct node));
temp1->next=(mynode *)0;
temp1->value=value;
if(*head==(mynode *)0)
{
*head=temp1;
*tail=temp1;
}
else
{
for(temp2 = *head; temp2->next!= (mynode *)0; temp2=temp2->next);
temp2->next = temp1;
*tail=temp1;
}
}
// Function to print the linked list.
void print_list(mynode *head)
{
mynode *temp;
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Doubly linked lists
This is really easy, just keep swapping the prev and next pointers and at the end swap the
head and the tail:)
#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;
mynode *head, *tail;
void add_node(int value);
void print_list();
void reverse();
int main()
{
head=NULL;
tail=NULL;
add_node(1);
add_node(2);
add_node(3);
add_node(4);
add_node(5);
print_list();
reverse();
print_list();
return(1);
}
void add_node(int value)
{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));
temp->next=NULL;
temp->prev=NULL;
if(head == NULL)
{
printf("\nAdding a head pointer\n");
head=temp;
tail=temp;
temp->value=value;
}
else
{
for(cur=head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
tail=temp;
}
}
void print_list()
{
mynode *temp;
printf("\n--------------------------------\n");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("\n[%d]\n",temp->value);
}
}
void reverse()
{
mynode *cur, *temp, *save_next;
if(head==tail)return;
if(head==NULL || tail==NULL)
return;
for(cur=head;cur!=NULL;)
{
printf("\ncur->value : [%d]\n",cur->value);
temp=cur->next;
save_next=cur->next;
cur->next=cur->prev;
cur->prev=temp;
cur=save_next;
} temp=head;
head=tail;
tail=temp;
}
2. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
This is a very good interview question
The solution to this is to copy the data from the next node into this node and delete the
next node!. Of course this won’t work if the node to be deleted is the last node. Mark it as
Dummy in that case. If you have a Circular linked list, then this might be all the more
Interesting. Try writing your own C program to solve this problem. Having a doubly
Linked list is always better.
3. How do you sort a linked list? Write a C program to sort a linked list.
This is a very popular interview question, which most people go wrong. The ideal
Solution to this problem is to keep the linked list sorted as you build it. This really saves a
Lot of time which would have been required to sort it.
However....
Method1 (Usual method)
The general idea is to decide upon a sorting algorithm (say bubble sort). Then, one needs
to come up with different scenarios to swap two nodes in the linked list when they are not
in the required order. The different scenarios would be something like
1. When the nodes being compared are not adjacent and one of them is the first node.
2. When the nodes being compared are not adjacent and none of them is the first node
3. When the nodes being compared are adjacent and one of them is the first node.
4. When the nodes being compared are adjacent and none of them is the first node.
One example bubble sort for a linked list goes like this
for(i = 1; i < n; i++)
{
p1 = head;
p2 = head->next;
p3 = p2->next;
for(j = 1; j <= (n - i); j++)
{
if(p2->value < p3->value)
{
p2->next = p3->next;
p3->next = p2;
p1->next = p3;
p1 = p3;
p3 = p2->next;
}
else
{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
}
As you can see, the code becomes quite messy because of the pointer logic. Thats why I
have not elaborated too much on the code, nor on variations such as soring a doubly
linked list. You have to do it yourself once to understand it.
Method1 (Divide and Conquer using merge sort)
The pseudocode for this method is
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail;
int size;
mynode *mergesort(mynode *list, int size);
void display(mynode *list);
mynode *mergesort(mynode *list, int size)
{
int size1, size2;
mynode *tempnode1, *tempnode2, *tempnode3;
if( size<=2 )
{
if(size==1)
{
// Nothing to sort!
return(list);
}
else
{
if(list->value < list->next->value
{
// These 2 nodes are already in right order, no need to sort
return(list);
}
else
{
// Need to swap these 2 nodes
/* Here we have 2 nodes
*
*node 1 -> node2 -> NULL
*
* This should be converted to
*
* node2 -> node1 -> NULL
*
*/
tempnode1 = list;
tempnode2 = list->next;
tempnode2->next = tempnode1;
tempnode1->next = NULL;
return(tempnode2);
}
}
}
else
{
// The size of the linked list is more than 2.
// Need to split this linked list, sort the
// left and right sub-linked lists and merge.
// Split.
// tempnode1 will have the first half of the linked list of size "size1".
// tempnode2 will have the second half of the linked list of size "size2".
<CODE TO SPLIT THE LINKED LIST INTO TWO>
// Sort the two halves recursively
tempnode1 = mergesort(tempnode1, size1);
tempnode2 = mergesort(tempnode2, size2);
// Now merge the sorted lists back, let tempnode3 point to that new list.
<CODE TO MERGE THE 2 LINKED LISTS BACK INTO A SINGLE
SORTED LINKED LIST>
return(tempnode3);
}
}
The code to merge the two already sorted sub-linked lists into a sorted linked list could be
something like this..
mynode * merge(mynode *a, mynode *b)
{
mynode *i, *j, *k, *c;
i = a;
j = b;
c = getNewNode();
k = getNewNode();
while(i != NULL && j != NULL)
{
if( i -> value < j -> value )
{
k -> next = i;
i = i -> next;
}
else
{
k -> next = j;
j = j -> next;
}
}
if( i != NULL)
k -> next = i ;
else
k -> next = j;
return( c -> next );
}
4.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!
5.Write a C program to implement a Generic Linked List.
Here is a C program which implements a generic linked list. 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 );
}
6. 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).
7.How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
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;
}r
eturn 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.
8.How do you find the middle of a linked list? Write a C program to return the
middle of a linked list
Another popular interview question
Here are a few C program snippets to give you an idea of the possible solutions.
Method1
p = head;
q = head;
if( q -> next -> next != NULL)
{
p = p -> next;
q = q -> next -> next;
}
printf("The middle element is %d",p->data);
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
struct node *middle(struct node *head)
{
struct node *middle=NULL;
int i;
for( i = 1 ; head ; head = head -> next , i++)
{
if( i == 1 )
middle = head ;
else if ( ( i % 2 ) == 1 )
middle = middle -> next ;
}r
eturn middle;
} 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 ).
9.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.
Check out the C program to implement a Generic linked list in the same FAQ.
10.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 ) ;
}
}r
eturn ( 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).
11.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 ) );
}
}
12.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 ) ;
}
13. Can we do a Binary search on a linked list
The answer is ofcourse, you can write a C program to do this. But, the question is, do you
really think it will be as efficient as a C program which does a binary search on an array?
Think hard, real hard.
Do you know what exactly makes the binary search on an array so fast and efficient? Its
the ability to access any element in the array in constant time. This is what makes it so
fast. You can get to the middle of the array just by saying array[middle]!. Now, can you
do the same with a linked list? The answer is No. You will have to write your own,
possibly inefficient algorithm to get the value of the middle node of a linked list. In a
linked list, you loosse the ability to get the value of any node in a constant time.
One solution to the inefficiency of getting the middle of the linked list during a binary
search is to have the first node contain one additional pointer that points to the node in the
middle. Decide at the first node if you need to check the first or the second half of the
linked list. Continue doing that with each half-list.
14.Write a C program to return the nth node from the end of a linked list.
Here is a solution which is often called as the solution that uses frames.
Suppose one needs to get to the 6th node from the end in this LL. First, just keep on
incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in
this case)
STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10
Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1)
reaches the end of the LL.
STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1)
So here you have! the 6th node from the end pointed to by ptr2!
Here is some C code..
struct node
{
int data;
struct node *next;
}mynode;
mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2 ;
int count ;
if( !head )
{
return( NULL ) ;
}
ptr1 = head ;
ptr2 = head ;
count = 0 ;
while( count < n )
{
count++ ;
if ( ( ptr1 = ptr1 -> next ) == NULL )
{
//Length of the linked list less than n. Error.
return( NULL );
}
}
while( ( ptr1 = ptr1 -> next ) != NULL )
{
ptr2 = ptr2 -> next ;
}r
eturn( ptr2 );
}
15. How would you find out if one of the pointers in a linked list is corrupted or not?
This is a really good interview question. The reason is that linked lists are used in a wide variety of scenarios and being able to detect and correct pointer corruptions might be a very valuable tool. For example, data blocks associated with files in a file system are usually stored as linked lists. Each data block points to the next data block. A single corrupt pointer can cause the entire file to be lost!Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part
of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it). · It is good programming practice to set the pointer value to NULL immediately
after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required. · Use a good debugger to see how the datastructures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily. · Avoid global variables when traversing and manipulating linked lists. Imagine what would happen if a function which is only supposed to traverse a linked list using a global head pointer accidently sets the head pointer to NULL!. · Its a good idea to check the addNode() and the deleteNode() routines and test them for all types of scenarios. This should include tests for inserting/deleting nodes at the front/middle/end of the linked list, working with an empty linked list, running out of memory when using malloc() when allocating memory for new nodes, writing through NULL pointers, writing more data into the node fields then they can hold (resulting in corrupting the (probably adjacent) "prev" and "next" pointer fields), make sure bug fixes and enhancements to the linked list code are reviewed and well tested (a lot of bugs come from quick and dirty bug fixing), log and handle all possible errors (this will help you a lot while debugging), add multiple levels of logging so that you can dig through the logs. The list is endless... · Each node can have an extra field associated* Linked Lists
·         How do you reverse a singly linked list? How do you reverse a doubly linked
list? Write a C program to do the same. Updated!
·         Given only a pointer to a node to be deleted in a singly linked list, how do you
delete it? Updated!
·         How do you sort a linked list? Write a C program to sort a linked list.
·         How to declare a structure of a linked list?
·         Write a C program to implement a Generic Linked List.
·         How do you reverse a linked list without using any C pointers? Updated!
·         How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
·         How do you find the middle of a linked list? Write a C program to return themiddle of a linked list
·         If you are using C language to implement the heterogeneous linked list, what pointer type will you use?
·         How to compare two linked lists? Write a C program to compare two linked lists.
·         How to create a copy of a linked list? Write a C program to create a copy of a linked list.
·         Write a C program to free the nodes of a linked list Updated!
·         Can we do a Binary search on a linked list?
·         Write a C program to return the nth node from the end of a linked list. New!
·         How would you find out if one of the pointers in a linked list is corrupted or not? Write a C program to do the same.
Here are a few C programs to reverse a singly linked list.
Method1 (Iterative)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals (not required, though).
mynode *head, *tail, *temp;
// Functions
void add(int value);
void iterative_reverse();
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
iterative_reverse();
//Print it again
print_list();
return(0);
}
// The reverse function
void iterative_reverse()
{
mynode *p, *q, *r;
if(head == (mynode *)0)
{
return;
}
p = head;
q = p->next;
p->next = (mynode *)0;
while (q != (mynode *)0)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head = p;
}
// Function to add new nodes to the linked list
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method2 (Recursive, without using any temporary variable)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals.
mynode *head, *tail, *temp;
// Functions
void add(int value);
mynode* reverse_recurse(mynode *root);
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
if(head != (mynode *)0)
{
temp = reverse_recurse(head);
temp->next = (mynode *)0;
}
//Print it again
print_list();
return(0);
}
// Reverse the linked list recursively
//
// This function uses the power of the stack to make this
// *magical* assignment
//
// node->next->next=node;
//
// :)
mynode* reverse_recurse(mynode *root)
{
if(root->next!=(mynode *)0)
{
reverse_recurse(root->next);
root->next->next=root;
return(root);
}
else
{
head=root;
}
}
// Function to add new nodes to the linked list.
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method3 (Recursive, but without ANY global variables. Slightly messy!)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Functions
void add(mynode **head, mynode **tail, int value);
mynode* reverse_recurse(mynode *current, mynode *next);
void print_list(mynode *);
int main()
{
mynode *head, *tail;
head=(mynode *)0;
// Construct the linked list.
add(&head, &tail, 1);
add(&head, &tail, 2);
add(&head, &tail, 3);
//Print it
print_list(head);
// Reverse it.
head = reverse_recurse(head, (mynode *)0);
//Print it again
print_list(head);
getch();
return(0);
}
// Reverse the linked list recursively
mynode* reverse_recurse(mynode *current, mynode *next)
{
mynode *ret;
if(current==(mynode *)0)
{
return((mynode *)0);
}
ret = (mynode *)0;
if (current->next != (mynode *)0)
{
ret = reverse_recurse(current->next, current);
}
else
{
ret = current;
}
current->next = next;
return ret;
}
// Function to add new nodes to the linked list.
// Takes pointers to pointers to maintain the
// *actual* head and tail pointers (which are local to main()).
void add(mynode **head, mynode **tail, int value)
{
mynode *temp1, *temp2;
temp1 = (mynode *) malloc(sizeof(struct node));
temp1->next=(mynode *)0;
temp1->value=value;
if(*head==(mynode *)0)
{
*head=temp1;
*tail=temp1;
}
else
{
for(temp2 = *head; temp2->next!= (mynode *)0; temp2=temp2->next);
temp2->next = temp1;
*tail=temp1;
}
}
// Function to print the linked list.
void print_list(mynode *head)
{
mynode *temp;
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Doubly linked lists
This is really easy, just keep swapping the prev and next pointers and at the end swap the
head and the tail:)
#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;
mynode *head, *tail;
void add_node(int value);
void print_list();
void reverse();
int main()
{
head=NULL;
tail=NULL;
add_node(1);
add_node(2);
add_node(3);
add_node(4);
add_node(5);
print_list();
reverse();
print_list();
return(1);
}
void add_node(int value)
{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));
temp->next=NULL;
temp->prev=NULL;
if(head == NULL)
{
printf("\nAdding a head pointer\n");
head=temp;
tail=temp;
temp->value=value;
}
else
{
for(cur=head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
tail=temp;
}
}
void print_list()
{
mynode *temp;
printf("\n--------------------------------\n");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("\n[%d]\n",temp->value);
}
}
void reverse()
{
mynode *cur, *temp, *save_next;
if(head==tail)return;
if(head==NULL || tail==NULL)
return;
for(cur=head;cur!=NULL;)
{
printf("\ncur->value : [%d]\n",cur->value);
temp=cur->next;
save_next=cur->next;
cur->next=cur->prev;
cur->prev=temp;
cur=save_next;
} temp=head;
head=tail;
tail=temp;
}
2. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
This is a very good interview question
The solution to this is to copy the data from the next node into this node and delete the
next node!. Of course this won’t work if the node to be deleted is the last node. Mark it as
Dummy in that case. If you have a Circular linked list, then this might be all the more
Interesting. Try writing your own C program to solve this problem. Having a doubly
Linked list is always better.
3. How do you sort a linked list? Write a C program to sort a linked list.
This is a very popular interview question, which most people go wrong. The ideal
Solution to this problem is to keep the linked list sorted as you build it. This really saves a
Lot of time which would have been required to sort it.
However....
Method1 (Usual method)
The general idea is to decide upon a sorting algorithm (say bubble sort). Then, one needs
to come up with different scenarios to swap two nodes in the linked list when they are not
in the required order. The different scenarios would be something like
1. When the nodes being compared are not adjacent and one of them is the first node.
2. When the nodes being compared are not adjacent and none of them is the first node
3. When the nodes being compared are adjacent and one of them is the first node.
4. When the nodes being compared are adjacent and none of them is the first node.
One example bubble sort for a linked list goes like this
for(i = 1; i < n; i++)
{
p1 = head;
p2 = head->next;
p3 = p2->next;
for(j = 1; j <= (n - i); j++)
{
if(p2->value < p3->value)
{
p2->next = p3->next;
p3->next = p2;
p1->next = p3;
p1 = p3;
p3 = p2->next;
}
else
{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
}
As you can see, the code becomes quite messy because of the pointer logic. Thats why I
have not elaborated too much on the code, nor on variations such as soring a doubly
linked list. You have to do it yourself once to understand it.
Method1 (Divide and Conquer using merge sort)
The pseudocode for this method is
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail;
int size;
mynode *mergesort(mynode *list, int size);
void display(mynode *list);
mynode *mergesort(mynode *list, int size)
{
int size1, size2;
mynode *tempnode1, *tempnode2, *tempnode3;
if( size<=2 )
{
if(size==1)
{
// Nothing to sort!
return(list);
}
else
{
if(list->value < list->next->value
{
// These 2 nodes are already in right order, no need to sort
return(list);
}
else
{
// Need to swap these 2 nodes
/* Here we have 2 nodes
*
*node 1 -> node2 -> NULL
*
* This should be converted to
*
* node2 -> node1 -> NULL
*
*/
tempnode1 = list;
tempnode2 = list->next;
tempnode2->next = tempnode1;
tempnode1->next = NULL;
return(tempnode2);
}
}
}
else
{
// The size of the linked list is more than 2.
// Need to split this linked list, sort the
// left and right sub-linked lists and merge.
// Split.
// tempnode1 will have the first half of the linked list of size "size1".
// tempnode2 will have the second half of the linked list of size "size2".
<CODE TO SPLIT THE LINKED LIST INTO TWO>
// Sort the two halves recursively
tempnode1 = mergesort(tempnode1, size1);
tempnode2 = mergesort(tempnode2, size2);
// Now merge the sorted lists back, let tempnode3 point to that new list.
<CODE TO MERGE THE 2 LINKED LISTS BACK INTO A SINGLE
SORTED LINKED LIST>
return(tempnode3);
}
}
The code to merge the two already sorted sub-linked lists into a sorted linked list could be
something like this..
mynode * merge(mynode *a, mynode *b)
{
mynode *i, *j, *k, *c;
i = a;
j = b;
c = getNewNode();
k = getNewNode();
while(i != NULL && j != NULL)
{
if( i -> value < j -> value )
{
k -> next = i;
i = i -> next;
}
else
{
k -> next = j;
j = j -> next;
}
}
if( i != NULL)
k -> next = i ;
else
k -> next = j;
return( c -> next );
}
4.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!
5.Write a C program to implement a Generic Linked List.
Here is a C program which implements a generic linked list. 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 );
}
6. 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).
7.How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
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;
}r
eturn 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.
8.How do you find the middle of a linked list? Write a C program to return the
middle of a linked list
Another popular interview question
Here are a few C program snippets to give you an idea of the possible solutions.
Method1
p = head;
q = head;
if( q -> next -> next != NULL)
{
p = p -> next;
q = q -> next -> next;
}
printf("The middle element is %d",p->data);
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
struct node *middle(struct node *head)
{
struct node *middle=NULL;
int i;
for( i = 1 ; head ; head = head -> next , i++)
{
if( i == 1 )
middle = head ;
else if ( ( i % 2 ) == 1 )
middle = middle -> next ;
}r
eturn middle;
} 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 ).
9.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.
Check out the C program to implement a Generic linked list in the same FAQ.
10.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 ) ;
}
}r
eturn ( 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).
11.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 ) );
}
}
12.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 ) ;
}
13. Can we do a Binary search on a linked list
The answer is ofcourse, you can write a C program to do this. But, the question is, do you
really think it will be as efficient as a C program which does a binary search on an array?
Think hard, real hard.
Do you know what exactly makes the binary search on an array so fast and efficient? Its
the ability to access any element in the array in constant time. This is what makes it so
fast. You can get to the middle of the array just by saying array[middle]!. Now, can you
do the same with a linked list? The answer is No. You will have to write your own,
possibly inefficient algorithm to get the value of the middle node of a linked list. In a
linked list, you loosse the ability to get the value of any node in a constant time.
One solution to the inefficiency of getting the middle of the linked list during a binary
search is to have the first node contain one additional pointer that points to the node in the
middle. Decide at the first node if you need to check the first or the second half of the
linked list. Continue doing that with each half-list.
14.Write a C program to return the nth node from the end of a linked list.
Here is a solution which is often called as the solution that uses frames.
Suppose one needs to get to the 6th node from the end in this LL. First, just keep on
incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in
this case)
STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10
Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1)
reaches the end of the LL.
STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1)
So here you have! the 6th node from the end pointed to by ptr2!
Here is some C code..
struct node
{
int data;
struct node *next;
}mynode;
mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2 ;
int count ;
if( !head )
{
return( NULL ) ;
}
ptr1 = head ;
ptr2 = head ;
count = 0 ;
while( count < n )
{
count++ ;
if ( ( ptr1 = ptr1 -> next ) == NULL )
{
//Length of the linked list less than n. Error.
return( NULL );
}
}
while( ( ptr1 = ptr1 -> next ) != NULL )
{
ptr2 = ptr2 -> next ;
}r
eturn( ptr2 );
}
15. How would you find out if one of the pointers in a linked list is corrupted or not?
This is a really good interview question. The reason is that linked lists are used in a wide variety of scenarios and being able to detect and correct pointer corruptions might be a very valuable tool. For example, data blocks associated with files in a file system are usually stored as linked lists. Each data block points to the next data block. A single corrupt pointer can cause the entire file to be lost!Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part
of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it). · It is good programming practice to set the pointer value to NULL immediately
after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required. · Use a good debugger to see how the datastructures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily. · Avoid global variables when traversing and manipulating linked lists. Imagine what would happen if a function which is only supposed to traverse a linked list using a global head pointer accidently sets the head pointer to NULL!. · Its a good idea to check the addNode() and the deleteNode() routines and test them for all types of scenarios. This should include tests for inserting/deleting nodes at the front/middle/end of the linked list, working with an empty linked list, running out of memory when using malloc() when allocating memory for new nodes, writing through NULL pointers, writing more data into the node fields then they can hold (resulting in corrupting the (probably adjacent) "prev" and "next" pointer fields), make sure bug fixes and enhancements to the linked list code are reviewed and well tested (a lot of bugs come from quick and dirty bug fixing), log and handle all possible errors (this will help you a lot while debugging), add multiple levels of logging so that you can dig through the logs. The list is endless... · Each node can have an extra field associated with it. This field indicates the
number of nodes after this node in the linked list. This extra field needs to be kept up-to-date when we inserte or delete nodes in the linked list (It might become slightly complicated when insertion or deletion happens not at end, but anywhere in the linked list). Then, if for any node, p->field > 0 and p->next == NULL, it surely points to a pointer corruption. · You could also keep the count of the total number of nodes in a linked list and use it to check if the list is indeed having those many nodes or not.
The problem in detecting such pointer corruptions in C is that its only the programmer who knows that the pointer is corrupted. The program has no way of knowing that something is wrong. So the best way to fix these errors is check your logic and test your code to the maximum possible extent. I am not aware of ways in C to recover the lost Nodes of a corrupted linked list. I have a hunch that interviewers who ask this question are probably hinting at something Called Smart Pointers in C++. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. This topic is out of scope here, but you can find lots of material on the Internet for Smart Pointers. If you have bett* Linked Lists
·         How do you reverse a singly linked list? How do you reverse a doubly linked
list? Write a C program to do the same. Updated!
·         Given only a pointer to a node to be deleted in a singly linked list, how do you
delete it? Updated!
·         How do you sort a linked list? Write a C program to sort a linked list.
·         How to declare a structure of a linked list?
·         Write a C program to implement a Generic Linked List.
·         How do you reverse a linked list without using any C pointers? Updated!
·         How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
·         How do you find the middle of a linked list? Write a C program to return themiddle of a linked list
·         If you are using C language to implement the heterogeneous linked list, what pointer type will you use?
·         How to compare two linked lists? Write a C program to compare two linked lists.
·         How to create a copy of a linked list? Write a C program to create a copy of a linked list.
·         Write a C program to free the nodes of a linked list Updated!
·         Can we do a Binary search on a linked list?
·         Write a C program to return the nth node from the end of a linked list. New!
·         How would you find out if one of the pointers in a linked list is corrupted or not? Write a C program to do the same.
Here are a few C programs to reverse a singly linked list.
Method1 (Iterative)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals (not required, though).
mynode *head, *tail, *temp;
// Functions
void add(int value);
void iterative_reverse();
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
iterative_reverse();
//Print it again
print_list();
return(0);
}
// The reverse function
void iterative_reverse()
{
mynode *p, *q, *r;
if(head == (mynode *)0)
{
return;
}
p = head;
q = p->next;
p->next = (mynode *)0;
while (q != (mynode *)0)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head = p;
}
// Function to add new nodes to the linked list
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method2 (Recursive, without using any temporary variable)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Globals.
mynode *head, *tail, *temp;
// Functions
void add(int value);
mynode* reverse_recurse(mynode *root);
void print_list();
// The main() function
int main()
{
head=(mynode *)0;
// Construct the linked list.
add(1);
add(2);
add(3);
//Print it
print_list();
// Reverse it.
if(head != (mynode *)0)
{
temp = reverse_recurse(head);
temp->next = (mynode *)0;
}
//Print it again
print_list();
return(0);
}
// Reverse the linked list recursively
//
// This function uses the power of the stack to make this
// *magical* assignment
//
// node->next->next=node;
//
// :)
mynode* reverse_recurse(mynode *root)
{
if(root->next!=(mynode *)0)
{
reverse_recurse(root->next);
root->next->next=root;
return(root);
}
else
{
head=root;
}
}
// Function to add new nodes to the linked list.
void add(int value)
{
temp = (mynode *) malloc(sizeof(struct node));
temp->next=(mynode *)0;
temp->value=value;
if(head==(mynode *)0)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
}
// Function to print the linked list.
void print_list()
{
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Method3 (Recursive, but without ANY global variables. Slightly messy!)
#include <stdio.h>
// Variables
typedef struct node
{
int value;
struct node *next;
}mynode;
// Functions
void add(mynode **head, mynode **tail, int value);
mynode* reverse_recurse(mynode *current, mynode *next);
void print_list(mynode *);
int main()
{
mynode *head, *tail;
head=(mynode *)0;
// Construct the linked list.
add(&head, &tail, 1);
add(&head, &tail, 2);
add(&head, &tail, 3);
//Print it
print_list(head);
// Reverse it.
head = reverse_recurse(head, (mynode *)0);
//Print it again
print_list(head);
getch();
return(0);
}
// Reverse the linked list recursively
mynode* reverse_recurse(mynode *current, mynode *next)
{
mynode *ret;
if(current==(mynode *)0)
{
return((mynode *)0);
}
ret = (mynode *)0;
if (current->next != (mynode *)0)
{
ret = reverse_recurse(current->next, current);
}
else
{
ret = current;
}
current->next = next;
return ret;
}
// Function to add new nodes to the linked list.
// Takes pointers to pointers to maintain the
// *actual* head and tail pointers (which are local to main()).
void add(mynode **head, mynode **tail, int value)
{
mynode *temp1, *temp2;
temp1 = (mynode *) malloc(sizeof(struct node));
temp1->next=(mynode *)0;
temp1->value=value;
if(*head==(mynode *)0)
{
*head=temp1;
*tail=temp1;
}
else
{
for(temp2 = *head; temp2->next!= (mynode *)0; temp2=temp2->next);
temp2->next = temp1;
*tail=temp1;
}
}
// Function to print the linked list.
void print_list(mynode *head)
{
mynode *temp;
printf("\n\n");
for(temp=head; temp!=(mynode *)0; temp=temp->next)
{
printf("[%d]->",(temp->value));
}
printf("[NULL]\n\n");
}
Doubly linked lists
This is really easy, just keep swapping the prev and next pointers and at the end swap the
head and the tail:)
#include<stdio.h>
#include<ctype.h>
typedef struct node
{
int value;
struct node *next;
struct node *prev;
}mynode ;
mynode *head, *tail;
void add_node(int value);
void print_list();
void reverse();
int main()
{
head=NULL;
tail=NULL;
add_node(1);
add_node(2);
add_node(3);
add_node(4);
add_node(5);
print_list();
reverse();
print_list();
return(1);
}
void add_node(int value)
{
mynode *temp, *cur;
temp = (mynode *)malloc(sizeof(mynode));
temp->next=NULL;
temp->prev=NULL;
if(head == NULL)
{
printf("\nAdding a head pointer\n");
head=temp;
tail=temp;
temp->value=value;
}
else
{
for(cur=head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->prev=cur;
temp->value=value;
tail=temp;
}
}
void print_list()
{
mynode *temp;
printf("\n--------------------------------\n");
for(temp=head;temp!=NULL;temp=temp->next)
{
printf("\n[%d]\n",temp->value);
}
}
void reverse()
{
mynode *cur, *temp, *save_next;
if(head==tail)return;
if(head==NULL || tail==NULL)
return;
for(cur=head;cur!=NULL;)
{
printf("\ncur->value : [%d]\n",cur->value);
temp=cur->next;
save_next=cur->next;
cur->next=cur->prev;
cur->prev=temp;
cur=save_next;
} temp=head;
head=tail;
tail=temp;
}
2. Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
This is a very good interview question
The solution to this is to copy the data from the next node into this node and delete the
next node!. Of course this won’t work if the node to be deleted is the last node. Mark it as
Dummy in that case. If you have a Circular linked list, then this might be all the more
Interesting. Try writing your own C program to solve this problem. Having a doubly
Linked list is always better.
3. How do you sort a linked list? Write a C program to sort a linked list.
This is a very popular interview question, which most people go wrong. The ideal
Solution to this problem is to keep the linked list sorted as you build it. This really saves a
Lot of time which would have been required to sort it.
However....
Method1 (Usual method)
The general idea is to decide upon a sorting algorithm (say bubble sort). Then, one needs
to come up with different scenarios to swap two nodes in the linked list when they are not
in the required order. The different scenarios would be something like
1. When the nodes being compared are not adjacent and one of them is the first node.
2. When the nodes being compared are not adjacent and none of them is the first node
3. When the nodes being compared are adjacent and one of them is the first node.
4. When the nodes being compared are adjacent and none of them is the first node.
One example bubble sort for a linked list goes like this
for(i = 1; i < n; i++)
{
p1 = head;
p2 = head->next;
p3 = p2->next;
for(j = 1; j <= (n - i); j++)
{
if(p2->value < p3->value)
{
p2->next = p3->next;
p3->next = p2;
p1->next = p3;
p1 = p3;
p3 = p2->next;
}
else
{
p1 = p2;
p2 = p3;
p3 = p3->next;
}
}
}
As you can see, the code becomes quite messy because of the pointer logic. Thats why I
have not elaborated too much on the code, nor on variations such as soring a doubly
linked list. You have to do it yourself once to understand it.
Method1 (Divide and Conquer using merge sort)
The pseudocode for this method is
typedef struct node
{
int value;
struct node *next;
}mynode;
mynode *head, *tail;
int size;
mynode *mergesort(mynode *list, int size);
void display(mynode *list);
mynode *mergesort(mynode *list, int size)
{
int size1, size2;
mynode *tempnode1, *tempnode2, *tempnode3;
if( size<=2 )
{
if(size==1)
{
// Nothing to sort!
return(list);
}
else
{
if(list->value < list->next->value
{
// These 2 nodes are already in right order, no need to sort
return(list);
}
else
{
// Need to swap these 2 nodes
/* Here we have 2 nodes
*
*node 1 -> node2 -> NULL
*
* This should be converted to
*
* node2 -> node1 -> NULL
*
*/
tempnode1 = list;
tempnode2 = list->next;
tempnode2->next = tempnode1;
tempnode1->next = NULL;
return(tempnode2);
}
}
}
else
{
// The size of the linked list is more than 2.
// Need to split this linked list, sort the
// left and right sub-linked lists and merge.
// Split.
// tempnode1 will have the first half of the linked list of size "size1".
// tempnode2 will have the second half of the linked list of size "size2".
<CODE TO SPLIT THE LINKED LIST INTO TWO>
// Sort the two halves recursively
tempnode1 = mergesort(tempnode1, size1);
tempnode2 = mergesort(tempnode2, size2);
// Now merge the sorted lists back, let tempnode3 point to that new list.
<CODE TO MERGE THE 2 LINKED LISTS BACK INTO A SINGLE
SORTED LINKED LIST>
return(tempnode3);
}
}
The code to merge the two already sorted sub-linked lists into a sorted linked list could be
something like this..
mynode * merge(mynode *a, mynode *b)
{
mynode *i, *j, *k, *c;
i = a;
j = b;
c = getNewNode();
k = getNewNode();
while(i != NULL && j != NULL)
{
if( i -> value < j -> value )
{
k -> next = i;
i = i -> next;
}
else
{
k -> next = j;
j = j -> next;
}
}
if( i != NULL)
k -> next = i ;
else
k -> next = j;
return( c -> next );
}
4.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!
5.Write a C program to implement a Generic Linked List.
Here is a C program which implements a generic linked list. 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 );
}
6. 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).
7.How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.
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;
}r
eturn 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.
8.How do you find the middle of a linked list? Write a C program to return the
middle of a linked list
Another popular interview question
Here are a few C program snippets to give you an idea of the possible solutions.
Method1
p = head;
q = head;
if( q -> next -> next != NULL)
{
p = p -> next;
q = q -> next -> next;
}
printf("The middle element is %d",p->data);
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
struct node *middle(struct node *head)
{
struct node *middle=NULL;
int i;
for( i = 1 ; head ; head = head -> next , i++)
{
if( i == 1 )
middle = head ;
else if ( ( i % 2 ) == 1 )
middle = middle -> next ;
}r
eturn middle;
} 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 ).
9.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.
Check out the C program to implement a Generic linked list in the same FAQ.
10.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 ) ;
}
}r
eturn ( 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).
11.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 ) );
}
}
12.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 ) ;
}
13. Can we do a Binary search on a linked list
The answer is ofcourse, you can write a C program to do this. But, the question is, do you
really think it will be as efficient as a C program which does a binary search on an array?
Think hard, real hard.
Do you know what exactly makes the binary search on an array so fast and efficient? Its
the ability to access any element in the array in constant time. This is what makes it so
fast. You can get to the middle of the array just by saying array[middle]!. Now, can you
do the same with a linked list? The answer is No. You will have to write your own,
possibly inefficient algorithm to get the value of the middle node of a linked list. In a
linked list, you loosse the ability to get the value of any node in a constant time.
One solution to the inefficiency of getting the middle of the linked list during a binary
search is to have the first node contain one additional pointer that points to the node in the
middle. Decide at the first node if you need to check the first or the second half of the
linked list. Continue doing that with each half-list.
14.Write a C program to return the nth node from the end of a linked list.
Here is a solution which is often called as the solution that uses frames.
Suppose one needs to get to the 6th node from the end in this LL. First, just keep on
incrementing the first pointer (ptr1) till the number of increments cross n (which is 6 in
this case)
STEP 1 : 1(ptr1,ptr2) -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
STEP 2 : 1(ptr2) -> 2 -> 3 -> 4 -> 5 -> 6(ptr1) -> 7 -> 8 -> 9 -> 10
Now, start the second pointer (ptr2) and keep on incrementing it till the first pointer (ptr1)
reaches the end of the LL.
STEP 3 : 1 -> 2 -> 3 -> 4(ptr2) -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 (ptr1)
So here you have! the 6th node from the end pointed to by ptr2!
Here is some C code..
struct node
{
int data;
struct node *next;
}mynode;
mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2 ;
int count ;
if( !head )
{
return( NULL ) ;
}
ptr1 = head ;
ptr2 = head ;
count = 0 ;
while( count < n )
{
count++ ;
if ( ( ptr1 = ptr1 -> next ) == NULL )
{
//Length of the linked list less than n. Error.
return( NULL );
}
}
while( ( ptr1 = ptr1 -> next ) != NULL )
{
ptr2 = ptr2 -> next ;
}r
eturn( ptr2 );
}
15. How would you find out if one of the pointers in a linked list is corrupted or not?
This is a really good interview question. The reason is that linked lists are used in a wide variety of scenarios and being able to detect and correct pointer corruptions might be a very valuable tool. For example, data blocks associated with files in a file system are usually stored as linked lists. Each data block points to the next data block. A single corrupt pointer can cause the entire file to be lost!Discover and fix bugs when they corrupt the linked list and not when effect becomes visible in some other part
of the program. Perform frequent consistency checks (to see if the linked list is indeed holding the data that you inserted into it). · It is good programming practice to set the pointer value to NULL immediately
after freeing the memory pointed at by the pointer. This will help in debugging, because it will tell you that the object was freed somewhere beforehand. Keep track of how many objects are pointing to a object using reference counts if required. · Use a good debugger to see how the datastructures are getting corrupted and trace down the problem. Debuggers like ddd on linux and memory profilers like Purify, Electric fence are good starting points. These tools should help you track down heap corruption issues easily. · Avoid global variables when traversing and manipulating linked lists. Imagine what would happen if a function which is only supposed to traverse a linked list using a global head pointer accidently sets the head pointer to NULL!. · Its a good idea to check the addNode() and the deleteNode() routines and test them for all types of scenarios. This should include tests for inserting/deleting nodes at the front/middle/end of the linked list, working with an empty linked list, running out of memory when using malloc() when allocating memory for new nodes, writing through NULL pointers, writing more data into the node fields then they can hold (resulting in corrupting the (probably adjacent) "prev" and "next" pointer fields), make sure bug fixes and enhancements to the linked list code are reviewed and well tested (a lot of bugs come from quick and dirty bug fixing), log and handle all possible errors (this will help you a lot while debugging), add multiple levels of logging so that you can dig through the logs. The list is endless... · Each node can have an extra field associated with it. This field indicates the
number of nodes after this node in the linked list. This extra field needs to be kept up-to-date when we inserte or delete nodes in the linked list (It might become slightly complicated when insertion or deletion happens not at end, but anywhere in the linked list). Then, if for any node, p->field > 0 and p->next == NULL, it surely points to a pointer corruption. · You could also keep the count of the total number of nodes in a linked list and use it to check if the list is indeed having those many nodes or not.
The problem in detecting such pointer corruptions in C is that its only the programmer who knows that the pointer is corrupted. The program has no way of knowing that something is wrong. So the best way to fix these errors is check your logic and test your code to the maximum possible extent. I am not aware of ways in C to recover the lost Nodes of a corrupted linked list. I have a hunch that interviewers who ask this question are probably hinting at something Called Smart Pointers in C++. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. This topic is out of scope here, but you can find lots of material on the Internet for Smart Pointers. If you have better answers to this question, let me know!

 er answers to this question, let me know!

  with it. This field indicates the
number of nodes after this node in the linked list. This extra field needs to be kept up-to-date when we inserte or delete nodes in the linked list (It might become slightly complicated when insertion or deletion happens not at end, but anywhere in the linked list). Then, if for any node, p->field > 0 and p->next == NULL, it surely points to a pointer corruption. · You could also keep the count of the total number of nodes in a linked list and use it to check if the list is indeed having those many nodes or not.
The problem in detecting such pointer corruptions in C is that its only the programmer who knows that the pointer is corrupted. The program has no way of knowing that something is wrong. So the best way to fix these errors is check your logic and test your code to the maximum possible extent. I am not aware of ways in C to recover the lost Nodes of a corrupted linked list. I have a hunch that interviewers who ask this question are probably hinting at something Called Smart Pointers in C++. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. This topic is out of scope here, but you can find lots of material on the Internet for Smart Pointers. If you have better answers to this question, let me know!



Thursday, June 2, 2011

Complex X86 instructions

Complex instructions
Avoid complex instructions ( lods, stos, movs, cmps, scas, loop, xadd, enter, leave). Complex instructions are instructions that do multiple things. For instance stosb writes a byte to memory and also increments EDI. They stopped making these fast with the original Pentium because they were trying to make it more RISC like. Using REP and the string instructions is still fast. That is the only exception to the case.

Don't use INC/DEC on P4
On the P4 use ADD/SUB in place of INC/DEC. Generally it is faster. ADD/SUB runs in 0.5 cycles. INC/DEC takes 1 cycle.

Rotating
Avoid rotate by a register or rotate by an immediate value of anything but a 1.

Eliminate unnecessary compare instructions
Eliminate unnecessary compare instructions by doing the appropriate conditional jump instruction based on the flags that are already set from a previous arithmetic instruction.

        dec     ecx
        cmp     ecx,0
        jnz     loop_again

;gets changed to
        dec     ecx
        jnz     loop_again
LEA is still really cool, except for on the P4 it tends to be slow.
You can perform multiple math operations all in one instruction and it does not affect the flags register so you can put in in between one register being modified and a flags comparison jump on the next line.

top_of_loop:

    dec   eax
    lea   edx,[edx*4+3]     ; multiply by 4 and add 3. Does not affect flags
    jnz   top_of_loop       ; so the next instruction doesn't get hosed.
ADC and SBB.
Most compilers don't really make good use of ADC and SBB. You can get good speeds ups with that. Adding 2 64-bit numbers together, or adding big numbers together. Keep in mind that on the P4 ADC and SBB are slow. As a work around you can use "addq", and use MMX to do this. So the second optimization suggestion for this is to use MMX to do the adding or subtracting. You just have to have a processor that supports MMX.

    add    eax,[fred]
    adc    edx,[fred+4]

    ; the above 2 statements are the same as the below 3 statements

    movd   mm0,[fred]       ; Get 32-bit value in MM0
    movd   mm1,[fred+4]     ; Get 32-bit value in MM1
    paddq  mm0,mm1          ; This is an unoptimized way to do it. You would
                            ; really pre-read MM0 and MM1 a loop in advance.
                            ; I did it this way for ease of understanding.
ROL, ROR, RCL, and RCR and BSWAP.
It is a cool trick to switch from Big Endian to Little Endian using BSWAP. Also you can use it for temporary storage of a 16-bit or 8-bit value in the upper half of the register. Likewise you can use ROL and ROR for storing 8-bit and 16-bit values. It's a way to get more "registers". If all you are dealing with are 16-bit values, you can turn your 8 32-bit registers into 16 16-bit registers. Which gives you a lot more registers to use. RCL and RCR can also easily be used for counting the number of bits that are set in a register. Keep in mind that ROL, ROR, RCL, RCR and BSWAP are all slow on the P4. The rotate instructions are about twice as fast as BSWAP. So if you have to use one or the other on the P4 use the rotate ones.

    xor   edx,edx           ; set both 16-bit registers to 0
    mov   dx,234            ; set the first 16-bit register to 234
    bswap edx               ; swap it so the second one is ready
    mov   dx,345            ; set the second 16-bit register to 345
    bswap edx               ; swap to the first one
    add   dx,5              ; add 5 to the first one
    bswap edx               ; swap to the second one
    add   dx,7              ; add 7 to it
String instructions.
Most compilers don't make good use of the string instructions ( scas, cmps, stos, movs, and lods). So checking to see if that is faster than some library routine can be a win. For instance I was really surprised when I looked at strlen() in VC++. In the radix40 code it ran in 416 cycles for a 100 byte string!!! I thought that was absurdly slow.

Multiply to divide.
If you have a full 32-bit number and you need to divide, you can simply do a multiply and take the top 32-bit half as the result. This is faster because multiplication is faster than division. ( thanks to pdixon for the tip).

Dividing by a constant.
There's some nice information now how to divide by a constant in Agner Fog's pentopt.pdf document. I wrote a program that you pass in the number you want to divide by, and it will print out the assembler code sequence. I will dig it up later and post it. Here is a link to Agner's document. Agner's Pentopt PDF (http://www.agner.org/assem/)

Unrolling.
This is a guideline. Unrolling falls in the General Optimiation category but I wanted to add a footnote. I always set up my Unrolling with a macro that unrolls an EQUATE value amount. That way you can try different values and see which is best easily. You want the unrolling to fit in the L1 code cache ( or trace cache). Using an equate makes it easy to try different unroll amounts to find the fastest one.

UNROLL_AMT       equ   16   ; # of times to unroll the loop
UNROLL_NUM_BYTES equ    4   ; # of bytes handled in 1 loop iteration

        mov     ecx,1024
looper:
offset2 = 0
REPEAT UNROLL_AMT
        add     eax,[edi+offset2]
offset2 = offset2 + UNROLL_NUM_BYTES
        add     edi,UNROLL_AMT * UNROLL_NUM_BYTES   ; we dealt with 16*4 bytes.
        sub     ecx,UNROLL_AMT  ; subtract from loop counter the # of loops we unrolled.
        jnz     looper
MOVZX.
Use MOVZX to avoid partial register stalls. I use MOVZX a lot. A lot of people XOR the full 32-bit register first. But MOVZX does the equivalent thing without having to have an extra XOR instruction. Also you had to do the XOR enough in advance to give it time to complete. With MOVZX you don't have to worry about that.

Using MOVZX to avoid a SHIFT and AND instruction
I ran across this bit of C code I was trying to speed up using assembler. The_array is a dword array. The code is trying to get a different byte from a dword in the array passed upon which pass this is over the data. "Pass" is a variable that goes from 0 to 3 for each byte in a particular dword.

        unsigned char c = ((the_array[i])>>(Pass<<3)) & 0xFF;

; I got rid of the "pass" variable by unrolling the loop 4 times.
; So I had 4 of these each one seperated by lots of C code.
        unsigned char c = (the_array[i])>>0) & 0xFF;
        unsigned char c = (the_array[i])>>8) & 0xFF;
        unsigned char c = (the_array[i])>>16) & 0xFF;
        unsigned char c = (the_array[i])>>24) & 0xFF;
What if I can get rid of the SHIFT and the AND using assembler? That would save me 2 instructions. Not to mention the fact that the P4 is very slow when doing SHIFT instructions ( 4 cycles!!!). So try to avoid shifts where possible. SO taking just the second to last line that shifts right 16 as our example
; esi points to the_array

        mov     eax,[esi]
        shr     eax,16
        and     eax,0FFh

; So how do we change that to get rid of the AND and SHR?
; We do a MOVZx with the 3rd byte in the dword.

        movzx   eax,byte ptr [esi+2]            ;unsigned char c = (the_array[i])>>16) & 0xFF;

Align, align, align.
It is really important to align both your code and data to get a good speed up. I generally align code on 4 byte boundaries. For data I align 2 byte data on 2 byte boundaries, 4 byte data on 4 byte boundaries, 8 byte data on 8 byte boundaries, 16 byte data on 16 byte boundaries. In general if you don't align your SSE or SSE2 data on a 16-byte boundary you will get an exception. You can align your data in VC++ if you have the processor pack. They added support for both static data and dynamic memory. For static data you use __declspec(align(4)) - alignes on a 4 byte boundary.

BSR for powers of 2.
You can use BSR to count the highest power of 2 that goes into a variable.

XORing a register with itself to zero it.
This is an oldie, but I am including it anyway. It also has a side benefit of clearing dependencies on the register. That is why sometimes you will see people use XOR in that fashion, before doing a partial register access. I prefer using MOVZX to doing it that way because it is trickier to do using a XOR ( read my above comments about in #12 above talking about MOVZX) . On the P4 they also added support for PXOR to break dependencies in that fashion. I think the P3 does the same thing.

Use XOR and DIV.
If you know your data can be unsigned for a DIVISION, use XOR EDX, EDX, then DIV. It's faster than CDQ and IDIV.

Try to avoid obvious dependencies.
If you modify a register and then compare it to some value on the very next line, instead try and put some other register modification in between. Dependencies are any time you modify a register and then read it or write it shortly afterwards.

   inc edi
   inc eax
   cmp eax,1    ; this line has a dependency with the previous line, so it will stall.
   jz  fred

;shuffling the instructions around we can help break up dependencies.
   inc eax
   inc edi
   cmp eax,1
   jz  fred
Instructions to avoid on P4.
On P4's try to avoid the following instructions, adc, sbb, rotate instructions, shift instructions, inc, dec, lea, and any instruction taking more than 4 uops. How do you know the processor running the code is a P4? CPUID.

Using lookup tables.
On the P4 sometimes you can get around the long latency instructions that I listed previously by doing lookup tables. Thankfully on P4's they come with really fast memory. So having to do a lookup table doesn't hurt performance as much if it isn't in the cache.

Use pointers instead of calculating indexes.
A lot of times in loops in C there will be multiplications by non-powers of 2 numbers. You can easily get around this by adding instead. Here is an example that uses a structure.

typedef struct fred
{
   int fred;
   char bif;
} freddy_type;

freddy_type charmin[80];
The size of freddy_type is 5 bytes. If you try and access them in a loop the compiler will generate code for multipling by 5 for each array access!!!! (Ewwwwwwwwwwwww). So how do we do it properly?

for ( int t = 0; t < 80; t++)
{
   charmin[t].fred = rand(); // the compiler multiplies by 5 to get the offset, EWWWWWWWW!
   charmin[t].bif = (char)(rand() % 256);
}

; in assembler we start with an offset of 0, that points to the first data item.
; And then we add 5 to it each loop iteration to avoid the MUL.

   mov   esi,offset charmin
   mov   ecx,80
fred_loop:
   ;... perform operations on the FRED and BIF elements in freddy_type
   add   esi,5                     ;make it point to the next structure entry.
   dec   ecx
   jnz   fred_loop
The MUL removal applies to loops as well. I have seen people do multiplies in loops as part of incrementing the variable or for terminating condition. So try doing addition instead.

Conform to default branch predictions.
Try to set up your code such that backward conditional jumps are usually taken, and forward conditional loops are almost never taken. That has to do with branch prediction. The static branch predictor uses that simple rule to guess if a conditional jump is taken or not. So have a loop that has a backwards conditional jump at the end. And then have special exit conditions from that same loop that executes a forward jump that only exits on a certain condition that doesn't often occur.

Eliminate branches
Eliminate branch where possible. This might seem obvious, but I have seen some people use too many branches in their assembler code. Keep it simple. Use as few branches as possible.

Using CMOVcc to remove branches
I have yet to see the CMOVcc instructions actually be faster than a conditional jump. So I recommend using conditional jumps over CMOVcc. It might be faster in the case where your jumps aren't easily guessable by the branch prediction logic. So if that is the case with you, benchmark it and see.

Local vs. Global variables
Use local variables for a procedure over using a global variable. If you use local variables you'll get less cache misses.



Sunday, May 1, 2011

C & Assembly to Perform 16x16 Pixel-Block Motion Estimation

char *btpr; /* pointer to start row of 16x16 pixel block being compressed */
char *cptr; /* pointer to start row of 16x16 pixel reference block */
val = 0;
for (i=0; i<16; i++) {
for (j=0; j<16; j++) {
data = (*(bptr++)- *(cptr++));
if (data<0){val -= data;}
else {val += data;}
}
/* Fast out after this row if best match has been exceeded */
 if (val > best_value) break;
/* Update pointer to next row */
 bptr += (rm->width - 16);
/* Update pointer  to next row */
  cptr += (cm->width - 16);
}

Example 2. Inner-Core Absolute Differences for 16x16 Pixel Block
__asm {
   movdqu xmm0, [m1]
   movdqu xmm1, [m2]
   movdqa xmm2, xmm0
   psubusb xmm0, xmm1
   psubusb xmm1, xmm2
   por xmm0, xmm1
   movdqa xmm1, xmm0
   punpcklbw xmm0, xmm6
   punpcklbw xmm1, xmm6
   movdqa xmm3, xmm1
   pshufd xmm1, xmm0, 238
   pshufd xmm3, xmm0, 68
 paddw xmm1, xmm3
movdqa xmm4, xmm1
pshufd xmm4, xmm4, 78
   paddw xmm1, xmm4

}

Monday, April 11, 2011

Scene Change Detection

Simple Scene Detection Using YDifference

Code:
#Script to find scene changes

filename = "e:\scenes.txt"
global blankthreshold=2.0
source = AVISource("e:\fs.avi").convertTOYV12().killaudio()

#Comment out the following lines after blankthreshold has been determined
script = """Subtitle("\nNext/Prev = " + String( YDifferenceToNext(last) \
                    / YDifferenceFromPrevious(last) ), lsp=0)"""
final = ScriptClip(source,script)
return final

#Uncomment the following two lines when doing the actual scene detection
#WriteFileIf(source, filename, "(YDifferenceToNext(last) / \
#           YDifferenceFromPrevious(last)>blankthreshold)", "current_frame+1", append = false)
Scene Detection Using MVTools2
Code:
#Script to find scene changes

Loadplugin("C:\Program Files\AviSynth 2.5\plugins\MVTools\mvtools2.dll")

filename = "e:\scenes.txt"
BlockChangeThresh  = 350 # Default: 350. Increase to reduce number of scenes detected;
                         # Determines whether a block has changed from prev frame.
Num_blocks_changed =  90 # Default: 90. Increase to reduce number of scenes detected;
                         # How many changed blocks (given threshhold) must change to trigger scene change

source=AVISource("e:\fs.avi").killaudio().colorYUV(autogain=true) #add this to increase contrast
source_fields=source.separatefields().selecteven().convertTOYV12(interlaced=false)
source_super = source_fields.MSuper(pel=2, sharp=0)

backward_vec = MAnalyse(source_super,isb = true, delta = 1, blksize=16,search=0)

SceneChange = MSCDetection (source_fields, backward_vec,thSCD1=BlockChangeThresh,thSCD2=Num_blocks_changed)

#Uncomment following line for troubleshooting. It puts the scene change threshold numbers on the screen.
ScriptClip(source_fields,"Subtitle(String(AverageLuma(SceneChange ) ),align=5)")

#This line writes frame numbers to file. These can be imported into Vegas or other editor
#WriteFileIf(source_fields,filename, "(AverageLuma(SceneChange)>30)" , "current_frame+1", flush=false)
Scene Detection Using Depanestimate
Code:
#Script to find scene changes
loadPlugin("c:\Program Files\AviSynth 2.5\plugins\depanestimate.dll")
loadPlugin("c:\Program Files\AviSynth 2.5\plugins\depan.dll")

filename = "e:\scenes.txt"

AVISource("e:\fs.avi").convertTOYV12(interlaced=true).killaudio()
separatefields().selecteven().convertTOYV12(interlaced=false)

#The "trust" parameter is what determines the scene change threshold.
#Lower number results in fewer scenes.
data=DepanScenes( DepanEstimate(trust=2) )

#Uncomment following line to help determine "trust" value.
ScriptClip("Subtitle(String(AverageLuma(data ) ),align=5)")

#WriteFileIf(filename, "(AverageLuma(data)>30)" , "current_frame", flush=false)
Scene Detection Using Remove Dirt
Code:
#Script to find scene changes
filename = "e:\scenes.txt"
Scene_Thresh = 2.0  #Higher number = more scenes

AVISource("e:\fs.avi").ConvertToYV12()

Begin = Blackness(last)
End=Subtitle("END OF SCENE", align=1,size=30)
Motion=Subtitle("GLOBAL MOTION",align=5,size=30)

#ScriptClip("Subtitle(String(SCSelect(Last,BlackFrame) ),align=5)")
scene_change = SCSelect(Last,Begin,End,Motion,Dfactor=Scene_Thresh )

#Remove comment to see scene change numbers
ScriptClip("Subtitle(String(AverageLuma(scene_change ) ),align=5)")

#Uncomment next line to actually create scene change numbers
#WriteFileIf(filename, "AverageLuma(scene_change )<17" , "current_frame", flush=false)