Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

All I need is the header file (LinkedList.h). Overview: The purpose of this assignment is for you to write a data structure called a Linked

All I need is the header file (LinkedList.h).

Overview:

The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Javas generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will serve you well in a variety of situations, not just for an assignment like this.

image text in transcribed

image text in transcribed

image text in transcribed

Here is the main:

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

/*==== Insertion =-=*/ void AddHead(const T &data); void AddTail(const T &data); void AddNodesHead (const T *data, unsigned int count); void AddNodesTail (const T *data, unsigned int count); void InsertAfter (Node *node, const T &data); void InsertBefore(Node *node, const T &data); void InsertAt (const T &data, unsigned int index); Removal* bool RemoveHead() bool RemoveTail) unsigned int Remove (const T &data); bool RemoveAt (int index); void Clear); /*=-= Operators-==*/ const T & operator[] (unsigned int index) const; T & operator[] (unsigned int index); bool operator (const LinkedList &rhs) const; LinkedList &rhs); /*==== Construction / Destruction ====*/ LinkedList); LinkedList (const LinkedList &list); LinkedList); Function Reference Behaviors PrintForward PrintReverse PrintForwardRecursive This function takes in a pointer to a Node-a starting node. From that node, Iterator through all of the nodes and print out their values, one a time Exactly the same as PrintForward, except completely the opposite recursively visit each node that follows, in forward order, and print their values. This function MUST be implemented using recursion, or tests using it will be worth no points. Check your textbook for a reference on recursion. PrintReverseRecursive Same deal as PrintForwardRecursive, but in reverse Accessors NodeCount How many things are stored in this list? FindAll Find all nodes which match the passed in parameter value, and store a pointer to that node in the passed in vector. Use of a parameter like this (passing something in by reference, and storing data for later use) is called an output parameter Find the first node with a data value matching the passed in parameter returning a pointer to that node. Returns null if no matching node found Given an index, return a pointer to the node at that index. Throws an exception if the index is out of range. Const and non-const versions Returns the head pointer. Const and non-const versions Find GetNode Head Tail Returns the tail pointer. Const and non-const versions Insertion Operations AddHead AddTail AddNodesHead Create a new Node at the front of the list to store the passed in parameter Create a new Node at the end of the list to store the passed in parameter Given an array of values, insert a node for each of those at the beginning of the list, maintaining the original order Ditto, except adding to the end of the list Given a pointer to a node, create a new node to store the passed in value, after the indicated node Ditto, except insert the new node before the indicated node Inserts a new Node to store the first parameter, at the index-th location. So if you specified 4 as the index, the new Node should have 3 Nodes before it. Throws an exception if given an invalid index AddNodesTail InsertAfter InsertBefore InsertAt Removal Operations RemoveHead Deletes the first Node in the list. Returns whether or not the operation was successful Deletes the last Node, returning whether or not the operation was successf Remove ALL Nodes containing values matching that of the passed-in parameter Returns how many instances were removed Deletes the index-th Node from the list, returning whether or not the operation was successful Deletes all Nodes. Don't forget the node count-how many nodes do you have after you deleted all of them? RemoveTail Remove RemoveAt Clear Operators operator[) operator= operator- Overloaded brackets operator. Takes an index, and returns the index-th node Throws an exception if given an invalid index. Const and non-const versions Assignment operator. After listA - listB, listAlistB is true. Can you utilize any of your existing functions to make write this one? (Hint: Yes you can.) Overloaded equality operator. Given listA and listB, is listA equal to listB? What would make one Linked List equal to another? If each of its nodes were equal to the corresponding node of the other. (Similar to comparing two arrays, just with non-contiguous data) Construction / Destruction LinkedList) Copy Constructor LinkedList() Default constructor. How many nodes in an empty list? What is head pointing to? What is tail pointing to? Initialize your variables! Sets "this" to a copy of the passed in LinkedList. The other list has 10 nodes, with values of 1-10? "this" should too The usual. Clean up your mess. (Delete all the nodes created by the list.) Template types and the typename keyword Tips A few tips for this assignment: The compilation process for templates requires specialization-that is, your compiler essentially copies- and-pastes a version of your template, replacing all the instances of cT> with the type you specified when creating an instance of the class. When dealing with templates within templates, nested template classes, et... the compiler sometimes needs a bjt of assistance when figuring out a type. Start small! Work on one bit of functionality at a time. Work on things like Addll and PrintForwardl) first, as well as accessors (brackets operator, Head()/Tail(), etc). You can't really test anything else unless those are working. . in order to know how to specialize, it has to know everything about the template class. If that template has some other template, it needs to know everything.. .before it knows everything... and therein lies the problem. . Your output is simple: print the data, print rnewline . Remember the "Big Three" or the "Rule of Three If you define one of the three special functions (copy constructor, assignment operator, or destructor), you should define the other two o The typename keyword is a way of telling your compiler "Hey, what immediately follows typename is a data type. Treat it as such when you compiler everything else." For example: Refer back to the recommended chapters in your textbook as well as lecture videos for an explanation of the details of dynamic memory allocation template class Foo o There are a lot of things to remember when memory allocation - Make charts, diagrams, sketches of the problem. Memory is inherently difficult to visualize, find public a way that works for you. Don't forget your node count! struct Nested . T saneThing; Sample Outputs Nested SomeFunctian); "Testing AddRead() functionality When defining the function "SomeFunction" you might write this Pr int 10 1Tiak forMard template Nested Foo<:sone function error what is a you could clean that up by specifying nested object part of the foo class: template t> Print 1ist in reverac: FoO::Nested Foo: :SoneFunction Okay... how about this? tenplate Errar, Foo is a teplate class label it as such FoocT: :Nested FooKT::SoneFunction) This SHOULD work, but. still doesn't. Why? Teating hddTa31o functionality Print list Erward: Whern the Fooclass is being defined,itis eferencing a type, Nested. This type is part of the foo class, but the compiler doesn't know it's part of the class until it's done defining the class... But, since Foo is in the process of being defined... how can something simultaneously be defined not-yet-defined? Answer: It can't. Solution: using the typename kewword, tell the compiler that Nested IS IN FACT A TYPE, and so the compiler doesn't have to wait to fully define Foo before finding out what it is. 23 Print 11st In reverse: 21 // typename: Yes, compiler, FoocT>: Nested is a type. Honest. Yau'll realize it later tenplate typename FODeT>: :Nested Foo::SomeFunction() Ugly? You bet! Part of the joy of working with templates? It sure is! Every programming language has quirks like this that you just have to learn over time. 1 #include-Linkedlist.h-1 2 #include 3 #include 4 #include 5 using nanespace std; 7 void TestAddHeadO 8 void TestAddTail) 9 void TestBracketsO; 10 void TestRemove); 11 void TestRemoveHeadTail); 12 void TestotherRemoval) 13 void TestaddingArrays) 14 void TestFindAl1o; 15 void TestFind); 16 void TestInsertAt); 17 void TestRecursion); 18 19 int main) 21 nt testNus; 22 cin 5 testNUm TestAddHead); TestAddTailo; TestBrackets) TestResove): TestRemoweHeadTaino TestotherResoval0: TestAddingArrays O TestFindA1IO TestrindO TestInsertAtO TestRecursion)i 23 if (testium1) 25 else if (testum2) 26 27 else if (testNum3) 28 29 else if (testhum 4) 30 31 else if (testNum 5) 33 else if (testNum 6) 34 35 else if (testNum" 7) 36 37 else if (testhum) 39 else if (testhus9) 41 else if (testium 10) 47 else if (testtun 11) 46 return 47 48 9TESTING FUNCT IONS 5e void TestAddHead) 51 ( 52 coute Testing Addiead) functionalityendl; 53, Linkedistdnt> data; 54 for (int i - o; i data; 67 for (int i-e; i 21; i3) 68 data.AddTail(i); 69 cout data; 106string test - "Test Resovetle to Renovele find Renovele all Resovetle matching ": 107 test.append( Resovele nodes Renmovele completed Resovete with Removele no ); 198 test.append ( RemovetteemvekeRemovete nodes Resoveke reaaining.") 109 stringstream x(test)s 110 string tmp; 1 while (getline(x, tap,) 112 113 114 cout data; data 151 data.AddTail( "Batan) 152 data.AddTail(" RemoveMe") 153 data. AddTail( Superman) 154 data.AddTail( Renovene) 155 data.AddTai1("Wonder Woman) 156 data.AddTail( "Removene") 157data.AddTail("The Flash"): 1list 212 1ist.AddHead() 213 1ist.AddHead(): 214 list.AddHead(**); 215 list.AddHead() 216 list.AddHead(") 217 list.Addiead) 218 list.AddHead) 219 list.AddHead("****) 220 list. AddHead 2it.Addblead 222 list.Addwead() 223 224 list. AddNodesHead(values, 5); 225 1ist.AddiodesTail(values, 5) 226 list.PrintForward); 227 ) 228 229 void TestFindall) 231 cout Testing FindAO functionality..' endl; 232 Linkedtistcint> data 233 Int searchval 256 234 215 data, AddTail(searchval): 236 data. AddTai1(10); 237 data.AddTail(20); 2 data.AddTail(searchval); 239 data.AddTai1(30); 240 data.AddTail(searchval); 41 data.AddTa1l(40); 242data Adrail(searchval): 243 244 cout : : Node "> nodes; 248 data.FindAll (nodes, searchval); 249 250 cout data prev 1 nullptr) cout prev->data else cout next >data else cout data 275 data. AddTail(1); 276 data. AddHead(9); s endi 277 data.AddTail (11); 278 data.AddHead(8); 279 data.AddTail (12); 280 281 cout prev)data endl; cout next->data data prev-odata next->data data; 323 string test "great benefit linked is the ability to easily into the" 324 stringstream x(test) 325 string tmp; 327 26 while (getline(x, tap,) data.AddTail(tmp); 328 329 cout power2; 349 int val 2; 50 for (inti -e; i s:Node "node- power2.Find (64); 9power2.printforwardRecursive(node)s 360 361 cout

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 Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

Identify who may be responsible for performance appraisal.

Answered: 1 week ago

Question

Explain the performance appraisal period.

Answered: 1 week ago