Program to insert a new node at the beginning in a linked list.


Data Structure :- 

It is a critical part of data management. A data structure is a basically a group of data elements that are put together under one name and which defines a particular way of storing and organizing data in a computer, so that it can be used efficiently.
                   
  In other words we can say  that ,Data structure is a representation of the logical relationship existing between individual elements of data and it is the way of organizing all data item that consider not only the element stored but also there relationships to each other. 

Classification of Data Structure:-

Two classes of data structure are use in data structuring:-

·        Primitive data structure

·        Non Primitive data structure 

Primitive data structure :-
 Primitive data structure are integer, real and  character data type. 

Non primitive data structure:- 

Non primitive data structure are of two type:-

·        Linear data structure
·        Non linear data structure

Linear data structure :-

Linear data structure are as follows:-

·        Array
·        Linked list
·        Stack
·        Queue

Non linear data structure :-

Non linear data structure are as follows :-

·        Tree 

·        Graph







Linked list:-

Linked list are the special type of data element usually structure that contain a reference to the data of its same type. So it is called self referential structure. In addition to another data linked list contains a pointer to a data that is the same type of as that of the structure.
With the help of this pointer data element links to one another. 


Struct link_list
    {
        Int data;
        Struct link_list *next;
    } L;

Types of linked list:-

Their are various type of linked list:-

·        Singly linked list
·        Doubly linked list
·        Circular singly linked list
·        Circular doubly linked list 

This program is based on singly linked list. 

Singly linked list:-
A singly linked list is the simple type of list in which every node contains two parts first is data and second is a pointer to the next node. A singly linked list allows traversal of data only in one way. 


The first node of the list is pointed by a pointer which is usually a head or start pointer. The last node of the list contains NULL value in its pointed field.


Struct linked_list

    {
       Int data;
       Struct linked_list *next;
   } node*


Algorithm to create a linked list 


Step 1: [initialize] start=NULL
Step 2: set ptr=start
Step 3:repeat step 4 to 6 while(ptr->! =NULL)
Step 4: allocate memory for ptr
Ptr=(node*)malloc(sizeof(node))
Start=ptr
Step 5: take value for ptr->data=val
Step 6: set ptr=ptr->next
Step 7: end of loop
Step 8: exit


Algorithm to display a linked list :-

Step 1: [initialize] start=NULL, ptr=NULL
Step 2: ptr=start
Step 3: repeat step 4 to 5 while(ptr! =NULL)
Step 4: print ptr->data
Step 5: set ptr=ptr->next
Step 6: end of loop
Step 7: exit

Algorithm to insert a node at the beginning of the linked list :-


Step 1: [initialize] set *start=NULL, *ptr=NULL, *newnode, val
Step 2: if start!=NULL
Step 3: newnode=(node*) malloc(sizeof(node))
Step 4:Set newnode->data=val
               Newnode->next=start
Step 5: start=newnode
Step 6:exit


In the program we have to first write the header file of C language that is #include<stdio.h>, #include<conio.h>. The we declare structure of list by writing keyword typedef struct list. 
 Which contain two variables one is data of int type to store value and second is next of struct 

list type to store the adders of next node. Then we write function prototype :-

·        Create of void type to  create a linked list
·        Display of void type to display the list
·        Insert beg of void type to insert a node at the beginning of the list


Then we start our main() which is known as driver of all the functions. In that we firstly take a variable option of int type to for the switch, then our loop starts do in that we firstly print massage for user like enter 1 to create list, 2 to display list, 3 to insert a node at the end and 0 to exit. Then the user will give her choice and the switch starts case 1 is to create a list, case 2  is to display a list, case 3 is to insert beg and case 0 is to exit. Then our loop ends by writing keyword while(option! =0)  and bracket get closed.

Then we starts writing the functions definition.

·        Void create() :  to create a list
We first give our first element in the data part  and put NULL at the next part. Then we start malloc function to take more memory for the new nodes and put that address in the previous node next part, though which they are linked to each other until we 999 as a number then it stops taking number and get exit. 

·        Void display() : to display a list
We initialize ptr=start and starts loop while(ptr! =NULL) and print ptr->data. 

·        Void insert beg: to insert a new node at the beginning
We first allocate memory for new node by malloc function and the user will give a new element.  And then in the newnode we put the address of start (newnode->next=start). After that we initialize start=newnode. Through this new node will get inserted at the beginning of the list. 






INSERT A NEW NODE AT BEGINNING   IN THE LINKED LIST USING C LANGUAGE



#include<stdio.h>
#include<conio.h>
typedef struct list
  {
    int data;
    struct list *next;
  }node;

node *start=NULL;
void creat();
void display();
 void insert_beg();


 void main()
   {
    // node *ptr=NULL;
     int option;
     clrscr();
     do
       {
     printf("\n Main Menu");
     printf("\n1:Creat list");
     printf("\n2:Display list");
     printf("\n3:Insert at begining");
     printf("\n0:Exit\n");
     printf("\nEnter your option:-");
     scanf("%d",&option);
     switch(option)
       {
         case 1:creat();
         printf("\nlinked list created");
         break;
         case 2:display();
         break;

       case 3:insert_beg();
         printf("\nNode inserted at begining:-");
         break;
         case 0:
         exit(0);
       }
       }

   while(option!=0);
   getch();
 }


void creat()
  {
    node *ptr,*newnode;
    int num;
    printf("Enter data,pres 999 to exit:-");
    scanf("%d",&num);
    while(num!=999)
      {
     newnode=(node*)malloc(sizeof(node));
     newnode->data=num;
     if(start==NULL)
       {
         newnode->next=NULL;
         start=newnode;
       }
     else
       {
         ptr=start;
         while(ptr->next!=NULL)
         ptr=ptr->next;
         ptr->next=newnode;
         newnode->next=NULL;
       }
         printf("Enter another data:-");
         scanf("%d",&num);
       }
        }

 void  display()
  {
  node *ptr;
  ptr=start;
  while(ptr!=NULL)
  {
  printf("\%d->",ptr->data);
  ptr=ptr->next;
  }

  }




  void insert_beg()

     {
       node *newnode,*ptr;
       int num;
       newnode=(node*)malloc(sizeof(node));
       printf("\nEnter num:-");
       scanf("%d",&num);
       newnode->data=num;
       newnode->next=start;
       start=newnode;

}

Post a Comment

Previous Post Next Post