Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SAMPLE LINKED LIST CLASS /******************************* * a simple LinkedList class * *******************************/ #include #include LinkedList.h using namespace std; /* * Initializes the list to empty

SAMPLE LINKED LIST CLASS

/******************************* * a simple LinkedList class * *******************************/

#include #include "LinkedList.h"

using namespace std;

/* * Initializes the list to empty creating a dummy header node. */ LinkedList::LinkedList() { first = new Node; first->next = NULL; }

/* * Destructor. Deallocates all the nodes of the linked list, * including the header node. */ LinkedList::~LinkedList() { Node *temp;

while (first != NULL) { temp=first; first=first->next; delete temp; } }

/* * Determines whether the list is empty. * * Returns true if the list is empty, false otherwise. */ bool LinkedList::isEmpty() { return first->next == NULL; }

/* * Prints the list elements. */ void LinkedList::display() { Node * current = first->next;

while(current != NULL) { cout << current->info << " "; current = current->next; }

cout << endl; }

/* * Adds the element x to the beginning of the list. * * x: element to be added to the list. */ void LinkedList::add(int x) { Node *p = new Node;

p->info = x; p->next = first->next;

first->next = p; }

/* * Removes the first occurrence of x from the list. If x is not found, * the list remains unchanged. * * x: element to be removed from the list. */ void LinkedList::remove(int x) { Node * old = first->next, * p = first;

//Finding the address of the node before the one to be deleted bool found = false; while (old != NULL && !found) { if (old->info == x) found = true; else { p = old; old = p->next; } } //if x is in the list, remove it. if (found) { p->next = old->next; delete old; } }

USING THE LIST CONTAINER

/******************************* * using the list container * *******************************/

#include #include

using namespace std;

int main() { list numbers;

for (int i=0; i<10; i++) numbers.push_back(rand()%100);

while(!numbers.empty()) { int x = numbers.front(); cout << x << " "; numbers.pop_front(); }

cout << endl;

return 0; }

QUESTIONS

PART 1

Modify the class Linked List in Exercise 1 to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards.

void addEnd(int x): create this method to add x to the end of the list.

void displayInReverse(): create this method to display the list elements from the last item to the first one.

Create a main() function to test your DoublyLinkedList class.

PART 2

Create a class Bag that uses a linked list to store the bag items. The item type must be char. The class should have the methods listed below. Create a main() that will store in a bag object a fixed number of characters entered by the program user. After the input is completed, the program should modify the bag content so that it does not contain any duplicate characters, if duplicates were entered. For example, if the user entered 'M' 'I' 'S' 'S' 'I' 'S' 'S' 'I' 'P' 'P' 'I', the characters remaining in the bag after the removal of duplicates would be 'M' 'I' 'S' 'P'.

  1. Bag(): default constructor
  2. ~Bag(): class destructor
  3. bool isEmpty(): determines whether the bag is empty
  4. void print(): prints the bag elements
  5. int getSize(): returns the number of items in the bag
  6. void clear(): removes all of the items from the bag
  7. void add(char item): adds an item to the bag
  8. void remove(char item): removes an item from the bag; only one occurrence of the item should be removed.
  9. int count(char item): counts the number of occurrences of an item in the bag.

(Note that you can reuse the code in Exercise 1 for the LinkedList class to create your Bag class. It will help you to save development time.)

PART 3

Write a program that fills a STL list object with 10 random integers, each in the interval [0, 20], and prints how many times each integer in the interval [0, 20] appears in the list.

C++

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions