


I need this in C++. This is all
one question
// C++ program to create and implement LinkedList class
#include <iostream>
#include <fstream>
using namespace std;
// structure to represent Node of the LinkedList
struct Node {
int number;
Node * nextNode;
Node * prevNode;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList();
Node * addNodeToEnd (int value);
Node * addNodeToBeginning (int value);
void printListForward ();
void printListBackward ();
void sort ();
int & operator [ ] (const int &);
~LinkedList();
};
// default constructor to create an empty list
LinkedList::LinkedList()
{
// create new node for head and tail
head = new Node;
tail = new Node;
head->prevNode= nullptr; // set prev of head to
null
head->nextNode = tail; // set next of head to
tail
tail->prevNode = head; // set prev if tail to
head
tail->nextNode = nullptr; // ste next of tail to
null
}
// function to add a node to the beginning of the linked
list
Node* LinkedList::addNodeToBeginning(int value)
{
// create a new node with value
Node *node = new Node;
node->number = value;
node->prevNode= head;
// set next of node to node which was previous next of
head
node->nextNode = head->nextNode;
head->nextNode->prevNode = node; // set prev of
node next to head to node
head->nextNode = node; // update next of head to
node
return node;
}
// function to add a node at the end of the linked list
Node * LinkedList::addNodeToEnd(int value)
{
if(head->nextNode == tail) // if empty list, call
addNodeToBeginning function
{
return
addNodeToBeginning(value);
}else
{
// create a new node with
value
Node *node = new Node;
node->number = value;
// set prev of node to prev of
tail
node->prevNode =
tail->prevNode;
// set next of node prev to tail to
node
tail->prevNode->nextNode =
node;
// set next of node to tail
node->nextNode = tail;
// set prev of tail to node
tail->prevNode = node;
return node;
}
}
// function to display the list in forward direction
void LinkedList::printListForward()
{
if(head->nextNode == tail) // empty list
{
cout<<"Empty
list"<<endl;
}else
{
Node *curr = head->nextNode; //
set curr to first node
int count = 0;
// loop over the list to display 10
elements in a line
while(curr != tail)
{
count++;
cout<<curr->number<<" ";
if((count == 10)
&& (curr->nextNode != tail))
cout<<"\n";
curr =
curr->nextNode;
}
cout<<endl;
}
}
// function to display the list in backward direction
void LinkedList::printListBackward()
{
if(head->nextNode == tail) // empty list
{
cout<<"Empty list"<<endl;
}else
{
Node *curr = tail->prevNode; //
set curr to last node
int count = 0;
// loop over the list to display 10
elements in a line
while(curr != head)
{
count++;
cout<<curr->number<<" ";
if((count == 10)
&& (curr->prevNode != head))
cout<<"\n";
curr =
curr->prevNode;
}
cout<<endl;
}
}
// function to sort the list in ascending order using bubble
sort
void LinkedList::sort()
{
if(head->nextNode != tail)
{
Node *curr;
bool swapped = true;
while(swapped)
{
curr =
head->nextNode;
swapped =
false;
while(curr->nextNode != tail)
{
if(curr->number >
curr->nextNode->number)
{
int value =
curr->number;
curr->number =
curr->nextNode->number;
curr->nextNode->number
= value;
swapped = true;
}
curr = curr->nextNode;
}
}
}
}
// function to return the element at index from the list
int& LinkedList::operator [](const int &index)
{
if(head->nextNode == tail) // empty list
{
cout<<"Index out of
bounds"<<endl;
return head->number;
}else
{
int pos = index; // set pos to
index
Node *curr = head->nextNode; //
set curr to first node
// loop to get the node at
pos
while(curr != tail && pos
> 0)
{
curr =
curr->nextNode;
pos--;
}
// valid index
if(pos == 0 && curr !=
tail)
{
return
curr->number;
}else // invalid index
{
cout<<"Index out of bounds"<<endl;
return
curr->number;
}
}
}
// destructor to delete all the nodes of the list
LinkedList::~LinkedList()
{
Node *temp;
while(head != nullptr)
{
temp = head;
head = head->nextNode;
delete(temp);
}
}
int main() {
ifstream fin("Perm_50.txt");
int value;
LinkedList list;
if(fin.is_open())
{
while(!fin.eof())
{
fin>>value;
list.addNodeToEnd(value);
}
fin.close();
cout<<"List forward
:"<<endl;
list.printListForward();
list.sort();
cout<<"List backward :
"<<endl;
list.printListBackward();
list[2] = 66;
cout<<"List forward
:"<<endl;
list.printListForward();
}else
cout<<"Unable to open file
Perm_50.txt"<<endl;
return 0;
}
//end of program
Output:
Input file:

Output:

I need this in C++. This is all one question Program 2: Linked List Class For...
c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...
C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...
Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful. Also note that the function must work no matter how many nodes...
LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...
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"...
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...
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,...
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...