Data Structure File PDF
Data Structure File PDF
include <stdio.h>
// Returns number of times x occurs in arr[0..n-1]
int countOccurrences(int arr[], int n, int x)
{
int res = 0;
for (int i = 0; i < n; i++)
if (x == arr[i])
res++;
return res;
}
// Driver code
int main()
{
#include <stdio.h>
// Find maximum between two
numbers.
intreturn (num1
max(int > num2)
num1, ? num1 : num2;
int num2)
{}
numbers.
intreturn (num1
min(int num1,> int
num2) ? num2 : num1;
num2)
{
}
int getMin(int arr[], int n)
{
#Include <stdio.h>
return m;
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}
int arr[] = { 5, 1, 4, 2, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
printf("Sorted array: ");
printArray(arr, N);
return 0;
}
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions
int main()
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1],
that are greater than key,
to one position ahead of
their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print
// an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main()
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp arrays
// L[] and R[]
for (i = 0; i < n1; i++)
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of
// R[], if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is
// right index of the sub-array
// of arr to be sorted
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
// Same as (l+r)/2, but avoids
// overflow for large l and r
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
// Driver code
int main()
{
}
Q9. WAP to implement Quick sort in C
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Q10. Menu driven program for all operations on singly linked list in C
// C program for the all operations in // the Singly Linked List #include <stdio.h>
#include <stdlib.h> // Linked List Node struct node {
int info;
struct node* link;
};
struct node* start = NULL;
// Function to create list with n nodes initially
void createList()
{
if (start == NULL) {
int n;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
if (n != 0) {
int data;
struct node* newnode;
struct node* temp;
newnode = malloc(sizeof(struct node));
start = newnode;
temp = start;
printf("\nEnter number to"
" be inserted : ");
scanf("%d", &data);
start->info = data;
for (int i = 2; i <= n; i++) {
head = head->link;
}
head->link = temp;
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
temp = temp->link;
i++;
}
newnode->link = temp->link;
temp->link = newnode;
}
// Function to delete from the front
// of the linked list
void deleteFirst()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else { temp = start;
start = start->link;
free(temp);
}
}
// Function to delete from the end
// of the linked list
void deleteEnd()
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
// Function to delete from any specified
// position from the linked list
void deletePosition()
{
printf("\nList is empty\n");
// Otherwise
else {
printf("\nEnter index : ");
// Position to be deleted
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
// Traverse till position
while (i < pos - 1) {
temp = temp->link;
i++;
}
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
}
}
// Function to find the maximum element
// in the linked list
void maximum()
{
int a[10];
int i;
struct node* temp;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
int max = temp->info;
// Traverse LL and update the
// maximum element
while (temp != NULL) {
int a[10];
int i;
struct node* temp;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// Stores the sum and count of
// element in the LL
int sum = 0, count = 0;
float m;
// Traverse the LL
while (temp != NULL) {
}
}
// Function to sort the linked list
// in ascending order
void sort()
{
// Traverse the LL
while (current != NULL) {
index = current->link;
// Traverse the LL nestedly
// and find the minimum
// element
while (index != NULL) {
}
}
}
// Function to reverse the linked list
void reverseLL()
{
printf("List is empty\n");
// Else
else {
// Traverse the LL
while (start != NULL) {
// reversing of
points t2 = start-
>link; start->link =
t1; t1 = start; start =
t2;
}
start = t1;
// New head Node
temp = start;
printf("Reversed linked "
"list is : ");
// Print the LL
while (temp != NULL) {
printf("%d ", temp->info);
temp = temp->link;
}
}
}
// Function to search an element in linked list
void search()
{
}
// printing found or not
if (found == 1) {
printf(
"Yes, %d is present in the linked list.\n",
key);
}
else {
printf("No, %d is not present in the linked "
"list.\n",
key);
}
}
}
// Driver Code
int main()
{
createList();
int choice;
while (1) {
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
reverseLL();
break;
case 12:
search();
break;
case 13:
exit(1);
break;
default:
printf("Incorrect Choice\n");
} }
return 0;
if(top == -1)
return 1;
else
return 0;
}
/* Check if the stack is full */
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Function to return the topmost element in the stack */
int peek(){
return stack[top];
}
/* Function to delete from the stack */
int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
/* Function to insert into the stack */
int push(int data){
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}
/* Main function */
int main(){
push(44);
push(10);
push(62);
push(123);
push(15);
printf("Element at top of the stack: %d\n" ,peek());
printf("Elements: \n");
int queue[MAX_SIZE];
int front = -1;
int rear = -1;
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("%d ", dequeue());
printf("%d ", dequeue());
printf("%d ", dequeue());
printf("%d ", dequeue());
return 0;
}
void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("Exiting....");
break;
}
default:
{}
printf("Please Enter valid choice ");
};
} } void
push () {
}
printf("Item pushed");
}
}
void pop()
{
printf("Underflow");
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i; struct node
*ptr; ptr=head;
if(ptr == NULL) {
} else {
printf("Stack is empty\n");