Question

In C++, Modify the following code of a single linked list, so insted of doing sort...

In C++, Modify the following code of a single linked list, so insted of doing sort insert in descending order, to do it in ascending order:


template <class T>
void IntSLList<T>::sortInsert(T val)
{
   //if empty the list
   if (head == 0)
   {
       //add as node to head with data as val
       addToHead(val);
   }
   //if not empty
   else
   {
       //create to pointers to the list
       IntSLLNode<T> *temp1, *temp2;
       //temp1 points to head=
       temp1 = head;
       //while the value is less that the info of the next node and temp1 isnt tail
       while ((val < temp1->getInfo()) && (temp1 != tail))
       {
           //set temp2 to temp1
           temp2 = temp1;
           //get next node of temp1
           temp1 = temp1->getNext();
       }
       //if temp1 is same as tail and the value is lower than the value in temp1
       if ((temp1 == tail) && (val < temp1->getInfo()))
       {
           //add node to tail which value is val
           addToTail(val);
       }
       else
       {
           //if temp1 is the same as head and the value is greater than the value in temp1
           if ((temp1 == head) && (val > temp1->getInfo()))
           {
               //add node to head which value is val
               addToHead(val);
           }
           else
           {
               //next node to temp2 is a new node with val as value
               temp2->setNext(new IntSLLNode<T>(val, temp1));
           }
       }
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

template <class T>
//changing method so that it inserts in ascending order
void IntSLList<T>::sortInsert(T val)
{
//if empty the list
if (head == 0)
{
//add as node to head with data as val
addToHead(val);
}
//if not empty
else
{
//create to pointers to the list
IntSLLNode<T> *temp1, *temp2;
//temp1 points to head=
temp1 = head;
//while the value is greater that the info of the next node and temp1 isnt tail
while ((val > temp1->getInfo()) && (temp1 != tail))
{
//set temp2 to temp1
temp2 = temp1;
//get next node of temp1
temp1 = temp1->getNext();
}
//if temp1 is same as tail and the value is greater than the value in temp1
if ((temp1 == tail) && (val > temp1->getInfo()))
{
//add node to tail which value is val
addToTail(val);
}
else
{
//if temp1 is the same as head and the value is lesser than the value in temp1
if ((temp1 == head) && (val < temp1->getInfo()))
{
//add node to head which value is val
addToHead(val);
}
else
{
//next node to temp2 is a new node with val as value
temp2->setNext(new IntSLLNode<T>(val, temp1));
}
}
}
}

Add a comment
Know the answer?
Add Answer to:
In C++, Modify the following code of a single linked list, so insted of doing sort...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • I'm having trouble getting this program to compile and run. It is in C++ Language and...

    I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be:       H create link at head       R remove link at head       T create link at tail       K remove link at tail       I remove link at ID          S search...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public:...

    C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT