Comprehensive Study Notes on Linear and Non-Linear Data Structures: Arrays and Stacks
Fundamentals of Data Structures
Purpose and Importance of Data Organization:
- Working with large quantities of data requires effective organization; lack of effective organization makes performing any task or operation on that data exceptionally difficult.
- When data is organized effectively, any operation can be executed with ease.
Definition of Data Structure:
- A data structure is a method of organizing a large amount of data more efficiently so that any operation on that data becomes easy.
Fundamental Principles of Data Structures:
- Every data structure is utilized specifically to organize large amounts of data.
- Every data structure adheres to a particular governing principle.
- Operations performed within a data structure must never violate its foundational principle.
Classification of Data Structures: Linear vs. Non-Linear
Basis of Classification:
- Data structures are categorized into two primary types based entirely on their method of organization: Linear Data Structures and Non-Linear Data Structures.
Linear Data Structures:
- Definition: A data structure that organizes data elements in a sequential or linear order.
- Arrangement: Data is arranged sequentially in a linear sequence.
- Traversal: Data items can be traversed completely in a single run.
- Implementation: Implementation is comparatively easy.
- Examples:
- Arrays
- Stack
- Queue
- List (Linked List)
Non-Linear Data Structures:
- Definition: A data structure that organizes data in a random order rather than a sequential order.
- Arrangement: Data is not arranged in a sequence.
- Traversal: Data cannot be traversed in a single run.
- Implementation: Implementation is difficult.
- Examples:
- Tree
- Graph
- Heaps, etc.
Comparative Summary:
- Sequence: Linear structures maintain a strict linear sequence, whereas non-linear structures do not arrange data sequentially.
- Traversal Passes: Linear items require only a single run for traversal, whereas non-linear items cannot be traversed in one single run.
- Complexity of Implementation: Linear data structures feature easy implementation, while non-linear data structures exhibit difficult implementation.
- Type Exemplars: Linear includes Array, Stacks, Linked list, and Queue; Non-linear includes Tree, Graph, and Heaps.
Criteria for Selecting a Suitable Data Structure
- Selection Framework:
- Choosing the appropriate data structure for a given problem requires evaluating four specific operational criteria:
- Data size and required memory: Evaluating the total volume of data elements and the corresponding physical memory overhead needed for storage.
- Dynamic nature of the data: Assessing whether the data collection is static or subject to runtime expansions, shrinkages, and structural modifications.
- Retrieval time to access any data item: Determining the required speed and access latency to search, locate, or read individual elements.
- Programming techniques: Aligning data organization with the chosen algorithms, paradigms, and coding methodologies.
Arrays in C Programming
Motivation and Program Complexity:
- When programs handle large numbers of values, using separate variables for each value requires an excessive number of distinct variable identifiers.
- As the count of individual variables increases, program complexity escalates, causing programmer confusion regarding variable names.
- The C programming language provides the "Array" construct to efficiently manage collections of large numbers of similar data values.
Formal Definitions of an Array:
- Definition 1: An array is a variable that can store multiple values of the same data type simultaneously.
- Definition 2: A collection of similar data items stored in continuous memory locations under a single name.
Memory Allocation for Independent Variables vs. Arrays:
- Independent Variable Allocation:
- Consider the declaration:
int a, b, c; - The compiler allocates of memory named
'a', another named'b', and an additional named'c'. - These three memory locations may be allocated in sequence or may not be in sequence across memory.
- Each independent variable holds only one value at a time.
- If assigned as
a = 10;,b = 20;, andc = 30;, the memory stores the integers , , and across three isolated blocks that can be scattered non-contiguously. - Array Variable Allocation:
- Consider the declaration:
INT A[3]; - The compiler allocates a total of of strictly continuous memory locations referenced under the single name
'a'. - It allows storing three different integer values simultaneously, each occupying of memory.
Indexing and Element Identification:
- Because all continuous memory cells share the identical identifier
'a', individual elements are distinguished using a compiler-assigned numerical value termed the "Index". - Syntax for Individual Element Reference:
ARRAYNAME[INDEXVALUE]- Index Mapping for
INT A[3]: - Index references element
a[0]. - Index references element
a[1]. - Index references element
a[2]. - Value Assignment Example:
- Executing
A[1] = 100;assigns the integer directly to the memory address corresponding to index . - Resulting state across indices:
- Index :
a[0](unassigned/empty) - Index :
a[1]contains - Index :
a[2](unassigned/empty)
- Index :
- Because all continuous memory cells share the identical identifier
Stacks: Definition and Array Representation
Definition of a Stack:
- A stack is a linear list of homogeneous elements in which all insertions and deletions are restricted to take place at only one end, termed the "top".
- Because of this structural constraint, a stack is defined as a Last-In-First-Out (LIFO) list.
Structural Example:
- Given a stack , defined across positions :
- Element is positioned at the bottom of the stack.
- Elements , , , and follow sequentially upwards.
- Element is positioned at the top of the stack.
Stack Representation Using an Array:
- The simplest approach to representing a stack is through a standard array.
- One fixed boundary of the array serves as the permanent bottom of the stack, while the opposite moving boundary serves as the top.
- The execution logic must continuously track the active position of the top element.
- Standard Declaration:
Datatype stackName [size];- Initialization of the Top Pointer:
int top = -1;
Stack Operations and Control Logic
Stack Full Check (
Stack Full):- Purpose: Verifies whether the array holding the stack has reached maximum capacity.
- Dependency: Evaluates the current value of the variable
top. - Timing: Must always be checked prior to executing a
Pushoperation. - Condition:
if top == size - 1 then stackfull
Stack Empty Check (
Stack Empty):- Purpose: Verifies whether the stack contains zero elements.
- Dependency: Evaluates the current value of the variable
top. - Timing: Must always be checked prior to executing a
Popoperation. - Condition:
if top == -1 then stackempty
Push Operation:
- Definition: Inserts a new element onto the stack at the top position.
- Algorithmic Procedure:
- Step 1: Check whether the stack is full by evaluating
top == size - 1. - Step 2 (Overflow Branch): If it is full, display
"Stack is FULL!!! Insertion is not possible!!!"and terminate the function immediately. - Step 3 (Insertion Branch): If it is not full, increment the top pointer by one (
top++), and assign the value to that position usingstack[top] = value.
Pop Operation:
- Definition: Removes an element from the stack, with the element always being deleted from the current top position.
- Algorithmic Procedure:
- Step 1: Check whether the stack is empty by evaluating
top == -1. - Step 2 (Underflow Branch): If it is empty, display
"Stack is EMPTY!!! Deletion is not possible!!!"and terminate the function immediately. - Step 3 (Deletion Branch): If it is not empty, delete the element located at
stack[top]and decrement the top pointer by one (top--).