Linked list operations

 /* Program to perform operations on a linked list  */
    #include
    #include
    struct link
        {
            int info;
            struct link *next;
        };
    typedef struct link node;
    void swap(int *x,int *y)
    {
        int temp=*x;
        *x=*y;
        *y=temp;
    }
    void create(node **ptr)
        {
            char ch;
            node *temp;
            temp=*ptr;
            do
            {
                printf("Enter information : ");
                scanf("%d",&temp->info);
                printf("\nDATA INSERTED : \n");
                printf("Want to add more ? (press y to continue) : ");
                scanf("%s",&ch);
                if(ch=='y'||ch=='Y')
                    {
                        temp->next=(node *)malloc(sizeof(node));
                        temp=temp->next;
                    }
            }    while(ch=='y'||ch=='Y');
            temp->next=NULL;
        }
    void insert_front(node **ptr)
        {
            node *new;
            new=(node *)malloc(sizeof(node));
           
                printf("\nEnter the data to insert : ");
                scanf("%d",&new->info);
                    new->next=*ptr;
                    *ptr=new;
                printf("\nDATA INSERTED : \n");
        }
    void insert_end(node **ptr)
        {
           
            node *new;
            node *temp;
            temp=*ptr;
            new=(node *)malloc(sizeof(node));
           
                printf("\nEnter the data to insert : ");
                scanf("%d",&new->info);
                    while(temp->next!=NULL)
                        temp=temp->next;
                    new->next=NULL;
                    temp->next=new;
                printf("\nDATA INSERTED : \n");
        }
    void insert_pos(node **ptr)
        {
           
            int i,pos;
            node *new;
            node *temp;
            temp=*ptr;
            new=(node *)malloc(sizeof(node));
            printf("\nEnter the position : ");
            scanf("%d",&pos);
            printf("\nEnter data to insert : ");
            scanf("%d",&new->info);
                    for(i=0;inext;
                    new->next=temp->next;
                    temp->next=new;
                printf("\nDATA INSERTED : \n");
               
        }
    void display(node *temp)
        {
           
            printf("\nThe list is  :");
            while(temp!=NULL)
            {
                printf(" %d --> ",temp->info);
                temp=temp->next;
            }
            printf("\b\b\b\b   ");
            printf("\n");
        }
    void sort(node **ptr)
        {
           
            node *i,*j;
            int temp;

                for(i=*ptr;i->next!=NULL; i=i->next)
                    for(j=i->next; j!=NULL; j=j->next)
                        if(i->info > j->info)
                            swap(&i->info,&j->info);
                           
                           
            printf("\n");
        }
    void reverse(node **ptr)
        {
            node *current,*prev,*temp;
            current=*ptr;
            prev=temp=NULL;
            while(current!=NULL)
                {
                    temp=prev;
                    prev=current;
                    current=current->next;
                    prev->next=temp;
                }
            *ptr=prev;
        }
    void del_pos(node **ptr)
        {
            int nd,x;
            node *current,*prev=NULL;
            current=*ptr;
            printf("Enter the element to delete : ");
            scanf("%d",&x);
            if(*ptr==NULL)
                printf("\nList is empty !!!!");
            while(current!=NULL)
            {   
                if(current->info==x)
                {
                    if(current==*ptr)
                    {
                        *ptr=current->next;
                        free(current);
                    }
                    else
                    {
                        prev->next=current->next;
                        free(current);
                    }
                }
                else
                {
                    prev=current;
                    current=current->next;
                }
            }                   
            printf("\nElement deleted \n");
        }
    main()
        {
            printf("\nPROGRAM TO PERFORM OPERATIONS ON LINKED LIST : \n");    
            int ch,pos;
            node *head;
            head=(node *)malloc(sizeof(node));
            do
            {
            printf("[1] Create a node : \n");
            printf("[2] Insert at the front : \n");
            printf("[3] Insert at the end : \n");
            printf("[4] Insert at other postions : \n");
            printf("[5] Delete a node : \n");
            printf("[6] Sort the linked list : \n");
            printf("[7] Reverse the linked list : \n");
            printf("[8] Display the list : \n");
            printf("[9] Quit \n");
            printf("Enter your choice : ");
            scanf("%d",&ch);
            switch(ch)
                {
                    case 1:
                        create(&head);
                        break;
                    case 2:
                        insert_front(&head);
                        break;                   
                    case 3:
                        insert_end(&head);
                        break;
                    case 4:
                        insert_pos(&head);
                        break;
                    case 5:
                        del_pos(&head);
                        break;
                    case 6:
                        sort(&head);
                        break;
                    case 7:
                        reverse(&head);
                        break;
                    case 8:
                        display(head);
                        break;
                }
            }while(ch!=9);
        }

Top