122 Midterm Theory Notes
122 Midterm Theory Notes
Process:
A sequence of operations performed to achieve a goal, do not have to terminate
Examples: Living and breathing
• Process represents higher complexity of work
• Process specification may contain ambiguity
• Process may be a continually ongoing activit
Malloc and Calloc both return NULL when su9icient memory is not available in Heap
Constructor
A special kind of class member function, automatically called when an object of the class
is instantiated, thus must be public.
• If no constructor is defined by the user, the system will create a default constructor.
DiSerence from other regular member functions:
1. They must have the same name as the class.
2. They cannot return a value; not even void
Advantages:
1. Initialize some or all member variables of an object.
2. Simply the procedure.
3. Add code to validate the entered initial values.
4. Other actions, e.g., print to system log when an object is instantiated
Deconstructor
Automatically called when an object of the class is deallocated.
• If the memory is statically allocated, destructor is called when the object’s scope is
closed/ended.
• If the memory is dynamically allocated, destructor is called when explicitly delete.
• Destructor must be named the same as the class, just with a ~ (tilde) sign preceding its
name.
Lecture 4 & 5 : Linked Lists & Sequential List
Abstract Data Types
• With ADT only necessary information is shown externally to the user while the
underlying details are hidden.
Big O notation
• describes the complexity of your code using algebraic terms.
• The running time/number of operations that’s independent of hardware, programming
language, etc
List ADT
A container structure for storing a connected sequence of data items
• A linked list allows for fast insertions and deletions of nodes anywhere in the list,
while a sequential list can only cheaply add or remove elements at the end of the
list.
• A linked list requires more memory for storing pointers, while a sequential list uses
less memory as no pointers are needed.
• A linked list can have variable length and structure, while a sequential list has a
fixed size and structure.
• A linked list can be used for implementing more complex data structures like trees
and graphs, while a sequential list can be used for simple data structures like stacks
and queues.
• A linked list does not support random access to nodes, while a sequential list
supports random access to elements.
Lecture 7: Introduction to Recursion
Recursion in coding: a function in your code calls itself as part of its own execution.
Is recursion more eSicient than iteration? àNO
Base case: The problem reaches its ending point when there is a trivial answer to the
problem.
Stack Frame:
Is a section of the stack dedicated to a particular function call.
• The stack is a region of memory that supports push and pop operations and is used to
store data in a Last-In-First-Out (LIFO) manner.
When a function is called:
• The stack allocates memory to store the function’s variables
• Those variables are stored in frame→ this is called a stack frame
Stack Overflow:
Excessively deep or infinite recursion, in which a function calls itself so many times that the
space needed to store the variables and information associated with each call is more than
can fit on the stack.
4. , rather than
diSerences within the
same order
Lecture 10: Algorithm Analysis Big-O Notation Cont.
The strategy is to count the number of operations that are performed in the worse-case as n
approaches infinity (asymptotic analysis):
1. In general no loop, means constants steps (not related to input size) →O(1)
2. In general one loop is repeated (an+ b) in the worse-case, where the constants (a,b) are
for the segments that are repeated in each iteration →O(n)
3. In general for k nested loops each loop is repeated (an+ b) in the worse-case→O(n^k)
4. In general if you have a combination of operations the highest order dominates (e.g.: O
(𝑛^2)+ O (log(𝑛))+ O (𝑛^1.6)+O(𝑛) →O(𝑛^2))
Lecture 11: Stack ADT
A list of data items where items are inserted and, removed based on the last-in first-out
(LIFO) principle.
• That is, you can only insert and remove an item from one end of the stack, which is
called the “top” of the stack.
• The item that you remove from the top of the stack will always be the last item that was
inserted into the stack
• Only the index of the top element is needed.
Push Operations:
Just like à Insert First (insert first element) in linked List
Input: data value to insert in the list
1. Create a new node to hold the value to insert
2. Check for representative cases; such as if the stack is full
3. If stack is not full
a. start at the top if its empty –insert the node as the top
b. if top is not empty –insert the new node at the top
c. increase the size of the stack
Pop Operations:
DeleteFirst (insert first element) and return the deleted node or value from the linked List
Input:N/A Output: data value or node
1. Check for representative cases, such as if the stack is empty and return some dummy
node or value
2. If stack not empty
a) Start at the top create a new node to hold the current top node
b) Make the next node the new top node
c) Delete and free the old node
d) Decrease the size of the stack
e) Return the value of the deleted node or the node itself
Peek Operation
Just like SelectFirst (select first element) and return the top node value from the linked List
Input: N/A | Output: top node data value
1. check for marginal cases –such as if the stack is empty or top is NULL and return some
dummy node or value.
2. If stack not empty just return the top node value
Linked Queue: representation is a linked list where each node in the linked list holds an
item and a pointer to the next node. It also has a pointer to the front node (head pinter) and
a pointer to the back node (tail pointer).
Refer to VS code for general syntax of Dequeue and Enqueue Operations, and Circular
Queue Array Operations.