CSE 101 UC Santa Cruz Data Structures and Algorithms Programming Assignment
Whole assignment description is in the PDF, skip shuffle part and go straight to the List ADT part.I have provided a header file with explanation on it. You don't need implement all the features. You only need Implement these features(I also made a test script help you test the program, please pass these tests):Class Constructors & Destructors and access functions if you need them
moveFront();
moveBack();
peekNext();
peekPrev();
moveNext();
movePrev();
insertAfter(int x);
insertBefore(int x);
eraseAfter();
eraseBefore();
findNext(int x);
findPrev(int x);
over write operator<< to print list
// List.h// Header file for List ADT. List is a double ended queue with a vertical// cursor standing between elements. The cursor position is always defined// to be an int in the range 0 (at front) to length of List (at back).// An empty list consists of the vertical cursor only, with no elements.//-----------------------------------------------------------------------------#include<iostream>#include<string>#ifndef List_H_INCLUDE_#define List_H_INCLUDE_// Exported type --------------------------------------------------------------class List{private: // private Node structstruct Node{ // Node fieldsint data;Node* next;Node* prev;// Node constructorNode(int x);};// List fieldsNode* frontDummy;Node* backDummy;Node* beforeCursor;Node* afterCursor; int pos_cursor; int num_elements;public: // Class Constructors & Destructors ---------------------------------------- // Creates new List in the empty state.List();// Copy constructor.List(const List& L);// Destructor~List();// Access functions -------------------------------------------------------- // isEmpty() // Returns true if this List is empty, false otherwise.bool isEmpty();// size() // Returns the size of this List.int size();// position() // Returns the position of the cursor in this List. The value returned // will be in the range 0 to size().int position();// Manipulation procedures ------------------------------------------------- // moveFront() // Moves cursor to position 0 in this List.void moveFront();// moveBack() // Moves cursor to position size() in this List.void moveBack();// peekNext() // Returns the element after the cursor. // pre: position()<size()int peekNext();// peekPrev() // Returns the element before the cursor. // pre: position()>0int peekPrev();// moveNext() // Advances cursor to next higher position. Returns the List element that // was passed over. // pre: position()<size()int moveNext();// movePrev() // Advances cursor to next lower position. Returns the List element that // was passed over. // pre: position()>0int movePrev();// insertAfter() // Inserts x after cursor.void insertAfter(int x);// insertBefore() // Inserts x before cursor.void insertBefore(int x);// eraseAfter() // Deletes element after cursor. // pre: position()<size()void eraseAfter();// eraseBefore() // Deletes element before cursor. // pre: position()>0void eraseBefore();// findNext() // Starting from the current cursor position, performs a linear search (in // the direction front-to-back) for the first occurrence of the element x. // If x is found, places the cursor immediately after the found element (so // eraseBefore() would remove the found element), and returns the final // cursor position. If x is not found, places the cursor at position size(), // and returns -1.int findNext(int x);// findPrev() // Starting from the current cursor position, performs a linear search (in // the direction back-to-front) for the first occurrence of the element x. // If x is found, places the cursor immediately before the found element (so // eraseAfter() would remove the found element), and returns the final // cursor position. If x is not found, places the cursor at position 0, and // returns -1.int findPrev(int x);// cleanup() // Removes any repeated elements in this List, leaving only unique data // values. The order of the remaining elements is obtained by retaining // the frontmost occurrance of each element, and removing all other // occurances. The cursor is not moved with respect to the retained // elements, i.e. it lies between the same two retained elements that it // did before cleanup() was called.void cleanup();// clear() // Deletes all elements in this List, setting it to the empty state.void clear();// concat() // Returns a new List consisting of the elements of this List, followed // the elements of L. The returned List's cursor will be at its front // (position 0).List concat(const List& L);// Other Functions --------------------------------------------------------- // to_string() // Returns a string representation of this List consisting of a comma // separated sequence of elements, surrounded by parentheses.std::string to_string();// equals() // Returns true if and only if this List is the same integer sequence as R. // The cursors in this List and in R are unchanged.bool equals(const List& R);// Overriden Operators ----------------------------------------------------- // operator<<() // Inserts string representation of L into stream.friend std::ostream& operator<<( std::ostream& stream, List& L );// operator==() // Returns true if and only if A is the same integer sequence as B. The // cursors in both Lists are unchanged are unchanged.friend bool operator==( List& A, const List& B );// operator=() // Overwrites the state of this List with state of L.List& operator=( const List& L );};#endif
// ListTest.cpp// test client for List ADT//-----------------------------------------------------------------------------#include<iostream>#include<string>#include"List.h"using namespace std;int main(){ List A, B, C , D, E, F, G, H, I, J, K , L, M; // ------------------------------------A.insertAfter(1);A.insertAfter(2);A.insertAfter(3);A.insertAfter(5);A.insertAfter(12);A.moveBack(); if (A.position() != 5) return 1; if (A.size() != 5) return 1; // ------------------------------------B.insertBefore(1);B.insertBefore(2);B.insertBefore(3);B.insertBefore(5);B.insertBefore(12);B.moveFront(); if (B.position() != 0) return 1; if (B.size() != 5) return 1; // ------------------------------------C.insertBefore(1);C.insertBefore(2);C.insertBefore(3);C.insertBefore(5);C.insertBefore(12);C.movePrev();C.eraseAfter(); if (C.size() != 4) return 1;C.findPrev(2);C.eraseAfter(); if (C.size() != 3) return 2; // ------------------------------------D.insertAfter(1);D.insertAfter(2);D.insertAfter(3);D.insertAfter(5);D.insertAfter(12);D.moveNext();D.eraseBefore(); if (D.size() != 4) return 1;D.findNext(2);D.eraseBefore(); if (D.size() != 3) return 2; // ------------------------------------E.insertAfter(200);E.insertAfter(176);E.insertAfter(16);E.insertAfter(5);E.insertAfter(1); if (E.position() != 0) return 1;E.moveBack();E.insertAfter(225); if (E.position() != 5) return 2;E.insertAfter(255);E.moveBack(); if (E.position() != 7) return 3; // ------------------------------------F.insertBefore(1);F.insertBefore(5);F.insertBefore(16);F.insertBefore(176);F.insertBefore(200); if (F.position() != 5) return 1;F.moveFront();F.insertBefore(225); if (F.position() != 1) return 2;F.insertBefore(255);F.moveFront(); if (F.position() != 0) return 3;// ---------------------------------------------------------------G.insertAfter(16);G.insertAfter(200);G.insertAfter(176);G.insertAfter(16);G.insertAfter(5);G.insertAfter(1);G.findNext(16); if (G.position() != 3) return 1;G.movePrev();G.insertBefore(225); if (G.position() != 3) return 2;G.moveNext();G.findNext(16); if (G.position() != 7) return 3;G.moveFront();G.findNext(16); if (G.position() != 4) return 4;// ---------------------------------------------------------------H.insertAfter(200);H.insertAfter(176);H.insertAfter(15);H.insertAfter(10);H.insertAfter(5);H.findNext(15);H.eraseAfter(); if (H.position() != 3) return 1;H.insertAfter(175);H.eraseAfter(); if (H.position() != 3) return 2;H.insertBefore(50);H.insertBefore(15); if (H.position() != 5) return 3;H.findPrev(15);H.eraseAfter(); if (H.position() != 4) return 4;H.findPrev(15); if (H.position() != 2) return 5;H.moveFront();H.eraseAfter();H.eraseAfter();H.eraseAfter();H.eraseAfter();H.eraseAfter(); if (H.position() != 0) return 6;// ---------------------------------------------------------------I.insertBefore(5);I.insertBefore(10);I.insertBefore(15);I.insertBefore(176);I.insertBefore(200);I.findPrev(15);I.eraseBefore(); if (I.position() != 1) return 1;I.insertAfter(175);I.eraseBefore(); if (I.position() != 0) return 2;I.insertBefore(15);I.insertBefore(50); if (I.position() != 2) return 3;I.findNext(15);I.eraseBefore(); if (I.position() != 3) return 4;I.findPrev(15); if (I.position() != 0) return 5;I.moveBack(); if (I.position() != 5) return 6;I.eraseBefore();I.eraseBefore();I.eraseBefore();I.eraseBefore();I.eraseBefore(); if (I.position() != 0) return 7;// ---------------------------------------------------------------J.insertAfter(3215);J.insertAfter(176);J.insertAfter(22);J.insertAfter(5);J.insertAfter(1);J.moveNext(); if (J.moveNext() != 5) return 1;J.eraseBefore();J.eraseBefore();J.moveNext();J.moveNext(); if (J.moveNext() != 3215) return 2;J.moveFront(); if (J.moveNext() != 22) return 3;K.insertBefore(1);K.insertBefore(5);K.insertBefore(22);K.insertBefore(176);K.insertBefore(3215);K.movePrev(); if (K.movePrev() != 176) return 1;K.eraseAfter();K.eraseAfter();K.movePrev();K.movePrev(); if (K.movePrev() != 1) return 2;K.moveBack(); if (K.movePrev() != 22) return 3;// ---------------------------------------------------------------L.insertAfter(77);L.insertAfter(55);L.insertAfter(1);L.insertAfter(34);L.insertAfter(1);L.insertAfter(77);L.insertAfter(77);L.insertAfter(34); if (L.findNext(99) != -1) return 1; if (L.position() != L.size()) return 2;L.moveFront(); if (L.findNext(1) != 4) return 3; if (L.findNext(1) != 6) return 4; if (L.findNext(1) != -1) return 5;L.moveFront();L.cleanup(); if (L.findNext(1) != 3) return 6; if (L.findNext(1) != -1) return 7;// ---------------------------------------------------------------M.insertBefore(34);M.insertBefore(77);M.insertBefore(77);M.insertBefore(1);M.insertBefore(34);M.insertBefore(1);M.insertBefore(55);M.insertBefore(77); if (M.findPrev(99) != -1) return 1; if (M.position() != 0) return 2;M.moveBack(); if (M.findPrev(1) != 5) return 3; if (M.findPrev(1) != 3) return 4; if (M.findPrev(1) != -1) return 5;M.moveBack();M.cleanup(); if (M.findPrev(1) != 2) return 6; if (M.findPrev(1) != -1) return 7;}/* *H = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)H.position() = 0B = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)B.position() = 0C = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)C.position() = 10D = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)D.position() = 10H = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)H.position() = 10B = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)B.position() = 10C = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)C.position() = 0D = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)D.position() = 0B: 10 9 8 7 6 5 4B.position() = 3C: 1 2 3 4 5 6 7C.position() = 7H==B is falseB==C is trueC==D is falseD==H is true5(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)(1, 2, 3, 4, 7, 8, 9, 10)41(3, 4, 7, 8, 9, 10)0-16-10H = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5, 5, 6, 4, 7, 3, 8, 2, 9, 1, 10)15H = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5)10E==H is trueH = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5)B = (3, 4, 7, 8, 9, 10)C = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)D = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)E = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5)F = (10, 1, 9, 2, 8, 3, 7, 4, 6, 5, 3, 4, 7, 8, 9, 10) * */