Docsity

Data Structures and Algorithms: Linear Data Structures and ArrayList Implementation, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and Algorithms: Linear Data Structures and ArrayList Implementation and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithms

LECTURE 02: LINEAR DATA STRUCTURES

• Dynamic Arrays

• Nodes

• Stacks

• Queues

• Linked Lists

Contents

• Supported operations and complexity:

– size() , isEmplty() , get() , set() – O(1)

– add() – the operation runs in amortized constant time

– adding n elements requires O(n) time

– all of the other operations like: add(int index, E

element) , contains() , indexOf(), remove(int index)

etc ., run in linear time O(n) (roughly speaking)

ArrayList - Operations

• Implemented using an array

• Adding new item requires new array

• This approach will copy all the elements for each add

operation – O(n)

ArrayList – Add O(n)

5

size = 4

add( 21 )

size = 5

Out of space

• Create an ArrayList data structure, which supports

– boolean add (T element)

– E get (int index)

– E set (int index, E elements)

– E remove (int index)

– int size ()

– int indexOf (E element)

– etc…

Problem: ArrayList

7

  • Constructor and fields: ArrayList – Constructor And Fields 8 public class ArrayList implements List { private static final int DEFAULT_CAPACITY = 4; private Object[] elements; private int size; public ArrayList() { this.elements = new Object[DEFAULT_CAPACITY]; } }

• Returns an element at index:

ArrayList – Get

10 public E get(int index) { checkIndex(index); return this.getElement(index); } private E getElement(int index) { return (E) this.elements[index]; }

• Sets an element at index:

ArrayList – Set

11 public E set(int index, E element) { checkIndex(index); E oldElement = this.getElement(index); this.elements[index] = element; return oldElement; }

  • Helper methods to sustain correct array behaviour: ArrayList – Grow and Shrink 13 private Object[] grow() { return Arrays.copyOf(this.elements, this.elements.length * 2); } private Object[] shrink() { return Arrays.copyOf(this.elements, this.elements.length / 2); }
  • indexOf (E element) – returns the zero based index of an element or - 1
  • contains (E element) – returns whether an element is present
  • size () – returns the number of elements
  • toArray () – returns the elements as an array
  • Implement other operations – think about the complexity ArrayList – Other Operations

• Many data structures use node chaining

Node - Application

16 public class LinkedList implements Deque { private Node head; }

head

element

next

Node 1

element

next

Noden- 1

element

next

null

• Create a class Node , that has:

– E element

– Node next

– Constructor

Problem: Node

17 private static class Node { private E element; private Node next; public Node(E value) { this.element = value; } }

• Supported operations and complexity:

– size (), isEmplty (), push (), pop (), peek () – O(1)

– all of the other operations run in linear time (roughly speaking):

• forEach ()

• contains ()

• etc…

Stack - Operations

  • Constructor and fields: Stack – Constructor And Fields 20 public class Stack implements AbstractStack { private Node top; private int size; private static class Node {...} // Node class public Stack() { } }