Linked Lists - Linked Lists Linked lists can contain...

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

View Full Document Right Arrow Icon
Linked Lists Linked lists can contain elements of the specified type. They are contained in nodes, and nodes are connected via links so that they have an ordering, see Fig. 1. ... head tail Figure 1: A linked list At first we are going to deal with singly-linked list, the simplest linked list. The nodes are small objects that contains a reference to an element, and a reference to the next node. A list contains only a reference to the head node, the first node of the list: class Node<E> { E iElement; Node<E> iNext; } public class LinkedList<E> { private Node<E> iHead; } If we are going to add an element in the list, what should we do? The first thing is to make a new node, put the reference of the element on the node, and set the next reference of the node to direct the first node of the original list, and set the head of the list into the new node. void addFirst(E el) { Node n = new Node(); n.iElement = el; n.iNext = iHead; iHead = n; } This is our first implementation. Isn’t this easy? To remove an element, we would change the head of the list into the next reference of the first node. void removeFirst() { iHead = iHead.iNext } Just one line!
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
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