Download presentation
Presentation is loading. Please wait.
Published byGordon Nash Modified over 8 years ago
1
List data type(ADT)
2
Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion 3.Search 4.Print 5.Isempty
3
List An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
4
Contd.. List allows duplicate elements. The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation. LIST interface is a member of the Java Collections Framework.
5
List interface An interface is shown, but the implementation details are hidden. This implementation could be using arrays or linked list. The various classes that implements list AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector
6
Implementing list using arrays Simple implementation is Arrays Size =Max 0 1 2...N j A
7
..... 0 1 2 3... N j A Insert(x,j) { For(k=size-1,k>j;k--) { A[k+1]=A[k]; } A[j]=x; Size++; } Inserting
8
Search Two types of Search functions 1.Find the element and return position. 1.Eg find(x); return the element x postion in the list 2.Find the element of given position. 1.Eg. Find_k th (p) which return k th element
9
Algorithm for find(x) find (x){ if (a[I]==x) print(i) /* Print the position of the element found */ }..... 0 1 2...N j A Searching
10
Delete(x) { Pos=findpos(x); For(k=pos,k<size;k++) A[k]=a[k+1]; Size--; } Deleting x..... 0 1 2...N Pos A
11
Whenever we insert an element we increase size, i.e. size++; Whenever we Delete an element we decrease size by one. i.e. size--; Size we are taking should not exceed Max_size
12
List(ADT) operations. add(); Appends an element to the end of the list. Add(int index,E element); inserts the element at the specified position in the list. addAll(Collection c); Append all of the elements in the collection to the end of the list. addAll(int index, collection c); Inserts all the elements in the collection in to the list starting from the specified index. Clear(); Removes all the elements from the list. Contains (Object o); Retuns true if the list contains specified element. containsAll(collection c); Returns true if the list contains all of the elements in the collection.
13
Contd… equals(object e); Compares specified object with list for equality. get(index e); Returns the element at the specified index. hashCode(); Returns the hashcode for the list. indexOf(Object o); Returns the index of first occurrence of the element o, returns -1 if not present. isEmpty(); Returns true if the list contains no elements. Remove(int index); Removes the element at the specified index); removeAll(Collection c); Remove all the elements from the list that are contained in the collection. toArray(); Returns the array of elements of the list.
14
Array List(ADT) Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.
15
Contd.. Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.