Understanding Data Structures in Java

cop3804 intermediate java n.w
1 / 10
Embed
Share

"Learn about linked lists, a fundamental data structure in Java, and how to implement your own linked list class. Explore the differences between ArrayList and LinkedList and when to use each for efficient data manipulation."

  • Java
  • Data Structures
  • Linked Lists
  • ArrayList
  • LinkedList

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COP3804 - INTERMEDIATE JAVA Data Structures

  2. Data Structures A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A list, in particular, is a collection that stores its elements in a position-based sequence. The memory allocation for the list elements may be contiguous allocation or linked allocation. ArrayList is an example of a data structure that uses contiguous allocation whereas LinkedList is an example of a data structure that uses linked allocation.

  3. Linked Lists A linked list is a data structure that consists of a number of nodes; each node has an object (the data we are storing) and a reference to the next node in the list. They allow for elements to be efficiently removed and added from the middle of the list since the rest of the elements in the list don t need to be moved (as in arrays), only the neighboring node references need to be updated. Visiting all the elements of a linked list in sequential order is efficient but accessing the object stored at an arbitrary location is not. Let s say you only want to access the object stored at position 5, you would first need to iterate through the first 4 elements in the list.

  4. Linked Lists In an application where a lot of elements need to be added or removed at any position in a list, a LinkedList would be a more efficient data structure to use than an ArrayList. For applications where the main operation is accessing elements at a specific location, an Arraylist would perform better.

  5. Linked Lists The Java library provides the java.util.LinkedList class, which has methods to access the first and last elements in the list: getFirst() and getLast() It also provides methods to add and remove the first and last elements: addFirst() addLast() removeFirst() removeLast()

  6. Implementing Linked Lists For assignment 3, you have to create your own implementation of a linked list class based on the one provided in the textbook. We called it ItemLinkedList. The purpose of this exercise is for you to have a better understanding of the inner workings of a linked list. The ItemLinkedList class has fewer methods than the java.util.LinkedList and the data type of the object stored inside each node is Item.

  7. Inner classes The implementation of the linked list class in the textbook, as well as the implementation of the ItemLinkedList class, make use of inner classes. Since the Node class is only used by the ItemLinkedList class, it was declared inside the ItemLinkedList class as a private inner class. (It cannot be used by classes outside of the ItemLinkedList class). The advantage of inner classes is that they have access to the private members (variables, methods, other inner classes) of the outer class. Although, in our implementation, the Node class is not accessing the private members of the ItemLinkedList class, which is the first instance variable.

  8. Stacks and Queues Stacks and queues are other data structures that allow inserting and removing items at the ends of the list only, not the middle. A stack is a collection of items with a last in, first out retrieval concept, known as LIFO. It lets you insert and remove elements at only one end, the top of the stack. You may think of it as a stack of plates at a buffet restaurant. Stacks can be used by any system that needs to process the elements in the reverse order that they were added to the list. A stack class normally provides the following methods: push - to add an element at the top of the stack pop - to remove a element from the top of the stack peek - to look at the element stored at the top of the stack without removing it. empty to check if there are elements in the stack

  9. Stacks and Queues Queues are similar to stacks but you add elements at the end of the queue and remove elements from the other end, which is called the head (the beginning) of the queue. Queues store items in a first in, first out fashion, also known as FIFO. Queues can be used by any system that needs to process elements in the same order that they were added to the list. For example, the list of print jobs for a network printer. A queue class normally provides the following methods: enqueue to add an element at the tail of the queue dequeue to remove the element at the head of the queue. peek to get the element at the head of the queue without removing it. empty to check if there are elements in the queue.

  10. References Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2nd ed. Boston, USA: Addison-Wesley, 2012 Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., 2010.

More Related Content