SlideShare a Scribd company logo
//This is the main file
#include "item.h"
#include "itemList.h"
int main()
{
//create a list of items
ItemList groceryList;
//create temp variables
Item anItem;
int count = 0, newNum = 0;
char newName[MAXCHAR], findName[MAXCHAR];
//read from user and add to list
cin >> count;
cin.ignore(100, 'n');
for(int i = 0; i < count; i++)
{
cin >> newName;
cin >> newNum;
cin.ignore(100, 'n');
anItem.setItemName(newName);
anItem.setNumItems(newNum);
groceryList.InsertAtFront(anItem);
}
groceryList.showList();
cin >> findName;
if(!groceryList.findItem(findName))
{
cout << "Item not found!";
}
return 0;
}
item.h
//Item header file
//
#pragma once
#include <iostream>
#include <cstring>
using namespace std;
//constant for char arrays
const int MAXCHAR = 101;
//class definition
class Item {
private:
char *itemName;
int numberOfItems;
public:
//Constructors
Item();
Item(char *, int);
//copy constructor
Item(const Item &anItem);
//destructor
~Item();
//mutators
void setItemName(char *);
void setNumItems(int);
//accessors
void getItemName(char *);
int getNumItems();
//print item
void printItem();
//assignment op overloading
Item& operator= (const Item& anItem);
};
item.cpp
//This is the implementation file for item.h (the Item class)
#include "item.h"
//default constructor
Item::Item()
{
//allocate memory for itemName
itemName = new char[MAXCHAR];
strcpy(this->itemName, "no name");
numberOfItems = 0;
}
//constructor with parameters
Item::Item(char tempItemName[], int tempNumItems)
{
//allocate memory for itemName
itemName = new char[strlen(tempItemName) + 1];
strcpy(itemName, tempItemName);
numberOfItems = tempNumItems;
}
//Copy Constructor
Item::Item(const Item &anItem)
{
//allocate memory for itemName
itemName = new char[strlen(anItem.itemName) + 1];
strcpy(itemName, anItem.itemName);
numberOfItems = anItem.numberOfItems;
}
//destructor
Item::~Item()
{
//deallocate the memory for itemName
if(itemName)
{
delete [] itemName;
itemName = NULL;
}
}
//mutator functions
void Item::setItemName(char *newItemName)
{
if(itemName)
{
delete [] itemName;
}
itemName = new char[strlen(newItemName) + 1];
strcpy(itemName, newItemName);
}
//for numberofitems
void Item::setNumItems(int newNum)
{
numberOfItems = newNum;
}
//accessor functions
void Item::getItemName(char *returnItemName)
{
strcpy(returnItemName, itemName);
}
int Item::getNumItems()
{
return numberOfItems;
}
//print the item
void Item::printItem()
{
cout << numberOfItems << ' ' << itemName << endl;
}
//assignment operator overload
Item& Item::operator= (const Item& anItem)
{
//if it is a self copy, don't do anything
if (this == &anItem)
return *this;
//make current object *this a copy of the passed in item
else
{
this->setItemName(anItem.itemName);
this->numberOfItems = anItem.numberOfItems;
return *this;
}
}
itemList.h
//This is the itemList header file.
//Class for a list of items
#pragma once
#include "item.h"
//define class for a list of items
class ItemList
{
private:
//struct for each Node
struct Node
{
Item data;
Node *next;
};
Node *head;
Node *tail;
int size;
public:
//constructors
ItemList();
//destructor
~ItemList();
//database functions
void InsertAtFront(Item &);
bool findItem(char *);
void showList();
int getSize();
};
itemList.cpp
//The implementation file itemList.h, ItemList class
#include "itemList.h"
//constructors
ItemList::ItemList()
{
head = NULL;
tail = NULL;
size = 0;
}
//destructor
ItemList::~ItemList()
{
//delete list
Node *curr = head;
while(curr)
{
head = curr->next;
delete curr;
curr = head;
}
tail = NULL;
}
//Add an item to the list
/*Your code goes here */
//Returns the size of the list
int ItemList::getSize(){
return size;
}
//displays the list
void ItemList::showList()
{
Node *curr;
for(curr = head; curr; curr = curr->next)
{
curr->data.printItem();
}
cout << endl;
}
//finds an item with the given name
/* Your code goes here */
22.10 LAB: Inventory (Linear Linked lists: insert at the front of a list and find something) Given
main), define the following functions: - An InsertAtFront0 member function in the ItemList class
that inserts items at the front of a linked list (always add to the head pointer). - A findltem0 function
in the ItemList class that returns something Check the prototypes in itemList.h before you write the
functions. Do not change these prototypes. Do not change anything else in any other files. The
code in main0 reads input from the user and calls the InsertAtFront() function to add the items.
The first input is the number of items followed by the item name and the number of items
separated by space. Then it reads an item name from the user and finds the item in the list. Ex. If
the input is: 4 plates 100 spoons 200 cups 150 forks 200 forks the output is:

