C++: I need implement this code using Double Linked List using the cosiderations
1. head point to null in an empty list
2. There is not need of a tail pointer
/*This class implements the singly linked list using
templates
Each list has two attributes:
-head: first node in the list
-tail: last node in the list
#include "circDLLNode.h"
template
class {
public:
//Default constructor: creates an empty list
();
//Destructor: deallocate memory
~();
//addToHead(T val): creates a new node with val as
info,
//and this new node is the new head
void addToHead(T val);
//addToTail(T val): creates a new node with val as
info,
//and this new node is the new tail
void addToTail(T val);
//deleteFromHead: remove head from the list,
//the new head is the previous head->next
//if the list had only one node, head and tail point
null
void deleteFromHead();
//deleteFromTail: remove tail from the list,
//the new tail is the previous node to tail
//if the list had only one node, head and tail point
null
void deleteFromTail();
//In the list is empty, returns true
//otherwise, returns false
bool isEmpty();
//sortInsert(T val): creates a new node, and it is
inserted sortly
void sortInsert(T val);
//insert(int pos, T val): creates a new node, and
it is inserted in position pos
void insert(int pos, T val);
//printList(): prints all nodes in the list from
head to tail
void printList();
//printList(): prints all nodes in the list from
tail to head
void printRevList();
private:
circDLLNode *head; //A pointer to the first node
};
/****************
Default constructor: creates an empty list
head and tail point null
*****************/
template
::()
{
head = 0;
}
/***********************
Destructor: deallocate memory removing each node from
the list
*****************/
template
::~()
{
while(!isEmpty())
this->deleteFromHead();
}
/***********************
Implement other methods
***********************/
template
void :: addToHead(T val)
{
circDLLNode *node = new circDLLNode(val);
node->setNext(head);
if(head == NULL)
tail = node;
head = node;
}
template
void circDLList:: addToTail(T val)
{
circDLLNode *node = new circDLLNode(val);
node->setNext(NULL);
if(head == NULL)
{
head = node;
}else
{
tail->setNext(node);
}
tail = node;
}
template
void circDLList:: deleteFromHead()
{
if(!isEmpty())
{
circDLLNode *node =
head;
head = head->next;
delete(node);
if(head == NULL)
tail =
NULL;
}
}
template
void circDLList:: deleteFromTail()
{
if(!isEmpty())
{
circDLLNode *curr =
head;
circDLLNode *prev =
NULL;
while(curr->next !=
NULL)
{
prev =
curr;
curr =
curr->next;
}
if(prev != NULL)
prev->next = NULL;
else
head =
NULL;
tail = prev;
delete(curr);
}
}
template
bool circDLList:: isEmpty()
{
return(head == NULL);
}
template
void circDLList:: sortInsert(T val)
{
circDLLNode *node = new circDLLNode(val);
if(head == NULL)
head = node;
else
{
circDLLNode *curr =
head;
circDLLNode *prev = NULL;
while(curr != NULL)
{
if(curr->getVal() > val)
{
if(prev == NULL)
{
node->setNext(head);
head = node;
}else
{
node->setNext(curr);
prev->setNext(node);
}
return ;
}
prev = curr;
curr =
curr->getNext();
}
tail->setNext(node);
tail = node;
}
}
template
void circDLList:: insert(int pos, T val)
{
if((pos == 0) || (head == NULL ))
addToHead(val);
else
{
circDLLNode *node = new
circDLLNode(val);
int idx = 0;
circDLLNode *curr =
head;
circDLLNode *prev = NULL;
while((curr != NULL)
&& (idx < pos))
{
prev =
curr;
curr =
curr->getNext();
idx++;
}
if(idx == pos)
{
prev->setNext(node);
node->setNext(curr);
if(node->getNext() == NULL)
tail = node;
}else
{
addToTail(val);
}
}
}
template
void circDLList:: printList()
{
if(head == NULL)
cout<<"Empty list"<
else
{
circDLLNode *curr = head;
while(curr != NULL)
{
cout<getVal()<
curr =
curr->getNext();
}
}
cout< }
#endif
struct Node
{
int value;
struct Node *next;
struct Node *prev;
};
void insertBegin(struct Node** start, int value)
{
struct Node *last =
(*start)->prev;
struct Node* nnode = new Node;
nnode->value =
value;
nnode->next = *start;
nnode->prev = last;
last->next = (*start)->prev =
nnode;
*start = nnod
}
void insertEnd(struct Node** start, int value)
{
if(*start == NULL) //if it is empty
list
{
struct Node* nnode
=new Node;
nnode->value =
value;
nnode->next =
nnode->prev = nnode;
*start =
nnode;
return;
}
//if list is not empty
Node *last = (*start)->prev;
struct Node* nnode = new
Node;
nnode->value = value;
nnode->next = *start;
(*start)->prev = nnode;
nnode->prev = last;
last->next = new_node;
}
void insertbetween(struct Node** start, int value1,
int
value2)
{
struct Node* nnode = new Node;
nnode->value = value1;
struct Node *temp = *start;
while(temp->value != value2)
temp =
temp->next;
struct Node *next = temp->next;
temp->next = nnode;
nnode->prev = temp;
nnode->next = next;
next->prev = nnode;
}
void display(struct Node* start)
{
struct Node *temp = start;
while(temp->next != start)
{
cout<<temp->value;
temp =
temp->next;
}
C++: I need implement this code using Double Linked List using the cosiderations 1. head point...
In C++, Modify the following code of a single linked list, so insted of doing sort insert in descending order, to do it in ascending order: template <class T> void IntSLList<T>::sortInsert(T val) { //if empty the list if (head == 0) { //add as node to head with data as val addToHead(val); } //if not empty else { //create to pointers to the list IntSLLNode<T>...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...
This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...
(1) Implement the countKey(T element) method, which should return a count of the number of times that the given key (the element) is found in the list. (2) Implement the indexOf(T element) method, which is similar as the indexOf method in the String class. It returns the index (the position starting from the head node) of the first occurrence of the given element, or -1, if the element does not occur in the list. You will need to track the...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...