DSA Week 4 Spring 2022
DSA Week 4 Spring 2022
Ordered by position
Everyday examples:
Grocery lists
Stacks of dinner plates
A line of customers waiting at a bank
“Data structure” and “concrete data type” refer to the internal representation of an ADT’s data
The two data structures most often used to implement collections in most programming languages are arrays
and linked structures
Different approaches to storing and accessing data in the computer’s memory
Different space/time trade-offs in the algorithms that manipulate the collections
In general, the logical and physical size tell us important things about the state of the array:
If the logical size is 0, the array is empty
Otherwise, at any given time, the index of the last item in the array is the logical size minus 1.
If the logical size equals the physical size, there is no more room for data in the array
These operations would be used to define methods for collections that contain arrays
To achieve more reasonable time performance, double array size each time you increase its size:
This operation occurs in Python’s list when a pop results in memory wasted beyond a threshold
Steps:
Create a new, smaller array
Copy the data from the old array to the new array
Reset the old array variable to the new array object
Steps:
Shift items from target index position to logical end of array up by one
To close hole left by removed item at target index
In addition to the double subscript, a grid must recognize two methods that return the number of rows and the
number of columns
Examples: getHeight and getWidth
The techniques for manipulating one-dimensional arrays are easily extended to grids:
After a grid has been created, we can reset its cells to any values:
The method __getitem__ is all that is needed to support the client’s use of the double subscript:
In a ragged grid, there are a fixed number of rows, but the number of columns in
each row can vary
An array of lists or arrays provides a suitable structure for implementing a ragged
grid
Dimensions can be added to grid definition if needed; the only limit is computer’s
memory
A three-dimensional array can be visualized as a box filled with smaller boxes stacked in
rows and columns
Depth, height, and width; three indexes; three loops
After arrays, linked structures are probably the most commonly used data structures in programs
Like an array, a linked structure is a concrete data type that is used to implement many types of collections,
including lists
We discuss in detail several characteristics that programmers must keep in mind when using linked structures to
implement any type of collection
An empty
link
A linked structure decouples logical sequence of items in the structure from any ordering in memory
Noncontiguous memory representation scheme
The basic unit of representation in a linked structure is a node:
Depending on the language, you can set up nodes to use noncontiguous memory in several ways:
Using two parallel arrays
In the discussion that follows, we use the terms link, pointer, and reference interchangeably
Note that when the data are displayed, they appear in the reverse order of their insertion
Removing an item at the end of an array (pop in a Python list) requires constant time and memory
Unless the array must be resized
Removing at the end of a singly linked structure must consider two cases:
The removal of the ith item from a linked structure has three cases:
The main advantage of singly linked structure over array is not time performance but memory performance