Question

C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

C++

Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1, 4, 7 } (enclosing braces and comma + space between values). Finally, you'll implement insertAtPosition and removeAtPosition, each of which takes two parameters: the first of type T and the second of type int and inserts or removes a value equal to the first parameter at the position equal to the second parameter. Legal values for the second parameter are from 1 to the list length + 1 for insert and from 1 to the list length for the remove. Out of range values have no effect. BTW positions start with 1 unlike array indexes which start with 0.

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED
#include<iostream>
#include<stdlib.h>
#include<stdexcept>

using namespace std;
template <class T>
struct Node
{
T data;
Node<T> *Next, *Previous;
};

template <class T>
class Linkedlist
{
private:
Node<T> *First = NULL;
Node<T> *Last = NULL;
Node<T> *Middle = NULL;
Node<T> *TempPrevious;
Node<T> *TempNext;
Node<T> *P;
int length;

public:
Linkedlist()
{
First = NULL;
Last = NULL;
length = 0;
}

void insertfirst(const T & data)
T getfirst()const;
T getlast()const;
void setfirst(const T &d);
void setlast(const T &d);
void insertlast(const T & d);
void removefirst(); //no parameters
void removelast();
int getlength(const);
bool isEmpty()const;
void clear();
void insertatposition( const T &d, int position);

};

#endif // LINKEDLIST_H_INCLUDED


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

include L cS-todeo ce Std), US?ng name l Node Cveated Stvuct node frt info Stvuct node next /I To sfove Previous Pointer Stvuclssdable Raked (ist PubRC ll Heod and ner oint start and 6d axt len II cveates a tnted st Vord Add To Font lI-Adds a nade atVoxd Remove Values CAnt) SP Vad Pint (), Il Dplay numbenode available Constu cor Double inked stC) Con Stvuctoir ал nt choce,l creates a tempora y rode eno tA // endし Cost L LL endon choc ce appropriate I : 녀 -Re Eleme忒.. cin elemert. dl· Cvate - lest (element) , break Case 2 temp = new (ND) Cout endl brCase , Note 9 to delete 〈〈 Erd câh >> elemet dk. Rmoe Celement) Yeak Cade 5: Cout Cout ze * Ekr tle valux t delete: cin S elYea Co.se 6 Covt 4L end reak Case .. emenb ave byeak CoSe 8 dedautt Ye To vemave a Hven Value drom V&A Doble lînked 迁:: Removcreats terpway nodea ND *- head tt Release Memo、 mf Ye tonIl ?f ke value to be deleted in beeen - next - > next -> next -> Pre - Cost从Element Deleted LLfrd! I/ Release memo u 녀 reto /ll Releare me ind Vod dasble linked [st:. ceate - st at valve lcreates tempovoy nodets new ( l Assqn value 5$ - Value temp nead tem /IS po-t»^ to head Moved S S-next S- > nextind on int daļa I s head null no node 3 ovailable CheodNotl) vetun ind IU Accepls data gi > data It Zhzert node temp -> Prevlemp odDeuble aked st : Add To 8ack Cuotmp Rt data Accets daia nodenext-term net N ぷ-Kon Il ceake -lempovaw node ll fost idex deleted lemp head head headexCoutくく.ement deleted. <<fnd l II Release memo End -Keod befween move U 6mSP 9 head loops ku endpS /I (oops tu ed nect Creare the one Count<i +Sample DoublylenKed ust I. Cveale ode 2. Add at e gin? 3 Add at nd 5. Remove Value SP 7 Sye 6nkv you Chce ertan Re Elemext o

Add a comment
Know the answer?
Add Answer to:
C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...
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
  • Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has...

    Suppose we implement a doubly linked list class template LinkedList with template type T. LinkedList has fields Node *headPtr, Node *tailPtr and int length, where the struct type Node has fields prev and next of type Node* along with data of type T. The prev and next pointers of each Node points to the previous and next Nodes in the list (or are respectively null in the case of the list’s head or tail node). We wish to detect "invalid"...

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

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below 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...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • C++ I have failed to call the insert and display. I am recycling code but cannot...

    C++ I have failed to call the insert and display. I am recycling code but cannot figure out how to insert a node at the beginning and display it. Please help #ifndef LINKEDLIST_H_INCLUDED #define LINKEDLIST_H_INCLUDED #include<iostream> #include<stdlib.h> #include<stdexcept> using namespace std; template <class T> struct Node { T data; Node<T> *Next, *Previous; }; template <class T> class Linkedlist { private: Node<T> *First; Node<T> *Last; Node<T> *TempPrevious; Node<T> *TempNext; Node<T> *P; int length; public: Linkedlist() { First = NULL; Last =...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...

    if we have following List classes: public class LinkedList<T> { class ListNode { protected T value; protected ListNode next; public ListNode(T val, ListNode nxt) { value = val; next = nxt; } public ListNode(T val) { this(val, null); } public ListNode() { this(null, null); } } can you write the folowing methods in java: 1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

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