More Related Content

DOCX
-- Reminder that your file name is incredibly important- Please do not.docx
DOCX
C++ detyrat postim_slideshare
DOCX
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
PDF
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
PDF
please follow all instructions and answer the inbedded questions- and.pdf
DOCX
Please answer the 4 questions using C- The expected output is shown be.docx
PDF
Using the C++ programming language1. Implement the UnsortedList cl.pdf
PDF
Written in C- requires linked lists- Please answer the 4 questions and.pdf
-- Reminder that your file name is incredibly important- Please do not.docx
C++ detyrat postim_slideshare
In C pls -- Write your name here -- Write the compiler used- Visual st.docx
-- Write the compiler used- Visual studio or gcc -- Reminder that your.pdf
please follow all instructions and answer the inbedded questions- and.pdf
Please answer the 4 questions using C- The expected output is shown be.docx
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Written in C- requires linked lists- Please answer the 4 questions and.pdf

Similar to This is the main file include itemh include itemList.pdf (20)

PDF
Written in C- requires linked lists- Please answer the 4 questions and (1).pdf
PDF
written in c- please answer the 4 questions and write the functions ba.pdf
DOCX
Write a program to find the number of comparisons using the binary se.docx
PDF
ItemNodeh include ltiostreamgt include ltstring.pdf
PDF
Implement the ListArray ADT-Implement the following operations.pdf
DOCX
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
PDF
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
PDF
maincpp include ListItemh include ltstringgt in.pdf
DOCX
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
PPT
강의자료7
PDF
In C++Add the function min as an abstract function to the classar.pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
#include iostream #include cstring #include vector #i.pdf
PDF
maincpp Build and procees a sorted linked list of Patie.pdf
DOCX
Implementation File- -------------------------------------------------.docx
DOCX
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
PDF
#includeiostream#includecstdio#includecstdlibusing names.pdf
PDF
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
DOCX
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
Written in C- requires linked lists- Please answer the 4 questions and (1).pdf
written in c- please answer the 4 questions and write the functions ba.pdf
Write a program to find the number of comparisons using the binary se.docx
ItemNodeh include ltiostreamgt include ltstring.pdf
Implement the ListArray ADT-Implement the following operations.pdf
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
maincpp include ListItemh include ltstringgt in.pdf
RightTrianglerightTriangle.cppRightTrianglerightTriangle.cpp.docx
강의자료7
In C++Add the function min as an abstract function to the classar.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
#include iostream #include cstring #include vector #i.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
Implementation File- -------------------------------------------------.docx
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
#includeiostream#includecstdio#includecstdlibusing names.pdf
Pleae help me with this C++ question, ill upvote thanks.Write the .pdf
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
Ad

More from info334223 (20)

