View the step-by-step solution to:

#include<iostream> #include<cstdlib> using namespace std; // Node struct node { int data; struct node *next; }*head; class...

Hi, this is working code, but when the node insert, it  does not keep it in order. It should be in order for small to larger. For example if 3, 2, 1, was inserted is should print 1, 2, 3.

Also, if you can do the same program using array, I'll give you 25% tip.

For  the other code in file program(2). Everything is working good, but the sorting is upside-down. It should be other way around.

I'm really sorry for the limited time. 

Thank you


#include<iostream> #include<cstdlib> using namespace std; // Node struct node { int data; struct node *next; }*head; class SinglyLinkedList { public: node* createNode(int); void insert(); void deleteNode(); void find(); void print(); SinglyLinkedList() { head = NULL; } }; // main function int main() { int choice, element, position, i; SinglyLinkedList LL; head = NULL; while (1) { cout << endl << "Menu Options" << endl; cout << "=============" << endl; cout << "1. Insert Node" << endl; cout << "2. Delete a Node" << endl; cout << "3. Find a Node" << endl; cout << "4. Print all Nodes" << endl; cout << "5. Exit " << endl << endl; cout << "Enter your choice : "; cin >> choice; switch (choice) { case 1: cout << "Inserting Node: " << endl; LL.insert(); cout << endl; break; case 2: cout << "Delete a Node: " << endl; LL.deleteNode(); break; case 3: cout << "Find a Node: " << endl; LL.find(); cout << endl; break; case 4: cout << "Print all Nodes: " << endl;
Background image of page 1
LL.print(); cout << endl; break; case 5: cout << "Exiting. .." << endl; exit(1); break; default: cout << "Invalid Choice!" << endl; } } } // creates a node node *SinglyLinkedList::createNode(int data) { struct node *temp; temp = new(struct node); if (temp != NULL) { temp->data = data; temp->next = NULL; return temp; } } // Insert Node void SinglyLinkedList::insert() { int data; cout << "Enter the data of node: "; cin >> data; struct node *temp, *s; temp = createNode(data); if (head == NULL) { head = temp; head->next = NULL; } else { s = head; while (s->next != NULL) { s = s->next; } temp->next = NULL; s->next = temp; } cout << "Node inserted" << endl; } // deletes a Node void SinglyLinkedList::deleteNode() { int position, i, counter = 0; if (head == NULL) {
Background image of page 2
Show entire document
#include <ctime> #include <iostream> #include<stdlib.h> using namespace std; int n = 60, arr[1000], key, samples = 40, sampleResults[100]; float scale = .5; int execTime = 0; // complexity int getSearchKey() { cout << "Enter key : "; cin >> key; return (key); } int seqSearch() { int i = 0; while (i<n && key != arr[i]) i++; if (key == arr[i]) return(i); return(-1); } int bSearchIteration(int first, int last) { while (first<last) { int mid = (first + last) / 2; execTime++; if (arr[mid] == key) { return mid; } if (arr[mid]>key) { first = mid + 1; } else { last = mid - 1; } } return -1; } int bSearchRecursion(int first, int last) { int mid = (first + last) / 2; if (first > last) return -1; else { if (arr[mid] > key) { execTime++; bSearchRecursion(mid + 1, last); } else if (arr[mid] < key) { execTime++; bSearchRecursion(first, mid - 1);
Background image of page 1
} else if (arr[mid] == key) return mid; } } void plot() { for (int i = 0; i < n; i++)//repeat for n lines { for (int j = 0; j < arr[i] * scale; j++) cout << ' ';//skip the given spaces for that line cout << '*' << endl;//draw * and go to the next line } } void initArray() { for (int i = 0; i < n; i++) arr[i] = 0; } void generArr() { for (int x = 0; x < n; x++) arr[x] = rand() % n; cout << "Data has been generated successfully\n"; } void printArr() { for (int i = 0; i < n; i++) cout << i << ")\t" << arr[i] << endl; } void sortArr() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (arr[i]>arr[j]) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } } void plotSeqSearchTimes() { for (int i = 0; i < samples; i++) { generArr(); sampleResults[i] = seqSearch(); }//analysis for seqSearch initArray(); for (int i = 0; i < samples; i++) arr[i] = sampleResults[i]; plot(); } void plotBinSearchTimesIterative() { for (int i = 0; i < samples; i++) {
Background image of page 2
Show entire document
Sign up to view the entire interaction

Top Answer

Hi, I have to go. Here are fixed code. I think... View the full answer

Program1_array.cpp

#include &lt;iostream&gt;
#define MAXLEN 100
// main function
int arr[MAXLEN];
int length = 0;
using namespace std;
void insert();
void delete_node();
void find();
void print();
int main()
{
int...

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online