0% found this document useful (0 votes)
13 views2 pages

Linked List Solutions

Uploaded by

random77335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Linked List Solutions

Uploaded by

random77335
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Solutions for Data Structure Questions

1. Memory Representation of Singly Linked List


A singly linked list consists of nodes where each node contains a data part and a pointer to
the next node in the sequence. The memory representation involves nodes stored non-
contiguously in memory, each node having a pointer that links it to the next node. This
forms a chain-like structure in memory, allowing for dynamic memory allocation and
efficient insertions and deletions.

2. Difference between Linked Lists and Arrays


- Memory Allocation: Arrays require contiguous memory allocation, while linked lists use
non-contiguous memory, which allows flexibility.
- Size: Arrays are static (fixed size after declaration), while linked lists are dynamic and can
grow or shrink.
- Access Time: Arrays offer direct access to elements using indices, while linked lists require
traversal from the head for element access.
- Insertions/Deletions: Linked lists are generally more efficient for insertions and deletions,
especially when the position is not at the end.

3. C Function to Print Values and Count Nodes in a Singly Linked List


void printAndCount(struct Node* head) {
struct Node* current = head;
int count = 0;
while (current != NULL) {
printf("%d ", current->data);
count++;
current = current->next;
}
printf("\nNumber of nodes: %d\n", count);
}

4. C Function to Search for a Value in a Singly Linked List


struct Node* search(struct Node* head, int value) {
struct Node* current = head;
while (current != NULL) {
if (current->data == value) return current;
current = current->next;
}
return NULL; // Value not found
}
5. C Function for Operations on Singly Linked List (Insert Front)
void insertFront(struct Node** head, int newData) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = *head;
*head = newNode;
}

6. C Function to Reverse a Linked List


struct Node* reverse(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}

7. C Function to Increase Marks by 5 and Count Students in a List


void increaseMarks(struct student* head) {
int count = 0;
struct student* current = head;
while (current != NULL) {
current->marks += 5;
count++;
current = current->next;
}
printf("Number of students: %d\n", count);
}

You might also like