DS File
DS File
DATA STRUCTURES
Source Code ➖
// WAP to swap 2 numbers using call by value method.
#include<stdio.h>
void swap (int a, int b){
int temp ;
temp = a ;
a=b;
b = temp ;
printf("Inside function:- a = %d , b = %d\n",a,b);
}
int main(){
int a, b ;
printf("Enter values of a and b:- ");
scanf("%d %d",&a,&b);
void main(){
int a, b;
printf("Enter values of a and b:- ");
scanf("%d %d",&a,&b);
printf("Before swapping:- a = %d , b = %d\n",a,b);
swap2(&a,&b) ;
printf("After swapping:- a = %d , b = %d\n",a,b);
}
➖
BUBBLE SORTING
Source Code
#include<stdio.h>
int main()
{
//Bubble Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Bubble Sort Algorithm
int flag = 0 ;
for (int i=0 ; i<n ; i++){
for (int j=0 ; j<n-i-1 ; j++){
if (a[j] > a[j+1])
{
int t = a[j] ;
a[j] = a[j+1] ;
a[j+1] = t ;
flag = 1 ;
}
}
if (flag == 0)
break ;
}
printf("\nArray after bubble sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;
}
Output –
INSERTION SORTING
Source Code–
#include<stdio.h>
int main()
{
//Insertion Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Insertion Sort Algorithm
for (int i=1 ; i<=n ; i++)
{
int temp = a[i] ;
int j ;
for (j=i-1 ; j>=0 && a[j] > temp ; j--)
{
a[j+1] = a[j] ;
}
a[j+1] = temp ;
}
printf("\nArray after Insertion sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;
Output–
SELECTION SORT
Source Code–
#include<stdio.h>
int main()
{
//Selection Sort
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
//Selection Sort Algorithm
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
printf("\nArray after selection sort:- ") ;
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]) ;
}
return 0 ;
}
Output-
QUICK SORT
Source Code –
#include <stdio.h>
// Quick Sort
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Quick Sort Algorithm
void quicksort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1);
int arr[n];
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
quicksort(arr, 0, n - 1);
Output –
Lab 3- Traversing and inserting elements in 1D and 2D array
#include<stdio.h>
void main()
{
//Printing total number of elements in an array.
int n;
printf("Enter total number of elements u want to enter:- ");
scanf("%d",&n);
int a[n];
for (int i=0 ; i<n ; i++)
{
printf("Enter element %d :- ",i+1);
scanf("%d",&a[i]);
}
int ttl = 0 ;
for (int i=0 ; i<n ; i++)
{
ttl += 1 ;
}
printf("Total number of elements in array is :- %d",ttl);
}
Output –
Q2- To count total number of elements in 2D array
Source Code-
#include<stdio.h>
void main()
{
int r, c ;
printf("Enter number of rows and columns for array:- ") ;
scanf("%d %d",&r,&c) ;
int a[r][c] ;
printf("Enter elements for array:- ") ;
for (int i=0 ; i<r ; i++)
{
for (int j=0 ; j<c ; j++)
{
scanf("%d",&a[i][j]) ;
}
}
int count = 0 ;
for (int i=0 ; i<r ; i++)
{
for (int j=0 ; j<c ; j++)
{
count ++ ;
}
}
Output –
Q.3- Insert an element in the beginning, middle and at end of an 1D array. DATE:30/09/24
Source Code:--
Beginning
#include<stdio.h>
void main(){
// Insertion at beginning of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
//Insertion at beginning of 1D array
int elm ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);
for (int i=n-1 ; i>=0 ; i--){
a[i+1] = a[i];
}
a[0] = elm ;
printf("\nArray after Insertion:- ");
for (int i=0 ; i<=n ; i++){
printf("%d\t",a[i]);
}
}
Output –
End
Source Code:-
#include<stdio.h>
void main(){
// Insertion at end of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
//Insertion at end of 1D array
int elm ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);
a[n] = elm ;
Output:-
Desired Location
Source Code:--
#include<stdio.h>
void main(){
// Insertion at Desired location of 1D array
int n ;
printf("Enter how many elements u want to enter in array:- ");
scanf("%d",&n);
int a[n+1] ;
printf("Enter all integer elements:- ");
for (int i=0 ; i<n ; i++){
scanf("%d",&a[i]);
}
printf("Entered array:- ");
for (int i=0 ; i<n ; i++){
printf("%d\t",a[i]);
}
// Insertion at Desired location of 1D array
int elm ,pos ;
printf("\nEnter the element which u want to enter:- ");
scanf("%d",&elm);
printf("\nEnter the position where u want to enter:- ");
scanf("%d",&pos);
for (int i=n-1 ; i>=pos-1 ; i--){
a[i+1] = a[i];
}
a[pos-1] = elm;
printf("\nArray after Insertion:- ");
for (int i=0 ; i<=n ; i++){
printf("%d\t",a[i]);
}
}
Output :--
LAB 4- Make and Traverse singly and doubly linked list and do insertion DATE:07/10/24
in singly linked list
Source Code-
Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int data, n;
printf("How many nodes do you want to add? ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter data for node %d: ", i + 1);
scanf("%d", &data);
insertNode(&head, data);
}
printList(head);
return 0;}
Output :--
Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
int data;
printf("Enter data for node 1: ");
scanf("%d", &data);
struct Node* head = createNode(data);
struct Node* temp = head;
return head;
}
int main() {
int n;
printf("Enter the number of nodes you want to enter: ");
scanf("%d", &n);
return 0;
}
Output :--
Insertion at beginning of singly linked list
Source Code-
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
int n, data;
// Ask for the number of elements
printf("Enter the number of nodes u want to enter: ");
scanf("%d", &n);
// Ask for data for each node
for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtEnd(head, data);
}
// Print the list after all insertions
printf("Linked List before inserting new element: ");
traverseList(head);
// Ask for another element to insert at the beginning
printf("Enter data to insert: ");
scanf("%d", &data);
// Insert the new element at the beginning
head = insertAtBeginning(head, data);
// Print the list after the new insertion
printf("Linked List after inserting new element: ");
traverseList(head);
return 0;
}
Output :--
Insertion at end of singly linked list
Source Code-
#include <stdio.h>
#include <stdlib.h>
// Function to traverse the singly linked list and print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data;
return 0;
}
Output :--
Insertion at specific position of singly linked list
Source Code-
#include <stdio.h>
#include <stdlib.h>
// Function to traverse the singly linked list and print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data, position;
Output :--
LAB 5- Deletion in singly linked list and insertion in doubly linked list. DATE:14/10/24
Source Code-
Deletion At Beginning
// Deletion at the Beginning in Singly Linked List:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
head->next = second;
second->next = third;
printf("\nBefore Deletion: ");
traverse(head);
head = deleteAtBeginning(head);
printf("After Deletion at Beginning: ");
traverse(head);
return 0;
}
Output :--
Deletion At Specific Location
Source Code-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);
head->next = second;
second->next = third;
third->next = fourth;
int position;
printf("Enter the position to delete: ");
scanf("%d", &position);
return 0;
}
Output:-
Deletion At End Location
Source Code:--
//3. Deletion at the End:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
head->next = second;
second->next = third;
head = deleteAtEnd(head);
return 0;
}
Output:--
Insertion in Doubly Linked List At Beginning
Source Code:--
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a doubly linked list node
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the end
struct Node* insertAtEnd(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
return newNode;
}
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
return head;
}
// Function to insert a node at the beginning
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head != NULL) {
head->prev = newNode;
newNode->next = head;
}
return newNode;
}
// Function to print the elements
void traverseList(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data;
// Ask for the number of elements
printf("Enter the number of nodes: ");
scanf("%d", &n);
// Ask for data for each node
for (int i = 1; i <= n; i++) {
printf("Enter data for node %d: ", i);
scanf("%d", &data);
head = insertAtEnd(head, data);
}
// Print the list after all insertions
printf("Doubly Linked List after initial insertion: ");
traverseList(head);
// Ask for another element to insert at the beginning
printf("Enter data to insert at the beginning: ");
scanf("%d", &data);
// Insert the new element at the beginning
head = insertAtBeginning(head, data);
// Print the list after the new insertion
printf("Doubly Linked List after inserting at beginning: ");
traverseList(head);
return 0;
}
Output:--
Insertion in Doubly Linked List At Specific Position
Source Code:--
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
int n, data, position;
return 0;
}
Output:--
Insertion in Doubly Linked List At End
Source Code:-
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);
struct Node* fifth = createNode(50);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;
fourth->next = fifth;
fifth->prev = fourth;
head = deleteAtBeginning(head);
return 0;
}
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
if (position == 1) {
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
return head;
}
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;
int position;
printf("Enter the position to delete: ");
scanf("%d", &position);
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
if (temp->next == NULL) {
free(temp);
return NULL;
}
temp->prev->next = NULL;
free(temp);
return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
struct Node* fourth = createNode(40);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = fourth;
fourth->prev = third;
head = deleteAtEnd(head);
return 0;
}
struct Node
{
int data;
struct Node *next;
};
if (head == NULL)
{
return newNode;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
return head;
}
int main()
{
struct Node *head = createNode(10);
struct Node *second = createNode(20);
struct Node *third = createNode(30);
head->next = second;
second->next = third;
third->next = head;
return 0;
}
Output 6.4: Insertion in the Singly Circular Linked List at the beginning
Program 6.5: Insertion in the Singly Circular Linked List at a Specific Location
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (position == 1) {
if (head == NULL) {
return newNode;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
return head;
}
newNode->next = temp->next;
temp->next = newNode;
return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
head->next = second;
second->next = third;
third->next = head;
return 0;
}
Output 6.5: Insertion in the Singly Circular Linked List at a Specific Location
Program 6.6: Insertion in the Singly Circular Linked List at the End
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
if (head == NULL) {
return newNode;
}
temp->next = newNode;
newNode->next = head;
return head;
}
int main() {
struct Node* head = createNode(10);
struct Node* second = createNode(20);
struct Node* third = createNode(30);
head->next = second;
second->next = third;
third->next = head;
return 0;
}
Output 6.6: Insertion in the Singly Circular Linked List at the End
Lab 7: Perform Traverse , Insertion , Deletion on Stack using Array DATE:11/11/24
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack underflow! Cannot pop.\n");
return -1;
} else {
int value = stack[top];
top--;
return value;
}
}
void traverse() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
pop();
printf("After popping an element:\n");
traverse();
return 0;
}
int stack[MAX];
int top = -1;
void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
printf("Current stack after insertions:\n");
display();
return 0;
}
int stack[MAX];
int top = -1;
int pop() {
if (top == -1) {
printf("Stack underflow! Cannot pop.\n");
return -1;
} else {
int value = stack[top];
top--;
printf("Popped %d from the stack.\n", value);
return value;
}
}
void display() {
if (top == -1) {
printf("The stack is empty.\n");
} else {
printf("Stack elements: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
push(10);
push(20);
push(30);
push(40);
display();
pop();
pop();
return 0;
}