Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @file list.cpp
- * Doubly Linked List (MP 3).
- */
- template <class T>
- List<T>::List() {
- // @TODO: graded in MP3.1
- head_ = NULL;
- tail_ = NULL;
- length_=0;
- }
- /**
- * Returns a ListIterator with a position at the beginning of
- * the List.
- */
- template <typename T>
- typename List<T>::ListIterator List<T>::begin() const {
- // @TODO: graded in MP3.1
- return List<T>::ListIterator(head_);
- }
- /**
- * Returns a ListIterator one past the end of the List.
- */
- template <typename T>
- typename List<T>::ListIterator List<T>::end() const {
- // @TODO: graded in MP3.1
- return List<T>::ListIterator(NULL);
- }
- /**
- * Destroys all dynamically allocated memory associated with the current
- * List class.
- */
- template <typename T>
- void List<T>::_destroy() {
- /// @todo Graded in MP3.1
- ListNode * temp = head_;
- if(temp==NULL)
- {
- return;
- }
- ListNode * temp2;
- while(temp!=NULL)
- {
- temp2=temp->next;
- delete temp;
- temp = temp2;
- }
- //return;
- }
- /**
- * Inserts a new node at the front of the List.
- * This function **SHOULD** create a new ListNode.
- *
- * @param ndata The data to be inserted.
- */
- template <typename T>
- void List<T>::insertFront(T const & ndata) {
- //std::cout<<"pass"<<std::endl;
- /// @todo Graded in MP3.1
- ListNode * newNode = new ListNode(ndata);
- newNode -> next = head_;
- newNode -> prev = NULL;
- if (head_ != NULL) {
- head_ ->prev = newNode;
- }
- if (tail_ == NULL) {
- tail_ = newNode;
- }
- head_=newNode;
- length_++;
- }
- /**
- * Inserts a new node at the back of the List.
- * This function **SHOULD** create a new ListNode.
- *
- * @param ndata The data to be inserted.
- */
- template <typename T>
- void List<T>::insertBack(const T & ndata) {
- /// @todo Graded in MP3.1
- ListNode * newNode = new ListNode(ndata);
- newNode -> next = NULL;
- newNode -> prev = tail_;
- if (head_ == NULL) {
- head_ = newNode;
- }
- if (tail_ != NULL) {
- tail_ -> next = newNode;
- }
- tail_=newNode;
- length_++;
- }
- /**
- * Helper function to split a sequence of linked memory at the node
- * splitPoint steps **after** start. In other words, it should disconnect
- * the sequence of linked memory after the given number of nodes, and
- * return a pointer to the starting node of the new sequence of linked
- * memory.
- *
- * This function **SHOULD NOT** create **ANY** new List or ListNode objects!
- *
- * This function is also called by the public split() function located in
- * List-given.hpp
- *
- * @param start The node to start from.
- * @param splitPoint The number of steps to walk before splitting.
- * @return The starting node of the sequence that was split off.
- */
- template <typename T>
- typename List<T>::ListNode * List<T>::split(ListNode * start, int splitPoint) {
- /// @todo Graded in MP3.1
- if(start==NULL)
- {
- return NULL;
- }
- if(splitPoint<1)
- {
- return start;
- }
- if(length_<splitPoint)
- {
- NULL;
- }
- ListNode * curr = start;
- for (int i = 0; i < splitPoint-1 && curr != NULL; i++) {
- if(curr->next!=NULL)
- curr = curr->next;
- }
- if (curr != NULL) {
- curr=curr->next;
- curr->prev->next = NULL;
- curr->prev = NULL;
- tail_=curr->prev;
- return curr;
- }
- return NULL;
- }
- /**
- * Modifies the List using the waterfall algorithm.
- * Every other node (starting from the second one) is removed from the
- * List, but appended at the back, becoming the new tail. This continues
- * until the next thing to be removed is either the tail (**not necessarily
- * the original tail!**) or NULL. You may **NOT** allocate new ListNodes.
- * Note that since the tail should be continuously updated, some nodes will
- * be moved more than once.
- */
- template <typename T>
- void List<T>::waterfall() {
- /// @todo Graded in MP3.1
- if(head_==NULL ||head_->next==NULL)
- {
- return;
- }
- ListNode * temp = head_;
- ListNode * temp2 = temp->next;
- int a = 0;
- while(temp->next->next!=NULL && temp->next!=NULL)
- {
- temp=temp->next;
- temp->prev->next=temp->next;
- temp->next->prev =temp->prev;
- temp2 = temp->next;
- tail_->next = temp;
- temp->prev = tail_;
- temp->next = NULL;
- tail_ = temp;
- temp= temp2;
- }
- }
- /**
- * Reverses the current List.
- */
- template <typename T>
- void List<T>::reverse() {
- reverse(head_, tail_);
- }
- /**
- * Helper function to reverse a sequence of linked memory inside a List,
- * starting at startPoint and ending at endPoint. You are responsible for
- * updating startPoint and endPoint to point to the new starting and ending
- * points of the rearranged sequence of linked memory in question.
- *
- * @param startPoint A pointer reference to the first node in the sequence
- * to be reversed.
- * @param endPoint A pointer reference to the last node in the sequence to
- * be reversed.
- */
- template <typename T>
- void List<T>::reverse(ListNode *& startPoint, ListNode *& endPoint) {
- /// @todo Graded in MP3.2
- }
- /**
- * Reverses blocks of size n in the current List. You should use your
- * reverse( ListNode * &, ListNode * & ) helper function in this method!
- *
- * @param n The size of the blocks in the List to be reversed.
- */
- template <typename T>
- void List<T>::reverseNth(int n) {
- /// @todo Graded in MP3.2
- }
- /**
- * Merges the given sorted list into the current sorted list.
- *
- * @param otherList List to be merged into the current list.
- */
- template <typename T>
- void List<T>::mergeWith(List<T> & otherList) {
- // set up the current list
- head_ = merge(head_, otherList.head_);
- tail_ = head_;
- // make sure there is a node in the new list
- if (tail_ != NULL) {
- while (tail_->next != NULL)
- tail_ = tail_->next;
- }
- length_ = length_ + otherList.length_;
- // empty out the parameter list
- otherList.head_ = NULL;
- otherList.tail_ = NULL;
- otherList.length_ = 0;
- }
- /**
- * Helper function to merge two **sorted** and **independent** sequences of
- * linked memory. The result should be a single sequence that is itself
- * sorted.
- *
- * This function **SHOULD NOT** create **ANY** new List objects.
- *
- * @param first The starting node of the first sequence.
- * @param second The starting node of the second sequence.
- * @return The starting node of the resulting, sorted sequence.
- */
- template <typename T>
- typename List<T>::ListNode * List<T>::merge(ListNode * first, ListNode* second) {
- /// @todo Graded in MP3.2
- return NULL;
- }
- /**
- * Sorts a chain of linked memory given a start node and a size.
- * This is the recursive helper for the Mergesort algorithm (i.e., this is
- * the divide-and-conquer step).
- *
- * Called by the public sort function in List-given.hpp
- *
- * @param start Starting point of the chain.
- * @param chainLength Size of the chain to be sorted.
- * @return A pointer to the beginning of the now sorted chain.
- */
- template <typename T>
- typename List<T>::ListNode* List<T>::mergesort(ListNode * start, int chainLength) {
- /// @todo Graded in MP3.2
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement