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...
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...
***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"...
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...
C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...
c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...
In C++ Assume entries in a linked list are of type struct listrec: struct listrec { struct listrec *prev; float value; struct listrec *next; }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...