Download presentation
Presentation is loading. Please wait.
1
Linked Lists [AJ 15]
2
Linked Lists – Revisited
a common way of storing arrays of data can insert or remove elements without reallocating or reorganizing of the entire structure allows insertion and removal of nodes at any point in the list we do not have to specify a fixed size many data structures often implemented using the concept of linked list stacks, queues, associative arrays, etc.
3
Anatomy of a Linked List
a linked list consists of: a sequence of nodes myList a b c d each node contains a value (data) and a link (pointer or reference) to some other node the last node contains a null link the list may have a header
4
An Oversimplified Linked List
a linked list that has a head, no tail, no iterator adds elements to the end of this list a specified element a specified element at a specified position removes the element at the specified position returns the element at the specified position This example is based on MyCSTutorials.com
5
Node Class a a node must contain:
reference to the next node in the chain (or null if there isn't one) data carried by this node, which could be of any type public class Node { Node next; Object data; // constructor section a
6
Node Constructors two constructors: to create a node with no next node
public Node(Object data) { this.next = null; this.data = data; } to create a node with the given data and next node public Node(Object data, Node next) { this.next = next;
7
Node Methods public Object getData() { return this.data; } public void setData(Object data) { this.data = data; public Node getNext() { return this.next; public void setNext(Node next) { this.next = next; returns data of this node set data of this node return the next node set next node to the given node
8
LinkedList Class we need a reference to the head node
public class LinkedList { private Node head; private int listCount; // constructor section // method section }
9
LinkedList Constructor
public LinkedList() { head = new Node(null); listCount = 0; } creates an empty list reference to the head node is set to a new node with no data listCount keeps count of the number of elements in the list
10
LinkedList add Method public void add(Object data) { Node temp = new Node(data); Node current = head; while(current.getNext() != null) current = current.getNext(); } current.setNext(temp); listCount++; adds the specified element to the end of this list start at the head node, then crawl to the end of the list the last node now referring to the new node that we have just added
11
LinkedList add Method public void add(Object data, int index) { Node temp = new Node(data); Node current = head; for(int i =1; i < index && current.getNext() !=null; i++) current = current.getNext(); } temp.setNext(current.getNext()); current.setNext(temp); listCount++; adds the specified element at the specified position crawls to the requested index or the last element in the list the new node's next-node is now referring to this node's next-node the last node now referring to the new node that we have just added
12
LinkedList remove Method
public boolean remove(int index) { if(index < 1 || index > size()) { return false; } Node current = head; for(int i = 1; i < index; i++) { if(current.getNext() == null) { current = current.getNext(); current.setNext(current.getNext().getNext()); listCount--; return true; removes the element at the specified position exit if the index is out of range crawls to the requested index or the last element in the list exit if the last element of the list the current node's next-node is now referring to this node's next-node
13
LinkedList get Method returns the element at the specified position
public Object get(int index) { if(index <= 0) return null; Node current = head.getNext(); for(int i = 1; i < index; i++) { if(current.getNext() == null) { } current = current.getNext(); return current.getData(); returns the element at the specified position index must be at least 1 exit if the last element of the list
14
A Simplified Linked List
a better, yet simplified, LinkedList implements the List interface have to provide an implementation for each method specified in the List interface remember, List interface extends the Iterable interface have to provide an implementation for the single method iterator specified in the Iterable
15
Node Class a a node must contain:
reference to the next node in the chain (or null if there isn't one) data carried by this node, which could be of any type public class Node<T> { private T element; private Node<T> next; // constructor section a
16
Node Constructors three constructors:
1) a node with no element and no next node public Node() { this.element = null; this.next = null; } 2) a node with the given element and next node public Node(T element, Node<T> next) { this.element = element; this.next = next;
17
Node Constructors 3) creates a deep copy of the given node.
public Node(Node<T> copy) { this.element = copy.element; if (copy.next == null) { this.next = null; } else { this.next = new Node<T>(copy.next); if an empty list
18
Node Methods public T getElement() { return this.element; } public Node<T> getNext() { return this.next; public void setNext(Node<T> next) { this.next = next; returns data of this node return the next node set next node to the given node
19
LinkedList Class public class LinkedList<T> implements List<T> { private Node<T> head; private Node<T> tail; // constructors public LinkedList() { this.head = null; this.tail = null; } public LinkedList(Node<T> head, Node<T> tail){ this.head = head; this.tail = tail; PEx09 complete the copy constructor that creates a deep copy of the given linked list creates an empty list create a list with the given node as head
20
LinkedList Methods PEx09
private Node<T> getHead() { return this.head; } PEx09 write a similar function that returns the tail of the list public boolean isEmpty() { return (this.head == null); public Iterator<T> iterator() { return new LinkedListIterator<T>(this.head); return the head of this list test if this list is empty return an iterator for this list
21
LinkedList Methods PEx09 write add methods
public int size() { int size = 0; for(T element : this) { size++; } return size; PEx09 write add methods the specified element to the end of this list use the tail the specified element at the specified position when passed index is zero when passed index is equal to the size or crawls to the requested index return the head of this list
22
LinkedList Methods PEx09 write reverse methods
1) use the head and the tail 2) another reverse method that takes two parameters, a list and a node get element that is stored in that node (position), and also the elements that are stored in following nodes then add these elements in the reversed order to the given list
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.