Sarthak Dsa File
Sarthak Dsa File
ALGORITHM
(Code – 21CSC201J)
B.Tech[CSE(DS)]
2nd Year - 3rd Semester
Code)
#include <stdio.h>
int main()
scanf("%d", &size);
scanf("%d", &arr[i]);
scanf("%d", &element);
if(arr[i] == element){
break;
if(i == size){
return 0;
Output)
Q-2) Program in C for BINARY SEARCH.
Code)
#include <stdio.h>
int l = 0, r = n - 1, mid;
while (l <= r)
mid = (l + r) / 2;
if (arr[mid] == element)
return mid;
r = mid - 1;
else
l = mid + 1;
return -1;
int main()
scanf("%d", &size);
scanf("%d", &arr[i]);
scanf("%d", &element);
if (index == -1)
{
else
return 0;
Output)
Q-3) Program in C for BUBBLE SORT.
Code)
#include <stdio.h>
int temp;
temp = arr[j];
arr[j + 1] = temp;
int main()
scanf("%d", &size);
scanf("%d", &arr[i]);
bubble_sort(arr, size);
return 0;
Output)
Q-4) Program in C for INSERTION SORT.
Code)
#include<stdio.h>
int j, temp;
temp = arr[i];
j = i-1;
arr[j+1] = arr[j];
j--;
arr[j+1] = temp;
int main(){
scanf("%d", &size);
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 *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)
int n;
head = 0;
scanf("%d", &n);
if (!n)
return head;
else
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
else
temp->next = newnode;
temp = newnode;
}
}
return head;
temp = head;
while (temp != 0)
if (temp->next == 0)
prinƞ("%d", temp->data);
temp = temp->next;
else
temp = temp->next;
if (!head)
else
temp = head;
head = head->next;
free(temp);
traversing_LL(head, temp);
return head;
if (!head)
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;
temp = head;
scanf("%d", &pos);
if (!head)
else if (pos == 1)
head = temp->next;
free(temp);
flag = 1;
}
while(temp->next){
count ++;
if(count == pos){
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
flag = 1;
else
temp = temp->next;
if(!flag){
return head;
traversing_LL(head, temp);
return head;
int main()
int choice;
traversing_LL(head, temp);
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)
else if (choice == 2)
else
prinƞ("Invalid choice!");
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 *creaƟon_LL(struct Node *head, struct Node *newnode, struct Node *temp)
int n;
head = 0;
scanf("%d", &n);
if (!n)
return head;
else
scanf("%d", &newnode->data);
newnode->next = 0;
if (head == 0)
else
temp->next = newnode;
temp = newnode;
return head;
}
temp = head;
while (temp != 0)
if (temp->next == 0)
prinƞ("%d", temp->data);
temp = temp->next;
else
temp = temp->next;
if (!head)
return;
int num;
scanf("%d", &num);
temp = head;
while (temp)
if (num == temp->data)
prinƞ("Element found");
return;
}
else
temp = temp->next;
int main()
traversing_LL(head, temp);
while(ans == 'y'){
search(head, temp);
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 *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
scanf("%d", &newnode->data);
newnode->next = 0;
if (!head)
newnode->prev = 0;
else
temp->next = newnode;
newnode->prev = temp;
temp = newnode;
}
return head;
temp = head;
while (temp)
temp = temp->next;
int main()
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 *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
scanf("%d", &newnode->data);
newnode->next = 0;
if (!head)
newnode->prev = 0;
else
temp->next = newnode;
newnode->prev = temp;
temp = newnode;
return head;
temp = head;
while (temp)
temp = temp->next;
struct Node *insert_at_begin(struct Node *head, struct Node *newnode, struct Node *temp)
{
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)
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;
scanf("%d", &newnode->data);
scanf("%d", &pos);
if(pos == 1){
newnode->next = temp;
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){
return head;
traversing_LL(head, temp);
return head;
int main()
int choice;
traversing_LL(head, temp);
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)
else if (choice == 2)
else
prinƞ("Invalid choice!");
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;
};
void push(int x)
newnode->data = x;
newnode->next = top;
top = newnode;
void pop()
if (!top)
else
top = top->next;
free(temp);
void peek()
if (!top)
prinƞ("Underflow! Can't peek.\n");
else
void display()
if (!top)
else
while (temp)
temp = temp->next;
int main()
while (ans == 1)
scanf("%d", &choice);
switch (choice)
case 1:
scanf("%d", &num);
push(num);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
prinƞ("\n");
break;
default:
prinƞ("Invalid choice!");
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;
};
void push(int x)
newnode->data = x;
newnode->next = 0;
if(!front)
else{
rear->next = newnode;
rear = newnode;
void pop()
if (!front || front>rear)
else
front = front->next;
free(temp);
}
}
void peek()
if (!front || front>rear)
else
void display()
if (!front || front>rear)
else
while (temp)
temp = temp->next;
int main()
while (ans == 1)
scanf("%d", &choice);
switch (choice)
case 1:
push(num);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
prinƞ("\n");
break;
default:
prinƞ("Invalid choice!");
scanf("%d", &ans);
return 0;
Output)