Monday, 19 October 2015

C program to implement single linked list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
int m,n;
struct node
{
    int data;
    struct node *next;
}*start;

void create_list(int);
void display();
void insert_at_beg(int);
void insert_at_end(int);
void insert_at_pos(int,int);
void selection(int);
void count();
void main()
{
    int i,choice;
    clrscr();
    start=NULL;
    while(1)
    {
        clrscr();
        printf("\n press 1 for list creation");
        printf("\n press 2 for displaying the list");
        printf("\n press 3 to insert a data at beginning");
        printf("\n press 4 to insert a data at end");
        printf("\n press 5 to insert at a position");
        printf("\n press 6 for selection");
        printf("\n press 7 for counting");
        printf("\n press 8 for exit");
        printf("\n enter your choice:");
        scanf("%d",&choice);

    switch(choice)
    {

    case 1:
        if(start!=NULL)
        {
            printf("\n list already created");
            break;
        }
        printf("\n enter total no. of lists to be created:");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            printf("\n enter the data:");
            scanf("%d",&m);
            create_list(m);
        }
        printf("\n list created");
        break;

    case 2:
        if(start==NULL)
        {
            printf("\n no lists created till now");
            break;
        }
        display();
        break;

    case 3:
        if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_beg(m);
        printf("\n data inserted");
        break;

    case 4:
            if(start==NULL)
        {
            printf("\n list not created......create list first");
            break;
        }
        printf("\n enter data to be inserted:");
        scanf("%d",&m);
        insert_at_end(m);
        printf("\n data inserted");
        break;

    case 5:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        printf("\n enter the data:");
        scanf("%d",&m);
        printf("\n enter the position:");
        scanf("%d",&n);
        insert_at_pos(m,n);
        break;

    case 6:
        if(start==NULL)
        {
            printf("\n no list created till now....error");
            break;
        }
        printf("\n enter data for selection:");
        scanf("%d",&m);
        selection(m);
        break;

    case 7:
        if(start==NULL)
        {
            printf("\n no list created......error");
            break;
        }
        count();
        break;

    case 8:
        exit(1);

        default:
            printf("\n wrong choice");

    }
    }
getch();
}

void create_list(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    if(start==NULL)
    {
        start=tmp;
        return;
    }
    p=start;
    while(p->next!=NULL)
        p=p->next;
     p->next=tmp;
    getch();
}

void display()
{
    struct node *p;
    p=start;
    while(p!=NULL)
    {
        printf("\n data= %d",p->data);
        p=p->next;
    }
    getch();
}

void insert_at_beg(int item)
{
    struct node *tmp;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=start;
    start=tmp;
    getch();
}

void insert_at_end(int item)
{
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    tmp->next=NULL;
    p=start;
    while(p->next!=NULL)
        p=p->next;
    p->next=tmp;
    getch();
}

void insert_at_pos(int item,int pos)
{
    int i;
    struct node *tmp,*p;
    tmp=(struct node *)malloc(sizeof(struct node));
    tmp->data=item;
    if(pos==1)
    {
        tmp->next=start;
        start=tmp;
        printf("\n node inserted at 1st position");
        getch();
    }
    p=start;
    for(i=1;i<pos;i++)
        p=p->next;
    tmp->next=p->next;
    p->next=tmp;
    if(p==NULL)
    {
        printf("\n position not in range");
        return;
    }
    printf("\n list inserted at %d position",i);
    getch();
}



void selection(int item)
{
    int i=1;
    struct node *p,*q;
    if(item==start->data)
    {
        p=start;
        printf("\n data found at 1st position");
        start=start->next;
        free(p);
        printf("\n node free");
        getch();
    }

    q=start;
    while(q->next->data!=item)
    {
        i++;
        q=q->next;
    }
    p=q->next;
    q->next=q->next->next;
    free(p);

    if(q==NULL)
    {
        printf("\n error");
        return;
    }
    printf("\n data found at %d position",i);
    printf("\n node deleted");
    getch();
}

void count()
{
    int i=1;
    struct node *p;
    p=start;
    while(p->next!=NULL)
    {
        p=p->next;
        i++;
    }
        printf("\n total no. of lists created is %d",i);
    getch();
}


No comments:

Post a Comment