0% found this document useful (0 votes)
6 views6 pages

Study Material

The document provides an overview of data structures and algorithms, detailing linear and non-linear structures such as arrays, linked lists, stacks, and queues. It explains the characteristics, advantages, and limitations of each data structure, as well as Java's collection framework, including interfaces like List, Set, and Map. Additionally, it covers generics and specific implementations like ArrayList, LinkedList, HashMap, and various types of sets and queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Study Material

The document provides an overview of data structures and algorithms, detailing linear and non-linear structures such as arrays, linked lists, stacks, and queues. It explains the characteristics, advantages, and limitations of each data structure, as well as Java's collection framework, including interfaces like List, Set, and Map. Additionally, it covers generics and specific implementations like ArrayList, LinkedList, HashMap, and various types of sets and queues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structure : storing and organizing of data.

Algorithm : set of steps to solve a problem.

Linear data Structure : elements are arranged in sequentially, each element is


connected to next and previous elements.
- Array, LinkedList, Stack, Queue
Non Linear Data Structure : elements are not arranged sequentially, one element is
connected to as many elements.

Array :
- a linear data Structure
- storing a fixed size of homogeneous data Types.
- easy to access a random element
- insert : from at specified position, need to shift all the elements to right
side and inserting that element at specified position
- delete : need to shift all the elements to left side after removing an element
limitations:
> cant store heterogenious elements
> size is fixed,once declared cant change
> insertion & deletion operations consume more memory as shifting of elements
> shiftingof elemets sometimes may lead to loss of data
> elemets are stored in continuous memory location.
> if required no of elemets / size is not declared then, array will not be
created.

LinkedList:
- it eliminates the disadvanteags of array
- it is a linear data structure used to store collections of objects
- linkedlist is made up of individual nodes which may present in diff locations.
they need not to be contiguous unlike arrays.
- Linked List : connecting of nodes
- nodes are connected to each other. each node containing two parts, one stores
data, another stores data/address of next connected node.
- the first element in linked list is Head,
- last element in linkedlist is tail
- the link of tail node always points to null. it indicating end of the linked
list

Node :
- can be created by class
- dataMember of datatype variable ;
- nextNodeAddress of Node variable;

- a linked list can be represented as a set of nodes connected.


- the data member variable can be of any primitive type or even objects of any
class based on the requirement

LinkedList Syntax:

class LinkedList{
Node head;
Node tail;
}
class Node{
datatype data;
Node next;
}

Stack:
- a linear data structure
- follows LIFO principle ( Last In First Out)
- DSA structure by class:
--> initial top = -1
--> full means : top==maxSize-1
--> empty means : top<0
- push() : to add an element at top/last of the stack
- pop() : to remove most recent added element
- peek() : to see the most recent added element
- isempty() : to check a stack have elements or not
- isFull() : to check a stack have max elements or not
- storing :
int instack = {1,5,8,23,5}

|___5__|
|__23__|
|___8__|
|___5__|
|___1__|

Queue :
- a linear data structure
- follows FIFO principle(First In First Out)
- DSA structure by class:
--> front initial =0;
--> rear initial = -1;
--> full means : rear>=maxSize-1
--> empty means : rear<front
- enqueue() : to add an element at last
- dequeue() : to remove the first most element
- isempty() : to check weather the queue is empty or not
- isfull() : to check weather the queue is full or not
- display() : display elememts from front to rear range
-storing
queue = {1,5,7,3,7};
__________________
front 1 | 5 | 7 | 3 | 7 rear
_________________
Java Collections :

- java has collection framework which provides build in implementations for many
data structures
- a collection is an object that groups multiple elements in single unit.
- collections are used to store, retrieve and manipulate data.
- it's a collection of interfaces & processing data efficiently.
- collection framework standardize the way we store and access the data from
collection and it is part of java.util.packages.

collections : the root of collection hierarchy ( represents group of objects as its


elements)
set : a collection that can't contain duplicates elements
list : an ordered collection (can contain duplicate elements, can access by index)
queue and deque(Deck) : collection used to hold multiple elements prior to
processing.
--> queue : follows by FIFO principle
--> deque : follows both FIFO,LIFO principles. can insert & remove from both
ends

Collecteions
|
|___ Set---> (HashSet,LinkedHashSet,TreeSet)
|
|___List---> (ArrayList,LinkedList,Vector(stack))
|
|___Queue---> (Queue,Dequeue)

Java Collections ( continued..)


