A vegetable shop owner maintains the data in linked list. Write a menu-driven program that maintains
a singly linked list. Each vegetables node must contain the features: unique id, date of inclusion in
stock, vegetable name, shape, raw eating/cooking feature, weight, price per kilogram. Execute all the
following operations and attach screenshots of outputs along with other data/code as written (code
screenshot will not be allowed) in instructions in the submission pdf file.
1. Inclusion of 2 different type of vegetables.
2. Display data of all vegetables in stock nodes.
3. Delete a node based on some ’key’ searching criteria such as stock inclusion date is 10 days ago
or if a customer buys a vegetable and weight of that vegetable in stock has reached to zero.
4. Inclusion of two new vegetables (same or different).
5. Display data of all vegetables in stock.
#include <iostream>
#include <string>
using namespace std;
class NodeVegetable
{
public:
// unique id, date of inclusion in
//stock, vegetable name, shape, raw eating/cooking feature, weight, price per kilogram.
int uniqueId;
int dayInclusion;
string name;
string shape;
string rawEatingCookingFeature;
float weight;
float price;
NodeVegetable* next;
NodeVegetable(){
this->next = NULL;
}
};
class LinkList
{
public:
NodeVegetable* head;
LinkList(){
this->head = NULL;
}
LinkList(NodeVegetable* head)
{
this->head = head;
}
~LinkList(){
delete head;
}
void displayAllVegetablesInStock(){
cout<<"All vegetables in stock:\n";
NodeVegetable* tempNode = this->head;
while (tempNode != NULL)
{
cout<<"Unique Id: "<<tempNode->uniqueId<<"\n";
cout<<"Day Inclusion: "<<tempNode->dayInclusion<<"\n";
cout<<"Name: "<<tempNode->name<<"\n";
cout<<"Shape: "<<tempNode->shape<<"\n";
cout<<"Raw eating/cooking feature: "<<tempNode->rawEatingCookingFeature<<"\n";
cout<<"Weight: "<<tempNode->weight<<"\n";
cout<<"Price: "<<tempNode->price<<"\n\n";
tempNode = tempNode->next;
}
}
int searchVegetable(int uniqueId){
NodeVegetable* tempNode = head;
int numberNodes = 0;
while (tempNode != NULL){
if(tempNode->uniqueId==uniqueId){
return numberNodes;
}
numberNodes++;
tempNode = tempNode->next;
}
return -1;
}
bool addVegetable(int uniqueId,int dayInclusion,string name,string shape,string rawEatingCookingFeature,float weight,float price){
NodeVegetable* newNode = new NodeVegetable();
newNode->uniqueId = uniqueId;
newNode->dayInclusion = dayInclusion;
newNode->name = name;
newNode->shape = shape;
newNode->rawEatingCookingFeature = rawEatingCookingFeature;
newNode->weight = weight;
newNode->price = price;
if (this->head == NULL)
{
this->head = newNode;
}
else
{
NodeVegetable* tempNode = this->head;
while (tempNode->next != NULL)
{
tempNode = tempNode->next;
}
tempNode->next = newNode;
}
return true;
}
int Length(){
NodeVegetable* tempNode = head;
int numberNodes = 0;
while (tempNode != NULL)
{
numberNodes++;
tempNode = tempNode->next;
}
return numberNodes;
}
int deleteVegetable(int uniqueId){
if (this->head->uniqueId==uniqueId)
{
head = head->next;
}
else
{
NodeVegetable* tempNode = head;
NodeVegetable* tempPrevious = head;
bool isFound = false;
while (!(isFound = tempNode->uniqueId==uniqueId) && tempNode->next != NULL)
{
tempPrevious = tempNode;
tempNode = tempNode->next;
}
if (isFound)
{
tempPrevious->next = tempNode->next;
}
else
{
cout<<"Vegetable not found!\n";
}
}
return 0;
}
};
int main(){
LinkList* linkList=new LinkList();
linkList->addVegetable(456,12,"name 1","Rectangle","rawEatingCookingFeature",45,30);
linkList->addVegetable(212,10,"name 2","Cube","rawEatingCookingFeature",15,45);
linkList->displayAllVegetablesInStock();
if(linkList->searchVegetable(212)!=-1){
cout<<"The Vegetable exists\n\n";
}
linkList->deleteVegetable(212);
linkList->displayAllVegetablesInStock();
int l;
cin>>l;
delete linkList;
return 0;
}
Comments
Leave a comment