Wednesday, 27 August 2014

create singly link list and insert element inside it and print them

#include<conio.h>
#include<malloc.h>
#include<stdio.h>
struct node
{
       int data;
       struct node*next;
       }*start=NULL;

       void insert_at_start();
       void insert_at_middle();
       void insert_at_end();
       void display();
       int main()
       {
           int ch;
           while(1)
           {
                   printf("\n insert element inside singly link list \n");
                   printf("\n 1.to insert at begining\n");
                   printf("\n 2.to insert at middle\n");
                   printf("\n 3.insert at end\n");
                   printf("\n 4.to print data\n");
                   printf("\n 0.to exit \n");
                   printf("\n enter your choice:\n");
                   scanf("%d",&ch);
                   switch(ch)
                   {
                             case 1:insert_at_start();
                             break;
                             case 2:insert_at_middle();
                             break;
                             case 3:insert_at_end();
                             break;
                             case 4:display();
                             break;
                             case 0:exit(0);
                             default:printf("\n please enter a valid choice\n");
                      }      
                      }        
                      }                      
                           
void insert_at_start()
    {
       int m;
       struct node*k;
       k=(struct node*)malloc(sizeof(struct node));
       printf("\n enter data to insert :");
       scanf("%d",&m);
       k->data=m;
       k->next=start;
       start=k;

     }
 void insert_at_middle()
     {
          int d,n;
          struct node*r;
          r=(struct node*)malloc(sizeof(struct node));
          printf("\n enter data to insert :");
          scanf("%d",&d);
          printf("\n enter data after which new data to be insert ");
          scanf("%d",&n);
             if(start==NULL)

                printf("\n list is empty");
             else
             {
                struct node*temp=start;
                   for(;temp!=NULL;temp=temp->next)

                   {
                      if(temp->data==n)
                      break;
                   }
                      r->data=d;
                      r->next=temp->next;
                      temp->next=r;
             }
     }
 void  insert_at_end()
    {
            struct node*r;
            int d;
            r=(struct node*)malloc(sizeof(struct node));
            printf("\n enter data to insert:\n");
            r->data=d;
            r->next=NULL;
            if(start==NULL)
            start=r;
            else
            {
                struct node*temp;
                for(temp=start;temp->next!=NULL;temp=temp->next)

                temp->next=r;
                }
                }
                void display()
                {
                     struct node*temp=start;
                     if(start==NULL)

                     printf("\n list is empty");
                     else
                     for(;temp!=NULL;temp=temp->next)
                         {
                            printf("\n data->%d",temp->next);
                            }
                            }



                  

No comments:

Post a Comment

Note: only a member of this blog may post a comment.