linked list in C

linked list in c
  Linked list
It is a collection of nodes which are linked each other.A node consist of element and memory address
                             One major advantage of linked list is that is support dynamic memory allocation so in this case memory is not wasted.The size of array is full this concept is known as overflow but in linked list overflow does not accrued,it means linked list have unlimited number of nodes.



There is three types of linked list.
 Types of linked list
1. Singly linked list
2. Doubly linked list
3. Circular linked list





Singly linked list

In this linked list each node is divided into two parts one part is called "info" and second part is called "LINK".


"INFO" part represent value of node and "LINK" part represent memory address of next node.In linked list link part of last node must have "NULL" which represent no any node is available.It support uni-directional so we can traverse node left to right


singly linked list program choice base

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
void create();//function prototype
void display();//function prototype
void sinsert();//function prototype
void endnode();
void pinsert();
void sdel();//function prototype
void edel();
void pdel();
void serching();
void sorting();
void findsum();
void nodecount();
void printrev();
struct node{
int info;
struct node *link;
};
struct node *first=NULL,*last=NULL,*new1;

int main()
{

    int choise;
    do
    {

        system("cls");//clrscr();
 printf("\033[1;36m");
        printf("Enter your chose=\n press 1 for create node.\n press 2 to display node.\n press 3 to insert node at  first position.\n press 4 to insert node at last position.\n press 5 for insert at perticular position\n press 6 to delete form start\n press 7 to delete node from last\n press 8 to delete any perticular node\n press 9 to serching any value of list\n press 10 for sorting list\n press 11 to count available node\n presss 12 to find sum\n press13 to print reverse order of node\n press 14 to exit..........\t");
        scanf("%d",&choise);
        switch(choise)
        {
        case 1:
            create(); break;
        case 2:
            display(); break;
        case 3:
            sinsert(); break;
        case 4:
            endnode(); break;
        case 5:
            pinsert(); break;
        case 6:
            sdel(); break;
        case 7:
            edel(); break;
        case 8:
            pdel(); break;
        case 9:
            serching(); break;
        case 10:
            sorting(); break;

        case 11:
            nodecount();break;
        case 12:
            findsum();break;
        case 13:
            printrev();break;
        case 14:
            exit(0);
        default:


            printf("envalid choise\n thank you.....");



        }
    }
    while(choise>=1 && choise<=13);
     printf("\033[0m");
return 0;

}



//Create node in singly linked list.

void create()
{

    new1=(struct node*)malloc (sizeof(struct node));
    printf("Enter the value of node=\t");
    scanf("%d",&new1->info);
    new1->link=NULL;
    if(first==NULL)

        first=new1;
        else
            last->link=new1;
        last=new1;
}

//Display the created node.


