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 2 bytes2\text{ bytes} of memory named 'a', another 2 bytes2\text{ bytes} named 'b', and an additional 2 bytes2\text{ bytes} 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;, and c = 30;, the memory stores the integers 1010, 2020, and 3030 across three isolated 2-byte2\text{-byte} blocks that can be scattered non-contiguously.
    • Array Variable Allocation:
    • Consider the declaration: INT A[3];
    • The compiler allocates a total of 6 bytes6\text{ bytes} of strictly continuous memory locations referenced under the single name 'a'.
    • It allows storing three different integer values simultaneously, each occupying 2 bytes2\text{ bytes} 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 00 references element a[0].
    • Index 11 references element a[1].
    • Index 22 references element a[2].
    • Value Assignment Example:
    • Executing A[1] = 100; assigns the integer 100100 directly to the memory address corresponding to index 11.
    • Resulting state across indices:
      • Index 00: a[0] (unassigned/empty)
      • Index 11: a[1] contains 100100
      • Index 22: a[2] (unassigned/empty)

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 S=(10,20,30,40,50,60)S = (10, 20, 30, 40, 50, 60), defined across positions (1…6)(1 \dots 6):
    • Element 1010 is positioned at the bottom of the stack.
    • Elements 2020, 3030, 4040, and 5050 follow sequentially upwards.
    • Element 6060 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 Push operation.
    • 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 Pop operation.
    • 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 using stack[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--).