Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goal of this assignment is to reinforce linked lists in C++. The archive list_methods.zip is not a project but provides the files to start

The goal of this assignment is to reinforce linked lists in C++. The archive list_methods.zip is not a project but provides the files to start a project. Your job is to supply the implementations of the functions in list.h by creating a file list.cpp and including the implementations in that file. If you are successful, the output from try_list_methods.cpp will look like the following output sample.

image text in transcribed

========================================================================\

THIS IS LIST.H, PLEASE implement the following function using the utitly functions given below and please provide an output that's the same as the above output

#ifndef ASSIGN04_LINKED_LISTS_LIST_H #define ASSIGN04_LINKED_LISTS_LIST_H

#include "node1.h"

typedef node::value_type value_type; typedef node *list;

/** * Return a list consisting of the list pointed by head_ptr without the first * element. * It is acceptable to use part or all of the argument list provided that no * change is made to the list. * * precondition: the list pointed to by head_ptr is not empty. */ list tail(list lst);

/* * Return the first element of the list pointed to by head_ptr. * * precondition: the list pointed by head_ptr is not empty. */ value_type head(list lst);

/** * Return true if and only if the list 'head' is empty. */ bool list_is_empty(list lst);

/** * Return a list that consists of 'val' followed by the list pointed to by head_ptr. * It is acceptable to use part or all of the argument list provided that no * change is made to the list. */ list cons(value_type val, const list lst);

/* * ************************************************* * * Implement these using just the functions above: * No references to nodes * No references to the node toolbox. * * ************************************************ */

/* * Create a new list that starts with the elements in the list pointed * to by head_ptr and ends with 'val'. * Return a pointer to the head of the new list. */ list append(const list lst, value_type val);

/** * Return a new list that consists of the two lists combined * into one list. * It is acceptable to use part or all of one or both argument lists provided that * no change is made to them. */ list append(const list lst_first, const list lst_second);

/** * Create and return a new list that contains the elements of head_ptr that * are not equal to val. */ list remove_all_of(const list lst, value_type val);

/** * Create a new list with the same values as the list pointed to by * head_ptr but omitting the last element. * * precondition: the list pointed to by head_ptr is not empty. */ list remove_last(const list lst);

/** * Return the last value in the list pointed to by head_ptr.

* precondition: the list pointed to by head_ptr is not empty. */ value_type last(const list lst);

bool equals(const list list1, const list list2);

#endif //ASSIGN04_LINKED_LISTS_LIST_H

===================================================================================

THIS IS THE TEST METHOD THAT you should use to produce the implmentation of those methods

// Created by Ben on 10/1/2015. //

#include "list.h" #include "utility.h" #include using namespace std;

int main() { double data1[] = {1,2,3,4,5,6,7,0}; list lst = build_list(data1); print_list(lst);

cout

cout

cout

list lst2 = cons(-22, lst); cout

cout

lst2 = append(lst, 238); cout

list lst3 = append(lst, lst2); cout

double data4[] = {1, 9, 2, 9, 9, 3, 9, 4, 5, 6, 9, 9, 7, 8, 9, 0}; list lst4 = build_list(data4); cout

cout

list lst6 = remove_last(lst5); cout

double data7[] = {1, 2, 3, 4, 5, 0}; double data8[] = {1, 2, 3, 0}; double data9[] = {1, 2, 3, 5, 4, 0}; list lst7 = build_list(data7); list lst8 = build_list(data8); list lst9 = build_list(data9); cout

==================================================================================

THIS IS THE NODE CLASS

// FILE: node1.cxx // IMPLEMENTS: The functions of the node class and the // linked list toolkit (see node1.h for documentation). // INVARIANT for the node class: // The data of a node is stored in data_field, and the link in link_field.

#include "node1.h" #include // Provides assert #include // Provides NULL and size_t

using namespace std;

size_t list_length(const node *head_ptr) // Library facilities used: cstdlib { const node *cursor; size_t answer;

answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) ++answer;

return answer; }

void list_head_insert(node *&head_ptr, const node::value_type &entry) { head_ptr = new node(entry, head_ptr); }

