Arrays and Linked Lists - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Arrays and Linked Lists

Description:

An array is a group of contiguous memory locations all having the same ... for ( int col = 0; col exampleArray[row].length; col ) exampleArray[row][col] = 0; ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 35
Provided by: Jim455
Category:
Tags: arrays | col | linked | lists

less

Transcript and Presenter's Notes

Title: Arrays and Linked Lists


1
Arrays and Linked Lists
  • MINS 116
  • Spring 2000
  • Data Structures

2
Topics
  • Arrays
  • Single Linked Lists
  • Double Linked Lists
  • Array implementation of Single Linked List
  • Array implementation of Double Linked List

3
Arrays
  • What is an Array?
  • - An array is a group of contiguous memory
    locations all having the same name and the same
    type.

4
Arrays - One Dimensional
  • Example
  • int exampleArray // Declare
    the array
  • exampleArray new int5 // Create the array
  • These two lines of Java code create a contiguous
    memory area containing space for 5 integers.

5
Arrays - One Dimensional
  • Example (contd)

6
Arrays - One Dimensional
  • Example (contd) Notice the index starts at 0.
    The last index is 4. Array size is 5.

7
Arrays - One Dimensional
  • To place an item in we simply assign a value to
    the array name at a certain index.
  • E.g.
  • exampleArray3 13 // put 13 into the
  • // array at
    index 3.

8
Arrays - One Dimensional
  • exampleArray3 13 // put 13 into the
  • // array at
    index 3.
  • We end up with

9
Arrays - One Dimensional
  • What will the following for loop produce?
  • for ( int i 0 i lt 5 i )
  • exampleArrayi 5-i

10
Arrays - One Dimensional
  • To copy a value out of the array, we simply make
    an assignment from the array at the desired array
    index.
  • E.g.
  • int arrayValue
  • arrayValue exampleArray3
  • Assigns the value stored in array index location
    3 to variable arrayValue.

11
Arrays - One Dimensional
  • Given the array loading loop
  • for ( int i 0 i lt 5 i )
  • exampleArrayi 5-i
  • What is the result of
  • int arrayValue
  • arrayValue exampleArray3

12
Arrays - One Dimensional
  • Arrays in Java are special. An allocated Java
    array will always know its length.
  • For exampleArray, what will be the result of
  • int a exampleArray.length
  • ?

13
Arrays - One Dimensional
  • A Java Vector is essentially a one dimensional
    array that can grow dynamically.
  • (Hmmmmm sounds a bit like a linked list)

14
Arrays - Two Dimensional
  • A two dimensional array is just what it sounds
    like. It is an array which exists in two
    dimensional space.
  • E.g. exampleArray -

15
Arrays - Two Dimensional
  • Example
  • int exampleArray //
    Declare the array
  • exampleArray new int32 // Create the
    array
  • These two lines of Java code create a contiguous
    memory area containing space for 6 integers. Note
    Java is row-by-column (just remember RC cola).

16
Arrays - Two Dimensional
  • Essentially, a 2 dimensional array is just two
    one dimensional arrays stuck together.
  • int exampleArray //
    Declare the array
  • exampleArray new int3 // Create the
    rows
  • exampleArray0 new int2 // Create column
    1
  • exampleArray1 new int2 // Create column
    2
  • exampleArray3 new int2 // Create column
    3

17
Arrays - Two Dimensional
  • Write a Java loop which will traverse the array
    exampleArray defined above.

18
Arrays - N Dimensional
  • Java Arrays can be extended to N dimensions.
  • Example
  • int exampleArray
    // Declare the array
  • exampleArray new int3253 // Create
    the array

19
Arrays - Initialization
  • Arrays can be initialized in loops
  • e.g.
  • for ( int row 0 row lt exampleArray.length
    row )
  • for ( int col 0 col lt exampleArrayrow.leng
    th col)
  • exampleArrayrowcol 0