PDF
Ethical hacking is the use of hacking tools and techniques t.pdf
PDF
Assume Victoria Inc VI has some outstanding bonds that ma.pdf
PDF
At December 312020 Marin Corporation had a deferred tax l.pdf
PDF
Aratrmaclar E coli tarafndan tipik olarak tketilmeyen bir.pdf
PDF
51 Case Study 21 Whom Would You Hire More texttospeech.pdf
PDF
6 A medical practice group consists of nine doctors four w.pdf
PDF
2 Adding Command Now we implement the filecopy command cp .pdf
PDF
1How will you manage building this infrastructure and appli.pdf
PDF
1818 LAB Grocery shopping list linked list inserting at .pdf
PDF
Dr Foleys new type of agriculture that he described as fa.pdf
PDF
Aeropuerto Internacional de Denver FONDO Cmo se conviert.pdf
PDF
A sample of size 39 was gathered from high school seniors to.pdf
PDF
While Mary Corens was a student at the University of Tenness.pdf
PDF
what is the significance related to reproduction and the sig.pdf
PDF
When oxygen is present which pair of processes allow conver.pdf
PDF
v f alculete the leogti mad varance of the Criting Phis d.pdf
PDF
423 A survey of 1000 US government employees who have an.pdf
PDF
The following partial information is taken from the comparat.pdf
PDF
3 Lets suppose the A600 of your 1100 diluted culture is 0.pdf
PDF
Select a business or organization of your choice Choose tw.pdf
Ethical hacking is the use of hacking tools and techniques t.pdf
Assume Victoria Inc VI has some outstanding bonds that ma.pdf
At December 312020 Marin Corporation had a deferred tax l.pdf
Aratrmaclar E coli tarafndan tipik olarak tketilmeyen bir.pdf
51 Case Study 21 Whom Would You Hire More texttospeech.pdf
6 A medical practice group consists of nine doctors four w.pdf
2 Adding Command Now we implement the filecopy command cp .pdf
1How will you manage building this infrastructure and appli.pdf
1818 LAB Grocery shopping list linked list inserting at .pdf
Dr Foleys new type of agriculture that he described as fa.pdf
Aeropuerto Internacional de Denver FONDO Cmo se conviert.pdf
A sample of size 39 was gathered from high school seniors to.pdf
While Mary Corens was a student at the University of Tenness.pdf
what is the significance related to reproduction and the sig.pdf
When oxygen is present which pair of processes allow conver.pdf
v f alculete the leogti mad varance of the Criting Phis d.pdf
423 A survey of 1000 US government employees who have an.pdf
The following partial information is taken from the comparat.pdf
3 Lets suppose the A600 of your 1100 diluted culture is 0.pdf
Select a business or organization of your choice Choose tw.pdf
Ad

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharma ospi slides which help in ospi learning
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Lesson notes of climatology university.
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
master seminar digital applications in india
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Insiders guide to clinical Medicine.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharma ospi slides which help in ospi learning
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
TR - Agricultural Crops Production NC III.pdf
Lesson notes of climatology university.
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
master seminar digital applications in india
STATICS OF THE RIGID BODIES Hibbelers.pdf
Complications of Minimal Access Surgery at WLH
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Supply Chain Operations Speaking Notes -ICLT Program
Insiders guide to clinical Medicine.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

