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

Sarthak Dsa File

Uploaded by

hacker7867867860
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)
17 views

Sarthak Dsa File

Uploaded by

hacker7867867860
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/ 35

DATA STRUCTURES AND

ALGORITHM
(Code – 21CSC201J)
B.Tech[CSE(DS)]
2nd Year - 3rd Semester

Name: SARTHAK JAGGA


Registration No: RA2311056030189
Section:B.Tech CSE - (Q) [DS]

DEPARTMENT OF SCIENCE AND


TECHNOLOGY
FACULTY OF ENGINEERING &
TECHNOLOGY
SRM INSTITUTE OF SCIENCE &
TECHNOLOGY, DELHI NCR
CAMPUS, MODINAGAR
`BONAFIDE CERTIFICATE

Registration no. RA2311056030189

Certified to be the bonafide record of work done by SARTHAK JAGGA of 3rd


semester 2nd year B.TECH (DS ) degree course in SRM INSTITUTE OF
SCIENCE AND TECHNOLOGY, NCR Campus of Department of Computer
Science & Engineering, in DATA STRUCTURES AND ALGORITHM , during the
academic year 2024- 2025.

Faculty Signature Head of the Department (CSE)

Submitted for university examination held on


/ _/ at SRM IST, NCR Campus.

Internal Examiner-I Internal Examiner-II


INDEX
S-No. Programs Pg-No. Date Signature of the
faculty

1. Program in C for LINEAR SEARCH. 1-2

2. Program in C for BINARY SEARCH. 3-5

3. Program in C for BUBBLE SORT. 6-7

4. Program in C for INSERTION SORT. 8-9

5. Program in C for DELETION in SINGLY 10-19


LINKED LIST at :-
(i) Beginning
(ii) End
(iii) Specific Position

6. Program in C for SEARCHING in SINGLY 20-24


LINKED LIST.

7. Program in C for CREATION and TRAVERSAL 25-28


in DOUBLY LINKED LIST.

8. Program for INSERTION in DOUBLY LINKED 29-38


LIST at :-
(i)
Beginning
(ii)
End
(iii)
At a specific position

9. Program in C for implementation of STACK 39-44


using LINKED LIST.

10. Program in C for implementation of QUEUE45-51


using LINKED LIST.
Q-1) Program in C for LINEAR SEARCH.

Code)

#include <stdio.h>

int main()

int arr[100], size, i, element;

prinƞ("Enter size of array: ");

scanf("%d", &size);

prinƞ("Enter elements of array: ");

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

scanf("%d", &arr[i]);

prinƞ("Enter element to search: ");

scanf("%d", &element);

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

if(arr[i] == element){

prinƞ("%d is found at index %d",element, i);

break;

if(i == size){

prinƞ("%d NOT found in the array", element);

return 0;

Output)
Q-2) Program in C for BINARY SEARCH.

Code)

#include <stdio.h>

int binary_search(int arr[], int n, int element)

int l = 0, r = n - 1, mid;

while (l <= r)

mid = (l + r) / 2;

if (arr[mid] == element)

return mid;

else if (arr[mid] > element)

r = mid - 1;

else

l = mid + 1;

return -1;

int main()

int arr[100], size, i, element;

prinƞ("Enter size of array: ");

scanf("%d", &size);

prinƞ("Enter elements of array in ascending order: ");

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

scanf("%d", &arr[i]);

prinƞ("Enter element to be searched in array: ");

scanf("%d", &element);

int index = binary_search(arr, size, element);

if (index == -1)
{

prinƞ("%d NOT found in array", element);

else

prinƞ("%d is found at index %d", element, index);

return 0;

Output)
Q-3) Program in C for BUBBLE SORT.

Code)

#include <stdio.h>

void bubble_sort(int arr[], int n)

int temp;

for (int i = 0; i < n - 1; i++)

for (int j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1])

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

prinƞ("AŌer BUBBLE Sort :-\n");

for (int i = 0; i < n; i++)

prinƞ("%d ", arr[i]);

int main()

int arr[100], size;

prinƞ("Enter size of array: ");

scanf("%d", &size);

prinƞ("Enter elements of array: ");

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

scanf("%d", &arr[i]);

bubble_sort(arr, size);

return 0;

Output)
Q-4) Program in C for INSERTION SORT.

Code)

#include<stdio.h>

void inserƟon_sort(int arr[], int n){

int j, temp;

for(int i = 1; i<n; i++){

temp = arr[i];

j = i-1;

while(j>=0 && arr[j]>temp){

arr[j+1] = arr[j];

j--;

arr[j+1] = temp;

prinƞ("AŌer INSERTION Sort :-\n");

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

prinƞ("%d ", arr[i]);

int main(){

int arr[100], size, i;

prinƞ("Enter size of array: ");

scanf("%d", &size);

prinƞ("Enter elements of array: ");

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

scanf("%d", &arr[i]);

inserƟon_sort(arr, size);

return 0;

Output)
Q-5) Program in C for DELETION in SINGLY LINKED LIST at :-

(i) Beginning
(ii) End
(iii) Specific Position

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next;

};

struct Node *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)

int n;

head = 0;

prinƞ("How many nodes you want to create in linked list? ");

scanf("%d", &n);

if (!n)

return head;

else

for (int i = 1; i <= n; i++)

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

prinƞ("Enter data for node %d: ", i);

scanf("%d", &newnode->data);

newnode->next = 0;

if (head == 0)

head = temp = newnode;

else

temp->next = newnode;

temp = newnode;

}
}

return head;

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;

prinƞ("Linked list :- \n");

while (temp != 0)

if (temp->next == 0)

prinƞ("%d", temp->data);

temp = temp->next;

else

prinƞ("%d -> ", temp->data);

temp = temp->next;

struct Node *delete_at_begin(struct Node *head, struct Node *temp)

if (!head)

prinƞ("Linked List is empty. Can't delete!");

else

temp = head;

head = head->next;

free(temp);

traversing_LL(head, temp);

return head;

struct Node *delete_at_end(struct Node *head, struct Node *temp)


{

struct Node *temp1;

if (!head)

prinƞ("Linked List is empty. Can't delete!");

else if (head->next == 0)

temp = head;

head = 0;

free(temp);

else

temp = head;

while(temp->next){

temp1 = temp;

temp = temp->next;

temp1->next = 0;

free(temp);

traversing_LL(head, temp);

return head;

struct Node *delete_at_specific_posiƟon(struct Node *head, struct Node *temp)

struct Node* temp1;

int pos, count = 1, flag = 0;

temp = head;

prinƞ("Enter posiƟon: ");

scanf("%d", &pos);

if (!head)

prinƞ("Linked List is empty. Can't delete!");

else if (pos == 1)

head = temp->next;

free(temp);

flag = 1;
}

else // for pos > 2

while(temp->next){

count ++;

if(count == pos){

temp1 = temp->next;

temp->next = temp1->next;

free(temp1);

flag = 1;

else

temp = temp->next;

if(!flag){

prinƞ("You entered wrong posiƟon for deleƟon of node");

return head;

traversing_LL(head, temp);

return head;

int main()

struct Node *head, *newnode, *temp;

char ans = 'y';

int choice;

head = creaƟon_LL(head, newnode, temp);

traversing_LL(head, temp);

while (ans == 'y')

prinƞ("\nWhere do you want to delete a node? \n1)Beginning\n2)End\n3)Specific PosiƟon\n(Enter your choice in numeric) : ");

scanf("%d", &choice);

if (choice == 1)

head = delete_at_begin(head, temp);

else if (choice == 2)

head = delete_at_end(head, temp);


else if (choice == 3)

head = delete_at_specific_posiƟon(head, temp);

else

prinƞ("Invalid choice!");

prinƞ("\nDo you want to delete another node (y/n) ? ");

scanf(" %c", &ans);

return 0;

Output)
Q-6) Program in C for SEARCHING in SINGLY LINKED LIST.

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next;

};

struct Node *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)

int n;

head = 0;

prinƞ("How many nodes you want to create in linked list? ");

scanf("%d", &n);

if (!n)

return head;

else

for (int i = 1; i <= n; i++)

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

prinƞ("Enter data for node %d: ", i);

scanf("%d", &newnode->data);

newnode->next = 0;

if (head == 0)

head = temp = newnode;

else

temp->next = newnode;

temp = newnode;

return head;
}

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;

prinƞ("Linked list created :- \n");

while (temp != 0)

if (temp->next == 0)

prinƞ("%d", temp->data);

temp = temp->next;

else

prinƞ("%d -> ", temp->data);

temp = temp->next;

void search(struct Node *head, struct Node *temp)

if (!head)

prinƞ("Linked List is empty. Can't search!");

return;

int num;

prinƞ("\nEnter element to be searched: ");

scanf("%d", &num);

temp = head;

while (temp)

if (num == temp->data)

prinƞ("Element found");

return;
}

else

temp = temp->next;

prinƞ("Element NOT found");

int main()

char ans = 'y';

struct Node *head, *newnode, *temp;

head = creaƟon_LL(head, newnode, temp);

traversing_LL(head, temp);

prinƞ("\n-----SEARCHING operaƟon in Linked List-----");

while(ans == 'y'){

search(head, temp);

prinƞ("\nDo you want to search for more elements (y/n)? ");

scanf(" %c", &ans);

return 0;

Output)
Q-7) Program in C for CREATION and TRAVERSAL in DOUBLY LINKED LIST.

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *prev;

struct Node *next;

};

struct Node *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)

int n;

head = 0;

prinƞ("How many nodes you want to create in doubly linked list? ");

scanf("%d", &n);

if (n <= 0)

return head;

else

for (int i = 1; i <= n; i++)

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

prinƞ("Enter data for node %d: ", i);

scanf("%d", &newnode->data);

newnode->next = 0;

if (!head)

head = temp = newnode;

newnode->prev = 0;

else

temp->next = newnode;

newnode->prev = temp;

temp = newnode;
}

return head;

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;

prinƞ("Doubly Linked list created :- \n");

while (temp)

prinƞ("%d ", temp->data);

temp = temp->next;

int main()

struct Node *head, *newnode, *temp;

head = creaƟon_LL(head, newnode, temp);

traversing_LL(head, temp);

return 0;

Output)
Q-8) Program for INSERTION in DOUBLY LINKED LIST at :-

(i) Beginning
(ii) End
(iii) At a specific position

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *prev;

struct Node *next;

};

struct Node *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)

int n;

head = 0;

prinƞ("How many nodes you want to create in doubly linked list? ");

scanf("%d", &n);

if (n <= 0)

return head;

else

for (int i = 1; i <= n; i++)

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

prinƞ("Enter data for node %d: ", i);

scanf("%d", &newnode->data);
newnode->next = 0;

if (!head)

head = temp = newnode;

newnode->prev = 0;

else

temp->next = newnode;

newnode->prev = temp;

temp = newnode;

return head;

void traversing_LL(struct Node *head, struct Node *temp)

temp = head;

prinƞ("Doubly Linked list created :- \n");

while (temp)

prinƞ("%d ", temp->data);

temp = temp->next;

struct Node *insert_at_begin(struct Node *head, struct Node *newnode, struct Node *temp)
{

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

prinƞ("Enter data for newnode: ");

scanf("%d", &newnode->data);

newnode->next = head;

newnode->prev = 0;

head = newnode;

traversing_LL(head, temp);

return head;

struct Node *insert_at_end(struct Node *head, struct Node *newnode, struct Node *temp)

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

prinƞ("Enter data for newnode: ");

scanf("%d", &newnode->data);

newnode->next = 0;

if (!head)

newnode->prev = 0;

head = newnode;

else

temp = head;

while (temp->next)

temp = temp->next;

temp->next = newnode;

newnode->prev = temp;
temp = newnode;

traversing_LL(head, temp);

return head;

struct Node *insert_at_specific_posiƟon(struct Node *head, struct Node *newnode, struct Node *temp)

temp = head;

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

prinƞ("Enter data for newnode: ");

scanf("%d", &newnode->data);

int pos, count = 1, flag = 0;

prinƞ("Enter posiƟon: ");

scanf("%d", &pos);

if(pos == 1){

newnode->next = temp;

head = temp = newnode;

flag = 1;

else{

while(temp){

count++;

if(count == pos){

newnode->prev = temp;

newnode->next = temp->next;

temp->next = newnode;

newnode->next->prev = newnode;

temp = newnode;
flag = 1;

else

temp = temp->next;

if(!flag){

prinƞ("You entered wrong posƟon");

return head;

traversing_LL(head, temp);

return head;

int main()

int choice;

char ans = 'y';

struct Node *head, *newnode, *temp;

head = creaƟon_LL(head, newnode, temp);

traversing_LL(head, temp);

while (ans == 'y')

prinƞ("\nWhere do you want to insert a new node? \n1)Beginning\n2)End\n3)Specific PosiƟon\n(Enter your choice in numeric) : ");

scanf("%d", &choice);

if (choice == 1)

head = insert_at_begin(head, newnode, temp);

else if (choice == 2)

head = insert_at_end(head, newnode, temp);


else if (choice == 3)

head = insert_at_specific_posiƟon(head, newnode, temp);

else

prinƞ("Invalid choice!");

prinƞ("\nDo you want to insert another node (y/n) ? ");

scanf(" %c", &ans);

return 0;

OUTPUT:
Q-9) Program in C for implementation of STACK using LINKED LIST.

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next;

};

struct Node *top = 0;

void push(int x)

struct Node *newnode;

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

newnode->data = x;

newnode->next = top;

top = newnode;

void pop()

if (!top)

prinƞ("Underflow! Can't pop.\n");

else

prinƞ("Popped element: %d\n", top->data);

struct Node *temp = top;

top = top->next;

free(temp);

void peek()

if (!top)
prinƞ("Underflow! Can't peek.\n");

else

prinƞ("Topmost element: %d\n", top->data);

void display()

if (!top)

prinƞ("Linked list empty!\n");

else

struct Node *temp = top;

prinƞ("STACK elements: \n");

while (temp)

prinƞ("%d ", temp->data);

temp = temp->next;

int main()

int choice, num, ans = 1;

prinƞ("-----STACK implementaƟon with LINKED LIST-----\n");

while (ans == 1)

prinƞ("Stack OperaƟons: \n1)Push\n2)Pop\n3)Peek\n4)Display\nEnter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

prinƞ("Enter a number: ");

scanf("%d", &num);

push(num);

break;

case 2:

pop();
break;

case 3:

peek();

break;

case 4:

display();

prinƞ("\n");

break;

default:

prinƞ("Invalid choice!");

prinƞ("Press 1 to conƟnue Stack operaƟons: ");

scanf("%d", &ans);

return 0;

Output)
Q-10) Program in C for implementation of QUEUE using LINKED LIST.

Code)

#include <stdio.h>

#include <stdlib.h>

struct Node

int data;

struct Node *next;

};

struct Node *front = 0, *rear = 0;

void push(int x)

struct Node *newnode;

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

newnode->data = x;

newnode->next = 0;

if(!front)

front= rear = newnode;

else{

rear->next = newnode;

rear = newnode;

void pop()

if (!front || front>rear)

prinƞ("Underflow! Can't pop.\n");

else

prinƞ("Popped element: %d\n", front->data);

struct Node *temp = front;

front = front->next;

free(temp);

}
}

void peek()

if (!front || front>rear)

prinƞ("Underflow! Can't peek.\n");

else

prinƞ("Element at front: %d\n", front->data);

void display()

if (!front || front>rear)

prinƞ("Underflow! Can't peek.\n");

else

struct Node *temp = front;

prinƞ("QUEUE elements: \n");

while (temp)

prinƞ("%d ", temp->data);

temp = temp->next;

int main()

int choice, num, ans = 1;

prinƞ("-----QUEUE implementaƟon with LINKED LIST-----\n");

while (ans == 1)

prinƞ("QUEUE OperaƟons: \n1)Push\n2)Pop\n3)Peek\n4)Display\nEnter your choice: ");

scanf("%d", &choice);

switch (choice)

case 1:

prinƞ("Enter a number: ");


scanf("%d", &num);

push(num);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

prinƞ("\n");

break;

default:

prinƞ("Invalid choice!");

prinƞ("Press 1 to conƟnue Queue operaƟons: ");

scanf("%d", &ans);

return 0;

Output)

You might also like