Tuesday, July 19, 2011

Program for singly linked list {insert_at_beg, insert_at_end, insert_at_position, sort, delete, display, reverse}

/*Serial No.155     [swami118.cpp]*/ 

/*Version 2*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef struct node
    {
    int info;
    struct node * next;
    }node;

void insert_position(node *head,int pos_value)
    {
    int ele,flag=1;
    while(head->info!=pos_value)
        {
        head=head->next;
        if(head==NULL)
            {
            flag=2;break;
            }
        }
    if(flag==1)
        {
    printf("\nEnter the no to insert= ");
    scanf("%d",&ele);
    node *temp=(node *)malloc(sizeof(node));
    temp->info=ele;
    temp->next=head->next;
    head->next=temp;
        }
    else
        printf("\nValue u entered is not in list.\n");
    }


void sort(node **head)
    {
    node *chk1,*chk2;
    int temp;
    for(chk1=*head;chk1->next!=NULL;chk1=chk1->next)
        {
        for(chk2=*head;chk2->next!=NULL;chk2=chk2->next)
            {
            if(chk2->next->info<chk2->info)
                {
                temp=chk2->next->info;
                chk2->next->info=chk2->info;
                chk2->info=temp;
                }
            }
        }
    }
void insert_at_end(node **head,int n)
    {
    node *temp,*q;
    temp=(node *) malloc(sizeof(node));
    temp->info=n;
    temp->next=NULL;
    if(*head==NULL)
        {
        *head=temp;
        return;
        }
     q=*head;
    while(q->next!=NULL)
        {
        q=q->next;
        }
     q->next=temp;
     }

void display(node *head)
    {
    if(head==NULL)
        {
        printf("list is empty");
        return;
        }
    while(head!=NULL)
        {
        printf("%d ",head->info);
        head=head->next;
        }
    }

void insert_at_beg(node **head, int n)
    {
    node *temp;
    temp=(node *)malloc(sizeof(node));
    temp->info=n;
    temp->next=*head;
    *head=temp;
    }


void delete_val(node **head,int n)
    {

    node *q=*head,*temp;
    if(q->info==n)
        {
        temp=q;
        *head=temp->next;
        }
    else
        {
        while(q->next!=NULL)
            {
            if(q->next->info==n)
                {
                temp=q->next;
                q->next=temp->next;
                break;
                }
            q=q->next;
            }
        if(q->next==NULL)
            {
            printf("\nNo. is not in list.\n\t!! If it is last node then it has been deleted.!!");
            return;
            }
        }
        free(temp);
        }

void Reverse_list(node **head)
    {
    node *f,*s,*t;
    f=*head;
    s=f->next;
    t=s->next;
    f->next=NULL;
    while(s!=NULL)
        {
        s->next=f;
        f=s;
        s=t;
        t=t->next;
        }
    *head=f;
    }


void main()
    {
    node *start1=NULL;
    int choice,ele,pos_value,option;
    clrscr();
    do
        {
        printf("\n\t[1] Create\\Insert at end");
        printf("\n\t[2] Create\\Insert at Begining");
        printf("\n\t[3] Sort List");
        printf("\n\t[4] Delete");
        printf("\n\t[5] Display");
        printf("\n\t[6] Reverse List");
        printf("\n\t[7] Insert at position");
        printf("\n\t[8] Exit");
        printf("\n\t\tEnter ur choice= ");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:printf("\nEnter the element=");
               scanf("%d",&ele);
               insert_at_end(&start1,ele);
               break;
        case 2:printf("\nEnter the element=");
               scanf("%d",&ele);
               insert_at_beg(&start1,ele);
               break;
        case 3:sort(&start1);
            break;
        case 4:printf("\nEnter the element which u want to delete= ");
               scanf("%d",&ele);
               delete_val(&start1,ele);
               break;
        case 5:printf("\nElements in list are:\n");
               display(start1);
               break;
        case 6:printf("\nReversing link list...:\n");
               Reverse_list(&start1);
               break;
        case 7:printf("\nEnter the element after which u want to insert= ");
               scanf("%d",&pos_value);
               insert_position(start1,pos_value);
               break;
        case 8:break;
        }
    }while(choice!=8);
}

No comments:

Post a Comment

If you have any doubt, feel free to ask...