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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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....
View Full Document

Page1 / 3

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online