negru 0 Light Poster

Create doubly linked list that has the following information about books: book_id, book_name, author_id, book_price. Sort list in ascending order by book_price using bubble sort algorithm. Delete all books with the smallest and the largest price.

How to modify the following program such that the sorting is done using bubble sort algorithm? Also, how to implement a function for deletion of all books with the smallest and the largest price (more books can have the same price)?

#include<stdio.h>
#include<stdlib.h>

void addToList(NODE **head,NODE **tail,BOOK *bk);//adding nodes to list and sorting
NODE* searchByPrice(NODE *head,NODE *tail,float book_price);
int deleteNodes(NODE **head,NODE **tail,float book_price);//deletes only the specified node
void readNode(BOOK *bk);
void printList(NODE *head);

typedef struct
{
    char book_id[8];
    char book_title[21];
    char author_id[10];
    float book_price;
}BOOK;

typedef struct node
{
    BOOK bk;
    struct node *left,*right;
}NODE;

void addToList(NODE **head,NODE **tail,BOOK *bk)
{
    NODE *p,*q,*new_node=(NODE *)malloc(sizeof(NODE));
    new_node->bk=*bk;
    new_node->left=0;
    new_node->right=0;

    if(*head == 0)
       *head=*tail=new_node;
    else if((*head)->bk.book_price > bk->book_price)
    {
        new_node->right=*head;
       (*head)->left=new_node;
        *head=new_node;
    }
    else if((*tail)->bk.book_price < bk->book_price)
    {
        new_node->left=*tail;
        (*tail)->right=new_node;
        *tail=new_node;
    }
    else
    {
        for(p=*head,q=(*tail)->left;
           (p->right->bk.book_price < bk->book_price) && (q->bk.book_price > bk->book_price);
            p=p->right,q=q->left);
        if(q->bk.book_price > bk->book_price)
                p=q;
        new_node->right=p->right;
        new_node->left=p;
        p->right->left=new_node;
        p->right=new_node;
    }
}

NODE* searchByPrice(NODE *head,NODE *tail,float book_price)
{
    BOOK bk;
    if(head == 0)
        return 0;
    while((head->bk.book_price < bk.book_price) && (tail->bk.book_price > bk.book_price))
    {
        head=head->right;
        tail=tail->left;
    }
    return (head->bk.book_price == book_price) ? head : ((tail->bk.book_price == book_price) ? tail : 0 );
}

int deleteNodes(NODE **head,NODE **tail,float book_price)
{
    NODE *p=searchByPrice(*head,*tail,book_price);
    if(p == 0)
        return 0;
    else if(p == *head && p == *tail)
        *head=*tail=0;
    else if(p == *head)
    {
        *head=(*head)->right;
        (*head)->left=0;
    }
    else if(p == *tail)
    {
        *tail=(*tail)->left;
        (*tail)->right=0;
    }
    else
    {
        p->left->right=p->right;
        p->right->left=p->left;
    }
    free(p);
    return 1;
}

void readNode(BOOK *bk)
{
    printf("Book ID: ");
    scanf("%s",bk->book_id);
    fflush(stdin);
    printf("Book title: ");
    gets(bk->book_title);
    printf("Author ID: ");
    scanf("%s",bk->author_id);
    printf("Book price: ");
    scanf("%f",&bk->book_price);
}

void printList(NODE *head)
{
   printf("BOOK_ID BOOK_TITLE           AUTHOR_ID BOOK_PRICE\n");
   printf("------- -------------------- --------- ----------\n");
   while(head)
   {
      printf("%-7s %-20s %-9s %10.2f\n",
             head->bk.book_id,head->bk.book_title,head->bk.author_id,head->bk.book_price);
      head=head->right;
   }
}

int main()
{
   NODE *head=0,*tail=0;
   BOOK bk;
   char option;
   float book_price;

       printf("Option 'A': add node to list.\n");
       printf("Option 'D': delete node from list by book price.\n");
       printf("Option 'P': print list.\n");
       printf("Option 'S': search list.\n");
       printf("Option '0': exit.\n");

    while(1)
    {
        fflush(stdin);
        printf("\nEnter option: ");
        scanf("%c",&option);

        if(option == 'A')
        {
          printf("Enter data: \n");
          readNode(&bk);
          NODE *p=searchByPrice(head,tail,bk.book_price);
          if(p)
            p->bk=bk;
          else
            addToList(&head,&tail,&bk);
        }

        else if(option == 'D')
        {
           printf("Book price: ");
           scanf("%f",&book_price);
           if(deleteNodes(&head,&tail,book_price))
             printf("data about book with price %f is deleted.",book_price);
           else printf("no data about book with price %f.",book_price);
        }

        else if(option == 'P')
           printList(head);

        else if(option == 'S')
        {
            printf("Book price: ");
           scanf("%f",&book_price);
           NODE *p=searchByPrice(head,tail,book_price);
           if(p)
             printf("%s has a price %f.",p->bk.book_id,p->bk.book_price);
           else
             printf("no data about book with price %f",p->bk.book_price);
        }

        else if(option == '0')
        {
            printf("End.");
            break;
        }

        else printf("Unknown option.\n");
    }

    return 0;
}