Friday, 5 July 2013

THIS PROGRAM CREATES A HEADER LINKED LIST




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

struct list
{
    int info;
    struct list *link;
};

int i;
int num;
struct list *start, *new1;

void insert(struct list *);
void create_hlist(struct list *);
void display (struct list *);

/* Function to create a header linked list */

void create_hlist(struct list *ptr)
{
    int n;

    start->link = NULL;  /* Empty list1 */

    ptr = start;      /* Point to the header ptr of the list1 */
    ptr->link = (struct list* ) malloc(sizeof(struct list)); /* Create header ptr */
    printf("\n How many nodes you want to create: ");
    scanf("%d",&n);
    i=0;
    printf("\n\n ==============================================================================\n");
    printf("\n Note That:\n -----------\n Enter numeric data in node\n");
    printf("\n ==============================================================================\n");
    while( i!=n)
    {
        ptr->link = (struct list* ) malloc(sizeof(struct list));
        ptr = ptr->link;
        printf("\n Input the ptr: %d: ", i+1);
        scanf("%d", &ptr->info);
        ptr->link = NULL;
        i++;
    }
    printf("\n\n -----------------");
    printf("\n Total nodes = %d", i);
    printf("\n -----------------\n");
    ptr = start;
    ptr->info = i;
}

/* Function to insert a node */

void insert(struct list *ptr)
{
    int loc,num = 1;
    int count;
    ptr = ptr->link;
    count = ptr->info;
    ptr = start;
    printf("\n -----------------------------------------------\n");
    printf("\n Enter the loc where you want to insert node: ");
    scanf("%d",&loc);
    while(count)
    {
        if((num) == loc)
        {
            new1 = (struct list* ) malloc(sizeof(struct list));
            new1->link = ptr->link ;
            ptr->link = new1;
            printf("\n -----------------------------------------------\n");
            printf("\n Enter the item you want to insert: ");
            scanf("%d", &new1->info);
            ptr = ptr->link;
        }
        else
        {
            ptr = ptr->link;
            count--;
        }
        num++;
    }
    ptr = start;
    ptr->info = ptr->info+1;
}

/* Function to diplay  header linked list */

void display(struct list *ptr)
{
    int count;
    ptr = start;
    count = ptr->info;
    ptr = ptr->link;
    while (count)
    {

        printf("\n\n\t\t%d", ptr->info);
        ptr = ptr->link;
        count--;
    }
}

/* Main Function */

void main()
{
    struct list *ptr = (struct list *) malloc(sizeof(struct list));
    clrscr();
    create_hlist (ptr);
    printf("\n\n ----------------------");
    printf("\n Header linked list is:");
    printf("\n ----------------------\n");
    display (ptr);
    insert(ptr);
    printf("\n\n\n -----------------------------------");
    printf("\n Header linked list after insertion:");
    printf("\n -----------------------------------\n");
    display (ptr);
    getch();
}

No comments:

Post a Comment