0% found this document useful (0 votes)
4 views3 pages

Dsa Lab

The first code snippet counts the total number of duplicate elements in an array and prints the result. The second code snippet implements a linked list with functions to insert elements at the end, display the list, and reverse the list, demonstrating basic linked list operations. Both snippets are written in C programming language.

Uploaded by

amla10000000001
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)
4 views3 pages

Dsa Lab

The first code snippet counts the total number of duplicate elements in an array and prints the result. The second code snippet implements a linked list with functions to insert elements at the end, display the list, and reverse the list, demonstrating basic linked list operations. Both snippets are written in C programming language.

Uploaded by

amla10000000001
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

Question Number 1:

#include <stdio.h>

int main() {

int arr[] = {1, 2, 3, 2, 4, 1, 1, 5};

int size = sizeof(arr) / sizeof(arr[0]);

int count = 0;

for (int i = 0; i < size; i++) {

for (int j = i + 1; j < size; j++) {

if (arr[i] == arr[j]) {

count++;

break; // Move to next element to avoid overcounting

printf("Total number of duplicate elements: %d", count);

return 0;

Question Number 3:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};
// Traverse and Print

void display(struct Node* head) {

while (head != NULL) {

printf("%d -> ", head->data);

head = head->next;

printf("NULL\n");

// Insert at End

void insertEnd(struct Node** head, int val) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = val;

newNode->next = NULL;

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL) temp = temp->next;

temp->next = newNode;

// Reverse the Linked List

void reverse(struct Node** head) {

struct Node *prev = NULL, *current = *head, *next = NULL;

while (current != NULL) {


next = current->next;

current->next = prev;

prev = current;

current = next;

*head = prev;

int main() {

struct Node* head = NULL;

insertEnd(&head, 10);

insertEnd(&head, 20);

insertEnd(&head, 30);

printf("Original List: ");

display(head);

reverse(&head);

printf("Reversed List: ");

display(head);

return 0;

You might also like