Perform all operation in a singly linked list using C language:

#include <stdio.h>

#include<stdlib.h>

struct S_list


{

    int age;

    struct S_list *next;

};

struct S_list *read_list();

struct S_list *create_list(int num);

void print_list(struct S_list * start);

void search(struct S_list *start,int find);

void delete_list( struct S_list *start);

int main()

{

 struct S_list *start;

 int find;

 start=read_list();

 printf("Data in the list:\n");

 print_list(start);

 printf("Enter the element to be search:\n");

 scanf("%d",&find);

 search(start,find);

 delete_list(start);

 printf("Data in the list:\n");

 print_list(start);

 printf("\n\nCodes Written by-Vikas kumar\n");

 printf("\n\n on %s\n",__DATE__);

return 0;

}


struct S_list *read_list()

{

    struct S_list* start,*newPtr,*endptr; 

    int size;

    printf("Enter number of elements\n");

    scanf("%d",&size);


    for(int i=0; i<size; i++)

    {

        int num ;

        printf("Enter Element[%d]:\n",i+1);

       scanf("%d",&num);

        newPtr=create_list(num);


        if(i==0)


        {

            start=endptr=newPtr;

        }


        else

        {

            endptr->next=newPtr;

            endptr=newPtr;

        }

    }

     return start;

}


struct S_list *create_list(int num)

{

    struct S_list *ptr=(struct S_list *)malloc(sizeof(struct S_list));

    ptr->age=num;

    ptr->next=NULL;

    return ptr;

}


void print_list(struct S_list * start)

{

    struct S_list *ptr=start;

    while(ptr!=NULL)

    {

        printf("%d ",ptr->age);

        ptr=ptr->next;

    }

    printf("\n");

}


void search(struct S_list *start,int find)

{

    struct S_list *ptr=start;

    int i=0, flag=0;

    while(ptr!=NULL)

    {

            if(ptr->age==find)

            {

               printf("\t%d is present at node %d in the list \n\n",find,i+1);

               flag=1;

            }  

            i++;

            ptr=ptr->next;

   }

   if(flag==0)

   {

       printf("%d is not present in the list \n\n;",find);

   }

}


void delete_list( struct S_list *start)

{


    struct S_list *ptr=start,*endptr;

    int num;

    printf("Enter the Element you want to deleted:\n");

    scanf("%d",&num);

    while(ptr!=NULL)

    {

            if(ptr->age==num)

            {

               endptr->next=ptr->next;

                printf(" Deleted element is %d\n",ptr->age);

                free(ptr);

                ptr=endptr;

            }

           endptr=ptr;

           ptr=ptr->next;    

    }

}


//OUTPUT:





Comments

Post a Comment

Popular posts from this blog

[ C++ program] Assume that a bank maintains two kinds of accounts for customers, | | one called as savings account and the other as current account. | | The savings account provides compound interest and withdrawal facilities but no cheque book facility. | | The current account provides cheque book facility but no interest. | | Current account holders should also maintain a minimum balance | | and if the balance falls below this level, a service charge is imposed. | | Create a class account that stores customer name, account number and type of account. | | From this derive the classes curacct and savacct to make them more specific to their requirements.

Write a program in C++ to create a base class shape and derive two classes named Circle and Square from it. Create and initialize objects from these classes using appropriate constructors.