Question

Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...

Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you wish. Your list must have functions to both insert and remove (and return) values from both the front and end of the list. Use of a "standard container" (such as an STL class, Vector, or ArrayList) is not permitted.

Using composition (with the List class,) define a Queue class that has enque and deque functions, as well as an isEmpty function.

(Each of your classes may have any additional functionality that you deem necessary)

Write a main function that will read an unspecified number (until EOF) of input strings (each string may include whitespace and is terminated with a newline) from STDIN. Enque each string into your Queue. After you have read all of the input, deque each string from your Queue, printing it to STDOUT (one per line) until the Queue us empty. Your program should have no other output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, here is the code. Try it and let me know if you need any changes.

Source & Screens: List.h & List.cpp

#ifndef LIST_H_
#define LIST_H_

#include <string>

using namespace std;

class List {
private:
   class LinkNode
   {
   public:
       string data;
       LinkNode * prev;
       LinkNode * next;

   };

public:
   LinkNode * head;
   LinkNode * tail;
   List();
   void insertFirst(string);
   void insertLast(string);
   string removeFirst();
   string removeLast();


};

#endif /* LIST_H_ */

/*
* List.cpp
*
* Created on: Feb 5, 2020
* Author: Casper
*/

#include "List.h"

List::List() {
   this->head = nullptr;
   this->tail = nullptr;
}

void List::insertFirst(string data) {
   // create a new LinkNode
   LinkNode * newNode = new LinkNode();
   // set data member
   newNode->data = data;
   // set head node as right
   newNode->next = head;
   // if head is not null
   if(head != nullptr){
       // set previous of head as newNode
       head->prev = newNode;
   }
   if(tail == nullptr)
   {
       tail = newNode;
   }
   // set newNode as head
   head = newNode;
}

void List::insertLast(string data) {
   // create a new LinkNode
   LinkNode * newNode = new LinkNode();
   // set data member
   newNode->data = data;
   // set head node as right
   newNode->prev = tail;
   // if tail is not null
   if(tail != nullptr){
       // set next of tail as newNode
       tail->next = newNode;
   }
   if(head == nullptr)
   {
       head = newNode;
   }
   // set newNode as head
   tail = newNode;

}

string List::removeFirst() {
   // assign head node to tempNode
   LinkNode * tempNode = head;
   head = head->next;
   if(head != nullptr)
   {
       // set previous of head to null
       head->prev = nullptr;
   }
   else
   {
       tail = nullptr;
   }
   // retrun data
   return tempNode->data;
}

string List::removeLast() {
   // assign head node to tempNode
   LinkNode * tempNode = tail;
   tail = tail->prev;
   if(tail!=nullptr)
   {
       // set previous of head to null
       tail->next = nullptr;
   }
   else
   {
       head = nullptr;
   }
   // retrun data
   return tempNode->data;

}

Source & Screens: Queue.H & Queue.cpp

#ifndef QUEUE_H_
#define QUEUE_H_
#include "List.h"

class Queue {
   List list;
public:
   Queue();
   void enque(string);
   string deque();
   bool isEmpty();
};

#endif /* QUEUE_H_ */

#include "Queue.h"

Queue::Queue() {
}

void Queue::enque(string data) {
   list.insertLast(data);
}

string Queue::deque() {
   return list.removeFirst();
}

bool Queue::isEmpty() {
   return list.head == nullptr;
}

Source & Screens: TestQueue.cpp

#include "Queue.h"

Queue::Queue() {
}

void Queue::enque(string data) {
   list.insertLast(data);
}

string Queue::deque() {
   return list.removeFirst();
}

bool Queue::isEmpty() {
   return list.head == nullptr;
}

Add a comment
Know the answer?
Add Answer to:
Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Using a doubly linked list as the underlying data structure, implement a list ADT that implements...

    Using a doubly linked list as the underlying data structure, implement a list ADT that implements the ListInterface.java found in the ProgProjTwo Eclipse project starting point for this assignment. In addition to the forward iterator defined by resetIterator( ) and getNextItem( ) in ListInterface.java, implement a backwards iterator by providing resetBackIterator( ) and getPreviousItem( ) methods. As noted in the syllabus addendum, you are encouraged to develop a find( ) helper method that can support various list ADT operations. A...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT