Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C Programme Implement sequential list on books and perform all operations.

ID: 3762928 • Letter: U

Question

Using C Programme Implement sequential list on books and perform all operations. Structure of book should contains name of book, author name, no of pages, price. Operations performed should be:

     1. Addition of book in the list based on sorted order of author name. if author name for two books is same then sort based on book name

     2. perform the deletion based on author name.

     3. perform the deletion based on book name.

     4. search a particular book based on name and author name

    5. Print all the books available

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
typedef struct books
{
    char author[100];
    char name[100];
    int pages;
    float price;
   struct books *next;
}books;
books* insert(books *start)
{
   books *new=malloc(sizeof(books));
   printf("What is the author name?:");
   scanf("%s",new->author);
   printf("What is the book name?:");
   scanf("%s",new->name);
   printf("How many are there in the book?:");
   scanf("%d",&new->pages);
   printf("What is the price of the book?:");
   scanf("%f",&new->price);
   books *temp,*prr;
   temp = start->next;
   if(temp==NULL)
   {
       start->next=new;
       new->next=NULL;
       return start;
   }
   while(temp->next!=NULL)
   {
       prr = temp->next;
       if(strcmp(new->author,prr->author)<0)
       {
           break;
       }
   }
   prr= temp->next;
   temp->next=new;
   new->next = prr;
   return start;
}
void print(books *start)
{
   books *temp=start;
   while(temp!=NULL)
   {
       printf("%s %s %d %f ",temp->author,temp->name,temp->pages,temp->price);
       temp = temp->next;
   }
}
void delete_author(books * start)
{
   books *prev=start;
   books *temp = start->next,*t2;
   char a[100];
   printf("What is the author name?:");
   scanf("%s",a);
   while(temp!=NULL)
   {
       if(strcmp(temp->author,a)==0)
       {
           prev->next = temp->next;
           free(temp);
           break;
       }
       prev=prev->next;
       temp=temp->next;
   }
}

void delete_name(books * start)
{
   books *prev=start;
   books *temp = start->next,*t2;
   char a[100];
   printf("What is the book name?:");
   scanf("%s",a);
   while(temp!=NULL)
   {
       if(strcmp(temp->name,a)==0)
       {
           prev->next = temp->next;
           free(temp);
           break;
       }
       prev=prev->next;
       temp=temp->next;
   }
}

books* search_author(books* start)
{
   books *temp = start->next;
   char a[100];
   printf("What is the author name?:");
   scanf("%s",a);
   while(temp!=NULL)
   {
       if(strcmp(temp->author,a)==0)
       {
           return temp;
       }
       temp=temp->next;
   }
   return NULL;
}

books* search_name(books* start)
{
   books *temp = start->next;
   char a[100];
   printf("What is the book name?:");
   scanf("%s",a);
   while(temp!=NULL)
   {
       if(strcmp(temp->name,a)==0)
       {
           return temp;
       }
       temp = temp->next;
   }
   return NULL;
}
int main(void) {
   // your code goes here
   int i;
   books *start=malloc(sizeof(books));
   start->next=NULL;
   while(1)
   {
        printf("Type 1 for adding a book. Type 2 for deleting a book based on author ");
        printf("Type 3 the deleting a book based on book name. ");
        printf("Type 4 for searching for a book based on author. ");
        printf("Type 5 for searching for a book based on book name. ");
        printf("Type 6 to print all the books available. ");
        printf("Type 7 to quit. ");
        printf("Choose one of the above: ");
        scanf("%d",&i);
       if(i==1)
       {
           start=insert(start);
       }
       else if(i==2)
       {
           delete_author(start);
       }
       else if(i==3)
       {
           delete_name(start);
       }
       else if(i==4)
       {
           books *temp = search_author(start);
           if(temp==NULL)
           {
               printf("No book found ");
           }
           else
           {
               printf("%s %s %d %f ",temp->author,temp->name,temp->pages,temp->price);
           }
       }
       else if(i==5)
       {
           books *temp = search_name(start);
           if(temp==NULL)
           {
               printf("No book found ");
           }
           else
           {
               printf("%s %s %d %f ",temp->author,temp->name,temp->pages,temp->price);
           }
       }
       else if(i==6)
       {
           print(start->next);
       }
        else if(i==7)
        {
            break;
        }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote