4.2 Data Structures Flashcards
Static vs Dynamic Data Structures
- when size is determined
- storage
- static: storage size determined before program is run (fixed size)
- dynamic: can change size during run time
- static: can waste storage space is number of data items is small compared to size
- dynamic: only take up amount of storage space required
- dynamic: need memory to store pointer to next items
Enumerated (definition)
In the form of a list
Arrays (2)
- elements must be of the same data type
- objects are mutable
Lists (3)
- elements can be of different data types
- objects are mutable
- is a dynamic data structure
Tuples (3)
- elements can be of different data types
- objects are immutable
- more efficient than lists since are immutable
Text Files can only store
Strings
Advantage of Text Files
They can be created, altered or prepared independently from the program
Binary Files (3)
- hard for human to understand
- are directly compatible with the computer
- tend to be more secure as without correct definitions would be difficult to convert data into meaningful information
CSV Files (4)
- comma separated files
- type of text file used to store a set of records
- each record is stored on a single line
- each field in the record is separated by a comma
Types of Access of Files
- read
- write
- append
- read binary file
- write to binary file
- r = read
- w = write (overwrites entire file)
- a = append
- rb = read binary file
- wb = write to binary file
Queues
- Acronym
- Number of Pointers
- FIFO (First In First Out)
- 2 pointers: rear and front
Linear Queues
- type of structure
- queue = empty if …
- disadvantage
- static data structure
- queue = empty if front > rear
- dis: limited amount of space
Circular Queues
- definition
- what pointers store
- queue which has a fixed amount of space but where the end of the queue is connected to the front
- front points to element of array which should be removed next
- rear points to last element added
Priority Queues (2 + Enqueue)
- when elements are assigned they are ordered according to some rule
- each item is assigned a priority as it is added to the queue
- enqueue: same as linear queue, except value is inserted into correct position according to a given priority
Enqueue Algorithm
- check if full (if rear + 1 = front)
- if not full
- increment rear by 1
- add item at the location indicated by the value of rear pointer
Push Function + Algorithm
- adds data pushed to top of stack
- check isFull()
- if not full
- increment top pointer by 1
- insert data at location indicated by the value of the top pointer
Stack
- type of structure
- acronym
- no. pointers
- static data structure
- LIFO (Last In First Out)
- 1 pointer: top
Dequeue Algorithm
- check if empty (if front > rear)
- if not empty
- get item at location indicated by front pointer
- increment value of front pointer by 1
- return item
Pop Function + Algorithm
- removes data at top of stack and returns it
- check isEmpty()
- if not empty
- get value at location indicated by the value of top pointer
- reduce top pointer by 1
- return value
Peek Function + Algorithm
- returns value at the top of stack without removing it
- return stackarray[top]
Test for Empty Stack AKA + Algorithm
- known as stack underflow
- if top == -1
- return True
Test for Full Stack AKA + Algorithm
- known as stack overflow
- if top == max
- return True
Method to Reverse the Order of a List Using a Stack
- push all elements in list onto stack
- for each element in stack:
- perform pop function
- store value returned in next available space in a list
Method to Reverse the Order of a Queue Using a Stack
- elements of queue are dequeued from queue
- elements are then pushed onto stack
- elements are popped off stack
- elements are enqueued to queue