Home Study tools
My courses
My books My folder Career Life Study Pack
Find solutions for your homework Search
Your subscription will expire on Dec 25, 2021. Turn on auto-renew to keep accessing solutions.
Turn on auto-renew
home / study / engineering / computer science / computer architecture / computer architecture solutions manuals / data structures and other objects using c++ / 4th edition / chapter 6 / problem 1pp
Data Structures and Other Objects Using C++ Post a question
(4th Edition)
Answers from our experts for your tough
homework questions
Enter question
Chapter 6, Problem 1PP 3 Bookmarks Show all steps: ON
Step-by-step solution
Continue to post
20 questions remaining
Step 1 of 11
sort_list()function definition:
My Textbook Solutions
The following code defines the function sort_list().
• It accepts one parameter and of type node* denoting the head node of the linked list.
• Function uses many temporary pointers of type node* to keep track and check required values.
Add a
• Outer loop iterates until the head pointer of initial node tends to NULL. textboo
• Inner loop iterates through the length of the linked list. Find and insert the node with maximum Data Auditing &
Structures... Assurance...
element value into the new list.
4th Edition 6th Edition
• Finally, return the head pointer to the new sorted list. View all solutions
Comment
Petland Frisco
Sweet-Faced, Lovable Pup
Step 2 of 11
Uncover The Missing Paw of You
Life.
//Function to sort the elements
node<int>* sort_list(node<int> *head)
//initializing sorted list pointers
Store info Directio
node<int> *sort_head=NULL,*sort_temp=NULL;
//Iterate till list becomes empty
while(head!=NULL)
node<int> *curr=head;
node<int> *prev=NULL;
int maxm=-9999;
node<int> *max_node=NULL,*prev_max=NULL;
//Iterate throught the list length
while(curr!=NULL)
//Find the maximum value node
if(curr->get_data()>maxm)
{ Home Study tools
My courses
My books My folder Career Life Study Pack
max_node=curr;
maxm=curr->get_data();
prev_max=prev;
prev=curr;
curr=curr->get_link();
//Update the original list pointers
if(max_node==head)
head=head->get_link();
else
prev_max->set_link(max_node->get_link());
//Insert node in new list
if(sort_head==NULL)
sort_head = max_node;
sort_head->set_link(NULL);
//Fror already initialized list
else
sort_temp=max_node;
sort_temp->set_link(sort_head);
sort_head=sort_temp;
//Return head pointer of sorted list
return sort_head;
Comment
Step 3 of 11
Complete program is as follows:
The following program demonstrates the functionality of the function sort_list().
//Header file
#include
using namespace std;
//Linked list node
template<class Item>
class node
//Private members
private:
int data;Home Study tools
My courses
My books My folder Career Life Study Pack
node<Item> *next;
//Public functions
public:
//Default constructor
node<Item>()
data=0;
next=NULL;
Comment
Step 4 of 11
//Function to set the data
void set_data(int val)
data=val;
//Function to det the next link
Comment
Step 5 of 11
void set_link(node<Item> *link)
next=link;
Comment
Step 6 of 11
//Function to get the data
int get_data()
return data;
Comment
Step 7 of 11
//Function to get the next link
node<Item>* get_link()
return next;
}
}; Home Study tools
My courses
My books My folder Career Life Study Pack
Comment
Step 8 of 11
//Function to sort the elements
node<int>* sort_list(node<int> *head)
//initializing sorted list pointers
node<int> *sort_head=NULL,*sort_temp=NULL;
//Iterate till list becomes empty
while(head!=NULL)
node<int> *curr=head;
node<int> *prev=NULL;
int maxm=-9999;
node<int> *max_node=NULL,*prev_max=NULL;
//Iterate throught the list length
while(curr!=NULL)
//Find the maximum value node
if(curr->get_data()>maxm)
max_node=curr;
maxm=curr->get_data();
prev_max=prev;
prev=curr;
curr=curr->get_link();
//Update the original list pointers
if(max_node==head)
head=head->get_link();
else
prev_max->set_link(max_node->get_link());
//Insert node in new list
if(sort_head==NULL)
sort_head = max_node;
sort_head->set_link(NULL);
//Fror already initialized list
else
{
sort_temp=max_node;
sort_temp->set_link(sort_head);
Home Study tools
My courses
My books My folder Career Life Study Pack
sort_head=sort_temp;
//Return head pointer of sorted list
return sort_head;
Comment
Step 9 of 11
//Main function
int main()
//Initializing the values array
int values[]={8,7,6,4,3,5,2,1,9};
int len = 9;
node<int>* head = NULL;
node<int>* temp1 = NULL;
//Inserting data values into the linked list
for(int i=0;i<len;i++)
//For empty list without head
if(head==NULL)
head = new node<int>();
head->set_data(values[i]);
head->set_link(NULL);
temp1=head;
//Fror already initialized list
else
node<int>* temp = new node<int>();
temp1->set_link(temp);
temp->set_data(values[i]);
temp->set_link(NULL);
Comment
Step 10 of 11
temp1=temp;
node<int> *temp=head;
cout<<"Initial linked list: ";
while(temp!=NULL)
{
cout<<temp->get_data()<<"
Home Study tools
My courses
My books My folder Career Life Study Pack
";
temp=temp->get_link();
cout<<endl;
node<int> *sort_head = sort_list(head);
temp=sort_head;
cout<<"\nLinked list after sorting: ";
while(temp!=NULL)
cout<<temp->get_data()<<" ";
temp=temp->get_link();
} return 0;
Comment
Step 11 of 11
Sample Output:
Initial linked list: 8 7 6 4 3 5 2 1 9
Linked list after sorting: 1 2 3 4 5 6 7 8 9
Comment
Was this solution helpful? 2 0
Recommended solutions for you in Chapter 6
Chapter 6, Solution 4PP
Program Plan: • Declare the class keyed_bag in the
file keybag.h.. • Implement the class keyed_bag in
the file...
See solution
COMPANY
LEGAL & POLICIES
CHEGG PRODUCTS AND SERVICES
CHEGG NETWORK
Home Study tools
My courses
My books My folder Career Life Study Pack
CUSTOMER SERVICE
© 2003-2021 Chegg Inc. All rights reserved.