question 1:
By using linked list ADT write a program that manages students list. The student is characterized by (Name, ID and Level). Linked list should be ordered by student ID.
First of all you should download the ADT file from the blackboard and use it in your cpp file.
1. After creating the linked list add the following students:
ID:222,name :"Sara",level:3
ID:111;name:"Mona"; level:3
ID:333;name:"Nora";level:3
2. Delete "Sara" from the list using her ID.
3. Print the details of all students in the linked list .
and this is my attempts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include<iostream>
#include"List.h"
using namespace std;
struct student
{
string name;
int key; // ID
int level;
};
int main()
{
List <student,int>studentlist;
student st1;
st1.key = 222;
st1.level = 3;
st1.name = "Sara";
student st2;
st2.key = 111;
st2.level = 3;
st2.name = "Mona";
student st3;
st3.key = 333;
st3.level = 3;
st3.name = "Nora";
studentlist.addNode(st1);
studentlist.addNode(st2);
studentlist.addNode(st3);
student sudentOUT;
studentlist.removeNode( 222, sudentOUT);
int key;
if (studentlist.listCount()==0)
cout<<"nothing in the list";
else
{
studentlist.getNext(0,sudentOUT);
do{
cout<<sudentOUT.key;
cout<<sudentOUT.level;
cout<<sudentOUT.name;
}
while (studentlist.getNext(1,sudentOUT));
}//else
return 0;
}
|
Question 2:
By using ADT, write a C++ program which implements a linked list that manages library books. A book is characterized by (author, price, unique ISBN number). It allows user to do one of the following:
1. Build a book list by adding book information until the user enters -1.
2. Print the details of a specific book by using ISBN number.
3. Change the price of all books that are higher than 100 SR by giving a 25% discounty4.
Notes:
- As the question said by using ADT ,you can only use the public functions in the class List.
- Book data type is implemented as struct:
struct Book{
string author;
double price;
int Key;//note that int ISBN; is incorrect
};
and this is my attempts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include<iostream>
#include"List.h"
using namespace std;
struct Book{
string author;
double price;
int key;//note that int ISBN; is incorrect
};
int main()
{
List<Book,int> booklist;
Book bk[100];
int key;
Book dataout;
for(int i=0;i<100;i++){
cout<<"Enter the outhor:\n";
cout<<"Enter the price:\n";
cin>>bk[i].price;
cout<<"Enter the ISBN:\n";
cin>>bk[i].key;
if(bk[i].price!=-1)
{booklist.addNode(bk[i]);}
else
{cout<<"end of list";
exit(1);}}
cout<<"enter the ISBN of book to print his details:\n";
cin>>key;
if(booklist.retrieveNode(key,dataout)==1)
{cout<<dataout.key;
cout<<dataout.price;
}
booklist.getNext(0,dataout);
while(booklist.getNext(1,dataout)!=false)
{if (dataout.price>100)
{dataout.price=dataout.price*25/100;}
return 0;}
|
Question 3:
You are asked to write a C++ program to model a shop that sells three types of laptops. The shop has decided to keep its laptop records in a linked list. Each laptop is characterized by an identifying number, type, quantity and price.
(the quantity would be 0 if currently there is not any one available of this type)
The company keeps its laptops in an ordered list.
Laptop Model Number (Key) Type Quantatiy Price
111 Toshiba 20 3000
222 IBM 30 4000
333 Sony 40 5000
Figure 1
First of all, download the ADT of linked list from the blackboard to include it in your cpp file. Then, given the code at the end do the following:
• Using ADT, implement and test the following two functions:
getTotalPriceFunction:
This function should return the total price that the shop would get from selling all the currently available quantity of a given laptop identifying number. Test your function to return 120000 if you send the key 222 as a parameter .
PrintLaptopsInfo function:
This function should print the details of laptops
and this is my attempts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#include<iostream>
#include"List.h"
#include<string>
using namespace std;
struct laptop{
int key;//identifying number
string type;
int quantity;
int price;
public:
int getTotalPrice(int);
void PrintLaptopsInfo();
};
int getTotalPrice(int key){
int quantity;
int price;
int total;
cout<<"the total is\n";
return total = quantity * price; }
void PrintLaptopsInfo(){
cout<<laptop1.type<<laptop1.quantity<<laptop1.price;
cout<<laptop2.type<<laptop2.quantity<<laptop2.price;
cout<<laptop3.type<<laptop3.quantity<<laptop3.price;}
int main()
{
List<laptop,int> laptops;
laptop laptop1,laptop2,laptop3;
laptop1.key=111; laptop1.type="Toshiba"; laptop1.quantity=20; laptop1.price=3000;
laptop2.key=222; laptop2.type="IBM"; laptop2.quantity=30; laptop2.price=4000;
laptop3.key=333; laptop3.type="Sony"; laptop3.quantity=40; laptop3.price=5000;
laptops.addNode(laptop1);
laptops.addNode(laptop2);
laptops.addNode(laptop3);
return 0;}
|