Download presentation
Presentation is loading. Please wait.
1
1 Applications of Collections LinkedList Nodes Declarations Using the LinkedList Collection Introduction to Stacks and Queues
2
2 The LinkedList Class The LinkedList Collection is a better choice in applications requiring frequent insertions and deletions from any place If, on the other hand, there are frequent retrievals, then the ArrayList Collection is a better choice for good performance. A linked list data structure is a collection of nodes storing data and links (references) to other nodes. Two such data structures are singly linked list (SLL) and doubly linked list
3
3 Example 1: Merging Linked Lists import java.util.*; public class InterleaveLinkedLists { public static void main(String[] args) { LinkedList list1 = new LinkedList (); LinkedList list2 = new LinkedList (); for(int i = 20; i <= 30; i++) list1.add(new Integer(i)); for(int i = 1; i <= 7; i++) list2.add(new Integer(i)); System.out.println("list1 is: " + list1); System.out.println("list2 is: " + list2); ListIterator iter1, iter2; for(iter1=list1.listIterator(),iter2=list2.listIterator(); iter2.hasNext();) { if(iter1.hasNext()) iter1.next(); iter1.add(iter2.next()); } System.out.println("The merged list is:"); System.out.println(list1); }
4
4 Example 2: Reversing a Linked List import java.util.*; public class ReversingLinkedList { public static void main(String[] args) { LinkedList c = new LinkedList (); ListIterator iter1,iter2; for(int i = 20; i <= 30; i++) c.add(new Integer(i)); System.out.println("Original : " + c); int j,size = c.size(); for(j=0,iter1 = c.listIterator(size),iter2 = c.listIterator(); iter1.hasPrevious() && iter2.hasNext() && j<size/2;) { exchange(c, c.indexOf(iter1.previous()), c.indexOf(iter2.next())); j++; } System.out.println("Reversed : " + c); } public static void exchange(List a, int i, int j) { Integer temp = a.get(i); a.set(i, a.get(j)); a.set(j, temp); }
5
5 The Stack Collection A stack is a LIFO (Last In First Out) data structure. The last element inserted is the first element to be removed. A stack can be implemented as a linked list in which insertion is always done at the first position (usually called the top) of the stack. Removal is also always done at this position. Usual stack operations: push(), pop(), isEmpty(), top() ( peek() in Java) top
6
6 Using java.util.Stack import java.util.Stack; class TestStack{ public static void main(String args[]){ Stack s = new Stack (); s.push("One"); s.push("Two"); System.out.println("Top: " + s.peek()); s.push("Three"); System.out.println("This is the order in which stack elts are accessed:"); while(!(s.isEmpty())) System.out.println(s.pop()); }
7
7 Example 3: Reversing a Text File import java.io.*; import java.util.Stack; public class ReverseTextFile{ public static void main(String[] args) throws IOException{ Stack stack = new Stack (); BufferedReader inputStream = new BufferedReader(new FileReader("infile.txt")); String inputLine, line; while((inputLine = inputStream.readLine()) != null) stack.push(inputLine); inputStream.close(); PrintWriter outputStream = new PrintWriter(new FileWriter("outfile.txt")); while(! stack.isEmpty()) outputStream.println(stack.pop()); outputStream.close(); System.out.println("The reversal process is complete"); }
8
8 Queues A queue is a FIFO (First In First Out) data structure. A queue can be implemented as a linked list in which insertion (enqueing) is always done at the last queue position (rear), and removal (dequeing) is always done at the first queue position (front). There is no Queue class in the Java API but the ArrayList methods addLast(), getFirst(), removeFirst() and isEmpty() can be used to simulate a queue. rear front
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.