0% found this document useful (0 votes)
40 views10 pages

21bce1716 - DSA LAB ASSIGNMENT

The document describes a C program to implement functions on a singly linked list such as insertion, deletion and searching. It includes functions to insert nodes at the beginning, after a specified node, at the end and delete nodes from a specified location or with a specified value. It provides sample code to create a linked list with values 21, 14, 20 etc, perform various operations on it like insertion, deletion and search and print the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

21bce1716 - DSA LAB ASSIGNMENT

The document describes a C program to implement functions on a singly linked list such as insertion, deletion and searching. It includes functions to insert nodes at the beginning, after a specified node, at the end and delete nodes from a specified location or with a specified value. It provides sample code to create a linked list with values 21, 14, 20 etc, perform various operations on it like insertion, deletion and search and print the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DSA LAB ASSIGNMENT

NAME:GARVIT AGRAWAL

REG NO:21BCE1716

Q:

1) Write a program to create a singly linked list and implement functions to perform the

following operations.

a. insert after a specified value

b. insert at the beginning

c. insert at the end

d. delete a node from a specified location

e. Search for a specified value

Create link list with 21 14 20 51 61 30

a) insert 32 at location 3

21 14 32 20 51 61 30

b) insert 9 at the beginning

9 21 14 32 20 51 61 30

c) insert 46 at the end

9 21 14 32 20 51 61 30 46

d) delete a 32

9 21 14 20 51 61 30 46

e) delete 2nd node

9 14 20 51 61 30 46

f) search 51

51 is available

g) search 90

90 is not available
ANSWER:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

int getCurrSize(struct Node* node){

int size=0;

while(node!=NULL){

node = node->next;

size++;

return size;

void insertAfterNthNode(int n, int data, struct Node** head)

int size = getCurrSize(*head);

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

newNode->data = data;

newNode->next = NULL;
if(n < 0 || n > size)

printf("Invalid position to insert\n");

else if(n == 0){

newNode->next = *head;

*head = newNode;

else

struct Node* temp = *head;

while(--n)

temp=temp->next;

newNode->next= temp->next;

temp->next = newNode;

}
void inBegin(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 inAft(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 inEnd(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 delNode(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 deletePosition(struct Node** head, int n){


struct Node* temp = *head;

struct Node* previous;

int size = getCurrSize(*head);

if(n < 1 || n > size){

printf("Enter valid position\n");

return;

if(n == 1){

*head = (*head)->next;

printf("Deleted: %d\n", temp->data);

free(temp);

return;

while (--n)

previous = temp;

temp = temp->next;
}

previous->next = temp->next;

printf("Deleted: %d\n", temp->data);

free(temp);

void printList(struct Node* node) {

while (node != NULL) {

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

node = node->next;

// Driver program

int main() {

struct Node* head = NULL;

inBegin(&head, 30);

inBegin(&head, 61);

inBegin(&head, 51);

inBegin(&head, 20);

inBegin(&head, 14);

inBegin(&head, 21);

printList(head);

printf("\n");

insertAfterNthNode(2, 32, &head);


printList(head);

printf("\n");

inBegin(&head,9);

printList(head);

printf("\n");

inEnd(&head, 46);

printList(head);

printf("\n");

deletePosition(&head, 1);

printList(head);

int item = 51 ;

if (searchNode(&head, item)) {

printf("\n%d is available", item);

} else {

printf("\n%d is not available", item);

item = 90;

if (searchNode(&head, item)) {

printf("\n%d is available", item);

} else {

printf("\n%d is not available", item);

}
}

Output window:

You might also like