MAP:
- Java also has as interface that represents a mapping between a key and values
- Map is collection : allows mapping of key and values as key - value pairs.
- key is an unique element that acts as identifier for its value.

--Benefits:
> ready to use classes and algorithms
> reduce programming effort
> increases program speed and quality
> encourages reusability

Generics :
declaration of Collection :
> public interface Collection <E> ....{}
<E> represents this interface is generic....
- E : Element
- K : Key
- N : Number
- T : Type

ArrayList:
Collection---> List ----> ArrayList
| | |
| | |
interface interface Class

- same like array but size is not fixed


- declaring an arrayList by generics is preferred

internal methods to deal with ArrayList:


- add : can add a value by index, or can add value without index (will add at
end)
- remove : can remove only y index
- get : retrives data by index
- set replaces a new data by index
- clear : removes all elements
- size : to know the no of elements in arrayList
- contains : return true/false if element present in ArrayList
- indexOf : returns first occurrence of element index
- lastIndexOf returns last occurrence o element index
--> listIterator : used to iterate over a list in bidirectional(forward,
backward)
- hasNext
- hasPrevious
- next
- previous

LinkedList :
Collection---> List ----> LinkedList
| | |
| | |
interface interface Class

- declaring an LinkedList by generics is preferred


- by using collection framework some internal methods to deal with linkedLIst is :
- add : can add a value by index, or can add value without index (will add at
end)
- remove : can remove by element. fist occurrence element will be removed
- get : retrieves data by index
- set replaces a new data by index
- clear : removes all elements
- size : to know the no of elements in arrayList
- contains : return true/false if element present in ArrayList
- indexOf : returns first occurrence of element index
- toArray : convers linkedlist to array
--> common in both:
indexOf : returns int -1 if element not present

diff b/w ArrayList & LinkedList about methods :

--> in arrayList can remove an element by index


--> in linkedList can remove an element directly not by index. first occur element
will be removed

Set: removes duplicate elements


------------>internal methods are same like linkedList
HashSet :
- arrangement is not in order & stores unique elements
- uses hashtable to store elemets
- depends on equals(),hashCode() methods to detect duplicates
- null values are allowed

LinkedHashSet :
- arrangement is in order & stores unique elements
- uses combination of hashTables & linkedLists for storing elements
- depends on equals(), hashCode() methods to detect duplicates
- null values are allowed

TreeSet:
- arrangement is in natural order(numerical, alphabetical) & stores unique
elements
- depends on compareTo() methods to detect duplicates
- null values are not allowed

HashMap :
- hashmap is implement of the Map. it doesn't extends collections
- arrangement is not in insertion order
- Key, value pair data
- uses hashTable to store data
- only unoque key data is stored. new key,value is considered
- allows null <key,values>
- some methods are:
-> put(key,value) : to add
-> get(key) : retrieve value of key, returns null if key not present
-> replace(key,oldvalue,newvalue)
-> remove(key) : can remove value by key only , can't remove by value directly
-> isEmpty()
-> size()
-> clear()
-> to traverse : like for loop
---- will use Map.Entry implementation
syntax : for(Map.Entry<Integer,Integer> item : list.entrySet())
--> getKey()
--> getValue()
-> keyset() : returns all key data
syntax: for(Integer i : list.keySet())
-> values() : returns all values data
syntax: for(Integer i : list.values())

Queue Interface:

--> it's an interface


--> it's an ordered list of elements
--> supports all methods of Collection interface bcz it extends collection
interface
--> All queues support insertion at the tail of the queue and removal from the head
of the queue, except Deque
--> Deques are queues but they support element insertion and removal at both ends
--> Priority queues, which order elements according natural ordering

-> Methods are:


> add() / offer()
> remove() / poll()
> element() / peek()

-> as queue is an interface we can use ArrayDeque or PriorityQueue as classes to


create an new Queue
Syntex : Queue

Queue<Integer> list = new ArrayDeque<Integer>();


or
Queue<Integer> list = new PriorityQueue<Integer>()

-> Synatx : Deque ----------> follows LIFO, FIFO principles


Deque<Integer> list = new ArrayDeque<Integer>();

-> methods for Deque :


> add()/offer()/addLast() : adds element at last
> push() /addFirst() : adds element at first
> remove()/poll()/pop()/removeFirst() : removes element at first
> removeLast() : removes element at last
> element() / peek() / peekFirst() : retreives first element
> peekLast() : retrieves last element

You might also like