Professional Documents
Culture Documents
ELEE28706D DataStructure S4
ELEE28706D DataStructure S4
Data Structures
Classifications:
- Linear (arrays, linked lists)
- Nonlinear (trees, graphs)
Arrays:
- Data is often available in tabular form
- Arrays are used to represent tabular data
- Matrix represents tabular date and can be represented
as a 2-dimensional array!
Data Structures
Arrays: Properties
- All elements of the array of the same type (int, double)
- Array is a random access data structure
- It is a static data structure. Once defined, can’t be
changed
- Access time for an array element is constant ( O(1))
- Array is perfect when fewer number of
insertions/deletions are required
- Array is suitable when lots of searching, and retrieval are
required.
Data Structures
Arrays: Parameters
Code Snippet
Data Structures
Code Snippet
Data Structures
Array Lists
- While collecting inputs, we don’t have idea how
many?
- In that case, an array list could be a solution:
Array Lists
- How to declare and use?
ArrayList <String> myfriends = new ArrayList<String>();
myFriends.add(“John”); An array list
String myfriend = myFriends.get(i); object of size 0
myFriends.set(i, Garry);
Array Lists
myFriends.add(“Georgre”);
myFriends.add(“Mustaque”);
myFriends.add(“Robi”); String name =
myFriends.get(2);?
myFriends
George
Mustaque
Robi
Data Structures
Array Lists
myFriends.add(1, “John”);
myFriends.remove(1);
Draw the
arrayList
(myFriends)
Data Structures
Array Lists
- There is no ArrayList<int> or ArrayList<double> (primitive data
types!)
- But way out, using Wrapper class!
- Example:
Code Snippet
Data Structures
Linked Lists
Data Structures
Linked Lists
- Linear collection of data elements (nodes) using
pointers
- Having two parts
Information field
Link field (next pointer field), contains the address
of the next node in the list
Data Structures
Deletion: Any
- To delete a node that holds a particular value x in a
linked list, we use
A pointer cur to point to this node and
Another pointer, prev, to point to the previous node
Data Structures
Deletion: Any
- Step 1: Initialize pointer cur to point to the first node (Head),
while pointer prev points to NULL
- Step 2: Traverse the entire list until pointer cur points to node
that contains value x, and prev points to the previous node
Data Structures
Deletion: Any
- Step 3: link the node pointed to by pointer prev to the node
after the cur’s node
- Step 4: Remove the node pointed to by cur
Data Structures