#include #include #include "intSLinkedList.h" using namespace std; intSLinkedList::intSLinkedList(): head(NULL), tail(NULL) { } intSLinkedList::intSLinkedList(const intSLinkedList& other){ head = NULL; intSNode* cursor1 = head, *cursor2 = other.head; while (cursor2!=NULL){ if (head==NULL){ head = new intSNode; head->elem = cursor2->elem; head->next = NULL; cursor1 = head; } else{ cursor1->next = new intSNode; cursor1->next->elem = cursor2->elem; cursor1->next->next = NULL; cursor1 = cursor1->next; } cursor2 = cursor2->next; } } bool intSLinkedList::empty() const{ return head == NULL; } const int& intSLinkedList::front() const{ return head->elem; } intSLinkedList::~intSLinkedList(){ while (!empty()) removeFront(); } void intSLinkedList::addFront(const int& e) {// add to front of list intSNode* v = new intSNode; // create new node v->elem = e; // store data v->next = head; // head now follows v head = v; // v is now the head if (head->next==NULL) tail = head; } void intSLinkedList::addBack(const int& e) {// add to front of list intSNode* v = new intSNode; // create new node v->elem = e; v->next = NULL; // store data tail->next = v; // head now follows v tail = v; } void intSLinkedList::removeFront() { // remove front item intSNode* old = head; // save current head head = old->next; // skip over old head delete old; // delete the old head } void intSLinkedList::print() { intSNode* v = head; while (v != NULL){ cout << v->elem << " "; v = v->next; } cout << endl; } int intSLinkedList::count() { intSNode* v = head; int n = 0; while (v != NULL){ n++; v = v->next; } return n; }