Question: Please help me l want the same this code but without write on file only read file then normal output NO write on file C++

Please help me l want the same this code but without write on file

only read file then normal output NO write on file

C++

#include #include #include

using namespace std; // For cout, cin, endl, etc.

// Node class class Node { public: // Public access specifier Node(char data) { // Constructor this->data = data; // Assign data to the data member this->next = NULL; // Assign next to NULL } // End constructor

char data; // Data member Node *next; // Pointer to the next node

}; // End Node class

// Linked List class class LinkedList { private: Node *head; // Pointer to the head node Node *tail; // Pointer to the tail node int size; // Size of the list

public: // Public access specifier LinkedList() { // Constructor head = NULL; // Assign head to NULL tail = NULL; // Assign tail to NULL } // End constructor

void insert(char data) { // Insert function Node *newNode = new Node(data); // Create a new node if (head == NULL) { // If head is NULL head = newNode; // Assign newNode to head tail = newNode; // Assign newNode to tail } else { // Else tail->next = newNode; // Assign newNode to tail->next tail = newNode; // Assign newNode to tail } // End else } // End insert function

void print() { // Print function Node *current = head; // Create a node pointer to head while (current != NULL) { // While current is not NULL cout << current->data << " "; // Print current->data current = current->next; // Move current to the next node } // End while cout << endl; // Print endl } // End print function

void sort() { // Sort function quickSort(head, tail); // Call quickSort function } // End sort function

void quickSort(Node *start, Node *end) { // Quick Sort function if (start == NULL || end == NULL) { // If start is NULL or end is NULL return; // Return } // End if Node *pivot = start; // Create a node pointer to start Node *left = NULL; // Create a node pointer to NULL Node *right = NULL; // Create a node pointer to NULL Node *current = start->next; // Create a node pointer to start->next while (current != end) { // While current is not end if (current->data < pivot->data) { // If current->data is less than pivot->data if (left == NULL) { // If left is NULL left = current; // Assign current to left } else { // Else left->next = current; // Assign current to left->next left = left->next; // Assign left->next to left } // End else } else { // Else if (right == NULL) { // If right is NULL right = current; // Assign current to right } else { // Else right->next = current; // Assign current to right->next right = right->next; // Assign right->next to right } // End else } // End else current = current->next; // Move current to the next node } // End while if (left != NULL) { // If left is not NULL left->next = NULL; // Assign NULL to left->next } // End if if (right != NULL) { // If right is not NULL right->next = NULL; // Assign NULL to right->next } // End if if (start->data > pivot->data) { // If start->data is greater than pivot->data pivot->next = right; // Assign right to pivot->next quickSort(start, right); // Call quickSort function } else { // Else pivot->next = left; // Assign left to pivot->next quickSort(left, end); // Call quickSort function } // End else } // End quickSort function void reverse() { // Reverse function Node *current = head; // Create a node pointer to head Node *prev = NULL; // Create a node pointer to NULL Node *next = NULL; // Create a node pointer to NULL while (current != NULL) { // While current is not NULL next = current->next; // Assign current->next to next current->next = prev; // Assign prev to current->next prev = current; // Assign current to prev current = next; // Assign next to current } // End while head = prev; // Assign prev to head } // End reverse function void write_to_file(int choice){ //1 for ascending, 2 for descending ofstream outfile; outfile.open("Out.txt"); Node *current = head; if(choice == 1) { outfile << "Ascending: " << endl; while (current != NULL) { outfile << current->data << endl; current = current->next; } } if(choice == 2){ outfile<< "Descending: "<data<next; } }

} }; // End LinkedList class

int main(){ LinkedList list; // Create a LinkedList object char data; // Create a char variable //open file ifstream LinkedListFile; LinkedListFile.open("LinkedList.txt"); //Check if file exists if(LinkedListFile.fail()){ cout << "File does not exist" << endl; return -1; }

//read file while(LinkedListFile >> data){ list.insert(data); // Call insert function } // End while LinkedListFile.close(); // Close file cout << "Unsorted: "; // Print unsorted list.print(); // Call print function cout << endl; // Print endl cout<<"Ascending or Descending(1 or 2): "; //write answer to file int choice; cin>>choice; if(choice==1){ cout<<"Sorted Ascending: "; // Print sorted list.sort(); // Call sort function list.print(); // Call print function cout << endl; // Print endl list.write_to_file(1); // Call write_to_file function } else{ cout<<"Sorted Descending: "; // Print sorted list.sort(); // Call sort function list.reverse(); // Call reverse function to make it descending list.print(); // Call print function cout << endl; // Print endl list.write_to_file(2); // Call write_to_file function }

cout << endl; // Print endl

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!