Rajat Malakar Pages

Sunday, 8 July 2012

SINGLE LINKED LIST Operations using DMA.


#include<stdio.h>
#include<conio.h>
#include<malloc.h>

struct node
{
       int data;
       struct node *next;
};
typedef struct node* NODE;

void display(NODE);
NODE insert_beg(NODE);
NODE insert_end(NODE);
NODE insert_specific(NODE);
NODE delete_beg(NODE);
NODE delete_end(NODE);
NODE delete_specific(NODE);
NODE reverse(NODE);

NODE head=NULL;
int main()
{
     int ch;

     int c;
     
     do
     {
      
         printf("\n 1. insert at the beg: ");

      printf("\n 2. insert at the end: ");

      printf("\n 3. insert at the specific: ");
       
      printf("\n 4. display link list: ");

      printf("\n 5. deletion from the beg: ");
               
      printf("\n 6. deletion from the end: ");
               
      printf("\n 7. deletion from the specific: ");
     
      printf("\n 8. reverse:");
     
      printf("\n enter ur choice:" );

      scanf("%d",&ch);

      switch(ch)
      {
            case 1:
            head=insert_beg(head);
            fflush(stdin);
            break;
            case 2:
            head=insert_end(head);
            fflush(stdin);
            break;
            case 3:
            head=insert_specific(head);
            fflush(stdin);
            break;
            case 4:
            display(head);
            fflush(stdin);
            break;
              case 5:
            head=delete_beg(head);
            fflush(stdin);
            break;
            case 6:
            head=delete_end(head);
            fflush(stdin);
            break;
            case 7:
            head=delete_specific(head);
            fflush(stdin);
            break;   
            case 8:
            head=reverse(head);
            fflush(stdin);
            break;   
            default:
            printf("\n enter the wrong choice!");

      }
      }
     
      while(1);
      getch();
      return 0;
   }


NODE insert_beg(NODE head)
{
     NODE ptr;
     int item;
     printf("\n enter the data");
     scanf("%d",&item);
     ptr=(NODE)malloc(sizeof(node));
     ptr->data=item;
     if(head==NULL)
     {
     ptr->next=NULL;
     }
     else
     {
     ptr->next=head;
     }
     head=ptr;
      return(head);
}
NODE insert_end(NODE head)
{
     NODE ptr,temp;
     temp=head;
     int item;
     printf("\n enter the data");
     scanf("%d",&item);
     ptr=(NODE)malloc(sizeof (NODE));
     ptr->data=item;
     ptr->next=NULL;
     if(head==NULL)
     head=ptr;
     else
     {

     while(temp->next!=NULL)
     temp=temp->next;
     temp->next=ptr;
      }
      return(head);
  }
  NODE insert_specific(NODE head)
  {
    NODE ptr,temp;
     int item,t,k;
     printf("\n enter the data");
     scanf("%d",&item);
     ptr=(NODE)malloc(sizeof (NODE));
     ptr->data=item;
     printf("\n enter a specific location:");
     scanf("%d",&t);
     temp=head;
     for(k=1;k<t-1;k++)
     {
      temp=temp->next;
     }
       ptr->next=temp->next;
       temp->next=ptr;
       return(head);
     }
    
     NODE delete_beg(NODE head)
    {
          NODE p=head;
          head=head->next;

          free(p);
          return(head);
      }
 
NODE delete_end(NODE head)
{
                      NODE p=head;
                     NODE temp=head;

                     p=p->next;

                     while(p->next!=NULL)
                     {
                     p=p->next;
                     temp=temp->next;
                    }
                    free(p);
                    temp->next=NULL;
                    return(head);
                    
}

NODE delete_specific(NODE head)
{
int t,k;
NODE p,temp;

printf("\n enter a specific location");                         
scanf("%d",&t);

temp=head;
p=head;
temp=temp->next;

for(k=1;k<t-1;k++)
{
                 
p=p->next;
temp=temp->next;                 
                 
                 
}
                         
p->next=temp->next;
free(temp);
return(head);
}
    
     void display(NODE head)
     {
      NODE p=head;
      while(p->next!=NULL)
      { printf(" %d",p->data);
                  p=p->next;
                 
      }
      printf(" %d",p->data);
     }


NODE reverse(NODE head)
{
    
     NODE ptr,save,temp;
     temp=head;
     save=NULL;
     ptr=NULL;
              while(temp!=NULL)
              {
                                    
              save=ptr;
              ptr=temp;
              temp=temp->next;
              ptr->next=save;                      
                                
              }
    
     head=ptr;
     return(head);
     }







No comments:

Post a Comment

Thanks for Your Comment.

satta king 786