Solved - Chapter 6 Problem 1PP Solution - Data Structures and Other Objects Using C++ 4th Edition
Solved - Chapter 6 Problem 1PP Solution - Data Structures and Other Objects Using C++ 4th Edition
My courses
My books My folder Career Life Study Pack
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
(4th Edition)
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
while(head!=NULL)
node<int> *curr=head;
node<int> *prev=NULL;
int maxm=-9999;
node<int> *max_node=NULL,*prev_max=NULL;
while(curr!=NULL)
max_node=curr;
maxm=curr->get_data();
prev_max=prev;
prev=curr;
curr=curr->get_link();
if(max_node==head)
head=head->get_link();
else
prev_max->set_link(max_node->get_link());
if(sort_head==NULL)
sort_head = max_node;
sort_head->set_link(NULL);
else
sort_temp=max_node;
sort_temp->set_link(sort_head);
sort_head=sort_temp;
return sort_head;
Comment
Step 3 of 11
//Header file
#include
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
data=val;
Comment
Step 5 of 11
next=link;
Comment
Step 6 of 11
int get_data()
return data;
Comment
Step 7 of 11
node<Item>* get_link()
return next;
}
}; Home Study tools
My courses
My books My folder Career Life Study Pack
Comment
Step 8 of 11
node<int> *sort_head=NULL,*sort_temp=NULL;
while(head!=NULL)
node<int> *curr=head;
node<int> *prev=NULL;
int maxm=-9999;
node<int> *max_node=NULL,*prev_max=NULL;
while(curr!=NULL)
if(curr->get_data()>maxm)
max_node=curr;
maxm=curr->get_data();
prev_max=prev;
prev=curr;
curr=curr->get_link();
if(max_node==head)
head=head->get_link();
else
prev_max->set_link(max_node->get_link());
if(sort_head==NULL)
sort_head = max_node;
sort_head->set_link(NULL);
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 sort_head;
Comment
Step 9 of 11
//Main function
int main()
int values[]={8,7,6,4,3,5,2,1,9};
int len = 9;
for(int i=0;i<len;i++)
if(head==NULL)
head->set_data(values[i]);
head->set_link(NULL);
temp1=head;
else
temp1->set_link(temp);
temp->set_data(values[i]);
temp->set_link(NULL);
Comment
Step 10 of 11
temp1=temp;
node<int> *temp=head;
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;
temp=sort_head;
while(temp!=NULL)
cout<<temp->get_data()<<" ";
temp=temp->get_link();
} return 0;
Comment
Step 11 of 11
Sample Output:
Comment
See solution
COMPANY
CUSTOMER SERVICE