All about programming in GNU/LINUX

Link lists:11 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;//you can make use of these flag values in the program if you want to
    }
    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;
}
NODE deleteinfo(NODE first,int key)
{
    NODE cur,prev;

    if(first==NULL)
    {
        field();
        printf(“\t\tList empty”);
        field();
        return first;
    }
    cur=allocate(cur);
    if(first->info==key)
    {
        cur=first;
        first=first->link;
        free(cur);
        field();
        printf(“\t\tThe element was in first node and is deleted “);
        field();
        return first;
    }
    prev=allocate(prev);
    prev=NULL;
    cur=first;
    while(cur!=NULL && cur->info!=key)
    {
        prev=cur;
        cur=cur->link;

    }
    if(cur==NULL)
    {
        field();
        printf(“\t\tElement not found “);
        field();
        return first;
    }
    prev->link=cur->link;
    free(cur);
    field();
    printf(“\t\tThe node with item %d is deleted”,key);
    field();
    return first;
}
NODE insertnodepos(NODE first,int pos,int element)
{
    NODE temp,prev,cur;
    int count=1;
    temp=allocate(temp);
    temp->info=element;
    temp->link=NULL;
    if(first==NULL && pos==1)
    {
        return temp;
    }
    if(pos==1)
    {
        temp->link=first;
        return temp;
    }
    prev=NULL;
    cur=first;
    while(cur!=NULL && count!=pos)
    {
        prev=cur;
        cur=cur->link;
        count++;
    }
    printf(“\ncount=%d”,count);
    if(count!=pos)
    {
        field();
        printf(“\t\tINVALID POSITION”);
        field();
        free(temp);
        return first;
    }
    prev->link=temp;
    temp->link=cur;
    return first;
}
NODE delposnode(NODE first,int pos)
{
    int count=1;
    NODE cur,prev;
    if(first==NULL)
    {
        field();
        printf(“\t\tNO NODES EXIST”);
        field();
        return NULL;
    }
    cur=allocate(cur);
    if(pos==1)
    {
        cur=first;
        first=first->link;
        free(cur);
        field();
        printf(“\t\tThe first node is freed”);
        field();
        return first;
    }
    prev=allocate(prev);
    prev=NULL;
    cur=first;
    //printf(“hi”);
    while(cur!=NULL && count!=pos)
    {
        prev=cur;
        cur=cur->link;
        count++;
    }
    printf(“\ncount: %d pos:%d”,count,pos);
    if(cur==NULL)
    {
        field();
        printf(“\t\tInvalid Entry”);
        field();
        return first;
    }
    prev->link=cur->link;
    free(cur);
    field();
    printf(“\t\tThe node at position %d is deleted “,pos);
    field();
    return first;
}
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*9.DELETE INFO   * “);
       printf(“\n\t*10.PUT ITEM POS * “);
       printf(“\n\t*11.DEL POS NODE * “);
       printf(“\n\t*12.EXIT”);
       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 ;
           case 9:printf(“\nEnter the info to be deleted: “);
                  scanf(“%d”,&key);
                  first=deleteinfo(first,key);
                  break ;
           case 10:printf(“\nEnter the position at which you want the node to be inserted: “);
                   scanf(“%d”,&key);
                   printf(“\nEnter the element to be inserted: “);
                   scanf(“%d”,&item);
                   first=insertnodepos(first,key,item);
                  break ;
           case 11:printf(“\nEnter the position where you want the node to be deleted: “);
                   scanf(“%d”,&key);
                   first=delposnode(first,key);
                   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