Friday, 5 July 2013

THIS PROGRAM CREATES STRING TYPE LINKED LIST



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

struct list
{
    char info[20];
    struct list *link;
    struct list *prev;
};

int i ;
struct list start;

void create_list (struct list *);
void display (struct list *);

 /* Function to create linked list */

 void create_list(struct list *ptr)
{
    int n;
    start.link = NULL;  /* Empty list */
    start.prev = NULL;
    ptr = &start;      /* Ptr points to the start pointer */
    printf("\n\n\n How many nodes you want to create: ");
    scanf("%d",&n);
    printf("\n\n ==============================================================================\n");
    printf("\n Note That:\n -----------\n Enter string values in nodes\n");
    printf("\n ==============================================================================\n");
    while( i!=n)
    {
        ptr->link = (struct list *) malloc(sizeof(struct list));
        ptr->link->prev = ptr;
        ptr = ptr->link;
        printf("\n Enter the values of the node : %d: ", (i+1));
        scanf("%s", ptr->info);
        ptr->link = NULL;
        fflush(stdin);
        i++;
    }
    printf("\n\n --------------------");
    printf("\n Total nodes = %d",i);
    printf("\n --------------------\n\n");
}

/* Function to display the list */

void display(struct list *ptr)
{
    ptr = start.link;

        while (ptr)
        {

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

/* Main Function */

void main()
{
    struct list *ptr = (struct list *) malloc(sizeof(struct list));
    clrscr();
    create_list (ptr);
    printf("\n Linked list is: ");
    printf("\n ----------------\n");
    display (ptr);
    getch();
}










No comments:

Post a Comment