void list_insert(node *previous_ptr, const node::value_type &entry) { node *insert_ptr;

insert_ptr = new node(entry, previous_ptr->link()); previous_ptr->set_link(insert_ptr); }

node *list_search(node *head_ptr, const node::value_type &target) // Library facilities used: cstdlib { node *cursor;

for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) if (target == cursor->data()) return cursor; return NULL; }

const node *list_search(const node *head_ptr, const node::value_type &target) // Library facilities used: cstdlib { const node *cursor;

for (cursor = head_ptr; cursor != NULL; cursor = cursor->link()) if (target == cursor->data()) return cursor; return NULL; }

node *list_locate(node *head_ptr, size_t position) // Library facilities used: cassert, cstdlib { node *cursor; size_t i;

assert (0 link(); return cursor; }

const node *list_locate(const node *head_ptr, size_t position) // Library facilities used: cassert, cstdlib { const node *cursor; size_t i;

assert (0 link(); return cursor; }

void list_head_remove(node *&head_ptr) { node *remove_ptr;

remove_ptr = head_ptr; head_ptr = head_ptr->link(); delete remove_ptr; }

void list_remove(node *previous_ptr) { node *remove_ptr;

remove_ptr = previous_ptr->link(); previous_ptr->set_link(remove_ptr->link()); delete remove_ptr; }

void list_clear(node *&head_ptr) // Library facilities used: cstdlib { while (head_ptr != NULL) list_head_remove(head_ptr); }

void list_copy(const node *source_ptr, node *&head_ptr, node *&tail_ptr) // Library facilities used: cstdlib { head_ptr = NULL; tail_ptr = NULL;

// Handle the case of the empty list. if (source_ptr == NULL) return;

// Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data()); tail_ptr = head_ptr;

// Copy the rest of the nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link(); while (source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data()); tail_ptr = tail_ptr->link(); source_ptr = source_ptr->link(); } }

=======================================================================================

THIS IS THE UTILITY FUNCTIONS

// // Created by Ben on 9/30/2015. //

#include "utility.h" #include using namespace std;

node *list1() { double data[] = {2, 3, 2, 3, 3, 2, 3, 3}; int size = 8; return build_list(data, size); }

/** * Assumes that values is a list that is 0 terminated. */ node *build_list(node::value_type values[]) { int num = 0; while(values[num] != 0) num++; return build_list(values, num); }

node *build_list(node::value_type values[], int number) { if(number == 0) return NULL; else { node *head = NULL; list_head_insert(head, values[0]); //cout link(); } return head; } }

void print_list(node* head) { cout data(); if(head->link() != NULL) cout link(); } cout

std::ostream & operatordata(); if(head->link() != NULL) str link(); } str

return str; }

LASTLY please provide an output that matches the picture above. Thank you!

,2, 3,4,5, 6,7 12, 3, 4,5,6,7 1-22, 1,2, 3,4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] ,2, 3,4,5,6,7,238 [1, 2, 3, 4, 5, 6, 7] ,2, 3,4,5, 6,7, 1,2,3,4,5,6, 7,238] ,2, 3,4,5,6,7,238 ,2, 3,4,5, 6,7 [1,9,2,9,9,3,9, 4, 5, 6,9,9,7,8,9 [1, 2,3,4,5,6,7,8 [1,9,2,9,9,3,9, 4, 5, 6,9,9,7,8,9 ,2, 3,4,5, 6,7, 8 [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5,6, 7, 8] ,2, 3,4,5, 6,7 12, 3, 4,5,6,7 1-22, 1,2, 3,4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] ,2, 3,4,5,6,7,238 [1, 2, 3, 4, 5, 6, 7] ,2, 3,4,5, 6,7, 1,2,3,4,5,6, 7,238] ,2, 3,4,5,6,7,238 ,2, 3,4,5, 6,7 [1,9,2,9,9,3,9, 4, 5, 6,9,9,7,8,9 [1, 2,3,4,5,6,7,8 [1,9,2,9,9,3,9, 4, 5, 6,9,9,7,8,9 ,2, 3,4,5, 6,7, 8 [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5,6, 7, 8]

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

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions