Download presentation
Presentation is loading. Please wait.
Published byVivien Freeman Modified over 8 years ago
1
15-121 Advanced Programming 7/10/20161 Ananda Gunawardena
2
7/10/20162
3
List Interface A list can be defined as an ordered collection In Java, several classes can be implemented using List interface ArrayList, LinkedList, Vector Most programming languages provide constructs for creating and managing lists Array lists Array’s are static lists size is fixed at compile time Can resize, but requires effort 7/10/20163
4
Fundamentals of a Dynamic List Many applications require data structures that can be changed easily For example, inserting an element to a static array is painful, in fact it is O(n) So think of a data structure that supports inserts and deletions in constant time or O(1) 7/10/20164
5
Linked Lists Linked Lists can be built dynamically Basic building of a dynamic linked list is a node A linked list is a collection of nodes, each having a “reference” to the next node 7/10/20165
6
Connecting Nodes A collection of nodes is a list A collection of these nodes connected to each other is called a Linked List 7/10/20166
7
References Each node holds a reference (or address) of the next node 7/10/20167 FFAAOO Address: FFAAOO data??data
8
Head of the List Head of the list is the “entry” point to the list Head of a list is a reference to first node 7/10/20168 FFAAOOdata??data head
9
Linked List Nodes A linked list node object can be generated from a class Minimally a node contains two things Data object A reference 7/10/20169
10
Implementation 7/10/201610
11
Code // Class node public class Node { public Comparable data; public Node next; …… } public class MyLinkedList implements List { Node head; ……… } 7/10/201611 Interface List MyLinkedList implements
12
Inserting to a LL Cases to consider List is empty List is not empty 7/10/201612 prev curr newNode
13
Deleting from a LL Cases to consider List is empty Delete the node after prev 7/10/201613 prevcurr
14
Circular Linked List 7/10/201614 head
15
Inserting to a Circular LL Draw a figure to show how this is done 7/10/201615
16
Deleting from a circular LL Draw a figure to show how this is done 7/10/201616
17
7/10/201617
18
Application of LL Polynomial representation 7/10/201618
19
Cloning a List 7/10/201619
20
Homework (not graded) Write the following methods of MyLinkedList Class public void reverse(MyLinkedList L); public MyLinkedList findDups(MyLinkedList L); Return a list that contains the duplicate elements (one each) of the original list (do not change the original) public void makeCircular(MyLinkedList L); Given a singly LL, make it circular public void print(MyLinkedList L); Prints the LL sequentially Tomorrow – Types of LL’s and their applications 7/10/201620
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.