/* Finding the element by value */ SinglyLinkedList* find(int value) { SinglyLinkedList *currentElement = this; while(currentElement) { if(currentElement->getValue() == value) break; currentElement = currentElement->getNext(); } return currentElement; }
int main() { cout << "Testing class SinglyLinkedList" <<endl; SinglyLinkedList *list1 = new SinglyLinkedList(10); cout << "Data in the list1 is " << list1->getValue() <<endl; SinglyLinkedList *list2 = new SinglyLinkedList(30); cout << "Data in the list2 is " << list2->getValue() <<endl; SinglyLinkedList *list3 = new SinglyLinkedList(50); cout << "Data in the list3 is " << list3->getValue() <<endl; cout << "Making chain of list" <<endl; list1->setNext(list2); list2->setNext(list3); cout << "value list 1: " << list1->getValue() <<endl ; cout << "value list 2: " << list1->getNext()->getValue() <<endl ; cout << "value list 3: " << list1->getNext()->getNext()->getValue() <<endl; cout << "==============" << endl; cout <<"Adding value in front of the list" <<endl; SinglyLinkedList::insertInFront(&list1 , 55); cout << "value list 1: " << list1->getValue() <<endl ; cout << "value list 2: " << list1->getNext()->getValue() <<endl ; cout << "value list 3: " << list1->getNext()->getNext()->getValue() <<endl; cout << "value list 4: " << list1->getNext()->getNext()->getNext()->getValue() <<endl; cout << "==============" << endl; SinglyLinkedList *foundElement = list1->find(50); cout << "found element , value is: "<<foundElement->getValue() <<endl; }