Question

In: Computer Science

Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...

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.

Solutions

Expert Solution

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;
   }
};


Related Solutions

Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr);...
Assume that struct Node{        int item;        Node* link; }; Write function void list_remove(NodePtr& prev_ptr); The function will remove the node after the node pointed by prev_ptr. c++
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You...
Below is for C language typedef struct _node { Node *next; char *str; } Node; You are given a function that takes in two Node* pointers to two different linked lists. Each linked list node has a string represented by a character array that is properly null terminated. You're function is supposed to return true if the concatenation of strings in a linked list match each other, else return false. EXAMPLES List 1: "h" -> "el" -> "l" -> "o"...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT