Starting Out with C++: Early Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
Starting Out with C++: Early Objects Plus MyLab Programming with Pearson eText -- Access Card Package (9th Edition)
9th Edition
ISBN: 9780134520520
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 17, Problem 10PC
Program Plan Intro

List Sort

Program Plan:

  • Include the required specifications into the program.
  • Declare a class ListNode.
    • Declare the member variables “value” and “*p” in structure named “ListNode”.
    • The data value of node is stored in variable v and address to next pointer is stored in pointer p
    • Declare the constructor, destructor, and member functions in the class.
    • Declare the structure variable “next” and a friend class Linked List
  • Declare a class LinkList.
    • Function to check whether a particular node with a data value n is a part of linked list or not “bool isMember(double n)”.
    • A recursive print method is defined to print all the data values present in the link list “void rPrint()”.
    • A link list method to insert elements into the link list called “void LinkedList::insert(double x, int pos)” is called that insert elements into the link list at specific positions.
    • A link list method called “void LinkedList::sort()” to sort all the elements present in the list according to their data values and then print the sorted list.
    • A link list method to remove elements from the link list called is called

      “void LinkedList::removeByPos(int pos)” that removes elements from the link list at specific positions.

    • A method to search for an element present in the link list called “ int LinkedList::search(double x)” is called that returns the position of the element present in the link list.
    • A destructor is called to delete the desired data value entered by the user called   “LinkedList::~LinkedList( )”.
    • A method to remove the element passed as a parameter from the link list is called “void LinkedList::remove(double x)”.
    • Declare a method “void reverse( )”  to reverse the elements present in the link list by traversing through the list and Move node at end to the beginning of the new reverse list being constructed.
    • Declaration of structure variable head to store the first node of the list “ListNode * head” is defined.
    • A function “void LinkedList::add(double n)” is defined which adds or inserts new nodes into the link list.
    • A function “bool LinkedList::isMember(double n)” is defined which  searches for a given data value within the nodes present in the link list.
    • A destructor “LinkedList::~LinkedList()” deallocates the memory for the link list.
    • A function “void LinkedList::print()” is used to print all the node data values present in the link list by traversing through each nodes in the link list.
    • A recursive member function check is defined called “bool LinkedList::rIsMember(ListNode *pList,double x)” .
      • If the data value entered is present within the link list, it returns true, else it returns false.
  • Declare the main class.
    • Create an empty list to enter the data values into the list.
    • Copy is done using copy constructor.
    • Input “5” numbers from user and insert the data values into the link list calling “void LinkedList::add(double n)” function.
    • Print the data values of the nodes present in the link list.
    •  “int LinkedList::search(double x)” function is called to search the element present in the link list and prints the position of the element.
    • “void LinkedList::removeByPos(int pos)” function is called to remove elements from the link list at specific positions.

Blurred answer
Students have asked these similar questions
C++ function Linked list   Write a function, to be included in an unsorted linked list class, called replaceItem, that will receive two parameters, one called olditem, the other called new item. The function will replace all occurrences of old item with new item (if old item exists !!) and it will return the number of replacements done.
(Circular linked lists) This chapter defined and identified various operations on a circular linked list.a. Write the definitions of the class circularLinkedList and its member functions. (You may assume that the elements of the circular linked list are in ascending order.)b. Write a program to test various operations of the class defined in (a).
Implement the following function using C++ remove_redundant() This method will clear out the duplicate numbers in your linked list. It is also imperative that you will use an instance of this Linked List class to hold the numbers that you have already checked and use the methods in this instance. In the example 10 -> 10 -> 20 -> 30 -> 10, calling remove_redundant() will remove the other 2 10's that occurred later, hereby making the list 10 -> 20 -> 30. Limitation: You cannot have a nested loop here nor use methods that employ loops as the performance of our linked list will suffer.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning