Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. // IntList.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //Got help from Raeed Shaikh, stack overflow, geeksforgeeks, past cs12 projects, and a past CS14 student
  3. #include "IntList.h"
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. IntList::IntList() : dummyHead(new IntNode(0)), dummyTail(new IntNode(0)) {
  8.     dummyHead->next = dummyTail;
  9.     dummyTail->next = dummyHead;
  10.     dummyHead->prev = dummyTail;
  11.     dummyTail->prev = dummyHead;
  12. }
  13. IntList::~IntList() {
  14.     IntNode* curr = dummyHead;
  15.     while (curr != dummyTail) {
  16.         IntNode* temp = curr;
  17.         curr = curr->next;
  18.         delete temp;
  19.     }
  20.     delete curr;
  21. }
  22.  
  23.  
  24.  
  25. void IntList::push_front(int value) {
  26.     IntNode* front= new IntNode(value);
  27.     IntNode* headNext= dummyHead->next;
  28.     front->prev = dummyHead;
  29.     front->next = headNext;
  30.     dummyHead->next = front;
  31.     headNext->prev = front;
  32. }
  33. void IntList::pop_front() {
  34.     if (!empty()) {
  35.         IntNode* headNext = dummyHead->next;
  36.         IntNode* dubNext = headNext->next;
  37.         dummyHead->next = dubNext;
  38.         dubNext->prev = dummyHead;
  39.         delete headNext;
  40.     }
  41. }
  42. void IntList::push_back(int value) { //same code as push_front(), just with dummyTail??
  43.         IntNode* tail = new IntNode(value);
  44.         IntNode* tailNext = dummyTail->prev;
  45.         tail->prev = tailNext;
  46.         tail->next = dummyTail;
  47.         dummyTail->prev = tail;
  48.         tailNext->next = tail;
  49. }
  50. void IntList::pop_back() { //same code as pop_front(), just with dummyTail??
  51.     if (!empty()) {
  52.         IntNode* tailPrev = dummyTail->prev;
  53.         //IntNode* tail = tailPrev->prev;
  54.         dummyTail->prev = tailPrev->prev;
  55.         tailPrev->prev->next = dummyTail;
  56.         delete tailPrev;
  57.     }
  58. }
  59. bool IntList::empty() const {
  60.     if (dummyHead->next == dummyTail) {
  61.         return true;
  62.     }
  63.     else {
  64.         return false;
  65.     }
  66. }
  67. void IntList::printReverse() const {
  68.     IntNode* temp = dummyTail->prev;
  69.     if (!empty()) {
  70.         while (temp->prev != dummyHead) {
  71.             cout << temp->data << " ";
  72.             temp = temp->prev;
  73.         }
  74.         cout << temp->data;
  75.     }
  76.    
  77. }
  78. ostream& operator<<(ostream& out, const IntList& rhs) {
  79.     IntNode* headNext = rhs.dummyHead->next;
  80.     if (rhs.empty()) {
  81.         return out;
  82.     }
  83.     else {
  84.         while (headNext->next != rhs.dummyTail) {
  85.             out << headNext->data << " ";
  86.             headNext = headNext->next;
  87.         }
  88.         out << headNext->data;
  89.     }
  90.     return out;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement