Sie sind auf Seite 1von 4

#include "stdafx.

h" #include "cmath" #include "iostream" using namespace std;

class Node { public: int data; Node *next; int getValue() {return data;} Node* getNext() {return next;} }; class List{ private: int count; Node* pHead; public: List() {pHead=NULL; count = 0;} void setHead(Node* node) {pHead = node;} Node* getHead() {return pHead;} void addFirst(int newdata) { Node* pTemp = new Node; pTemp->data = newdata; pTemp->next = pHead; pHead = pTemp; count++; } int sum(Node* node) { if (node == NULL) return 0; else if(node->getNext() == NULL) return node->getValue(); else return (node->getValue() + sum(node->getNext())); } int sumOdd(Node* node) { if (node == NULL) return 0; if (node->getValue()%2==1) return node->getValue() + sum(node->getNext()); return sum(node->getNext()); } int addFirstIfPerfectCubic (int n) { if ((n==pow((int)pow(n,(float)1/3),3))&&(n>0)) { addFirst(n); return 1; } else return 0; } void reverseList() { if (pHead==NULL || pHead->next==NULL) return; Node* pTemp = pHead->next; Node* pRev; pHead->next = NULL; while (pTemp!=NULL) { pRev = pTemp; pTemp = pTemp->next; pRev->next = pHead; pHead = pRev; } } void addLast (int n) { if (pHead!=NULL) reverseList();

addFirst(n); reverseList(); } int countIncreLists() { if (pHead == NULL) return 0; if (pHead->next == NULL) return 1; Node *pTemp = pHead, *pPrev = pHead; int count = 0; bool singleIncreLists = false; while (pTemp->next != NULL) { if (pTemp->data >= pTemp->next->data) { if (pPrev == NULL) singleIncreLists = true; else count++; pPrev = NULL; } else pPrev = pTemp; pTemp = pTemp->next; } pPrev = NULL; if (singleIncreLists) return ++count; return count; } void merge(Node* node1, Node* node2) { sortList(node1); sortList(node2); Node* auxNode = node2; while (auxNode !=NULL ) { auxNode = auxNode->getNext(); addLast(INT_MAX); } auxNode = node1; while (node1!=NULL && node2!=NULL) { if (node1->getValue() > node2->getValue()) { tradeData(node1->data, node2->data); setToRightPos(node2); } node1 = node1->getNext(); } node1 = auxNode; } void tradeData(int& data1, int& data2) { data1 = data2 - data1; data2 = data2 - data1; data1 = data2 + data1; } void setToRightPos(Node* node) { if (node == NULL) return; Node * auxNode = node->getNext(); while (auxNode != NULL) { if (node->getValue() > auxNode->getValue()) tradeData(node->data, auxNode->data); auxNode = auxNode->getNext(); } } void clearList() { Node* pTemp = pHead; while (pTemp!=NULL) { pTemp = pTemp->next; if (pTemp != NULL) delete pHead; pHead = pTemp; } } void displayPagesList(Node* node) {

sortList(node); deleteDuplicate(node); while (node != NULL) { if ( node->getNext()!=NULL && node->getNext()->getNext()!=NULL && node->getValue()+1==node->getNext()->getValue() && node->getNext()->getValue()+1 ==node->getNext()->getNext()->getValue() ) { cout << node->getValue() << "-"; node = node->getNext()->getNext(); while ( node->getNext()!=NULL && node->getValue()+1==node->getNext()->getValue() ) node = node->getNext(); cout << node->getValue() << " "; } else cout <<node->getValue() << " "; node = node->getNext(); } } void displayReverse(Node* node) { if (node->getNext() != NULL) displayReverse(node->getNext()); cout << node->data << " "; } void display() { Node *pTemp = pHead; while (pTemp!=NULL) { cout << pTemp->data << " "; pTemp = pTemp->next; } } ~List() { Node* pTemp = pHead; while (pTemp!=NULL) { pTemp = pTemp->next; if (pTemp != NULL) delete pHead; pHead = pTemp; } } void sortList(Node* node) { if (node == NULL) return; int count = -1; Node* auxNode = node; while (auxNode != NULL) { auxNode = auxNode->getNext(); count++; } auxNode = node; for (int i=0; i<count; i++) { node = auxNode; while (node->getNext() != NULL) { if (node->getValue() > node->getNext()->getValue()) tradeData(node->data, node->getNext()->data); node = node->getNext(); } } } void deleteDuplicate(Node *node) { Node *auxNode, *savedNode = node; while (node!=NULL && node->getNext()!=NULL) { if (node->getValue() == node->getNext()->getValue()) { auxNode = node->getNext(); node->next = node->getNext()->getNext();

delete auxNode; auxNode = NULL; } else node = node->getNext(); } node = savedNode; } }; List* buildNegLinkedList() { List *pList = new List; int valid=1; char choice; int num; while (valid) { cout << "Do you want to enter a number? (Y/N):"; cin >> choice; if ((choice == 'y') || (choice == 'Y')) { cin >> num; if (num < 0) pList->addFirst(num); } else valid = 0; } return pList; } List *buildOddLinkedList() { List *pList = new List; int valid=1; char choice; int num; while (valid) { cout << "Do you want to enter a number? (Y/N):"; cin >> choice; if ((choice == 'y') || (choice == 'Y')) { cin >> num; if (abs(num)%2 == 1) pList->addFirst(num); } else valid = 0; } return pList; } void main() { //TO DO ADD YOUR CODE HERE. THE CODE IS IN ACCODE WITH LAB1 CODE REQUEST cout << endl; system("pause"); }

Das könnte Ihnen auch gefallen