1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
andrezito [222]
3 years ago
13

Modify the NumberedList class we implementd during the lecture by adding a member function: void NumberedList::insertPosition(in

t num, int pos) for inserting a new item at a specified position. A position of 0 means that the value will become the first item on the list, a position of 1 means that the value will become the second item on the list, and so on. A position equal to or greater than the length of the list means that the value is placed at the end of the list.
Computers and Technology
1 answer:
satela [25.4K]3 years ago
3 0

Answer:

Check the explanation

Explanation:

#include<iostream>

using namespace std;

class LinkedList

{

private:

   // Declare a structure for the list.

   struct ListNode

   {

   int value;        // The value in this node.

   struct ListNode *next;// To point to the next node.

   };

   ListNode *head;        // List head pointer.

public:

   // Constructor.

   LinkedList()

       { head = NULL; }

   

   // Destructor

   ~LinkedList();

   

   // Linked list operations.

   void appendNode( int );

   void insertNode( int );

   void insertNodeAt(int,int);

    void deleteNode( int );

   void Reverse();

   void deleteAt(int);

   int Search(int);

   void display() const;

};

// appendNode appends a node containing the      

// value passed into num, to the end of the list.  

void LinkedList::appendNode( int num )

{

  ListNode *newNode; // To point to a new node.

  ListNode *nodePtr; // To move through the list.

  // Allocate a new node and store num there.

  newNode = new ListNode;

  newNode->value = num;

  newNode->next = NULL;

  // If there are no nodes in the list.

  // make newNode the first node.

  if ( !head )

     head = newNode;

  else // Otherwise, insert newNode at end.

  {

     // Initialize nodePtr to head of list.

     nodePtr = head;

     // Find the last node in the list.

     while ( nodePtr->next )

        nodePtr = nodePtr->next;

     // Insert newNode as the last node.

     nodePtr->next = newNode;

  }    //    end else-if

  display();  

}    //    end function appendNode

// displayList shows the value stored in each              

// node of the linked list pointed to by head.      

                       

void LinkedList::display() const

{

   ListNode *nodePtr; // To move through the list

   if ( !head )

   {

       cout << "\n\tThe list is empty.";

       return;

   }

   // Position nodePtr at the head of the list.

   nodePtr = head;

   cout << "\n\n\tThe elements in the list are:\n\t";

   // While nodePtr points to a node, traverse the list.

 

    while (nodePtr)

   {

       // Display the value in this node.

       cout << nodePtr->value << " -> ";

       // Move to the next node.

       nodePtr = nodePtr->next;

   }    //    end while.

   cout << "Null";

}    //    end function displayList.

// Reverse function re-arranges node in the list.

void LinkedList::Reverse()

{

   ListNode *nodePtr;

   ListNode *next;

   ListNode *result=NULL;

   if ( !head )

   {

       cout << "\n\tThe list is empty.";

       return;

   }

   // Position nodePtr at the head of the list.

   nodePtr = head;

   while (nodePtr!=NULL)

   {

       next=nodePtr->next;

       nodePtr->next=result;

       result=nodePtr;

       nodePtr=next;

   }

   head=result;

display();

}

// The insertNode function inserts a node with num copied to its value member.                

void LinkedList::insertNode( int num )

{

   ListNode *newNode;             // A new node.

   ListNode *nodePtr;             // To traverse the list.

   ListNode *previousNode = NULL; // The previous node.

   // Allocate a new node and store num there.

   newNode = new ListNode;

   newNode->value = num;

   newNode->next = NULL;

 

   // If there are no nodes in the list make newNode the first node.

   if ( !head )

       head = newNode;

   else // Otherwise, insert newNode.

   {

       // Position nodePtr at the head of list.

       nodePtr = head;

       //    Initialize previousNode to NULL.

       previousNode = NULL;

       //    Skip all nodes whose value is less than num.

       while ( nodePtr != NULL && nodePtr->value < num )

       {

           previousNode = nodePtr;

           nodePtr = nodePtr->next;

       }

       //If the new node is to be the 1st in the list,

       //    insert it before all other nodes.

       if ( previousNode == NULL )

       {

           head = newNode;

           newNode->next = nodePtr;

       }

       else // Otherwise insert after the previous node.

       {

           previousNode->next = newNode;

           newNode->next = nodePtr;

       }

   }    //    end else-if

     

display();  

}    //    end function insertNode.

// The insertNode function inserts a node at pos  

//with num copied to its value member.          

void LinkedList::insertNodeAt( int num ,int pos)