This is the main file include itemh include itemList.pdf

  • 1. //This is the main file #include "item.h" #include "itemList.h" int main() { //create a list of items ItemList groceryList; //create temp variables Item anItem; int count = 0, newNum = 0; char newName[MAXCHAR], findName[MAXCHAR]; //read from user and add to list cin >> count; cin.ignore(100, 'n'); for(int i = 0; i < count; i++) { cin >> newName; cin >> newNum; cin.ignore(100, 'n'); anItem.setItemName(newName); anItem.setNumItems(newNum); groceryList.InsertAtFront(anItem); } groceryList.showList(); cin >> findName; if(!groceryList.findItem(findName)) { cout << "Item not found!"; } return 0; } item.h //Item header file // #pragma once #include <iostream> #include <cstring> using namespace std; //constant for char arrays const int MAXCHAR = 101; //class definition class Item {
  • 2. private: char *itemName; int numberOfItems; public: //Constructors Item(); Item(char *, int); //copy constructor Item(const Item &anItem); //destructor ~Item(); //mutators void setItemName(char *); void setNumItems(int); //accessors void getItemName(char *); int getNumItems(); //print item void printItem(); //assignment op overloading Item& operator= (const Item& anItem); }; item.cpp //This is the implementation file for item.h (the Item class) #include "item.h" //default constructor Item::Item() { //allocate memory for itemName itemName = new char[MAXCHAR]; strcpy(this->itemName, "no name"); numberOfItems = 0; } //constructor with parameters Item::Item(char tempItemName[], int tempNumItems) { //allocate memory for itemName itemName = new char[strlen(tempItemName) + 1]; strcpy(itemName, tempItemName); numberOfItems = tempNumItems; } //Copy Constructor
  • 3. Item::Item(const Item &anItem) { //allocate memory for itemName itemName = new char[strlen(anItem.itemName) + 1]; strcpy(itemName, anItem.itemName); numberOfItems = anItem.numberOfItems; } //destructor Item::~Item() { //deallocate the memory for itemName if(itemName) { delete [] itemName; itemName = NULL; } } //mutator functions void Item::setItemName(char *newItemName) { if(itemName) { delete [] itemName; } itemName = new char[strlen(newItemName) + 1]; strcpy(itemName, newItemName); } //for numberofitems void Item::setNumItems(int newNum) { numberOfItems = newNum; } //accessor functions void Item::getItemName(char *returnItemName) { strcpy(returnItemName, itemName); } int Item::getNumItems() { return numberOfItems; } //print the item
  • 4. void Item::printItem() { cout << numberOfItems << ' ' << itemName << endl; } //assignment operator overload Item& Item::operator= (const Item& anItem) { //if it is a self copy, don't do anything if (this == &anItem) return *this; //make current object *this a copy of the passed in item else { this->setItemName(anItem.itemName); this->numberOfItems = anItem.numberOfItems; return *this; } } itemList.h //This is the itemList header file. //Class for a list of items #pragma once #include "item.h" //define class for a list of items class ItemList { private: //struct for each Node struct Node { Item data; Node *next; }; Node *head; Node *tail; int size; public: //constructors ItemList(); //destructor ~ItemList(); //database functions
  • 5. void InsertAtFront(Item &); bool findItem(char *); void showList(); int getSize(); }; itemList.cpp //The implementation file itemList.h, ItemList class #include "itemList.h" //constructors ItemList::ItemList() { head = NULL; tail = NULL; size = 0; } //destructor ItemList::~ItemList() { //delete list Node *curr = head; while(curr) { head = curr->next; delete curr; curr = head; } tail = NULL; } //Add an item to the list /*Your code goes here */ //Returns the size of the list int ItemList::getSize(){ return size; } //displays the list void ItemList::showList() { Node *curr; for(curr = head; curr; curr = curr->next) { curr->data.printItem(); }
  • 6. cout << endl; } //finds an item with the given name /* Your code goes here */ 22.10 LAB: Inventory (Linear Linked lists: insert at the front of a list and find something) Given main), define the following functions: - An InsertAtFront0 member function in the ItemList class that inserts items at the front of a linked list (always add to the head pointer). - A findltem0 function in the ItemList class that returns something Check the prototypes in itemList.h before you write the functions. Do not change these prototypes. Do not change anything else in any other files. The code in main0 reads input from the user and calls the InsertAtFront() function to add the items. The first input is the number of items followed by the item name and the number of items separated by space. Then it reads an item name from the user and finds the item in the list. Ex. If the input is: 4 plates 100 spoons 200 cups 150 forks 200 forks the output is: