Algo: Linked Lists Flashcards
1
Q
Algorithm for insertAtBeginning implementation
A
- Create a new Node
- Point the new Node’s next pointer to the current head.
- Update the head of the linked list as the new node.// Function to Insert a new node at the beginning of the list
void insertAtBeginning(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
}
2
Q
Algorithm for insertAtEnd implementation
A
- Create a new Node
- If the linked list is empty, update the head as the new node.
- Otherwise traverse till the last node of the linked list.
- Update the next pointer of the last node from NULL to new node.// Function Insert a new node at the end of the list
void insertAtEnd(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;// If the list is empty, update the head to the new node if (!head) { head = newNode; return; } // Traverse to the last node Node* temp = head; while (temp->next) { temp = temp->next; } // Update the last node's next to the new node temp->next = newNode; }
3
Q
Algorithm for insertAtPosition implementation
A
- Check if the provided position by the user is a valid poistion.
- Create a new node.
- Find the node at position -1.
- Update the next pointer of the new node to the next pointer of the current node.
- Update the next pointer of the current node to new node.// Function to Insert a new node at a specific position in the list
void insertAtPosition(int value, int position) {
if (position < 1) {
cout «_space;“Position should be >= 1.” «_space;endl;
return;
}if (position == 1) { insertAtBeginning(value); return; } Node* newNode = new Node(); newNode->data = value; // Traverse to the node before the desired position Node* temp = head; for (int i = 1; i < position - 1 && temp; ++i) { temp = temp->next; } // If the position is out of range, print an error message if (!temp) { cout << "Position out of range." << endl; delete newNode; return; } // Insert the new node at the desired position newNode->next = temp->next; temp->next = newNode; }
4
Q
Algorithm for deleteFromBeginning
A
- Check whether the Head of the linked list is not NULL. If Head is equal to NULL return as the linked list is empty, there is no node present for deletion.
- Store the head of the linked list in a temp pointer.
- Update the head of the linked list to next node.
- Delete the temporary node stored in the temp pointer.// Function to Delete the first node of the list
void deleteFromBeginning() {
if (!head) {
cout «_space;“List is empty.” «_space;endl;
return;
}Node* temp = head; head = head->next; delete temp; }
5
Q
Algorithm for deleteFromEnd implementation
A
- Verify whether the linked is empty or not before deletion.
- If the linked list has only one node, delete head and set head to NULL.
- Traverse till the second last node of the linked list.
- Store the last node of the linked list in a temp pointer.
- Pointer the next pointer of the second last node to NULL.
- Delete the node represented by the temp pointer.// Function to Delete the last node of the list
void deleteFromEnd() {
if (!head) {
cout «_space;“List is empty.” «_space;endl;
return;
}if (!head->next) { delete head; head = NULL; return; } // Traverse to the second-to-last node Node* temp = head; while (temp->next->next) { temp = temp->next; } // Delete the last node delete temp->next; // Set the second-to-last node's next to NULL temp->next = NULL; }
6
Q
Algorithm for deleteFromPosition implementation
A
- Check if the provided postion by the users is a valid position in the linked list or not.
- Find the node at position -1.
- Save node to be deleted in a temp pointer.
- Set the next pointer of the current node to the next pointer of the node to be deleted.
- Set the next pointer of temp to NULL.
- Delete the node represented by temp pointer.// Function to Delete a node at a specific position in the list
void deleteFromPosition(int position) {
if (position < 1) {
cout «_space;“Position should be >= 1.” «_space;endl;
return;
}if (position == 1) { deleteFromBeginning(); return; } Node* temp = head; for (int i = 1; i < position - 1 && temp; ++i) { temp = temp->next; } if (!temp || !temp->next) { cout << "Position out of range." << endl; return; } // Save the node to be deleted Node* nodeToDelete = temp->next; // Update the next pointer temp->next = temp->next->next; // Delete the node delete nodeToDelete; }
7
Q
Algorithm for display implementation
A
- Check if the Head pointer of the linked list is not equal to NULL.
- Set a temp pointer to the Head of the linked list.
- Until temp becomes null:
- Print temp->data
- Move temp to the next node.// Function to print the nodes of the linked list
void display() {
if (!head) {
cout «_space;“List is empty.” «_space;endl;
return;
}Node* temp = head; while (temp) { cout << temp->data << " -> "; temp = temp->next; } cout << "NULL" << endl; }
8
Q
Struct and class definition for linked list (include constructor)
A
// Structure for a node in the linked list
struct Node {
int data;
Node* next;
};
// Define the linked list class
class LinkedList {
// Pointer to the first node in the list
Node* head;
public:
// Constructor initializes head to NULL
LinkedList() : head(NULL) {}
…
};