LinkedList




#include <stdio.h>
#include <stdlib.h>
int count;
bool flag=YES;
int choice;
struct object
{
    int data;
    struct object *address;
};
void printlist(struct object*);
void insert(struct object*,int pos,int data);
void delete(struct object*,int pos);
int main (int argc, const char * argv[])
{
   
    int pos;
    int data;
    struct object *list;
   
    //create root if the list is empty
    if (list==NULL)
    {
        list = (struct object*) malloc(sizeof(struct object));
        list->data = 99;
        list->address = NULL;
    }
   
    printf("%lu",sizeof(struct object));
   
    while (flag==YES)
    {
        Printf("\nEnter the choice: 1-insert 2-print 3-delete 5-exit");
        scanf("%d",&choice);
       
        switch (choice)
        {
            case 1:
                    printlist(list);
                Printf("\nEnter the position:\n");
                scanf("%d",&pos);
                Printf("\nEnter the data:\n");
                scanf("%d",&data);
                insert(list, pos, data);
                break;
           
            case 2:
                printlist(list);
                break;
           
            case 3:
                printlist(list);
                Printf("\nEnter the position:\n");
                scanf("%d",&pos);
                delete(list,pos);
                break;
            case 5:
               
                Printf("Breaking");
                flag = NO;
                break;
               
               
            default:
                break;
        }
    }
   
    return 0;
}
//printing the list and counting the number of objects in them
void printlist(struct object* list)
{
    count = 0;
   
    if (list==NULL)
    {
        Printf("\nlist is null");
    }
   
    while (list->address!=NULL)
    {
        Printf("\n%d",list->data);
        list = list -> address;
        count++;
    }
    Printf("\n%d",list->data);
    printf("\ncount=%d",count);
}
//inserting elements in their respective position with data
void insert(struct object* list,int pos, int data)
{
 
    if (count==pos-1) //inserting to the last like appending
    {
        while (list->address!=NULL) {
            list = list->address;
        }
       
        list->address = (struct object*)malloc(sizeof(struct object));
        list->address->data = data;
        list->address->address = NULL;
    }
    else if (pos==1 && count>1)// inserting at first position after header
    {
            if (list->data == 99)
            {
                struct object *temp = (struct object*)malloc(sizeof(struct object));
                temp->address = list->address;
                temp->data = list->data;
               
                list->address = (struct object*)malloc(sizeof(struct object));
                list->address->data    = data;
                list->address->address = temp->address;
               
                free(temp);
            }
    }
    else //else insert it into the middle
    {
        int i;
       
        if (count>1 && pos >1 && count!=pos-1)
        {
            while (list->address!=NULL)
            {
                list = list->address;
                i++;
                if (i==pos)
                {
                    break;
                }
               
            }
           
            struct object *temp = (struct object*)malloc(sizeof(struct object));
            temp->address = list->address;
            temp->data = list->data;//just to check
           
            list->address = (struct object*)malloc(sizeof(struct object));
            list->address->data    = data;
            list->address->address = temp->address;
           
            free(temp);
           
        }
       
    }
 
}
void delete(struct object* list,int pos)
{
    int localcount;
       if (count==pos) //Deleting the last element
       {
          struct object *temp = (struct object*)malloc(sizeof(struct object));
           while (localcount!=count-1)
           {
               list = list->address;
               localcount++;
           }
         
         
           temp->address = list->address->address;
           temp->data = list->address->data;
         
           list->address = NULL;
         
           free(temp);
               
     
       }
     else if (pos==1 && count>1)//deleting the first element
     {
        struct object *temp = (struct object*)malloc(sizeof(struct object));
         while (list->address!=NULL)
         {
             if (list->data == 99)
             {
                 temp->address = list->address;
                 temp->data = list->data;
               
                 list->address = temp->address->address;
               
                 free(temp);
                 break;
             }
         }
         
       
     }
    else //else deleting the intermediate values
    {
        int k=0;
         struct object *temp = (struct object*)malloc(sizeof(struct object));
        if (count>1 && pos >1 && count!=pos-1)
        {
            while (list->address!=NULL)
            {
       
                k++;
                if (k==pos)
                {
                    break;
                }  
                       list = list->address;
            }
           
            temp->address = list->address;
            temp->data = list->data;
           
            list->address = temp->address->address;
           
            free(temp);
        }
       
    }
   
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments:

Post a Comment