C++ - Lesson 2 1. Explain the following declarations: a) int *a(int a[]); This is a function prototype. 'a' is a function that takes an integer array argument and returns an integer pointer. b) const char [100]; This declaration is for a string of 100 characters whose data is constant. c) const string *sptr; 'sptr' is a pointer to a constant string. [the string is R(Read-only) / 'sptr' is RW(Readable/Writable)] d) string *const sptr; 'sptr' is a constant pointer to a string. [the string is RW / 'sptr' is R] e) const string *const sptr 'sptr' is a constant pointer to a constant string. [the string is R / 'sptr' is R] f) bool (*comp)(int, int); 'comp' is a pointer to a function that takes two integer arguments and returns a bool. g) void f(bool (*c) (int, int)); // Write the call to f 'f' is a function prototype that takes a function pointer (like the question above) and returns void. Example: f(comp); h) int ia[3][4]; 'ia' is a 2-dimensional array with 3 rows and 4 columns. i) int (*ip)[4]; 'ip' is a pointer to an array of 4 integer elements. ( to have an array 4 integer pointers you would write: int *ip[4] )
2. Write a template-class for doubly linked lists. Your class should provide the following functionalities: a. A constructor to create an empty list. b. A copy constructor. c. A destructor. d. A member function to insert an element in a list. e. A member function to remove an element from a list. f. A member function to search for an element in a list. g. An assignment operator. h. A member function that merges two sorted lists, L1 and L2, and returns a new sorted listed. The user should be able to call this function through the expression L1+L2. //----- doublelist.h -----// /******** DOUBLE LIST NODE *********/ class DoubleListNode public: DoubleListNode(DoubleListNode *prevnode, DoubleListNode *nextnode) : next(nextnode), prev(prevnode) T value; DoubleListNode<T> *next; DoubleListNode<T> *prev; ; /********* DOUBLE LIST ***********/ class DoubleList public: DoubleList(); DoubleList(const DoubleList &); ~DoubleList(); DoubleList& insert(t); DoubleList& remove(t); T* search(t value); const DoubleList& operator=(const DoubleList &); DoubleList operator+(doublelist&); private: void copyfromlist(const DoubleList &); DoubleListNode<T> *head; DoubleListNode<T> *tail; ; //----- END doublelist.h -----//
//----- doublelist.cpp -----// #include <doublelist.h> DoubleList<T>::DoubleList() head = new DoubleListNode<T>(NULL, NULL); tail = new DoubleListNode<T>(head, NULL); head->next = tail; DoubleList<T>::DoubleList(const DoubleList<T> ©) head = new DoubleListNode<T>(NULL, NULL); tail = new DoubleListNode<T>(head, NULL); head->next = tail; copyfromlist(copy); DoubleList<T>::~DoubleList() DoubleListNode<T> *index = head; while (index!= NULL) DoubleListNode<T> *temp = index->next; delete index; index = temp; /* * Insert a new value at the end of the list. */ DoubleList<T>& DoubleList<T>::insert(T val) while(index->next!= NULL) DoubleListNode<T> *newnode = new DoubleListNode<T>(index->prev, index); newnode->value = val; // point old links to the new node. index->prev = index->prev->next = newnode; DoubleList<T>& DoubleList<T>::remove(T val) while (index->next!= NULL && index->value!= val) if (index->next!= NULL) index->prev->next = index->next; index->next->prev = index->prev; delete index;
T* DoubleList<T>::search(T val) while (index->next!= NULL) if (index->value == val) return &(index->value); return NULL; const DoubleList<T>& DoubleList<T>::operator=(const DoubleList<T> ©) if (this == ©) // Delete all elements in current list. DoubleListNode<T> *tmp; while (index->next!= NULL) tmp = index; index= index->next; delete tmp; // index now points to the tail head->next = index; // join the head and index->prev = head; // tail together. // Now copy over all elements. copyfromlist(copy); DoubleList<T> DoubleList<T>::operator+(DoubleList<T> ©) // Merge two SORTED lists together (ascending). DoubleList<T> newlist; DoubleListNode<T> *index1 = head->next; DoubleListNode<T> *index2 = (copy.head)->next; while (index1->next!= NULL index2->next!= NULL) if ( index1->next!= NULL && (index2->next == NULL (index2->value >= index1->value))) newlist.insert(index1->value); index1 = index1->next; if (index1->next == NULL && index2->next == NULL) break; if ( index2->next!= NULL && (index1->next == NULL (index1->value >= index2->value))) newlist.insert(index2->value); index2 = index2->next; return newlist; void DoubleList<T>::copyFromList(const DoubleList<T> ©) DoubleListNode<T> *index = (copy.head)->next; while (index->next!= NULL)
insert(index->value); //----- END doublelist.cpp -----//