All about programming in GNU/LINUX

Linked List functions

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

bool emptytest;//TO TEST FOR EMPTY LIST
typedef struct node* NODE;
struct node
{
    int info;
    NODE link;
};//dont miss ; at the end of the structure

NODE allocate(NODE temp)//function to allocate memory for created nodes
{
    temp=(NODE)malloc(sizeof(struct node));
    return temp;
    /*no need to return temp as the operation is performed on the pointer (all elements of type NODE
      are pointers ), the change is observed implicitly)*/
}
void field()
{
    int i;
    printf(“\n\t\t”);
    for( i=0;i<10;i++)
     printf(“*”);
    printf(“\n”);
}
NODE insertfront(NODE first,int item)

{
    emptytest=false;
    NODE temp;
    temp=allocate(temp);

    temp->info=item;
    temp->link=first;
    return temp;

}
NODE deletefront(NODE first)
{
    NODE temp;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO Nodes Exist”);
        field();

        return first;
    }

    temp=allocate(temp);
    temp=first;

    first=first->link;
    field();
    printf(“The first node with item %d is deleted”,temp->info);
    field();
    free(temp);
    if(first==NULL)
    {
        emptytest=true;
    }
    return first;

}
NODE insertrear(NODE first,int item)
{
    NODE current,temp;
    current=allocate(current);
    emptytest=false;
    if(first==NULL)
    {

        current->info=item;
        return current;

    }
    temp=allocate(temp);
    current=first;
    while(current->link!=NULL)
    {
        current=current->link;
    }
    current->link=temp;
    temp->info=item;
    temp->link=NULL;
    return first;

}
NODE deleterear(NODE first)
{
    NODE current,prev;
    if(first==NULL)
    {

        field();
        printf(“\n\tNo elements in the List”);
        field();
        return first;
    }
    if(first->link==NULL)
    {
        field();
        printf(“\t\tOnly one node was present and is deleted “);
        printf(“\nThe data in the field was: %d”,first->info);
        field();

        free(first);
        return NULL;
    }
    current=allocate(current);
    prev=allocate(prev);
    current=first;
    prev=NULL;
    while(current->link!=NULL )
    {
        prev=current;
        current=current->link;

    }
    prev->link=NULL;
    field();
    printf(“\tTHE LAST NODE WITH INFORMATION %d is deleted”,current->info);
    free(current);
    if(first==NULL)
    {
        emptytest=true;
    }
    return first;

}
void display(NODE first)
{

    NODE current;
    current=allocate(current);
    current=first;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO NODES EXIST”);
        field();
        return ;
    }
    printf(“\n”);

    while(current->link!=NULL)
    {
        printf(“|%d|->”,current->info);
        current=current->link;
    }
    printf(“|%d|”,current->info);
    printf(“\n\n”);
}
void countnode(NODE first)
{
    NODE current;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO nodes Exist”);
        field();
        return ;
    }
    current=allocate(current);
    current=first;
    while(current!=NULL)
    {
        current=current->link;
        ++count;
    }
    field();
    printf(“\t\tThe no.of.nodes in the list=%d”,count);
    field();
}
void searchelement(NODE first,int key)
{
    NODE temp;
    int count=0;
    if(first==NULL)
    {
        field();
        printf(“\t\tThe List is Empty “);
        field();
        return ;
    }
    temp=allocate(temp);
    temp=first;
    while(temp)
    {
        count++;
        if(temp->info==key)
        {
            field();
            printf(“\t\tThe key found at location no %d in the list “,count);
            return ;
        }
        temp=temp->link;
    }
    field();
    printf(“Key not found”);
    field();
}

NODE reverse(NODE first)
{
    NODE previous,current;
    char* ch;
    if(first==NULL)
    {
        field();
        printf(“\t\tList is Empty”);
        field();
        return ;
    }
    previous=allocate(previous);
    current=allocate(current);
    previous=NULL;
    while(first!=NULL)
    {
        current=first;
        first=first->link;
        current->link=previous;
        previous=current;
    }
    field();

   return previous;
}
int main()
{

   NODE first;
   int choice,item,key;

   first=allocate(first);
   first=NULL;//VERY IMPORTANT INITIALIZATION
   emptytest=true;//INDICATES THAT THE LIST IS EMPTY
   while(1)
   {
      printf(“\n\t********************”);
       printf(“\n\t*1.INSERT FRONT  *******”);
       printf(“\n\t*2.DELETE FORNT  *     *”);
       printf(“\n\t*3.INSERT REAR   *     *”);
       printf(“\n\t*4.DELETE REAR   *     *”);
       printf(“\n\t*5.DISPLAY       *     *”);
       printf(“\n\t*6.COUNT NODES   *******”);
       printf(“\n\t*7.SEARCH ELEMENT*”);
       printf(“\n\t*8.REVERSE LIST  *”);
       printf(“\n\t******************”);
       printf(“\nEnter the choice: “);
       scanf(“%d”,&choice);
       switch(choice)
       {
           case 1:
                  printf(“\nEnter the number to be added to the Beginning of the list: “);
                  scanf(“%d”,&item);
                  first=insertfront(first,item);
                  break;
           case 2:
                  first=deletefront(first);
                  break;
           case 3:
                  printf(“\n\tEnter the item to be inserted to the ned of the list: “);
                  scanf(“%d”,&item);
                  first=insertrear(first,item);
                  break;

           case 4:
                  first=deleterear(first);
                  break;
           case 5:
                  display(first);
                  break ;
           case 6:
                  countnode(first);
                  break ;
           case 7:
                   if(emptytest)
                   {
                       field();
                       printf(“\t\tList is empty “);
                       field();
                       break ;
                   }
                   field();
                   printf(“Enter the key to be searched: “);
                   field();
                   scanf(“%d”,&key);

                   searchelement(first,key);
                   break;
           case 8:
                  first=reverse(first);
                  field();

                  break ;

           default:exit(0);
       }
   }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s