0% found this document useful (0 votes)
4 views

Lab8_LinkedListOperations

Uploaded by

zunzurkarcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab8_LinkedListOperations

Uploaded by

zunzurkarcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab-8 Submission: Linked List Operations

1. Linked List Operations Code in C

#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Append a new node to the end of the list


void append(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Delete a node by value


void deleteByValue(struct Node** head, int value) {
struct Node* temp = *head, *prev = NULL;
if (temp != NULL && temp->data == value) {
*head = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}

// Display the Linked List


void display(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
append(&head, 10);
append(&head, 20);
append(&head, 30);
printf("Linked List after insertions: ");
display(head);
deleteByValue(&head, 20);
printf("Linked List after deletion: ");
display(head);
return 0;
}

2. Results/Outputs with Screenshots and Descriptions


Include screenshots demonstrating:

1. Insertion Results
- Description: After adding nodes with values 10, 20, and 30, the Linked List displays all
nodes in order. This demonstrates the appending operation, showing the new nodes added
at the end of the list.

2. Deletion Results
- Description: Shows the Linked List after deleting the node with value 20. Only the
specified node is removed, and the remaining nodes are correctly linked.

3. Traversal Results
- Description: Displays the entire Linked List in sequence. This confirms the structure is
intact, and all nodes can be accessed sequentially.

3. Algorithm Write-up

This section will describe:


- Linked List Overview: Explain nodes and links.
- Types of Linked Lists:
- Singly Linked List: Allows traversal in one direction.
- Doubly Linked List: Enables bidirectional traversal.
- Circular Linked List: The last node points to the first.
- Complexities of Operations:
- Insertion: O(1) for adding at the head, O(n) for the end.
- Deletion: O(n) if we search for the node, O(1) for known nodes.
- Traversal: O(n), as each node must be accessed sequentially.

4. Viva Questions and Solutions

Example questions and answers:

1. What is a Linked List, and what are its uses?


- Answer: A Linked List is a series of connected nodes where each node points to the next.
It's useful for dynamic data storage where size can vary.

2. Why use a Linked List instead of an array?


- Answer: Linked Lists are more dynamic than arrays and allow efficient insertions and
deletions without needing to shift elements.

3. Explain the complexity of Linked List insertion operations.


- Answer: Head insertion has O(1) complexity, while tail insertion has O(n) because the
entire list is traversed.

4. What is a Circular Linked List, and how is it used?


- Answer: It’s a list where the last node points to the first, enabling circular traversal,
useful in applications needing continuous looping.

5. Difference between a Singly and a Doubly Linked List?


- Answer: A Singly Linked List allows traversal in one direction only, while a Doubly
Linked List supports both directions, making operations like deletion easier.

You might also like