Question

In: Computer Science

C++ ONLY -- LAB ASSIGNMENT DIFFICULT We have to turn in two files - List.h and...

C++ ONLY -- LAB ASSIGNMENT DIFFICULT

We have to turn in two files - List.h and Lab5.cpp

What should the code for those two files look like?

INSTRUCTIONS AS FOLLOWS:

What Should This Program Do?

Linked List Class

  • Design your own linked list class (List.h) to hold a series of strings.
  • The linked list node should be implemented as a struct.
  • The class should have member functions for appending, inserting, and deleting nodes.
  • You should also have a display function that will traverse the list & display each node’s value.
  • Don’t forget to add a destructor that destroys the list.

DRIVER – Lab5.cpp

Write a driver program (Lab5.cpp) that will do the following:

  1. Create a linked list object
  2. Call the linked list’s append function to append the following strings to your linked list. Afterwards, print to the screen to tell the user that you are inserting several strings to the list.
    1. “boogeyman”
    2. “ghost”
    3. “scarecrow”
    4. “witch”
    5. “zombie”
  3. Now call the linked list’s display function to print the list.
  4. Now call the linked list’s insert function to insert the “vampire” string in the correct sorted position. Print to the screen to tell the user that you are inserting “vampire” in to the list.
  5. Now call the linked list’s display function again to print the list.
  6. Now call the delete function to delete “ghost” from the list. Print to the screen to tell the user that you are deleting “ghost” from the list.
  7. Last, call the linked list’s display function again to print the list.

Solutions

Expert Solution

PROGRAM :

List.h

#ifndef LIST_H
#define LIST_H
#include<string>
#include<iostream>
using namespace std;
struct Node
{
string data;
Node *next;
};

class List
{
public:
List();
void appending(string);
void inserting(string);
void deleting(string);
void print();
private:
Node * head;
Node * tail;
};
#endif

List.cpp implementation file

#include"List.h"

List::List()
{
head = NULL;
tail = NULL;
cout << "The linked list has been created." << endl;
}

void List::appending(string data)
{
Node *newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
{
head = tail=newNode;
return;
}
tail->next = newNode;
tail = newNode;
}
void List::inserting(string data)
{
Node *newNode = new Node();
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
{
head = tail = newNode;
return;
}
if (head->data > data)
{
newNode->next = head;
head = newNode;
}
if (tail->data < data)
{
tail->next = newNode;
tail = newNode;
}
Node *curr = head;
Node *prev = head;
while (curr != tail)
{
if (curr->data > data)
break;
prev = curr;
curr = curr->next;
}
prev->next = newNode;
newNode->next = curr;
}
void List::print()
{
Node *temp=head;
while (temp)
{
cout << temp->data << endl;
temp = temp->next;
}
}
void List::deleting(string data)
{
if (head == NULL)
{
return;
}
if (head->data == data)
{
Node *temp = head;
head = head->next;
delete temp;
}

Node *curr = head;
Node *prev = head;
while (curr != tail)
{
if (curr->data == data)
break;
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
}

Lab5.cpp

#include"List.h"

int main()
{
List lst;
cout << "I am appending several strings to the list." << endl;
lst.appending("boogeyman");
lst.appending("ghost");
lst.appending("scarecrow");
lst.appending("witch");
lst.appending("zombie");
lst.print();
cout << "I am inserting vampire in the list." << endl;
lst.inserting("vampire");
lst.print();
cout << "I am deleting ghost from the list." << endl;
lst.deleting("ghost");
lst.print();
system("pause");
}

OUTPUT :


Related Solutions

UNIX ONLY -- DIFFICULT LAB ASSIGNMENT This is an extremely difficult subject for me. Could you...
UNIX ONLY -- DIFFICULT LAB ASSIGNMENT This is an extremely difficult subject for me. Could you please give step by step instructions on how to write a script called "encrypt_password.sh" and say line by line what the code should be to complete the assignment according to the instructions and examples below? The assignment will be done entirely in Terminal on Mac Our Lab assignment is as follows: In this lab, you will write a script called encrypt_password.sh to encrypt passwords....
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
NOTE: This assignment is for the design only Nothing you turn in should look like C/C++...
NOTE: This assignment is for the design only Nothing you turn in should look like C/C++ code For this assignment, we are going to design a system to Manage loans from the local public library For this, we will need the following entities, plus collections for each of the entities: Patrons, Books, and Loans The data for a Book will contain at least the following: Author Title ISBN Number Library ID number Cost Current Status (In, Out, Repair, Lost) You...
C++ ONLY -- PRACTICE ASSIGNMENT We are given a code (Driver.cpp) and asked to create a...
C++ ONLY -- PRACTICE ASSIGNMENT We are given a code (Driver.cpp) and asked to create a file (StringStack.h) and turn it in. What should the code for StringStack.h look like based on the instructions below? Download Driver.cpp Create a class named StringStack in a file named StringStack.h. Create a ListNode structure as a private member of the class. The node should be able to hold a string called value.   Create a top pointer as private attributes of the class. (this...
In C++ In this lab we will creating two linked list classes: one that is a...
In C++ 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. IsEmpty...
C++...This program is broken down into phases for your convenience only. Please turn in only the...
C++...This program is broken down into phases for your convenience only. Please turn in only the final phase. Before turning in your program, please make sure that it does something reasonable if the user enters a negative number the first time. Phase I: Write a program for a theater that will keep track of how many people in each of 5 age categories attended a particular movie. Use the 5 age categories listed below in the sample screen output. The...
C++ Chapter 4/5 Lab Assignment Using concepts from chapters 1 – 5 only. Grading will be...
C++ Chapter 4/5 Lab Assignment Using concepts from chapters 1 – 5 only. Grading will be based on chapter 4 concepts more. Design a menu driven program that can keep track of five player’s scores. Your program must have the following documentation: A. Your name B. The program name C. Program Description D. The date the exe file was created E. The code: a. Use a menu approach do the following: i. to Add a player information ii. to Search...
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....
This lab assignment will correspond to Elasticities and Secondary Axis Graphs. There are two lab exercises...
This lab assignment will correspond to Elasticities and Secondary Axis Graphs. There are two lab exercises listed below. Please complete this assignment in one Excel workbook in separate sheets for each exercise. You may work with one other individual (no more than two people per team) or by yourself. In Blackboard, open Lab3_Workbook. This will become the Lab Set that you turn in. Exercise 1. Taco Del Mar has completed a study of weekly demand for its tacos in Washington...
CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This...
CSI 1440 Lab 6 “Class Templates” Templates Templates in C++ is not a difficult idea. This lab is not intended to help you understand the details of how templates work. It is only intented to give students an opportunity to start developing code using templated classes. Everyone in the class has had to come to grips with the usage of variables in general. With templates, you can think of the type of the variable being a variable itself. The programmer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT