Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // IntList.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //Got help from Raeed Shaikh, stack overflow, geeksforgeeks, past cs12 projects, and a past CS14 student
- #include "IntList.h"
- #include <iostream>
- using namespace std;
- IntList::IntList() : dummyHead(new IntNode(0)), dummyTail(new IntNode(0)) {
- dummyHead->next = dummyTail;
- dummyTail->next = dummyHead;
- dummyHead->prev = dummyTail;
- dummyTail->prev = dummyHead;
- }
- IntList::~IntList() {
- IntNode* curr = dummyHead;
- while (curr != dummyTail) {
- IntNode* temp = curr;
- curr = curr->next;
- delete temp;
- }
- delete curr;
- }
- void IntList::push_front(int value) {
- IntNode* front= new IntNode(value);
- IntNode* headNext= dummyHead->next;
- front->prev = dummyHead;
- front->next = headNext;
- dummyHead->next = front;
- headNext->prev = front;
- }
- void IntList::pop_front() {
- if (!empty()) {
- IntNode* headNext = dummyHead->next;
- IntNode* dubNext = headNext->next;
- dummyHead->next = dubNext;
- dubNext->prev = dummyHead;
- delete headNext;
- }
- }
- void IntList::push_back(int value) { //same code as push_front(), just with dummyTail??
- IntNode* tail = new IntNode(value);
- IntNode* tailNext = dummyTail->prev;
- tail->prev = tailNext;
- tail->next = dummyTail;
- dummyTail->prev = tail;
- tailNext->next = tail;
- }
- void IntList::pop_back() { //same code as pop_front(), just with dummyTail??
- if (!empty()) {
- IntNode* tailPrev = dummyTail->prev;
- //IntNode* tail = tailPrev->prev;
- dummyTail->prev = tailPrev->prev;
- tailPrev->prev->next = dummyTail;
- delete tailPrev;
- }
- }
- bool IntList::empty() const {
- if (dummyHead->next == dummyTail) {
- return true;
- }
- else {
- return false;
- }
- }
- void IntList::printReverse() const {
- IntNode* temp = dummyTail->prev;
- if (!empty()) {
- while (temp->prev != dummyHead) {
- cout << temp->data << " ";
- temp = temp->prev;
- }
- cout << temp->data;
- }
- }
- ostream& operator<<(ostream& out, const IntList& rhs) {
- IntNode* headNext = rhs.dummyHead->next;
- if (rhs.empty()) {
- return out;
- }
- else {
- while (headNext->next != rhs.dummyTail) {
- out << headNext->data << " ";
- headNext = headNext->next;
- }
- out << headNext->data;
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement