Question

In: Computer Science

*In C++ Please! This problem uses the concept of linked list What is a C++ structure?...

*In C++ Please! This problem uses the concept of linked list

  1. 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.

  2. 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.

  3. Suppose the product names are stored in alphabetical order, Write the C++ delete function to remove product ‘tooth brush’ in that linked list.

  4. Suppose the product names are stored in alphabetical order, Write the C++ count function to display the number of products in that linked list

Solutions

Expert Solution

#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;

}

}


Related Solutions

Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h>...
Please use C++, linked list and Bubble Sort to slove this problem. #include <iostream> #include <time.h> using namespace std; struct ListNode { int data; ListNode *next; ListNode(int x) : data(x), next(nullptr) {} }; class LinkedList { private: ListNode *head = nullptr; public: void addNode(int x) { ListNode *p = new ListNode(x); if (head == nullptr) head = p; else { ListNode *q = head; while (q->next != nullptr) q = q->next; q->next = p; } } void display() { ListNode...
In C++ please: In this lab we will creating two linked list classes: one that is...
In C++ please: In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list....
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT