Bharati Vidyapeeth (Deemed University) Kharghar,Navi mumbai JAVA DATA STRUCTURES AND ALGORTITHMS Presentation By Name : Ishaan Suresh Roll no : 31 Branch : Cse ( Aiml ) CONTENTS Introduction Arrays Stack Queue Interface INTRODUCTION What is data structure with application INTRODUCTION The term data structure refers to a collection of data with well-defined operations, behaviour, or properties. A data structure is a special manner of storing or organising data in computer memory so that we can use it effectively. 4 5 APPLICATIONS Data structures provide numerous benefits to IT-related processes, particularly as applications become more complex and the amount of data available grows. The following are some of the reasons why data structures are so important. Efficiency: If the choice of a data structure for implementing a particular ADT is proper, it makes the program very efficient in terms of time and space. Reusability: The data structure provides reusability means that multiple client programs can use the data structure Abstraction: The data structure specified by an ADT also provides the level of abstraction. The client cannot see the internal working of the data structure, so it does not have to worry about the implementation part. The client can only see the interface. ARRAYS What is Array with Example WHAT IS ARRAY Normally, an array is a collection of similar type of elements which has contiguous memory location. Java array is an object which contains elements of a similar data type. Additionally, The elements of an array are stored in a contiguous memory location. It is a data structure where we store similar elements. We can store only a fixed set of elements in a Java array. 7 EXAMPLE OF ARRAY Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on. In Java, array is an object of a dynamically generated class. Java array inherits the Object class, and implements the Serializable as well as Cloneable interfaces. We can store primitive values or objects in an array in Java. Like C/C++, we can also create single dimentional or multidimentional arrays in Java. 8 PROGRAM 9 STACK What is Stack with Example and types WHAT IS STACK The stack is a linear data structure that is used to store the collection of objects. It is based on LastIn-First-Out (LIFO). Java collection framework provides many interfaces and classes to store the collection of objects. One of them is the Stack class that provides different operations such as push, pop, search, etc. 11 EXAMPLE OF STACK The stack data structure has the two most important operations that are push and pop. The push operation inserts an element into the stack and pop operation removes an element from the top of the stack. Let's see how they work on stack 12 OPERATIONS Performing various operations on Stack class – 1. Adding Elements: In order to add an element to the stack, we can use the push() method. This push() operation place the element at the top of the stack. 13 OPERATIONS 2. Removing Elements: To pop an element from the stack, we can use the pop() method. The element is popped from the top of the stack and is removed from the same. 14 OPERATIONS 3. Accessing the Element: To retrieve or fetch the first element of the Stack or the element present at the top of the Stack, we can use peek() method. The element retrieved does not get deleted or removed from the Stack. 15 OPERATIONS 4. Checking Stack: If the stack has no element is known as an empty stack. When the stack is empty the value of the top variable is -1. 16 QUEUE INTERFACE What is Queue with example and operations WHAT IS QUEUE The interface Queue is available in the java.util package and does extend the Collection interface. It is used to keep the elements that are processed in the First In First Out (FIFO) manner. It is an ordered list of objects, where insertion of elements occurs at the end of the list, and removal of elements occur at the beginning of the list. 18 EXAMPLE OF QUEUE The Java Queue interface is a subtype of the Java Collection interface. It represents an ordered sequence of objects just like a Java List, but its intended use is slightly different. Because the Java Queue interface is a subtype of the Java Collection interface, all methods in the Collection interface are also available in the Queue interface. 19 20 OPERATIONS Let’s see how to perform a few frequently used operations on the queue using the Priority Queue class. 1. 2. 3. Adding Elements: In order to add an element in a queue, we can use the add() method. The insertion order is not retained in the PriorityQueue. The elements are stored based on the priority order which is ascending by default. Removing Elements: In order to remove an element from a queue, we can use the remove() method. If there are multiple such objects, then the first occurrence of the object is removed. Apart from that, poll() method is also used to remove the head and return it. Iterating the Queue: There are multiple ways to iterate through the Queue. The most famous way is converting the queue to the array and traversing using the for loop. However, the queue also has an inbuilt iterator which can be used to iterate through the queue. THANK YOU