LinkedList - Linked List A linked list is a collection of...

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Linked List A linked list is a collection of components called nodes . In the C++ terminology, a linked list is classified as a container . In Java, it is called a collection . Every node (except the last node) contains the address of the next node. A node has two components, the data (or info) and the link (or next) . data link The address of the first node in the list is stored in a separate pointer variable usually called head , first , or list . The null pointer (NULL, physical value 0 in C/C++) is used to denote the end of the list, or not a valid address . Example: a linked list of 4 integers. 45 65 34 76 head NULL In typical C++ implementations, we shall define the node using struct , and the linked list is defined as a class . struct node { int info; node *link; // another commonly used var name is next }; node *head;
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Example program codes that operate on a linked list // To find the length of the linked list int len = 0; node *cur = head; //traverse the list using cur while (cur != NULL) // cur points to a valid node { len++; cur = cur->link; //move to the next node } // Insert a new element x at the front of the list node *p = new node; // create the node for storing x p->info = x; p->link = head; // step 1 head = p; // step 2 step 1: 45 65 34 76 head NULL x p (step 1) step 2: 45 65 34 76 head NULL x p (step 2)
Image of page 2
3 // Remove the element x (1 st instance) from the linked list // To remove a node from the linked list, we need to // know the reference to its predecessor. node *cur = head; node *prev = NULL; // prev points to the predecessor of cur while (cur != NULL && cur->info != x) { prev = cur; cur = cur->link; } // if cur == NULL, x is not found in the linked list if (cur != NULL) // cur->info == x { if (prev != NULL) prev->link = cur->link; else // cur is the first node in the list head = cur->link; delete cur; //free the storage of the removed node }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 1. Locate the node storing the value x, e.g. x = 34 45 65 34 76 head NULL cur prev 2. Update the links 45 65 34 76 head NULL cur prev 3. Physically delete the node 45 65 34 76 head NULL cur prev Structure of the list after removing the element 34 45 65 76 head NULL
Image of page 4
5 // Insert a new element x into an ordered list node *p = new node; p->info = x; if (head == NULL || x <= head->info) { p->link = head; //insert at front head = p; } else //head != NULL && x > head->info { node *prev = head; node *cur = head->link; while (cur != NULL && x > cur->info) { prev = cur; cur = cur->link; } // insert node p after node prev p->link = prev->link; prev->link = p; } Remark: Null-pointer exception is a common error in programs that manipulate linked list. A pointer must be properly initialized or tested for not equal to NULL before you can use it to access a data member or the next node.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Implementing linked list as a class in C++ Operations that we would perform on a linked list: - Initialize the list. - Clear the list. - Determine if the list is empty. - Print the list. - Find the length of the list. - Make a copy of the list, e.g. assignment operator= and the copy constructor .
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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