void display()
{
   printf("\033[1;36m");
    struct node *temp;
    if(first==NULL)
    printf("There is no any node");
    else{
        printf("All nodes are=\n");
        for(temp=first;temp!=NULL;temp=temp->link)
        {
            printf("%d \n",temp->info);
        }
    }

   system("pause");

}
//Insert node at first position
void sinsert()
{

    new1=(struct node*)malloc (sizeof(struct node));
    printf("Enter the value of node at insert first");
    scanf("%d",&new1->info);
    new1->link=NULL;
    new1->link=first;
    first=new1;

}
//Insert node at last position
void endnode()
{

    new1=(struct node*)malloc (sizeof(struct node));
    printf("Enter the value of node at insert last");
    scanf("%d",&new1->info);
    new1->link=NULL;
    last->link=new1;
    last=new1;


}
//Insert at particular position
void pinsert()
{
    struct node *temp;
    int n,i;
    new1=(struct node*) malloc(sizeof(struct node));
    printf("which position want to you insert");
    scanf("%d",&n);
    printf("enter value of new node\n");
    scanf("%d",&new1->info);
     if(n==1)
     {
         new1->link=first;
         first=new1;
     }
     else
     {


       temp=first;
     for(i=1;i<n-1;i++)
     {
         temp=temp->link;
     }
     new1->link=temp->link;
     temp->link=new1;

     }



}
//Delete node at first position
void sdel()
{
printf("record deleted from first is=%d\n",first->info);
 first=first->link;
system("pause");

}
//Delete node at last position
void edel()
{
        struct node *temp,*pre;
        if(first==NULL)
            printf("there is no any node\n");
        else
    {
    temp=first;
    while(temp!=last)
    {
        pre=temp;
        temp=temp->link;
    }
    pre->link=NULL;
    last=pre;
    printf("deletd recod from last=%d",temp->info);

    }
    system("pause");
}
//Delete node at particular position
void pdel()
{
    struct node *temp,*pre;
    int n,count1=0;
    printf("enter position to deletion\n ");
    scanf("%d",&n); temp=first;
    while(temp!=NULL)
    {
     temp=temp->link;
    count1++;
    }
    if(count1<1||n>=count1)
    {
    printf("there is no node available which you enter\n errror 404.....");
    }
    else
    {
        count1=0;
        temp=first;
        while(count1!=n-1)
        {
         pre=temp;
         temp=temp->link;
         count1++;
        }
        printf("deleted recode=%d\n",temp->info);
        pre->link=temp->link;

    }




system("pause");
}
//Searching node in linked list
void serching()
{
   struct  node *temp; int sv,flag=0;
   printf("enter your search value=\n");
   scanf("%d",&sv);
   if(first==NULL)
   {
       printf("there is no any node\n");
   }

   else{
   temp=first;
   while(temp!=NULL)

   {
       if(temp->info==sv)
       {
           flag=1;
           break;
       }
       temp=temp->link;

  }
}

   if(flag==1)
   printf("your searching is found...");
   else
    printf("your searching is not found...");

   system("pause");

}


//Sorting node in linked list
 void sorting()
{
    struct node *temp,*temp1;
    if(first==NULL)
        printf("there is no nodes");
    else
    {


   int prev;
    for(temp=first;temp!=NULL;temp=temp->link)
    {
        for(temp1=temp->link;temp1!=NULL;temp1=temp1->link)
        {
            if(temp->info>temp1->info)
            {
                prev=temp->info;
                temp->info=temp1->info;
                temp1->info=prev;
            }
            //free(temp);
            //free(temp1);
        }
        //free(temp);
    }}
    printf("All nodes are\n");
    for(temp=first;temp!=NULL;temp=temp->link)
    {
        printf("%d\n",temp->info);
    }
    system("pause");
}
//Sum of all node in linked list
void findsum()
{
    struct node *temp;
    int esum=0,osum=0;
    if(first==NULL)
        printf("no node is available\n");
    else{
    for(temp=first;temp!=NULL;temp=temp->link)
    {
        if(temp->info %2==0)

         esum=esum+temp->info;


        else
            osum=osum+temp->info;

    }
    }
    printf("sum of even node=%d\nsum of odd node=%d\t",esum,osum);
}
//Count node in linked list
void nodecount()
{
    struct node *temp;
    int c=0;

        for(temp=first;temp!=NULL;temp=temp->link)
        {
            c++;
        }

    printf("total node=%d\n",c);
    system("pause");

}
//Display all node in reverse order
void printrev()
{
    struct node *temp;
    int nodevalue[50],i=0,c;
    if(first==NULL)
        printf("NO node here !\n");
    else{
    for(temp=first;temp!=NULL;temp=temp->link)
        {
           nodevalue[i]=temp->info;
           i++;
        }
        c=i;
        for(i=c;i>=0;i--)
        {
            printf("%d\n",nodevalue[i]);
        }
    }
        system("pause");

}

  I hope this post is useful for you......
    plz follow my blog..
    thank u...............

  you can also follw my facebook page.(let's_code)

  link  is give below....Click me














Comments

Popular posts from this blog

python pattern programming

What is function?What is function in python?

Searching and sorting in array