20
Arrays - Initialization
  • Arrays can also be initialized upon declaration.
  • // Declare and create the array
  • int exampleArray 5, 7, 9, 3, 1, 11
  • What value is stored in exampleArray21?

21
Linked List
  • The problem with arrays is they dont grow
    dynamically.
  • What happens if you have written a program with
    an array of size 100 to store inventory items
    when the store you are working for adds item
    101?

22
Linked List
  • Linked Lists provide
  • Dynamic allocation of memory space.
  • List size limited only by physical machine
    characteristics (i.e. physical RAM).
  • Dynamic manipulation of information.
  • Lower overhead for insert.
  • Lower overhead for delete.

23
Singly Linked List
  • Conceptually, a linked list looks like

24
Singly Linked List
  • The previous diagram depicts a list with 4 items
    of information in it.
  • Where
  • List - where all the work and manipulation is
    done.
  • Node - the area that contains the information
    being stored.
  • Head - points to the head (start) of the list.
  • Tail - points to the end (tail) of the list.
  • Next - points to the next node on the list.
  • currLoc - the current location in list traversal.
  • prevLoc - the previous location in list traversal.

25
Singly Linked List - Adding an item
  • Assume the new node is called addNode
  • 1) To the end of a linked list
  • tail.next addNode
  • tail addNode
  • 2) To the center of a linked list
  • walk till currLoc points at node following
    addNode
  • prevLoc.next addNode
  • addNode.next currLoc
  • Q What is the state of currLoc and prevLoc after
    this operation?

26
Singly Linked List - Adding an item
  • 3) To the front of a linked list
  • tempNode head
  • head addNode
  • head.next tempNode
  • 4) First item in a linked list
  • head addNode
  • tail addNode
  • Do these four cases cover all necessary
    operations for adding items to a linked list?

27
Singly Linked List - Removing an item.
  • Assume the node to remove is called removeNode
  • 1) From anywhere in a linked list
  • walk to currLoc for node to delete
  • removeNode currLoc
  • prevLoc.next currLoc.next
  • Q What about head and tail? Are these special
    conditions?

28
Singly Linked List - An array based implementation
  • An array can be used to implement a linked list.
  • What are the elements needed for a single linked
    list?
  • What size of array is needed?
  • What do the cells of the array represent?

29
Doubly Linked List
  • A Doubly Linked List is like a Singly Linked
    List, however, each node has a next pointer and a
    previous pointer.

30
Doubly Linked List
31
Doubly Linked List - Inserting an item
  • Assume the new node is called addNode
  • 1) To the end of a linked list
  • tail.next addNode
  • addNode.prev tail
  • tail addNode
  • 2) To the center of a linked list
  • walk till currLoc points at node following
    addNode
  • addNode.prev currLoc.prev
  • currLoc.prev.next addNode
  • currLoc.prev addNode
  • addNode.next currLoc

32
Doubly Linked List - Inserting an item
  • 3) To the front of a linked list
  • tempNode head
  • head addNode
  • tempNode.prev addNode
  • head.next tempNode
  • 4) First item in a linked list
  • head addNode
  • tail addNode
  • Do these four cases cover all necessary
    operations for adding items to a doubly linked
    list?

33
Doubly Linked List - Removing an item
  • Assume the node to remove is called removeNode
  • 1) From anywhere in a linked list
  • walk to currLoc for node to delete
  • removeNode currLoc
  • currLoc.prev.next currLoc.next
  • currLoc.next.prev currLoc.prev
  • Q What about head and tail? Are these special
    conditions?

34
Summary
  • Arrays are contiguous memory used to store values
    of the same type.
  • Arrays have had bounds.
  • Linked Lists are used to store values in a
    collection which can be expanded indefinitely
    (dependent on physical computing
    characteristics).
  • Java contains special classes to cover all types
    of linked list based operations.
Write a Comment
User Comments (0)
About PowerShow.com