DSA - Arrays, Linked Lists & ADTs Flashcards
Define an Array
Data Structure stored as contiguous list of cells OR sequences of bytes in memory.
What does INDEXING mean?
Giving each element in the array a value, first cell with have index 0 and final cell will have index ((length of array) -1 ).
What is a TYPE?
Type refers to the DATA TYPE of the Data Structure.
OR
A set of Possible values (ADTs)
What is the time access any element in the array?
Constant Time regardless of the index
Code to create ARRAY
int[ ] nums = new int [4]
IN int[ ] nums = new int [4] what is nums?
The array name OR identifier
IN int[ ] nums = new int [4] what does int[] do?
Declares the datatype of Arrays
IN int[ ] nums = new int [4] what does new do?
Allocates memory for the Array
IN int[ ] nums = new int [4] what does 4 represent?
Declares how many elements are in our Array
How to get values from Array?
DT val = nums[0],
where DT = Datatype
In Datatype val = nums[0] what does DT val represent?
Initialises the variable value and assigns the datatype
In Datatype val = nums[0] what does num[0]?
nums[0] declares the array and which index value we want
How to Assign Values in Arrays?
nums[i] = 23 , where i is an index in the Array
How to find Length of the Array?
len = length(nums)
Why is it important to use length of the Array?
Important when using if statements OR loops to find values in the Arrays or add items to an array.
How much memory does an integer occupy?
4 bytes
How do you calculate OFFSETS?
Index of Element * NO. bytes for Data type
List ADTs using ARRAYS
List ADTs and arrays are more sophisticated than Arrays, however tend to use basic Arrays when implementing them;
ADTs lists have more complex operations i.e- list size, sort list, concatenating list, etc.
Memory Management using Java:
- Memory allocation is automatic
- Freeing memory is automatic (Using garbage collector)
- Bounds of arrays are checked (Check if array is out of range)
- Slow & Safe
Memory Management using C/C++
&
What does Explicit mean
1- Allocation are explicit
2- Freeing memory is explicit
3- Bounds are not checked
- Fast & Dangerous
Explicit 1) Need to allocate space & include Data Type
2) Must free up allocated space otherwise causes error and Data will never get removed.
Common Mistakes in Array
int[ ] nums = new int [4]
int[4] is an error
The list has no index 4, the range of the index is 0-3
Java will return an error but in C/C++ will lead to corruption of data in memory
Define What a Linked List is:
- Data structure in which the objects are ordered linearly
- Order determined by the pointer in each object (NODE)
- Collection of NODES connected to each other
What is a Singly Linked List ?
Each element has a next pointer NO prev pointer
(CAN ONLY POINT TO NEXT NODE)
What is Doubly Linked List?
Can point to prev node as well as next node
(CAN BE CIRCULAR OR NOT DEPENDS IF FIRST NODE POINTS TO LAST)
What is a Sorted Linked List?
Linear order of list corresponds to linear order of key stored in elements of list
What is Not Sorted Linked List?
Elements appear in Any order
What is a Circular Linked List?
All nodes can point to prev & next node, First points to last node for prev node and last points to First node for next pointer
What is a Non Circular Linked List?
Can Be anything else but a circular Linked List