In: Computer Science
Assume that
struct Node
{
int item; Node* link;
};
typedef Node* NodePtr;
1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head.
2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head.
3. Write function NodePtr list_search(NodePtr head, int target); The function will search through the linked list that is pointed by head and return a NodePtr that points to the Node that contains target as its item. If there is no such a Node, NULL will be returned.
4. Suppose that you have a class called Student that is defined in student.h file. The class is in a namespace called fhsuzeng. Fill in the following blanks for the structure of the header file student.h.
#ifndefine STUDENT_H
____________________________
namespace __________________
{
___________ Student
{
};
} ____________________
5. Suppose that Account class has a method called withdraw, which will be inherited by Checking and Savings class. The withdraw method will perform according to account type. So, the late bind is needed. Declare the withdraw method in Account class. The method should take a double input as withdraw amount and output true if the withdraw successfully and false if the withdraw unsuccessfully.
6. Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount) and double getBalance() const. The method names explain its purpose. Further suppose that child class Savings has one more attribute double interest_rate and a public method void addInterest() which will update the balance according the formula new balance = old balance * (1 + interest_rate/100.0); Implement addInterest method.
solution of 1,2,3 with hardcoded main function
------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
struct Node
{
int item;
Node* link;
};
typedef Node* NodePtr;
void list_head_insert(NodePtr& head, int entry)
{
NodePtr temp = (NodePtr)malloc(sizeof(NodePtr));
temp->item=entry;
temp->link=head;
head=temp;
}
void list_head_remove(NodePtr& head)
{
if(head==NULL)
head=NULL;
else
head=head->link;
}
NodePtr list_search(NodePtr head, int target)
{
NodePtr temp=head;
while(temp!=NULL)
{
if(temp->item==target)
return
temp;
temp=temp->link;
}
return NULL;
}
void display(NodePtr head)
{
if(head==NULL)
return ;
NodePtr temp=head;
while(temp!=NULL)
{
cout<<temp->item<<"
";
temp=temp->link;
}
cout<<endl;
}
int main()
{
NodePtr head,find;
list_head_insert(head,4);
list_head_insert(head,5);
list_head_insert(head,6);
list_head_insert(head,7);
display(head);
list_head_remove(head);
display(head);
find=list_search(head,6);
cout<<find->item;
return 0;
}
----------------------------------------------------------------------------------------------------------------------
Solution of 4
------------------------------------------------------------------------------------------------------------------------
#ifndefine STUDENT_H
using namespace Student;
namespace fhsuzeng;
{
Student Student;
{
};
}using namespace fhsuzeng;
------------------------------------------------------------------------------------------------------------------
Solution 5,6
------------------------------------------------------------------------------------------------------------------
class Account
{
private : double balance;
public : bool withdraw(double withdraw_amount)
{
if(withdraw_amount>balance)
return
false;
balance = balance -
withdraw_amount;
return true;
}
public : void setBalance(double amount)
{
balance=balance + amount;
}
public : double getBalance()
{
return balance;
}
};
class Savings : Account
{
private: double interest_rate;
public : bool withdraw(double withdraw_amount)
{
double balance=getBalance();
if(withdraw_amount>balance)
return
false;
balance = balance -
withdraw_amount;
setBalance(balance);
return true;
}
public : void addInterest()
{
double balance=getBalance();
balance = balance * (1 +
interest_rate/100.0);
setBalance(balance);
}
};
class Checking: Account
{
double balance=getBalance();
public : bool withdraw(double withdraw_amount)
{
if(withdraw_amount>balance)
return
false;
balance = balance -
withdraw_amount;
setBalance(balance);
return true;
}
};