Linked Lists Explored...

 
Append Node to the List:
    Appending involves add Node to the end of the Linked List , consider doubly linked list provided in the below example code ,

This is the Node looks like :


Code Example:
===========
#include <stdio.h>
#include <stdlib.h>

 
typedef struct sList
{
    char *name;
    struct sList *next_sList;
    struct sList *prev_sList;
} UtilList;


#define MAX_NAME_SIZE 255
/* Generic Operations done in Doubly Linked List - API's */
void utilAppendList(char *name,UtilList **pUtilList);
void utilAddListAtHead(char *name,UtilList **pUtilList);
int  utilRemoveSpecList(UtilList **pUtilList,int listnum2delete);
void utilRemoveList(UtilList **pUtilList);
void utilReverseList(UtilList **pUtilList);
void displayList(UtilList *pUtilList);

/* Other Operations */
void utilSortList(UtilList **pUtilList);
void utilSwapDList(UtilList **pxUtilList,UtilList **pyUtilList);

int main()
{
    char *name[] = {"Test1","Test2","Test3","Test4","Test5"};
    UtilList *pUtilList = NULL;
    /* Add List at Head Node */
    printf("Adding Node at Head Node\n");
    utilAddListAtHead(name[0],&pUtilList);
    utilAddListAtHead(name[1],&pUtilList);
    utilAddListAtHead(name[2],&pUtilList);
    utilAddListAtHead(name[3],&pUtilList);
    utilAddListAtHead(name[4],&pUtilList);
    displayList(pUtilList);
    /* Add List at End */
    printf("Add List at End\n");
    utilAppendList(name[0],&pUtilList);
    utilAppendList(name[1],&pUtilList);
    utilAppendList(name[2],&pUtilList);
    /* Remove a Specified list at given location */
    utilRemoveSpecList(&pUtilList,2);
    utilAppendList(name[3],&pUtilList);
    utilAppendList(name[4],&pUtilList);
    printf("Display list after added both at Head and End\n");
    displayList(pUtilList);
    /* Reversiong List */
    utilRemoveList(&pUtilList);
    printf("After Removing List :\n");
    displayList(pUtilList);
    /* Issues based on appendList */
    return(0);
}

void utilAppendList(char *name,UtilList **pUtilList)
{
    UtilList *ptempUtilList    = (UtilList *)malloc(sizeof(UtilList));
    UtilList *ptempUtilEnd     = NULL;
    if(!(*pUtilList))
    {
        ptempUtilList->name       = (char *)malloc(sizeof(char)* MAX_NAME_SIZE);
        strcpy(ptempUtilList->name,name);
        ptempUtilList->next_sList = NULL;
        ptempUtilList->prev_sList = NULL;
        *pUtilList = ptempUtilList;
    }
    else
    {
        ptempUtilList->name       = (char *)malloc(MAX_NAME_SIZE * sizeof(char));
        ptempUtilEnd              = *pUtilList;
        strcpy(ptempUtilList->name,name);
        while(ptempUtilEnd->next_sList)
        {
            ptempUtilEnd = ptempUtilEnd->next_sList;
        }
        ptempUtilList->prev_sList = ptempUtilEnd;
        ptempUtilList->next_sList = NULL;
        ptempUtilEnd->next_sList  = ptempUtilList;
    }
}

void utilAddListAtHead(char *name,UtilList **pUtilList)
{
    UtilList *ptempUtilList    = (UtilList *)malloc(sizeof(UtilList));
    UtilList *ptempUtilHead    = NULL;
    if(!(*pUtilList))
    {
        ptempUtilList->name       = (char *)malloc(sizeof(char) * MAX_NAME_SIZE);
        strcpy(ptempUtilList->name,name);
        ptempUtilList->next_sList = NULL;
        ptempUtilList->prev_sList = NULL;
        *pUtilList = ptempUtilList;
    }
    else
    {
        ptempUtilList->name       = (char *)malloc(MAX_NAME_SIZE * sizeof(char));
        strcpy(ptempUtilList->name,name);
        (*pUtilList)->prev_sList  = ptempUtilList;
        ptempUtilList->next_sList = *pUtilList;
        *pUtilList = ptempUtilList;
    }
}

void displayList(UtilList *pUtilList)
{
    UtilList *ptempUtilList = pUtilList;
    if(ptempUtilList)
    {
        while(ptempUtilList)
        {
            printf("Name = %s \n",ptempUtilList->name);
            ptempUtilList = ptempUtilList->next_sList;
        }
    }
    else
    {
        printf("List is empty\n");
    }

}

void utilRemoveList(UtilList **pUtilList)
{
    UtilList *pCurrUtilList = NULL;
    while(*pUtilList)
    {
        pCurrUtilList = *pUtilList;
        *pUtilList    = (*pUtilList)->next_sList;
        free(pCurrUtilList->name);
        free(pCurrUtilList);
    }
    *pUtilList = NULL;
}

int utilRemoveSpecList(UtilList **pUtilList,int listnum2delete)
{
    UtilList *pCurrUtilList = *pUtilList;
    int ret                 = 0;
    int count               = 1;

    while((pCurrUtilList) && (count != listnum2delete))
    {
        pCurrUtilList    = pCurrUtilList->next_sList;
        count++;
        ret = 1;
    }
    (*pUtilList)->next_sList = (*pUtilList)->next_sList->next_sList;
    (*pUtilList)->prev_sList = (*pUtilList);
    free(pCurrUtilList->name) ;
    free(pCurrUtilList);
    return ret;
}

void utilSortList(UtilList **pUtilList)
{

}

void utilSwapDList(UtilList **pxUtilList,UtilList **pyUtilList)
{
    UtilList *ptempUtilList = NULL;

}

No comments: