Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstdio>
- #include <iostream>
- using namespace std;
- #include "IntLinkedList.h"
- IntLinkedList::IntLinkedList()
- {
- head = NULL;
- tail = NULL;
- // add the initialization for the tail pointer
- }
- /* addHead
- * add an item to the front of the linked list
- *
- * TODO: Add code that modifies the tail properly
- */
- void IntLinkedList::addHead(int v)
- {
- // create a new IntNode and put v into that IntNode
- IntNode *l = new IntNode(v);
- // if the list is empty, this link is the only thing in it
- // all we need to do is point head to the link
- if (head == NULL)
- {
- head = l;
- tail = l;
- }
- // if there is already something there, we point this new link
- // to the old head of the list. Then, when we point head to this
- // IntNode, it becomes the beginning of the list, and the old beginning
- // is now 2nd.
- else
- {
- l->setNext(head);
- head = l;
- }
- }
- /* addTail
- *
- * Add an item to the end (tail) of the list
- * TODO: Make sure tail is updated properly AND make the code more
- * efficient by taking advantage of the tail pointer
- */
- void IntLinkedList::addTail(int v)
- {
- // make a new link for the new item
- IntNode *l = new IntNode(v);
- // if the list is empty, this is the only one
- if (head == NULL)
- {
- head = l;
- }
- // otherwise, we need to go to the back and add the node
- else
- {
- // TODO: Modify this so it takes advantage of tail pointer
- // advance l2 so it points to the last link
- IntNode *l2;
- for(l2 = head;l2->getNext() != NULL; l2 = l2->getNext())
- ;
- // now add this l to the end by hooking l2 to it
- l2->setNext(l);
- tail = l2;
- }
- }
- /* removeHead
- *
- * Removes the item at the head of the list
- * TODO: Update the tail pointer
- */
- bool IntLinkedList::removeHead()
- {
- // if the list is empty, return 0
- if (head == NULL)
- return false;
- // set a temporary pointer to head
- IntNode *tmp = head;
- // advance head so it points to the next one
- head = head->getNext();
- // delete the IntNode
- if(tmp->getNext() == NULL)
- tail = NULL;
- delete tmp;
- // return the value
- return true;
- }
- /* removeTail
- * removes the last (tail) item from a linked list
- * Returns true if it removed something, false if the list was empty
- * TODO: Write the code for this method.
- * hint: Be very careful about where you stop your loop
- */
- bool IntLinkedList::removeTail()
- {
- IntNode *tmp = head;
- if(head == NULL)
- return false;
- if(tail ==NULL)
- return false;
- if(tmp->getNext() ==NULL)
- {
- tail = NULL;
- head = NULL;
- delete tmp;
- return true;
- }
- else
- {
- while(tmp->getNext()!=tail)
- {
- tmp = tmp->getNext();
- }
- delete tail;
- tail = tmp;
- return true;
- }
- }
- /* peekHead
- * return the item at the head of the list
- * If the list is empty, return 0
- */
- int IntLinkedList::peekHead()
- {
- // TODO: fill in this code
- if(head == NULL)
- return 0;
- else
- {
- IntNode *tmp = head;
- return tmp->getValue();
- }
- }
- /* peekTail
- * return the item at the end (tail) of the list
- * If the list is empty, return 0
- * TODO: Implement this function. Make sure you take advantage of the tail ptr
- */
- int IntLinkedList::peekTail()
- {
- // TODO: fill in this code
- if(head == NULL)
- return 0;
- else
- {
- IntNode *tmp = tail;
- return tmp->getValue();
- }
- }
- /* printList
- *
- * print all of the items in the linked list
- */
- void IntLinkedList::printList(FILE *f)
- {
- IntNode *l;
- fprintf(f,"List: ");
- for(l = head; l != NULL; l = l->getNext())
- {
- l->printIntNode(f);
- fprintf(f, ", ");
- }
- fprintf(f,"\n");
- }
- IntLinkedList::~IntLinkedList()
- {
- IntNode *temp = this->head;
- while(temp!=NULL)
- {
- IntNode *temp1 = temp;
- temp = temp->getNext();
- delete temp1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement