In: Computer Science
*In C++ Please! This problem uses the concept of linked list
What is a C++ structure? Store product-id, product-name, and price per unit in a C++ structure. Use a C++ class with a member variable of that structure and provide read and write member functions for the product details.
Suppose the product names are stored in alphabetical order, Write the C++ insert function to insert a new product ‘tooth brush’ in that linked list.
Suppose the product names are stored in alphabetical order, Write the C++ delete function to remove product ‘tooth brush’ in that linked list.
Suppose the product names are stored in alphabetical order, Write the C++ count function to display the number of products in that linked list
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node{ string name;
node *next;
};
node *A = NULL;
void addnode(string newname){ node *add, *last, *current;
add = new node;
add->name = newname;
if (A == NULL){
add->next = A;
A = add;
}
else{
current = A;
last = A;
while (current && current->name < newname)
{
last = current;
current = current->next;
}
if (current == A){
/* Insert before 1st node */ add->next = A;
A = add;
}
else{
/* Insert between last and current or at the end of the list */
last->next = add;
add->next = current;
}
}
}
void deleteName(string name) {
node *curr;
node *nextNode;
curr = A;
nextNode = curr;
while(curr){
if(curr -> next -> name == name){
nextNode = curr -> next;
curr -> next = nextNode -> next;
}
}
}
void display() {
node *curr;
curr = A;
while(curr){ if(A == NULL){
break;
}
cout << A->name << endl;
A = A->next;
}
}
int main(){ int input, count;
count = 0;
ifstream dataFile;
dataFile.open("Data.txt");
string item;
string name;
while(dataFile)
{
dataFile >> item;
addnode(item);
count++;
}
cout << "1. Display the linked list\n";
cout << "2. Display the length of the list\n";
cout << "3. Delete name from the list\n";
cout << "4. display the length of a section of the list\n";
cout << "5. Print out section of list\n";
cin >> input;
switch (input) { case 1: display();
break;
case 2: cout << "There are " << count - 1 << " names in the list\n";
break;
case 3: cout << "Type in the name that you want to be deleted: ";
cin >> name;
deleteName(name);
display();
break;
case 4: break;
case 5: break;
}
system("PAUSE");
return 0; }
alphabetic order
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct nodeType
{
string info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first, nodeType*& last);
int main()
{
nodeType *first, *last;
string words;
ifstream inData;
ofstream outData;
createList(first,last);
printList(first,last);
system("PAUSE");
return 0;
}
void createList(nodeType*& first, nodeType*& last)
{
ifstream inData("input.txt");
string words;
int emptyList;
nodeType *newNode;
first = NULL;
last = NULL;
while(inData >> words)
{
newNode = new nodeType; // create new node
newNode->info = words;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
}
void printList(nodeType*& first,nodeType*& last) // print words in alphabetical order
{
ofstream outData("output.txt");
nodeType *current=first;
nodeType *alphab = NULL;
first=current->link;
current->link = alphab;
last = current;
alphab= last;
while (current != NULL)
{
outData << current->info <<endl;
current = current->link;
}
}