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
C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...
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 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 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 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 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(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 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 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; 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 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...