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

Assignment No.5 s57

The document contains an assignment on data structures focusing on the implementation of a singly linked list in C. It includes functions for inserting, deleting, searching, and sorting nodes, as well as a main function demonstrating these operations. Additionally, it mentions a doubly linked list implementation but does not provide details for it.

Uploaded by

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

Assignment No.5 s57

The document contains an assignment on data structures focusing on the implementation of a singly linked list in C. It includes functions for inserting, deleting, searching, and sorting nodes, as well as a main function demonstrating these operations. Additionally, it mentions a doubly linked list implementation but does not provide details for it.

Uploaded by

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

ASSIGNMENT 5 data structures

name - SHREEYA PRAVIN JOSHI

roll no- 57
prn -2122000723

singly linked list implementation:-

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

// Create a node
struct Node {
int data;
struct Node* next;
};

void insertAtBeginning(struct Node** head_ref, int new_data) {

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

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}

void insertAfter(struct Node* prev_node, int new_data) {


if (prev_node == NULL) {
printf("the given previous node cannot be NULL");
return;
}

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


new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}

void insertAtEnd(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref; /* used in step 5*/

new_node->data = new_data;
new_node->next = NULL;

if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

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


last->next = new_node;
return;
}

void deleteNode(struct Node** head_ref, int key) {


struct Node *temp = *head_ref, *prev;

if (temp != NULL && temp->data == key) {


*head_ref = temp->next;
free(temp);
return;
}

while (temp != NULL && temp->data != key) {


prev = temp;
temp = temp->next;
}

if (temp == NULL) return;

prev->next = temp->next;

free(temp);
}

int searchNode(struct Node** head_ref, int key) {


struct Node* current = *head_ref;

while (current != NULL) {


if (current->data == key) return 1;
current = current->next;
}
return 0;
}

void sortLinkedList(struct Node** head_ref) {


struct Node *current = *head_ref, *index = NULL;
int temp;

if (head_ref == NULL) {
return;
} else {
while (current != NULL) {

index = current->next;

while (index != NULL) {


if (current->data > index->data) {
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void printList(struct Node* node) {
while (node != NULL) {
printf(" %d ", node->data);
node = node->next;
}
}

int main() {
struct Node* head = NULL;

insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);

printf("Linked list: ");


printList(head);

printf("\nAfter deleting an element: ");


deleteNode(&head, 3);
printList(head);

int item_to_find = 3;
if (searchNode(&head, item_to_find)) {
printf("\n%d is found", item_to_find);
} else {
printf("\n%d is not found", item_to_find);
}

sortLinkedList(&head);
printf("\nSorted List: ");
printList(head);
}

DOUBLY LINKED LIST IMPLEMENTATION:-

You might also like