Program to delete a 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 delete a node at the beginning of the linked list :-
Step 1: [initialize] set *start=NULL, *ptr=NULL, *newnode, val
Step 2: ptr=start
Step 3: start=ptr->next
Step 4: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
·        Del beg of void type to delete 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 delete a node at the beginning 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 del 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 del beg:
      to delete a new node at the beginning
We first initialize *start=NULL and *ptr and after that we initialize ptr=start. After that we move to the second node by putting address of the second node in the first by ptr=ptr->next. And then we make start=ptr through which the address of second node will get in the start by this the first node will be removed or deleted.












#include<stdio.h>
#include<conio.h>
 

typedef struct list
  {
    int data;
    struct list *next;
  }node;

 

node *ptr;
node *start=NULL;
node *creat(node*);
node *display(node*);
int countnode(node*);
void delfirst();

 void main()
   {

     int option,c=0;
     clrscr();
     do
       {
     printf("\n Main Menu");
     printf("\n1:Creat list");
     printf("\n2:Display list");
     printf("\n3:Count node");
     printf("\n4:delete first node");
     printf("\n0:Exit\n");
     printf("\nEnter your option:-");
     scanf("%d",&option);
     switch(option)
       {
         case 1:start=creat(start);
         printf("\nlinked list created");
         break;
         case 2:start=display(start);
         break;
         case 3:c=countnode(start);
         printf("\nThese are %d node in list\n",c);
         break;
         case 4:delfirst();
         printf("node deleted successful");
         break;

         case 0:
         exit(0);
       }
       }

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



node *creat(node *start)
  {
    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);
       }
     return start;
      }


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

 int countnode(node* start)
   {
     node *ptr;
     int x=0;
     ptr=start;
     while(ptr!=NULL)
     {
     x++;
     ptr=ptr->next;
     }
     return x;
   }

         void delfirst()
         {

         ptr=start;
         ptr=ptr->next;
         start=ptr;
         }






Post a Comment

Previous Post Next Post