Linear Data structures
--
commonly used linear data structures.
🔭Types of Data Structures:-
Data structures are divided into two categories:
- Linear data structures
- Non-linear data structures
Linear data structures:-
In linear data structures, elements are arranged in sequence one after the other.
Array:-
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together.
int[] intArray = new int[20];
For dynamic use of arrays, we can use ArrayList:-
ArrayList<Integer> arrli = new ArrayList<Integer>();
//EmptyArrayListArrayList<Integer> arrc = new ArrayList<Integer>(C);
//ArrayList with C collectionsArrayList<Integer> arrli = new ArrayList<Integer>();
//EmptyArrayList with int n capacity//common used functions
arrli.add(5);
arrli.remove(5);
arrli.set(index,element);//replaces the element
arrli.set(index,element);//inserts the element
Stack:-
It works in the LIFO(last in first out) principle. I use to remember this structure like placing all C.D’s one by one in the holder while searching we need to take out last in C.D. (OR) Pack of tennis balls.
Stack stack = new Stack(); // can push any datatype
Stack<String> stack1 = new Stack<String>(); // only String😁
//operations
stack.push(2);// adding element 2
stack.push(3);// adding element 3
stack.pop();// returns top element and removes it---3
stack.peek();//returns top element---2
stack.search(2);//returns index of element---0
Queue:-
It works in the FIFO(First in first out) principle. I use to remember this structure using the movie tickets queue, bus…