📝 Written Questions & Code Review

← Back to Home
Topic 1: Overview in Data Structures
What is the definition of the Data Structure?
Answer: Data structure is a collection of data elements whose logical organization reflects a relationship among the elements. It is also characterized by accessing operations used to store and retrieve individual data elements.
What is the definition of an array?
Answer: An array is a data structure that stores a collection of elements, typically of the same data type, in contiguous memory locations.
Topic 2: Pointers
What are Pointers?
Answer: A pointer is a variable whose value is the address of another variable.
What are the three main benefits of using pointers?
Answer: The three main benefits are runtime-sized data structures, resizable data structures, and memory sharing.
Why use Pointers?
Answer:
  • Pointers allow passing arrays and strings to functions more efficiently and save memory.
  • They reduce the length and complexity of a program and allow a function to return more than one value.
  • Pointers increase processing speed through direct access to memory locations.
What is the difference between Pointers and arrays?
Answer: The identifier of an array is equivalent to the address of its first element, making the concepts effectively the same.
Topic 3: Linked Lists
What are the differences between an array and a Linked List?
Answer:
  • Arrays have a fixed size at compile time and store elements contiguously, whereas linked lists are dynamic and nodes are not normally stored contiguously.
  • Arrays allow immediate access to any element, while linked lists do not afford direct access, making individual access more expensive.
What are the advantages and disadvantages of Linked List?
Answer:
  • Advantages: They are appropriate when the number of elements is unpredictable and allow for efficient insertion operations anywhere in the list.
  • Disadvantages: They lack direct access, meaning you must traverse the list from the head to find a specific node.
💻 Linked List Implementation

Implementation includes:

  1. Insert at beginning
  2. Delete first node
  3. Display list
  4. Find minimum
  5. Count nodes
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;
};

class LinkedList {
    Node* head;
public:
    LinkedList() { head = NULL; }

    void insertFirst(int val) {
        Node* newnode = new Node();
        newnode->data = val;
        newnode->next = head;
        head = newnode;
    }

    void deleteFirst() {
        if (head == NULL) return;
        Node* temp = head;
        head = head->next;
        delete temp;
    }

    void display() {
        Node* temp = head;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }

    int getMin() {
        if (head == NULL) return -1;
        int minVal = head->data;
        Node* temp = head;
        while (temp != NULL) {
            if (temp->data < minVal) 
                minVal = temp->data;
            temp = temp->next;
        }
        return minVal;
    }

    int count() {
        int counter = 0;
        Node* temp = head;
        while (temp != NULL) {
            counter++;
            temp = temp->next;
        }
        return counter;
    }
};
Topic 4: Stacks
💻 Stack Implementation (Array)

Implementation includes:

  1. Push operation
  2. Pop operation
  3. Peek at top
  4. Display stack
  5. Find minimum
#include <iostream>
using namespace std;

class Stack {
    int* stack_arr;
    int top;
    int size;
public:
    Stack(int s) {
        size = s;
        stack_arr = new int[size];
        top = -1;
    }

    void push(int item) {
        if (top == size - 1) 
            cout << "Stack Overflow\n";
        else 
            stack_arr[++top] = item;
    }

    int pop() {
        return (top == -1) ? -1 : stack_arr[top--];
    }

    int peak() {
        return (top == -1) ? -1 : stack_arr[top];
    }

    void display() {
        for (int i = top; i >= 0; i--) 
            cout << stack_arr[i] << " ";
        cout << endl;
    }

    int getMin() {
        int minVal = stack_arr[0];
        for (int i = 1; i <= top; i++)
            if (stack_arr[i] < minVal) 
                minVal = stack_arr[i];
        return minVal;
    }
};
Topic 5: Queues
💻 Queue Implementation (Linked List)

Implementation includes:

  1. Enqueue operation
  2. Dequeue operation
  3. Get rear element
  4. Display queue
  5. Calculate average
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;
};

class Queue {
    Node *front, *rear;
public:
    Queue() { front = rear = NULL; }

    void enqueue(int item) {
        Node* newnode = new Node();
        newnode->data = item;
        newnode->next = NULL;
        if (front == NULL) 
            front = rear = newnode;
        else {
            rear->next = newnode;
            rear = newnode;
        }
    }

    void dequeue() {
        if (front == NULL) return;
        Node* temp = front;
        front = front->next;
        delete temp;
    }

    int getRear() {
        return rear->data;
    }

    void display() {
        Node* temp = front;
        while (temp != NULL) {
            cout << temp->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }

    double getAverage() {
        int sum = 0, count = 0;
        Node* temp = front;
        while (temp != NULL) {
            sum += temp->data;
            count++;
            temp = temp->next;
        }
        return (count == 0) ? 0 : (double)sum / count;
    }
};
Key Terms
Tree
A hierarchical, non-linear data structure that organizes nodes connected by edges.
Root Node
A node with no parent.
Leaf Node
A node which has no child.
Graph
A pair G = (V, E) where V is a finite set of vertices and E is a set of edges.
Path
A sequence of nodes where each adjacent pair is connected by an edge.