Code Competitions

Coding competition
Showing posts with label Link list implementation. Show all posts
Showing posts with label Link list implementation. Show all posts

Sunday, 12 May 2013

Program to implement the link list creation.

#include<alloc.h>
#include<stdio.h>
#include<conio.h>
struct linklist
{
    int info;
    struct linklist *p;
};
void create(struct linklist *);
void print(struct linklist *);
int menu();
struct linklist *start,*ptr;
void main()
{
    int c;
    clrscr();
    //start=(struct linklist *)malloc(sizeof(struct linklist));
    ptr=(struct linklist *)malloc(sizeof(struct linklist));
    //ptr=start;
    while(1)
    {
        c=menu();
        if(c>=1 || c<=3)
        {
            switch(c)
            {
                case 1:
                    create(ptr);
                    break;
                case 2:
                    printf("\n\n START-->");
                    print(ptr);
                    getch();
                    break;
                case 3:
                    exit(0);
                default:
                    printf("You Have Entered Wrong Choice..... Press <Cr> to Continue......");
                    getch();
                    break;
              }
        }
    }
}
void create(struct linklist *m)
{
    char ch;
    printf("\n Enter the Value in node : ");
    scanf("%d",&m->info);
    printf("\n Do you want to continue Y/N :");
    ch=getche();
    if(ch=='n'|| ch=='N')
    {
        m->p=NULL;
        return;
    }
    else
    {
        m->p=(struct linklist *)malloc(sizeof(struct linklist));
        create(m->p);
    }
}
void print(struct linklist *m)
{
    if(m->p==NULL)
    {
        printf("%d-->X",m->info);
        return;
    }
    else
    {
        printf("%d-->",m->info);
        print(m->p);
    }
}

int menu()
{
    int c;
    clrscr();
    printf("\n\t\t\t\t===========");
    printf("\n\t\t\t\tLINK - LIST");
    printf("\n\t\t\t\t===========");
    printf("\n\n");
    printf("\n\t\t\t1. CREATION");
    printf("\n\t\t\t2. DISPLAY");
    printf("\n\t\t\t3. EXIT");
    printf("\n\n\n Please Enter Your choice ");
    scanf("%d",&c);
    return(c);
}