Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++ Lab Exe Linked Lists In this lab, you will be introduced to what is neces

ID: 3711630 • Letter: I

Question

IN C++

Lab Exe Linked Lists In this lab, you will be introduced to what is necessary to implement linked lists. We will take a program using linked lists and examine how it works, and implement a small piece of functionality In the lab we will be working with a collector. A collector maintains a list of distinct items and their cardinality (how many times they appear in the collection). You can prompt the collector to grab a thing, and if the collector already has that item, the cardinality of that item increases by one. If the collector does not have that item, it adds it to the collection and sets its cardinality to one. You can also prompt the collector to leave the items, after which the collection is empty This lab also introduces you to working with linked lists. A linked list is a series of objects, each of which points to the next object. For this lab, each object is called a CollectorNade, and has three data members: an enumeration saying what sort of thing is being collected, an integer representing the count (cardinality) of the item type, and a refe rence to the next node. For example Coin This cell means that we've collected three coins. The pointer to the next cell is a null pointer, as it is not linked. If we link it with two other cells, we have a non-trivial linked list pre curr Coin ring stone head 0 The head pointer points to the first node and is where we enter the list. The picture also shows the possible position of two local CollectorNode variables, pre and cur.

Explanation / Answer

#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
struct node
{
char item[10];
int cardinality;
struct node *next;
};
struct node *insert_new(struct node *head_,char *item_)
{
struct node *new1=(struct node *)malloc(sizeof(struct node));
strcpy(new1->item,item_);
new1->cardinality=1;
new1->next=head_;
head_=new1;
return head_;
}
struct node *search_item(struct node *head_,char *item_)
{
struct node *traverse=head_;
while(traverse!=NULL)
{
  if((!strcmp(traverse->item,item_)))
  {
   (traverse->cardinality)++;
   return traverse;
  }
  traverse=traverse->next;
}
return NULL;
}
struct node *remove(struct node *head_)
{
struct node *temp=head_;
while(head_!=NULL)
{
  head_=head_->next;
  free(temp);
  temp=head_;
}
return NULL;
}
int display(struct node *head_)
{
struct node *traverse=head_;
int count=0;
while(traverse!=NULL)
{
  count++;
  cout<<"ITEM:"<<count<<"--";
  cout<<traverse->item<<" ";
  cout<<traverse->cardinality<<endl;
  traverse=traverse->next;
}
return count;
}
int main()
{
struct node *head=NULL;
char item_[10];
int ch=-1;
while(ch!=4)
{
  cout<<" 1) Search and replace.";
  cout<<" 2) Remove";
  cout<<" 3) Dispaly";
  cout<<" 4) Exit";
  cout<<"    Enter your choice";
  cin>>ch;
  switch(ch)
  {
   case 1:
    cout<<" Enter item:";
    cin>>item_;
    if(search_item(head,item_))
    {
     cout<<" Find And Incremented Successful ";
    }
    else
    {
     cout<<" New Entry Saved ";
     head=insert_new(head,item_);
    }
    break;
   case 2:
    head=remove(head);
    break;
   case 3:
    if(!display(head))
    {
     cout<<" LIST IS EMPTY ";
    }
    break;
   default :
    if(ch!=4)
    {
     cout<<" invalid Choice...!! ";
    }
    break;
  }
}
return 1;
}