Monday, September 20, 2010

Single Linked List Example - Add/Delete

Here is the sample program, I have written to explain the singly linked list. This has three functions, one to insert to the list at the head, which is pretty easy by making the new item as the head, and other one to remove from the list when the data is found, 3rd one to insert to the tail of the list, in this case you need to traverse till the end of the list and then insert the item there.

#include <stdio.h>
#include <malloc.h>

typedef struct node_type {
    int data;
    struct node_type *next;
} node;

node *list_head = NULL;

void insert_list(node **plist_head, int data)
{
    node *item = (node *)malloc(sizeof(node));
    item->data = data;
    item->next = *plist_head;
    *plist_head = item;
}

void insert_list_tail(node **plist_head, int data)
{
    node **list_ptr = plist_head;
    node *item = (node *)malloc(sizeof(node));
    item->data = data;
    item->next = NULL;

    while(*list_ptr) 
        list_ptr = &(*list_ptr)->next;

    *list_ptr = item;
}

void show_list(node **list_head)
{
    node **list_ptr = list_head;
    while(*list_ptr)
    {    
        printf("%d\t", (*list_ptr)->data);
        list_ptr = &(*list_ptr)->next;
    } 
    printf("\n");
}

void remove_list(node **plist_head, int data)
{
    node **list_ptr = plist_head;
    while(*list_ptr)
    {
        if((*list_ptr)->data == data)
        {
            node *tmp = *list_ptr;
            *list_ptr = (*list_ptr)->next;
            free(tmp);
            break;
        }
        list_ptr = &(*list_ptr)->next;
    }
}

main()
{
    show_list(&list_head);
    insert_list(&list_head, 2);
    show_list(&list_head);
    insert_list(&list_head, 5);
    show_list(&list_head);
    insert_list(&list_head, 9);
    show_list(&list_head);
    insert_list(&list_head, 1);
    show_list(&list_head);
    remove_list(&list_head, 9);
    show_list(&list_head);
    remove_list(&list_head, 2);
    show_list(&list_head);
    insert_list_tail(&list_head, 8);
    insert_list_tail(&list_head, 12);
    show_list(&list_head);
}

Hope this will help someone who just has just started learning Linked list in C, let me know if something is not clear.

Thursday, September 16, 2010

Where is ZERO size array is used?

If you normally try to declare a zero size array, you would end of getting array, but if the same is used as the last element of a structure then it does not give any error. Infact its method or trick to use variable size array for the last element, this can be achived as shown in the below program.

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct 
{
    int d;
    char name[0];
}s;

int main(void)
{
    char str[] = "prasad";
    int len;
    s *s1;
    len = strlen(str);
    s1 = (s *)malloc(sizeof(*s1) + len + 1);
    strcpy(s1->name, str);
    s1->d = 12345678;
    printf("d : %d  name : %s", s1->d, s1->name);
}

This kind of trick is normally not required, and you can program most of the things in this world with out using this, but still some people want to show that they can do something special than everybody, so they can do this trick, but when you do this you must be extra careful as you need to allocate right amount of memory for the variable size array, else you would end of corrupting others memory.

Tuesday, September 14, 2010

How to setup SDL-Library for Visual Studio Compiler

  1. On the SDL download page, get the latest version of the Windows-VC development libraries.
  2. If you want to use the SDL_mixer library (for sound and music), download the Windows-VC development libraries for SDL_mixer.
  3. Uncompress the SDL and SDL_mixer libraries to your local drive.
  4. Consolidate the SDL includes and libraries with the SDL_mixer includes and libraries, so that they're all in a single set of \include and \lib folders. It's easier this way!
  5. Launch Visual Studio .NET, but do not open a project or solution.
  6. From the Tools menu, select Options...
  7. In the tree view, click on Projects and then select VC++ Directories.
  8. In the Show directories for box, select Include files.
  9. Click the new-folder icon, and browse to the \include directory where you consolidated your SDL and SDL_mixer header files.
  10. In the Show directories for box, you now need to select Library files.
  11. Click the new-folder icon, and browse to the \lib directory where you consolidated your SDL and SDL_mixer library files.
  12. Click OK.
Your build environment is now set up, and VC++.NET will now know where to find the SDL and SDL_mixer headers and libraries. To create an SDL project, perform the following steps.
  1. Launch Visual Studio .NET and from the File menu, select New and then Project...
  2. In the tree view, expand Visual C++ Projects and select Win32.
  3. If you don't want a console window to appear when your program is executed, select Win32 Project. Otherwise, select Win32 Console Project. Give your project a name and location, then click OK.
  4. From the Project menu, select Properties... at the bottom of the menu.
  5. In the tree view, expand C/C++ and select Code Generation.
  6. Set the Runtime Library value to Multi-threaded DLL or Multi-threaded Debug DLL.
  7. We're still in the Properties dialog. In the tree view, now expand Linker and select Command Line.
  8. In the Additional Options field, enter SDL.lib SDLmain.lib and if you are using SDL_mixer, also add SDL_mixer.lib.
  9. VC++ is now set up. You may add your SDL source code to the project, and proceed as normal.
NOTES:
  • The SDL Windows FAQ states that this step is necessary because, "SDL is dynamically linked with the multi-threaded version of the Microsoft Visual C runtime."
  • If you plan on running the VC++ debugger with SDL you will have to pass the value SDL_INIT_NOPARACHUTE to SDL_Init in order for debugging to succeed!
  • When you compile your executable, you will need to copy the SDL.dll and SDL_mixer.dll files into the same folder as the EXE. If these DLLs are missing, when the program is executed you will be given an error message regarding the needed DLLs.

Monday, September 13, 2010

How to use Pointers for 2D Arrays

This is when, you want to have a two dimentional array and whos column size of fixed, but how many number of rows you need, you want to decide run time, in that case you may want to allocate memory for the whole 2D array dynamically. But the access to that array should be as simple as accessing it normally when declared using the standard syntax, i.e by using row and column numbers.

Remember that you cannot use, int **p for this purpose, or even int *p[4] for this purpose.

#include <stdio.h>
#include <malloc.h>

main()
{
    int i, j;
    int a[3][4];
    int (*p)[4];

    int size = 3;
    p = (int (*)[4])malloc(12*4);
    for(i=0; i<3; i++)
        for(j=0; j<4; j++)
        {
            p[i][j] = i+j;
            a[i][j] = p[i][j];
        }

    for(i=0; i<3; i++)
    {
        for(j=0; j<4; j++)
            printf("%d ", a[i][j]);
        printf("\n");
    }
}

Have a nice time, and enjoy programming.!