{

   ListNode *newNode;             // A new node.

   ListNode *nodePtr;             // To traverse the list.

   ListNode *previousNode = NULL; // The previous node.

   int i=0;

   // Allocate a new node and store num there.

   newNode = new ListNode;

   newNode->value = num;

   newNode->next = NULL;

   // Position nodePtr at the head of list.

       nodePtr = head;

   if(pos==0)//to inserted at first.

   {  

       newNode->next=head;

       head=newNode;

   }

   else

   {

   while(nodePtr != NULL && i<pos) //loop to reach position.

       {  

           previousNode=nodePtr;

           nodePtr=nodePtr->next;

           i++;

       }

       if(nodePtr==NULL)//position not found.

           cout<<"Invalid Position :"<<endl;

       else//inserts node.

       {

           newNode->next=nodePtr;

           previousNode->next=newNode;

       }

   }

   display();

}

//    The deleteNode function searches for a node with num as its value.  

//The node, if found, is deleted from the list and from memory.

void LinkedList::deleteNode( int num )

{

   ListNode *nodePtr;       // To traverse the list.

   ListNode *previousNode;//To point to the previous node.

   // If the list is empty, do nothing.

   if ( !head )

   {

       cout << "\n\tFailed to delete as list is empty.";  

       return;

   }

   // Determine if the first node is the one.

   if ( head->value == num )

   {

       nodePtr = head->next;

       delete head;

       head = nodePtr;

   }

   else

   {

       // Initialize nodePtr to head of list.

       nodePtr = head;

       // Skip all nodes whose value member is not equal to num.

       while (nodePtr != NULL && nodePtr->value != num)

       {

           previousNode = nodePtr;

           nodePtr = nodePtr->next;

       }

You might be interested in
The only way to print a photo is on a special glossy paper using professional printing services in a store
DaniilM [7]

i believe this is true


8 0
2 years ago
Read 2 more answers
Learning in a digital environment is also called [blank] learning.
Dafna1 [17]

Answer:

The blank is online learning

4 0
2 years ago
Read 2 more answers
Use the drop-down menus to complete statements about the two formats for importing contacts.
oee [108]

Answer:

The PST file format can only be imported into contacts by the outlook

program.

The CSV file type can be created from a document or spreadsheets

and can be imported by multiple email programs.

Explanation:

6 0
2 years ago
Read 2 more answers
¿Cuál crees tú sea la finalidad de los virus informático, y porque son creados? ¿Cuáles son las precauciones y / o recomendacion
pantera1 [17]

Un virus informático es un tipo de programa de software malicioso ("malware") que, cuando se ejecuta, se replica modificando otros programas informáticos e insertando su propio código. Cuando esta replicación tiene éxito, se dice que las áreas afectadas están "infectadas" con un virus informático.

Un virus informático es un tipo de programa de software malicioso ("malware") que, cuando se ejecuta, se replica modificando otros programas informáticos e insertando su propio código. Cuando esta replicación tiene éxito, se dice que las áreas afectadas están "infectadas" con un virus informático.

lo mejor es mantener tu computadora actualizada

6 0
3 years ago
Write a function DrivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar
posledela

Answer:

#include <iostream>

using namespace std;

double DrivingCost(int drivenMiles,double milesPerGallon,double dollarsPerGallon)

{

   double dollarsperMile=dollarsPerGallon/milesPerGallon;//calculating dollarsperMile.

   return dollarsperMile*drivenMiles;//returning thr driving cost..

}

int main() {

double ans;

int miles;

cout<<"Enter miles"<<endl;

cin>>miles;

ans=DrivingCost(miles,20.0,3.1599);

cout<<ans<<endl;

return 0;

}

Output:-

Enter miles

10

1.57995

Enter miles

50

7.89975

Enter miles

100

15.7995

Explanation:

In the function first I have calculated the dollars per mile and after that I have returned the product of dollarspermile and driven miles.This will give the cost of the Driving.

4 0
2 years ago
Other questions:
  • TCP will guarantee that your packets will arrive at the destination, as long as the connection is still established. True False
    11·1 answer
  • Suppose your SOHO network connects to the Internet using cable modem. When you open your browser and try to access a web site, y
    8·1 answer
  • Write a program that uses for loops to perform the following steps: Prompt the user to input two integers: firstNum and secondNu
    15·1 answer
  • Oracion con punto focal​
    6·1 answer
  • ________ is a remote access client/server protocol that provides authentication and authorization capabilities to users who are
    10·1 answer
  • Explain the importance of mobile computing in communication​
    7·2 answers
  • Lines s, t, and u are perpendicular bisectors of the sides FGH and meet at J. If JG=2x+2, JH=2y-4, JF=8 and HI=3z-3, find x, y,
    15·2 answers
  • Why are computers useful for modeling situations?
    13·2 answers
  • If Anime Characters were real , Who Would Your Anime Wife Would Be (Tell Who And Why)
    10·1 answer
  • WILL GIVE BRAINLIEST!! NO LINKS!!!
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!