Cpsc 221 data structures algorithms linkedlist class

Info icon This preview shows pages 3–8. Sign up to view the full content.

CPSC 221 Data Structures & Algorithms LinkedList Class class LinkedList { protected : ListNode * head, * tail; public : LinkedList() : head(NULL), tail(NULL) { } ~LinkedList(); ListNode * getHead() const { return head; } bool isEmpty() const { return head == NULL; } int first() const throw (EmptyLinkedListException); void insertFirst( int newobj); int removeFirst() throw (EmptyLinkedListException); void insertLast( int newobj); friend ostream& operator <<(ostream& out, const LinkedList &ll); }; c circlecopyrt Teresa Leyk Slide 3 Linked Lists
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CPSC 221 Data Structures & Algorithms LinkedList Class Implementation void LinkedList::insertFirst( int newobj) { ListNode * newNode = new ListNode(newobj); if (head == NULL) tail = newNode; newNode->next = head; head = newNode; } Stack Heap newNode head c circlecopyrt Teresa Leyk Slide 4 Linked Lists
Image of page 4
CPSC 221 Data Structures & Algorithms void LinkedList::insertLast( int newobj) { ListNode * newNode = new ListNode(newobj); if (head == NULL) head = tail = newNode; else { tail->next = newNode; tail = newNode; } } head newNode tail c circlecopyrt Teresa Leyk Slide 5 Linked Lists
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CPSC 221 Data Structures & Algorithms int LinkedList::removeFirst() throw (EmptyLinkedListException) { if (head == NULL) throw EmptyLinkedListException("Empty Linked List"); ListNode * node = head; head = node->next; if (head == NULL) tail = NULL; int obj = node->obj; delete node; return obj; } head tail deleted c circlecopyrt Teresa Leyk Slide 6 Linked Lists
Image of page 6
CPSC 221 Data Structures & Algorithms
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern