CSCDSC152: Lab on Data Structure

This document provides a comprehensive guide to all programming assignments listed in the CSCDSC152 syllabus. It includes the core logic, algorithms, and implementation tips needed for a practical exam or viva.

Table of Contents

Course Objectives & Outcomes

Course Objectives

The main objectives of this lab are to:

Course Outcomes

Upon successful completion, students will be able to:

A Note on C++ Templates

Many assignments specify using templates. This allows you to write generic code that works with any data type (e.g., int, float, char).

A template class for a Stack would start like this:

template <typename T>
class Stack {
private:
    T arr[100];
    int top;
public:
    void push(T data) {
        // ... logic
    }
    T pop() {
        // ... logic
    }
    // ... other methods
};

When you create an object, you specify the type:

Stack<int> intStack; (A stack for integers)
Stack<float> floatStack; (A stack for floats)

The same logic applies to template functions for sorting or searching.

Section 1: Searching, Sorting & Matrix Assignments

Assignment 1: Linear & Binary Search (Templates)

Task: Write a template program to search an element from a list, offering users the option of Linear or Binary search.

Logic

Assignment 2: Insertion, Bubble & Selection Sort (Templates)

Task: Write a template program (WAP) to sort a list, offering users options for Insertion, Bubble, or Selection sort.

Logic

Assignment 15: Sparse Matrix Conversion

Task: WAP to convert a Sparse Matrix into non-zero form and vice-versa.

Logic

The "non-zero form" (or Triplet Form) stores only non-zero elements in a k x 3 matrix, where k is the number of non-zero elements.

triplet[k][3] -> [row, column, value]

A header row triplet[0] is often used to store [total_rows, total_cols, total_non_zero].

Assignments 18, 19, 20, 21: Special Matrix Implementations

Task: Implement Diagonal, Lower Triangular, Upper Triangular, and Symmetric matrices using a 1D array.

Logic

The goal is to save space by only storing the non-zero (or unique) elements in a 1D array. This requires a "mapping formula" to convert 2D (i, j) indices to a 1D (k) index.

Section 2: Recursion Assignments

Assignment 11: Factorial (Recursive vs. Iterative)

Logic

Assignment 12: Fibonacci (Recursive vs. Iterative)

Logic

Assignment 13: GCD (Recursive vs. Iterative)

Logic (using Euclidean Algorithm)

Section 3: Linked List Assignments

Key Structure (C++ Template): All linked list assignments will use a Node structure.
template <typename T>
struct Node {
    T data;
    Node* next; // For Singly & Circular
    Node* prev; // For Doubly
};

Assignment 3: Singly Linked List (Templates)

Task: Implement insertion, deletion, search, reverse, and concatenate.

Logic

Assignment 4: Doubly Linked List (Templates)

Task: Implement insertion, deletion, search, and reverse.

Logic

Key: Every operation must correctly update both next and prev pointers.

Assignment 5: Circular Linked List (Templates)

Task: Implement insertion, deletion, search, and reverse.

Logic

Key: The list never has a NULL end. The head's "previous" is the tail, and the tail's next is the head. A tail pointer is often maintained instead of a head, as tail->next gives you the head.

Assignment 10: Polynomial Addition

Task: WAP to scan a polynomial using a linked list and add two polynomials.

Logic

Section 4: Stack & Queue Assignments

Assignment 7: Stack using Array (Templates)

Logic

Assignment 6: Stack using Linked List

Logic

Assignment 8: Queue using Circular Array (Templates)

Logic

Assignment 9: Deque using Linked List

Task: Create and perform operations on a Double-ended Queue using a Doubly Linked List.

Logic

Be very careful with front and rear pointers. When deleting the *last* element, you must set *both* front and rear to NULL.

Assignment 16: Reverse Stack (using additional Stack)

Logic

This is simple. To reverse S1 using S2:

  1. While S1 is not empty, push(S1.pop()) onto S2.
  2. Now S2 holds all elements in reverse order.
  3. (Optional) If you need the reversed elements back in S1, use a third stack S3 (or a Queue) as an intermediary, or just assign S1 = S2 if the class supports it.

Assignment 17: Reverse Stack (using additional Queue)

Logic

To reverse Stack S using Queue Q:

  1. While S is not empty, enqueue(S.pop()) into Q. (Elements are now in Q in the original stack order).
  2. While Q is not empty, push(Q.dequeue()) onto S. (Elements are put back into S in reverse order).

Section 5: Tree Assignments

Assignment 14: Binary Search Tree (BST) Operations

Task: Create a BST and implement a large number of operations.

Logic

Assignment 22: Threaded Binary Tree

Task: Create a Threaded Binary Tree (TBT) as per inorder traversal and implement operations like find successor/predecessor, insert, and inorder traversal.

Logic

Assignment 23: AVL Tree Operations

Task: Implement various operations on an AVL Tree.

Logic

This builds on Assignment 14 (BST). Your insert and delete functions must now be modified to include rebalancing.

  1. Perform the standard BST insertion/deletion.
  2. After the operation, move back up the tree (via recursion) to the root.
  3. At each node, update its height.
  4. Calculate the Balance Factor (BF): BF = height(node->left) - height(node->right).
  5. If BF > 1 (Left-Heavy):
    • If key was inserted in node->left->left: Perform LL Rotate (Right Rotate).
    • If key was inserted in node->left->right: Perform LR Rotate (Left-Right Rotate).
  6. If BF < -1 (Right-Heavy):
    • If key was inserted in node->right->right: Perform RR Rotate (Left Rotate).
    • If key was inserted in node->right->left: Perform RL Rotate (Right-Left Rotate).
A viva for this assignment will almost certainly involve drawing the rotations. Be prepared to show the LL, RR, LR, and RL rotations on a whiteboard.