Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Linked List I need help implementing those codes below by implementing an ordered linked list either ascending or descending. In addition to the usual

C++ Linked List

I need help implementing those codes below by implementing an ordered linked list either ascending or descending. In addition to the usual insert() and delete() methods, please also implement union(), intersection() and reverse() methods. Ordered linked lists are great for getting max or min values in O(1) time (based on the ordering). Hence, you will also write print_topk() and print_bottomk() methods.

//node.h

#ifndef OREDERD_LINKEDLIST_NODE_H #define OREDERD_LINKEDLIST_NODE_H class node { private: node* next; int data; public: node(); node(int input); node(int input[], int length); ~node(); node *insert(int value); node* remove(int value); void print(); int get_value(int location); }; #endif //ORDERED_LINKEDLIST_NODE_H 

//node.cpp

#include "node.h" // Take in value and create a node node::node(int input) { } // Takes in an array of values and creates the ordered nodes node::node(int values[], int length) { } // Default destructor node::~node() { } // Insert a new node in appropriate location node * node::insert(int value) { // Must return head pointer location } // Remove the node if found value node* node::remove(int value) { // Must return head pointer location } // Print all nodes void node::print() { } // Get the value of a given node int node::get_value(int location) { } 

//order_linkedlist.h

#ifndef ORDERED_LINKEDLIST_LINKEDLIST_H #define ORDERED_LINKEDLIST_LINKEDLIST_H #include "node.h" class linked_list { private: node* head; public: linked_list(); linked_list(int value); linked_list(int values[], int length); ~linked_list(); void insert(int value); void remove(int value); void reverse_list(); void list_intersect(linked_list* list_to_intersect); void list_union(linked_list* list_to_union); bool check_order(); int get_value(int location); void print(); void print_topk(); void print_bottomk(); }; #endif //ORDERED_LINKEDLIST_LINKEDLIST_H 

//ordered_linkedlist.cpp

#include "ordered_linkedlist.h" #include  // Default constructor for creating a linked list with nothing in it linked_list::linked_list() { head = nullptr; } // Default constructor for creating a linked list with a given value linked_list::linked_list(int value) { head= new node(value); } // Default constructor for creating an ordered linked list from a given integer array linked_list::linked_list(int values[], int length) { head= new node(values,length); } // Default destructor. Should run through each of the nodes and delete them linked_list::~linked_list() { delete head; } // Add a single value to the ordered list void linked_list::insert(int value) { if(head== nullptr) { return; } else { head= head->insert(value); } } // Remove a value if exists void linked_list::remove(int value) { if(head== nullptr) { return; } else { head= head->remove(value); } } // Reverse the entire linked list void linked_list::reverse_list() { } // Intersect the linked list with a given ordered linked list void linked_list::list_intersect(linked_list* list_to_intersect) { } // Union the linked list with a given ordered linked list void linked_list::list_union(linked_list* list_to_union) { } // Check whether the list is accending or decending bool linked_list::check_order() { } // Get the data at the given location int linked_list::get_value(int location) { return head->get_value(location); } // Print top k values void linked_list::print_topk() { } // Print bottom k values void linked_list::print_bottomk() { } // Print the entire linked list void linked_list::print() { head->print(); } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

ISBN: 0130913677, 978-0130913678

More Books

Students also viewed these Databases questions