Question

Assignment Description. In this assignment, you will design a class to represent a book of recipes...

Assignment Description. In this assignment, you will design a class to represent a book of recipes using a doubly linked list. Your recipe book will support methods to add a recipe and get a recipe at a position; your main will use these to print all the names of all the recipes in the book.

  1. Create a C++ class called RecipeBook. Your class must implement the following variables, constructors, and methods:
    • Create a private inner class called Node for implementing doubly-linked list structures. Nodes should have variables for the node’s data, as well as references to the Next and Previous nodes in the list.
    • RecipeBook needs member variables for the head and tail Nodes in the list, as well as a count of the number of elements in the list.
    • An accessor method getCount(), to get the number of elements in the list.
    • A method add, which takes a Recipe object parameter and adds it to the end of the linked list.
    • A method public: Recipe get(int index), which takes an integer parameter and returns the Recipe object at that index (0-based) in the list.
    • Additionally, implement these methods:
      1. public: boolean remove(Recipe r)
        • Takes a Recipe object and removes the node containing that object from the linked list, rearranging links as necessary to maintain the structure of the remaining list. Returns true if the given Recipe object was removed from the list, and false if it was not (meaning it was not found in the list).
      2. public: Recipe searchByName(String recipeName)
        • Searches for a Recipe in the book with the exact name as given. Returns the first Recipe whose name matches the parameter, or null if no such recipe is found.

Deliverables. Submit your .cpp source code file in a folder named assign6 in your csci133 folder. Make sure your compiled file name is assign6.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

// A linked list node
struct node
{
   string data;
   struct node *next;
   struct node *prev;
};

struct node *head=NULL,*tail=NULL;
int count=0 ;

void append()
{
count++;
string n;
  
cin >> n;
  
struct node* temp1=(struct node*)malloc(sizeof(struct node));
temp1->data=n;
temp1->next=NULL;
temp1->prev=NULL;
  
if(head==NULL)
{
head=temp1;
  
}
else
{
struct node* temp=head;
  
while(temp->next!=NULL)
{
temp=temp->next;
}
  
temp->next=temp1;
temp1->prev=temp;
  
  
}
  
}

void display()
{
struct node* temp=head;
  
if(head==NULL)
{
cout << "no elements in the list"<< endl;
}
else
{
while(temp!=NULL)
{
cout << temp->data;
  
temp=temp->next;
if(temp!=NULL)
{
cout << "-->";
}
}
}
cout<<endl;
}


void getindex()
{
int n,i;
cin >> n;
  
  
  
struct node* temp=head;
  
for(i=0;i<n;i++)
{
temp=temp->next;
}

cout << temp->data <<endl;
}

void getelement()
{
string s;
cin >> s;
int c=0;
struct node* temp=head;
  
while(temp->data!=s)
{
temp=temp->next;
c++;
}
  
if(temp==NULL)
{
cout << "element doesmt exist" << endl;
}
else
{
cout << "data present at " << c << " node" << endl;
}
}


void deleteelement()
{
string n;
cin >> n;
  
struct node *temp=head;
  
while(temp->data!=n)
{
temp=temp->next;
  
}
  
if(temp==NULL)
{
cout << "false : element doesmt exist" << endl;
}
else
{
  
struct node *temp1,*temp2;
  
if(temp->next!=NULL) // true if the the node to be deleted is not last node
{
temp1=temp->prev;
temp2=temp->next;
  
temp1->next=temp2;
temp2->prev=temp1;
}
else
{
temp1=temp->prev;
temp1->next=NULL;
}
cout << "true : data present and deleted successfully" << endl;
count--;
}
  
}

void countnodes()
{
cout << "no of nodes is " << count << endl;
}
int main()
{
  
  
while(1)
{
int o;
cin >> o;
  
switch (o)
{
case 1:append();break;
case 2:display();break;
case 3:countnodes();break;
case 4:getindex();break;
case 5:getelement();break;
case 6:deleteelement();break;
case 7:exit(0);
}
  
  
}
  
}

 

input

1
a
1
j
1
a
2
4
1
5
j
6
j
2
3
7

output

a-->j-->a
j
data present at 1 node
true : data present and deleted successfully
a-->a
no of nodes is 2

 
 
Add a comment
Know the answer?
Add Answer to:
Assignment Description. In this assignment, you will design a class to represent a book of recipes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input...

    Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following:  The instance variables for the class (recipeName, serving size, and...

  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • Write a class called Book. Here are the relevant attributes: title author yearPublished            bookPriceInCAD Provide...

    Write a class called Book. Here are the relevant attributes: title author yearPublished            bookPriceInCAD Provide a constructor that takes parameters to initialize all the instance variables. The constructor calls the mutator (set) methods to initialize the instance variables. Provide an accessor (get) and mutator (set) for each instance variable. The mutators all validate their parameters appropriately and use them only if valid. If the passed parameter was invalid an IllegalArgumentException will be thrown with a proper error message A...

  • Static methods can be called directly from the name of the class that contains the method....

    Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5);   System.out.println(var); } } For your final exercise, create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • For this assignment you will be creating a multi-file project in which you implement your own...

    For this assignment you will be creating a multi-file project in which you implement your own templated linked list and use it to create a simple list of composers. When doing this assignment, take small, incremental steps; trying to complete the lab in one go will make the lab more difficult. This means that any time you finish part of the lab, such as a linked list method, you should immediately test and debug it if necessary. Part 1: Creating...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT