A menu driven program to implement a link list where every node of the link list consists of the following information:
1. Name of the book
2. Price of the book
3. Number of copies
The program has the following options:
1. Add a node at the beginning of list.
2. Display the link list.
3. Display the information of only those books which have number of copies greater than 5 and price greater than Rs.150/-.

 

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

struct info
{
char book_name[30];
float price;
int copy;
};

struct node
{
info data;
node*link;
};

node *start=NULL,*x,*y;

void add()
{
if(start==NULL)
{
start=new node;
cout<<"\nEnter book name: ";
cin>>start->data.book_name;
cout<<"Enter price: ";
cin>>start->data.price;
cout<<"Enter no. of copies: ";
cin>>start->data.copy;
start->link=NULL;
}
else
{
x=new node;
cout<<"Enter book name: ";
cin>>x->data.book_name;
cout<<"Enter price: ";
cin>>x->data.price;
cout<<"Enter no. of copies: ";
cin>>x->data.copy;
x->link=start;
start=x;
}
}

void display1()
{
x=start;
while(x!=NULL)
{
cout<<"\nName of book: "<<x->data.book_name<<endl;
cout<<"Price: "<<x->data.price<<endl;
cout<<"No. of copies: "<<x->data.copy<<"\n\n";
x=x->link;
}
}

void display2()
{
x=start;
while(x!=NULL)
{
if(x->data.price>150 && x->data.copy>10)
{
cout<<"Name of book: "<<x->data.book_name<<endl;
cout<<"Price: "<<x->data.price<<endl;
cout<<"No. of copies: "<<x->data.copy<<"\n\n";
}
else
{
cout<<"There are no books which have more than 5 copies";
cout<<"\t\tand price greater than Rs 150."<<"\n\n";
}
}
}

void main()
{
 clrscr();

 int ch;

 start=NULL;

 do
  {
    cout<<"1. Add at begining."<<endl;
    cout<<"2. Display list."<<endl;
    cout<<"3. Display particular."<<endl;
    cout<<"4. Exit"<<endl;
    cout<<"Enter your choice: ";
    cin>>ch;

    switch(ch)
     {
       case 1: {
                    add();
                    break;
                   }
       case 2: {
                    display1();
                    break;
                   }
       case 3: {
                    display2();
                    break;
                   }
       case 4: {
                    break;
                   }
       default: {
                    cout<<"Wrong choice entered."<<"\n";
                   }
    }
   }
 while(ch!=4);
}

 

< Back