Question: Please write in C++ and use comments explaining steps being done. FOR A DATA STRUCT C++ CLASS. PLEASE WRITE IN C++ FORMAT. LEAVE COMMENTS 1.

 Please write in C++ and use comments explaining steps being done.

Please write in C++ and use comments explaining steps being done.

FOR A DATA STRUCT C++ CLASS. PLEASE WRITE IN C++ FORMAT. LEAVE

COMMENTS 1. a) Create 100 random numbers of value 100-150 as elements

in a .txt file b) Read values from the file to create

a class of singular linked list in sorted order and print forward

and backward of the list. Create a member function to insert 1000

FOR A DATA STRUCT C++ CLASS. PLEASE WRITE IN C++ FORMAT. LEAVE COMMENTS 1. a) Create 100 random numbers of value 100-150 as elements in a .txt file b) Read values from the file to create a class of singular linked list in sorted order and print forward and backward of the list. Create a member function to insert 1000 in the 41s element of the list. Print the final list using an iterator class. c) Read values from the file to create a template class of doubly linked list and print forward and backward the list. Create a member function to delete all the elements in the range 120-130. Print the shortened list using an iterator class. d) Read values from the file to create a class of circular linked list and print forward and backward the list. Search element 135 using a member function and if present replace it (and duplicates) with 150. Print the final list using an iterator class. e) Code to create a linked list say having 5 nodes using static allocation. // Weekly_Task2.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include // set basic structure for node struct node { int data; int key; struct node* next; }; //set where both sides start on NULL struct node* head = NULL; struct node* current = NULL; //void tp display the list void printList() { struct node* printList = head; printf(" [ "); //telling function to start from the beginning while (printList != NULL) { printf("%d, %d) ", printList->key, printList->data); printList = printList->next; } printf(" ]"); } //inserting link at the first location void insert First(int key, int data) { struct node* link = (struct node*)malloc(sizeof(struct node)); 1/types of links link->key = key; link->data = data; //point it to old first node link->next = head; //point first to new first node head = link; } 1/function to delete the first item in the list struct node* deleteFirst() { //save reference to first link struct node* tempLink = head; head = head ->next; //return the deleted link return tempLink; } //to find if the list is empty bool isEmpty() { return head == NULL; } //to find the length of the current link int length() { int length = 0; struct node* current; for (current = head; current != NULL; current = current->next) { length++; } return length; } //find a link with given key struct node* find(int key) { //start from the first link struct node* current = head; //if list is empty if (head == NULL) { return NULL; } while (current ->key != key) { //if it is last node if (current->next == NULL) { return NULL; } else { current = current->next; } } //if data found, return the current Link return current; } *DELETE IS NOT WORKING EVEN THOUGH IT IS //delete a link with given key DESIGNATED TO A POINTER* struct node* delete(int key) { //start from the first link struct node* current = head; struct node* previous = NULL; //if list is empty if (head == NULL) { return NULL; } /avigate through list while (current->key != key) { //if it is last node if (current->next == NULL) { return NULL; } else { //store reference to current link previous = current; //move to next link current = current->next; } } if (current == head) { 1/change first link to point to next link head = head->next; } else { previous->next = current->next; } return current; } void sort() { int i, j, k, tempkey, tempData; struct node* current; struct node* next; int size = length(); k = size; for (i = 0; i next; for (j = 1; j data > next->data) { tempData = current->data; current->data = next->data; next->data = tempData; tempkey = current->key; current->key = next->key; next ->key = tempkey; } current = current->next; next = next->next; } } } // reverse the order void reverse(struct node** head_ref) { struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } int main() { insert First(1, 10); insert First(2, 20); insert First(3, 30); insertFirst(4, 1); insert First(5, 40); insert First(6, 56); printf("Original List: "); //print list printList(); while (!isEmpty() { struct node* temp = deleteFirst(); printf(" Deleted value: "); printf("%d,%d) ", temp->key, temp->data); } printf(" List after deleting all items: "); printList(); insertFirst(1, 10); insert First(2, 20); insert First(3, 30); insert First(4, 1); insert First(5, 40); insert First(6, 56); printf(" Restored List: "); printList(); printf(" "); struct node* foundLink = find(4); if (foundLink != NULL) { printf("Element found: "); printf("%d, %d) ", foundLink->key, foundLink->data); printf(" "); } else { printf("Element not found."); } //delete the fourth element delete(4); *AGAIN DELETE WILL NOT WORK, I WANTED TO DELETE THE FOURTH ELEMENT, BUT WILL ONLY ALLOW DELETION OF "POINTERS" printf("List after deleting an item: "); printList(); printf(" "); foundLink = find(4); if (foundLink != NULL) { printf("Element found: "); printf("%d, %d) ", foundLink->key, foundLink->data); printf(" "); } else { printf("Element not found."); } //data to print at the end of the file printf(" "); sort(); printf("List after sorting the data: "); printList(); reverse(&head); printf(" List after reversing the data: "); printList(); }

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!