You are on page 1of 17

 Program for Inserting element in array

#include <stdio.h>
void main()
{
int array[100];
int i, n, x, pos;
printf("Enter the number of elements in the array \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are: \n");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\nEnter the new element to be inserted: ");
scanf("%d", &x);
printf("Enter the position where element is to be inserted: ");
scanf("%d", &pos);
//shift all elements 1 position forward from the place
//where element needs to be inserted
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x; //Insert the element x on the specified position
//print the new array
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
Getch();
}
 Program for Deleting element in array
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete element\n");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
 Program for Linear Search in array
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
 Program for Binary Search in array
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;
}
 Program for Bubble sort
#include <stdio.h>
int main()
{
int i, j, n, temp, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
For(i=0;i<n-1;i++)
{
For(j=0;j<n-1-i;j++)
{
If(a[j]>a[j+1])
{
Temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Printf(“sorted array is:”);
For(i=0;i<n;i++)
{
Printf(“%d”,a[i]);
}
Getch();
}
 Program for Selection Sort
#include <stdio.h>
int main()
{
int i, j, n, temp,min, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
{
scanf("%d",&array[i]);
}
For(i=0;i<n-1;i++)
{
Min=i;
For(j=i+1;j<n;j++)
{
If(a[j]<a[min])
{
Min=j;
}
}
Temp=a[i];
a[i]=a[min];
a[min]=temp;
}
Printf(“sorted array is:”);
For(i=0;i<n;i++)
{
Printf(“%d”,a[i]);
}
Getch();
}

 Program for Insertion Sort


#include <stdio.h>
int main()
{
int n, array[1000], i, j, t, flag = 0;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i= 1 ; i <= n - 1; i++) {
t = array[i];
for (j= i - 1 ; j >= 0; j--) {
if (array[j] > t) {
array[j+1] = array[j];
flag = 1;
}
else
break;
}
if (flag)
array[j+1] = t;
}
printf("Sorted list in ascending order:\n");
for (i= 0; i<= n - 1; i++) {
printf("%d\n", array[i]);
}
return 0;
}

 Program for Single linked list:


#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a new node at the end of the list
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
// Function to display the linked list
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main()
{
struct Node* head = NULL; // Initialize an empty linked list
// Insert elements at the end of the list
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
// Display the linked list
printf("Linked List: ");
displayList(head);
// Free the allocated memory
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}

 Program for Circular Linked List:


#include <stdio.h>
#include <stdlib.h>
// Define a structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the beginning of the circular linked list
struct Node* insertAtBeginning(struct Node* head, int data) {
struct Node* newNode = createNode(data);
if (head == NULL) {
newNode->next = newNode; // If the list is empty, make it point to itself
} else {
newNode->next = head->next;
head->next = newNode;
}
return newNode;
}
// Function to display the circular linked list
void display(struct Node* head) {
struct Node* current = head;
if (head == NULL) {
printf("Circular Linked List is empty.\n");
return;
}
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main()
{
struct Node* head = NULL;
int choice, data;
while (1) {
printf("1. Insert at the beginning\n");
printf("2. Display\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
head = insertAtBeginning(head, data);
break;
case 2:
printf("Circular Linked List: ");
display(head);
break;
case 3:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
 Program for Linear queue
#include <stdio.h>
#define MAX_SIZE 10
int queue[MAX_SIZE];
int front = -1, rear = -1;
int isFull() {
return rear == MAX_SIZE - 1;
}
int isEmpty() {
return front == -1;
}
void enqueue(int item) {
if (isFull()) {
printf("Queue is full. Cannot enqueue.\n");
} else {
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = item;
printf("%d enqueued to the queue.\n", item);
}
}
int dequeue() {
int item;
if (isEmpty()) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
item = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front++;
}
printf("%d dequeued from the queue.\n", item);
return item;
}
}
void display() {
if (isEmpty()) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}
 Program for Circular queue
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
int front = -1, rear = -1;
int isFull() {
return ((rear + 1) % MAX_SIZE) == front;
}
int isEmpty() {
return front == -1;
}
void enqueue(int item) {
if (isFull()) {
printf("Queue is full. Cannot enqueue.\n");
} else {
if (front == -1) {
front = 0;
}
rear = (rear + 1) % MAX_SIZE;
queue[rear] = item;
printf("%d enqueued to the queue.\n", item);
}
}
int dequeue() {
int item;
if (isEmpty()) {
printf("Queue is empty. Cannot dequeue.\n");
return -1;
} else {
item = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX_SIZE;
}
printf("%d dequeued from the queue.\n", item);
return item;
}
}
void display() {
if (isEmpty()) {
printf("Queue is empty.\n");
} else {
int i = front;
printf("Queue elements: ");
do {
printf("%d ", queue[i]);
i = (i + 1) % MAX_SIZE;
} while (i != (rear + 1) % MAX_SIZE);
printf("\n");
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
enqueue(40);
display();
return 0;
}
 Program for Tree Traversing
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
// Function to perform an inorder traversal
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
// Function to perform a preorder traversal
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
// Function to perform a postorder traversal
void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
int main() {
// Create a sample binary tree
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
// Perform inorder traversal
printf("Inorder traversal: ");
inorder(root);
// Perform preorder traversal
printf("\nPreorder traversal: ");
preorder(root);
// Perform postorder traversal
printf("\nPostorder traversal: ");
postorder(root);
return 0;
}

You might also like