3 Lecture

CS301

Midterm & Final Term Short Notes

Linked List inside Computer Memory

A linked list is a data structure where each element, called a node, contains a value and a reference to the next node in the list. In computer memory, each node is typically represented as a block of memory that contains the value and a pointer


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. In a linked list, each node contains: a. A value and a pointer to the previous node b. A value and a pointer to the next node c. A key and a value d. A key and a pointer to the next node Answer: b

  2. The first node in a linked list is called the: a. Head b. Tail c. Root d. Leaf Answer: a

  3. In computer memory, each node in a linked list is typically represented as: a. A block of memory that contains the value and a pointer to the previous node b. A block of memory that contains the value and a pointer to the next node c. A hash table that contains the key and the value d. An array that contains the key and a pointer to the next node Answer: b

  4. What is the time complexity of inserting a node at the beginning of a linked list? a. O(1) b. O(n) c. O(log n) d. O(n log n) Answer: a

  5. What is the time complexity of inserting a node at the end of a linked list? a. O(1) b. O(n) c. O(log n) d. O(n log n) Answer: b

  6. Deleting a node from a linked list requires updating the: a. Previous node's pointer to the next node b. Next node's pointer to the previous node c. Current node's value to NULL d. None of the above Answer: a

  7. Traversing a linked list means: a. Deleting a node from the list b. Inserting a node into the list c. Moving through the list from the head to the tail d. Sorting the list in ascending order Answer: c

  8. Which of the following is a disadvantage of linked lists compared to arrays? a. Linked lists allow for efficient insertion and deletion of nodes b. Linked lists use memory flexibly c. Linked lists can grow dynamically d. Linked lists have slow access times for specific nodes Answer: d

  9. Which of the following operations can be performed in constant time on a linked list? a. Finding the maximum value in the list b. Inserting a node at the end of the list c. Removing the head node from the list d. Sorting the list in descending order Answer: c

  10. What is the space complexity of a linked list? a. O(n) b. O(log n) c. O(1) d. O(n log n) Answer: a



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a linked list and how is it different from an array? Answer: A linked list is a data structure where each element (node) contains a value and a reference to the next node. The first node in the list is called the head, and each subsequent node is linked to the previous node. In contrast, an array stores a fixed number of elements of the same type in contiguous memory locations.

  2. How are nodes in a linked list allocated in memory? Answer: Each node in a linked list is typically represented as a block of memory that contains the value and a pointer to the next node. The head node is stored in a variable, and each subsequent node is allocated dynamically as needed.

  3. What is the time complexity of inserting a node at the beginning of a linked list? Answer: The time complexity of inserting a node at the beginning of a linked list is O(1), as it involves updating the head node pointer to point to the new node.

  4. How do you traverse a linked list? Answer: To traverse a linked list, start at the head node and follow the next node pointers until the end of the list is reached.

  5. What is the difference between a singly linked list and a doubly linked list? Answer: In a singly linked list, each node contains a reference to the next node, while in a doubly linked list, each node contains references to both the next and previous nodes.

  6. What is the time complexity of inserting a node at the end of a linked list? Answer: The time complexity of inserting a node at the end of a linked list is O(n), as it involves traversing the list to find the last node and updating its next node pointer to point to the new node.

  7. How do you delete a node from a linked list? Answer: To delete a node from a linked list, update the previous node's next node pointer to point to the next node, effectively removing the node from the list.

  8. What is a circular linked list? Answer: A circular linked list is a linked list where the last node's next node pointer points to the head node, creating a circular structure.

  9. What is a sentinel node in a linked list? Answer: A sentinel node is a special node added to the beginning or end of a linked list that acts as a marker to indicate the start or end of the list.

  10. What is the space complexity of a linked list? Answer: The space complexity of a linked list is O(n), where n is the number of nodes in the list. This is because each node requires its own block of memory.

Linked list is a dynamic data structure that consists of a sequence of nodes, each containing a value and a pointer to the next node in the sequence. Linked lists are a popular data structure in computer science and are often used to implement other data structures, such as queues, stacks, and hash tables. In computer memory, each node in a linked list is typically represented as a block of memory that contains the value and a pointer to the next node. The head node is stored in a variable, and each subsequent node is allocated dynamically as needed. This allows linked lists to be resized and reorganized dynamically, making them an ideal choice for applications where the size of the data set is unknown or constantly changing. Linked lists come in two main varieties: singly linked lists and doubly linked lists. In a singly linked list, each node contains a reference to the next node, while in a doubly linked list, each node contains references to both the next and previous nodes. This allows for more efficient traversal and manipulation of the list, as each node can be accessed from both directions. One of the advantages of linked lists over arrays is that they allow for efficient insertion and deletion of nodes. Inserting a node at the beginning of a linked list requires only updating the head node pointer to point to the new node, while inserting a node at the end of the list requires traversing the entire list to find the last node and updating its next node pointer to point to the new node. Deleting a node from a linked list requires updating the previous node's next node pointer to point to the next node, effectively removing the node from the list. This operation can be performed in constant time, as long as the position of the node to be deleted is known. One of the disadvantages of linked lists compared to arrays is that they have slower access times for specific nodes. Unlike arrays, which store elements in contiguous memory locations, linked lists store nodes in arbitrary locations in memory. This can make it more difficult to access specific nodes in the list quickly, as each node must be accessed individually by following the next node pointers. In conclusion, linked lists are a versatile and powerful data structure that is widely used in computer science. By allowing for dynamic resizing and efficient insertion and deletion of nodes, linked lists are an ideal choice for many applications where the size of the data set is unknown or constantly changing.