Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the instructions below to create the main file. Thank you! Use the Linked List toolkit provided in nodel h and node1 cpp to perform

Use the instructions below to create the main file. Thank you! image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Use the Linked List toolkit provided in nodel h and node1 cpp to perform the following operations. You needed. You will be creating a main.cpp for the below listed operations: r code should take an empty list into account and print appropriate messages as 1. Create a new list wih the following values in order : 5, 4,6,s,a 2. Print the values in the list separated by a space 3. Print the value stored in the 3"d node 4. Locate the 3d node in this list and change its value to 7 5. Delete the head node and print the updated list 6. Delete the tail node and print the list 7. Copy the remaining list into a new list pointed to by newHead ptr. Print the values in this new list 1 FILE: nodel.h 2 PROVIDES: A class for a node in a linked list, and list manipulation 3 // functions, all within the namespace main savitch 5 5 TYPEDEF for the node class: Each node of the list contains a piece of data and a pointer to the next node. The type of the data is defined as node: :value type in a typedef statement. The value type may be any of the built-in C++ classes (int, char, ..) or a class with a copy constructor, an assignment operator, and a test for equality (xy) 12 CONSTRUCTOR for the node class: 13/ node ( 14s 15 node init linkNULL 16 1 const value_type& init data-value type () 17Postcondition: The node contains the specified data and link. 18 NOTE: The default value for the init_data is obtained from the default 19 7/ constructor of the value type. In the ANSI/ISO standard, this notation is also allowed for the built-in types, providing a default value of zero. The init link has a default value of NULL. 21 1/ 23/ NOTE: 24 Some of the functions have a return value which is a pointer to a node 25 Each of these functiona comes in two versions: a non-const version (where 26the return value is node*) and a const version (where the return value 27 / is const node). 28 EXAMPLES 29. // const node", 30link( activates the const version of link 31 11ist search (c,. .. calls the const version of list search 32 ode p 33 p-link() activates the non-const version of link 34 1ist_ search (p,...calls the non-const veraion of list_search 36 // MEMBER FUNCT IONS for the node class: 37 void set data (const value type& new data) Postcondition: The node now contains the specified new data 39 7/ 40 1 void set_link (node* new_link) 41 // Postcondition: The node now contains the specified new link 42 7/ 43// value type data() const Postcondition: The return value is the data from this node. 46 47 48 49 const node link( const 0. 81 / Postcondition: The pointer returned points to the node at the specified 82// position in the list. (The head node is position 1, the next node is position 2, and so on). If there is no such position, then the null pointer is returned. 83// 86 void 1ist_head_ remove (node*& head ptr) 87Precondition: head ptr is the head pointer of a linked list, with at 88least one node. 89 Postcondition: The head node has been removed and returned to the heap: 90 head ptr is now the head pointer of the new, shorter linked list. 92void 1ist remove (node previous ptr) 93Precondition: previous ptr points to a node in a linked list, and this 94 11 is not the tail node of the 1ist. Postcondition: The node after previous ptr has been removed from the linked 1ist. 98 void list clear (node& head ptr) 99Precondition: head ptr is the head pointer of a linked list. 100 // 101// 102 // Postcondition: All nodes of the list have been returned to the heap, and the bead-ptr is now NULL. void list_copy (const node source ptr, node & head ptr, node& tail ptr) 104 // 105// Precondition: source ptr is the head pointer of a linked list. Postcondition: head ptr and tail ptr are the head and tail pointers for 106 a new list that contains the same items as the list pointed to by 107 // 108void list piece ( 109const node start ptr, const node end ptr, 110node& head ptr, node & tail ptr source ptr. The original list is unaltered. 112 Precondition: start ptr and end ptr are pointers to nodes on the same 113 linked list, with the start ptr node at or before the end ptr node 114 Postcondition: head ptr and tail ptr are the head and tail pointers for a 115 new list that contains the items from start ptr up to but not including 116end ptr. The end_ptr may also be NULL, in which case the new list 117contains clements from start ptr to the end of the 11st. 119 / DYNAMIC MEMORY usage by the toolkit: 120 If there is insufficient dynamic memory, then the following functions throw 121 bad_alloc: the constructor, list head ingert, list insert, list copy, 122 list piece. 123 124 #ifnde f MAIN. SAVITCH-NODE1_A 125 #define MAIN. SAVITCHNODE .H 126 #include // Provides sizet and NULL 127 - - 128 namespace main_ savitch 5 129 ( 130 class node 131 132 133 TYPEDEF 134 typedef double value type 135 136 /1 CONSTRUCTOR 137 node ( pblic: void list_copy (const node source ptr, node & head ptr, node*& tail_ptr) i 172 173 174 175 #endif 176 1// FILE: node1.cxx 2 IMPLEMENTS: The functions of the node class and the 3 // linked list toolkit (see nodel.h for documentation). 4 INVARIANT for the node class: 5/ The data of a node is stored in data field, and the link in link field. 7 ,include "node 1.h" 8 finclude 10 using namespace std: Provides assert // Provides NULL and size 12 namespace main_savitch 5 13 t 14 size t list length (const node+ head ptr) 15 16 // Library facilities used: cstdlib 17 const node cursor 18 size t answer: 19 20 answer 0: 21 for (cursorhead ptr; cursor != NULL; cursor = cursor->link( )) ++answer , 23 24 return answer: 25 26 void 1ist _head_insert (node*& head ptr, const node::value_types entry) 28 29 head_ptr new node (entry, head_ptr) 30 31 32 void 1ist_insert (node previous_ptr, const node: :value_types entry) 34 node insert ptr 36 insert ptr new node (entry, previous _ptr-link() 37 previous_ptr->set_link (insert _ptr): 38 39 40 node list_search (node head ptr, const node: :value type& target) 41 42 43 node cursor // Library facilities used: cstdlib 45 46 47 48 49 50 for (cursor = head-ptr; cursor != NULL; cursor = cursor->link ( )) if (target cursor->data( )) return cursor: return NULL 51 const node 1ist search (const node* head ptr, const node:avalue types target) 52 53 54 const node cursori // Library facilities used: catdlib LL; cursor= cursor->link ( )) 57 58 if (target cursor->data()) return cursori 59 return NULL 60 61 62 node list_locate (node head ptr, size t position) 63 64 65 node cursor: 66 size t ii 67 68 assert (0 link() 72 return cursor; 73 74 75 const node list_locate (const node+ head_ptr, size t position) 76 // Library facilities used: cassert, cstdlib 78 const node cursor 79 size t i: 80 81 assert (0 link) (ilink 94 delete remove _ptr 95 96 97 98 void 1ist remove (node* previous ptr) 99 node remove ptr: 100 101 remove_ptr previous ptr->link() 102 previous_ptr->set_link( remove ptr->link() ) 103 delete remove ptr 104 105 106 void list_clear (node*& head ptr) 107 108 // Library facilities used: cstdlib 109 while (head ptr- NULL) 110 list_head remove (head_ptr): 112 113 void list_copy (const node* source ptr, node& head ptr, node & tail ptr) 114 115 116 head ptrNULL 117 tail-ptr= NULL; 118 119 Handle the case of the empty list. 120 if (source ptrNULL) 121 122 123 I Make the head node for the newly created list, and put data in it. 124 list head insert (head ptr, source ptr->data() 125 tail ptr head ptr: 126 127 I Copy the rest of the nodes one at a time, adding at the tail of new list. 128 source-ptr = source_ptr->link( ); 129 while (source-ptr != NULL) 130t 131 132 133 134 135 136 137 void iat, copy(const mod OUFGe tr, node'*s tail ptr) // Library facilities used: cstdlib returni list insert (tail ptr, source ptr->data()) tail ptr tail ptr->link) source_ptr source_ptr->link()

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

Describe components of an operational budget.

Answered: 1 week ago