linked_list_class.cpp - #include using namespace...

This preview shows page 1 - 3 out of 3 pages.

#include <iostream> using namespace std; class linked_list { private: struct Node { int value; Node *next; Node(int value, Node* next = nullptr) { this->value = value; this->next = next; } }; //this pointer points to the first node Node *head; public: linked_list() { head = nullptr; } //create nodes and insert them in order void add(int value) { //this are like temps Node *prev, *current; // if the void list is empty or the value neing added is the lowest, we will then add node if (head == nullptr || head ->value > value) { head = new Node(value, head); } else { //find an insertion point - traverse the list until we get to either the end or we hit a value that is greater prev = head; current = head->next; while (current != nullptr && current->value < value) { prev = prev->next; current = current->next; } //insert node right before current prev->next = new Node(value, current); } } //display the linked list void display() { Node *ptr = head; while (ptr != nullptr) { cout << ptr->value << endl; ptr = ptr->next; }
Image of page 1

Subscribe to view the full document.

cout << endl; } //conduct a linerar search to find the element to remove from the list
Image of page 2
Image of page 3
  • Spring '15
  • Guillermo Flores

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern