Saturday, 15 December 2012

linked list operations sample code with functionalities (createList, display, delete by data, delete all, reverse list insertAt(int position, insertAtStart)

#include <stdio.h>
#include <stdlib.h>

struct node
{

    int info;
    struct node * link;
};

struct node * start=NULL;
void createList(int);
void display(void);
void insertAtStart(int data);
void insertAt(int position, int data);
void delByData(int data);
int del(void);
void reverseList(void);

int main()
{
    int choice=0, n, m, i, position;

    while(1)
    {

    printf("1.Create list\n");
    printf("2.Display\n");
    printf("3.Quit\n");
    printf("4.Insert at start\n");
    printf("5.Insert at position\n");
    printf("6.del by data\n");
    printf("7.del list\n");
    printf("8.Reverse list\n");

    scanf("%d", &choice);

    switch(choice)
    {
        case 1:
            system("clear");
  //          start = NULL;
            printf("How many nodes u want\n");
            scanf("%d", &n);
            for(i=0 ; i<n ; i++)
            {
                printf("enter element\n");
                scanf("%d", &m);
                createList(m);
            }
            break;
        case 2:
            system("clear");
            display();
            break;
        case 3:
            del();
            exit(0);
        case 4:
            system("clear");
            printf("enter element\n");
            scanf("%d", &m);
            insertAtStart(m);
            break;
        case 5:
            system("clear");
            printf("enter element position\n");
            scanf("%d", &position);
            printf("enter element data\n");
            scanf("%d", &m);
            insertAt(position, m);
            break;
        case 6:
            system("clear");
            printf("enter element data to delete\n");
            scanf("%d", &position);
            delByData(position);
            break;
        case 7:
            system("clear");
            if(del())
            {
                printf("successfully list deleted\n");
            }
            break;
        case 8:
            system("clear");
            reverseList();
            break;
        default:
            system("clear");
            printf("wrong choice\n");
            break;
    }
    }

    return 0;
}

void reverseList(void)
{
    struct node *p1, *p2, *p3;

    p1 = start;
    p2 = p1->link;
    p3 = p2->link;

    p1->link = NULL;
    p2->link = p1;

    while(p3 != NULL)
    {
        p1 = p2;
        p2 = p3;
        p3 = p3->link;
        p2->link = p1;
    }
    start = p2;
}

int del(void)
{
    struct node *tmp = start, *q;
    while(tmp != NULL)
    {
        q = tmp;
        tmp = tmp->link;
        free(q);
    }
    return 1;
}

void delByData(int data)
{
    struct node * tmp, *q;
    tmp = start;

    while(tmp->link != NULL)
    {
        if(tmp->link->info != data)
        {
            tmp  = tmp->link;
        }
        else
        {
            q = tmp->link;
            tmp->link = tmp->link->link;
            free(q);
            return;
        }
    }
}

void createList(int data)
{
    struct node *tmp, *q;
    tmp = malloc(sizeof(struct node));
    tmp->info=data;
    tmp->link=NULL;

    if(start==NULL)
    {
        start = tmp;
    }
    else
    {
        q = start;
        while(q->link!=NULL)
            q=q->link;
        q->link = tmp;
    }
}

void display(void)
{
    struct node *ptr=start;
    while(ptr != NULL)
    {
        printf("%d ", ptr->info);
        ptr = ptr->link;
    }
    printf("\n\n");
}

void insertAtStart(int data)
{
    struct node * tmp;
    tmp = malloc(sizeof(struct node));
    tmp->link = start;
    tmp->info = data;
    start = tmp;
}

void insertAt(int position, int data)
{
    struct node * tmp, * ptr = start;
    int count=1;

    while(ptr != NULL)
    {
        if(count != position)
        {
            ptr = ptr->link;
            count++;
        }
        else if(position > 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = ptr->link;
            ptr->link = tmp;
            tmp->info = data;
            return;
        }
        else if(position == 1)
        {
            tmp = malloc(sizeof(struct node));
            tmp->link = start;
            start = tmp;
        }
    }
    printf("no such position found..\n");
}