Document From Abhishek Singh
Document From Abhishek Singh
INPUT:
#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);
n=n+1;
for(i = n-1; i >= pos; i--)
array[i]=array[i-1];
array[pos-1]=x;
OUTPUT:
int main() {
int arr[MAX_SIZE];
int size, newElement;
return 0;
}
Output:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Enter the element to insert at the end: 7
Array after insertion:
234567
EX-2
#include <stdio.h>
int main() {
int arr[100], size, i, pos;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter elements of array: ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("Enter the position to delete: ");
scanf("%d", &pos);
if (pos < 0 || pos >= size) {
printf("Invalid position!\n");
return 1;
}
for(i = pos; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
printf("Array after deletion:\n");
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
OUTPUT:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Enter the position to delete: 2
Array after deletion:
2356
Input:
#include <stdio.h>
int main() {
int arr[MAX_SIZE];
int size, i;
return 0;
}
Output:
Enter the size of the array: 5
Enter elements of array: 2 3 4 5 6
Array after deletion:
3456
Q-3Write a program in c to delete any element from
the ending of the array
Input:
#include <stdio.h>
int main() {
int arr[MAX_SIZE];
int size, i;
return 0;
}
Output:
PRACTICAL-1
*Write a program to allocate memory dynamically using malloc
and calloc function.
input:malloc
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
return 0;
}
OUTPUT:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
Input:calloc
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
OUTPUT:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
PRACTICAL-2
*Write a program to implement
(a) linear Search
#include <stdio.h>
int main()
{
int a[10]={1,2,3,13,15,19,21,24,33,40};
int n,i,flag=0;
int main()
{
int a[10]={1,2,3,13,15,19,21,24,33,40};
int i,num,low,high,mid;
#define MAX 20
int pop() {
int val;
if (top == -1) {
printf("\nStack underflow");
return -1;
} else {
val = st[top];
--top;
return val;
}
}
void peck() {
if (top == -1) {
printf("\nStack is empty");
} else {
printf("\nThe value at top is: %d\n", st[top]);
}
}
void display() {
int i;
if (top == -1) {
printf("\nStack is empty");
} else {
for (i = top; i >= 0; i--) {
printf("\n%d", st[i]);
}
printf("\n");
}
}
int main() {
int val, option;
do {
printf("\nChoose an option:");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Peck");
printf("\n4. Display");
printf("\n5. Exit");
printf("\nEnter your choice: ");
scanf("%d", &option);
switch (option) {
case 1:
printf("\nEnter the number to push: ");
scanf("%d", &val);
push(val);
break;
case 2:
val = pop();
if (val!= -1) {
printf("\nThe value deleted is: %d\n", val);
}
break;
case 3:
peck();
break;
case 4:
display();
break;
}
} while (option!= 5);
return 0;
}
output:-
[?2004l
Choose an option:
1. Push
2. Pop
3. Peck
4. Display
5. Exit
Enter your choice: 1
Choose an option:
1. Push
2. Pop
3. Peck
4. Display
5. Exit
Enter your choice: 2
Practical-4
Implement a program to convert infix notation
to postfix notation-using stack.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 50
char infix[MAX];
char postfix[MAX];
char stack[MAX];
int top = -1;
int main() {
printf("Enter an infix expression: ");
scanf("%s", infix);
process();
printf("%s\n", postfix);
return 0;
}
void process() {
int i = 0, j = 0;
while (infix[i]!= '\0') {
if (isalpha(infix[i]) || isdigit(infix[i])) {
postfix[j] = infix[i];
i++;
j++;
} else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '^') {
while (top!= -1 && precedence(infix[i]) <= precedence(stack[top])) {
postfix[j] = pop();
j++;
}
push(infix[i]);
i++;
} else {
printf("Invalid expression\n");
exit(1);
}
}
while (top!= -1) {
postfix[j] = pop();
j++;
}
postfix[j] = '\0';
}
char pop() {
char val;
if (top == -1) {
printf("Stack underflow\n");
} else {
val = stack[top];
top--;
}
return val;
}
Output:-
Enter an infix expression: a+b-c
ab+c-
Practical-5
Write a program to implement QUEUE using arrays that
performs following operations
(a)INSERT (b) DELETE (c) DISPLAY
#include<stdio.h>
#define MAX 50
int queue[MAX];
int front=-1,rear=-1;
void insert();
int delet();
void display();
void insert()
{
int num;
printf("Enter the number you want to enter :");
scanf("%d",&num);
if(rear==MAX-1)
{
printf("!! QUEUE OVERFLOW !!");
}
else
{
rear++;
queue[rear]=num;
}
}
int delet()
{
int val;
if(front==-1 || front>rear)
{
printf("!! QUEUE UNDERFLOW !!");
return -1;
}
else
{
val=queue[front];
front++;
if(front>rear)
front=rear=-1;
return val;
}
}
void display()
{
int i;
if(front==-1 || front>rear)
{
printf("!! QUEUE IS EMPTY !!");
}
else
{
for(i=front;front<=rear;i++);
printf("%d",queue[i]);
}
}
int main()
{
int op,x;
do{
printf("CHOODE AN OPTION :\n*\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.display\n");
printf("4.Exit\n*\n");
printf("Your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
insert();
break;
case 2:
{
x=delet();
if(x!=-1)
printf("deleted value : %d",x);
}break;
case 3:
display();
break;
}
}while(op!=4);
return 0;
}
Output:-
CHOOsE AN OPTION :
*
1.Insert
2.Delete
3.display
4.Exit
*
Your Choice : 1
Enter the number you want to enter :13
Practical-6,7,8
Write a menu driven program to implement following operations on
the singly linked list.
(a) Insert a node at the front of the linked list.
(b) Insert a node at the end of the linked list.
(c) Insert a node at the specified position
(d) Delete a first node of the linked list.
(e) Delete a node before specified position.
(f) Delete a node after specified position.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
struct node //construction of nodes
{
int data;
struct node *next;
};
struct node *start = NULL; // this is just like object but methods as object.
struct node *create_ll();
struct node *display();
struct node *insert_beg();
struct node *insert_end();
struct node *insert_after();
struct node *delete_beg();
struct node *delete_end();
struct node *delete_after();
struct node *delete_list();
int main ()
{
int option;
do
{
switch (option)
{
case 1:
start = create_ll ();
printf ("\n LINKED LIST CREATED");
break;
case 2:
start = display ();
break;
case 3:
start = insert_beg ();
break;
case 4:
start = insert_end ();
break;
case 5:
start = insert_after ();
break;
case 6:
start = delete_beg ();
break;
case 7:
start = delete_end ();
break;
case 8:
start = delete_after ();
break;
case 9:
start = delete_list ();
printf ("\n LINKED LIST DELETED");
break;
}
}while (option != 10);
return 0;
}
return start;
}
return start;
}
return start;
}
return start;
}
return start;
}
return start;
}
}
}
return start;
}
Output:-
**MAIN MENU **
1: Create a list
2: Display the list
3: Add a node at the beginning
4: Add a node at the end
5: Add a node after a given node
6: Delete a node from the beginning
7: Delete a node from the end
8: Delete a node after a given node
9: Delete the entire list
10: EXIT
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* start = NULL;
int choice, data, position;
while (1) {
printf("\n1. Insert at the end\n2. Insert before a position\n3. Delete the first node\n4. Delete after a
position\n5. Display\n6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
insertAtEnd(&start, data);
break;
case 2:
printf("Enter the value to insert: ");
scanf("%d", &data);
printf("Enter the position before which to insert: ");
scanf("%d", &position);
insertBeforePosition(&start, data, position);
break;
case 3:
deleteFirstNode(&start);
break;
case 4:
printf("Enter the position after which to delete: ");
scanf("%d", &position);
deleteAfterPosition(&start, position);
break;
case 5:
displayList(start);
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
newNode->data = data;
newNode->next = NULL;
if (*startRef == NULL) {
*startRef = newNode;
return;
}
last->next = newNode;
}
newNode->data = data;
newNode->next = NULL;
if (position == 1) {
newNode->next = current;
*startRef = newNode;
return;
}
newNode->next = current;
previous->next = newNode;
}
if (*startRef == NULL) {
printf("List is empty\n");
return;
}
*startRef = (*startRef)->next;
free(temp);
}
if (current == NULL) {
printf("List is empty\n");
return;
}
if (position == 1) {
printf("Deletion after the first node is not allowed\n");
return;
}
while (current != NULL && position > 1) {
current = current->next;
position--;
}
nextNode = current->next;
current->next = nextNode->next;
free(nextNode);
}
output:-
practical-11
Write a program to implement stack using linked list
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
int is_empty() {
return top == NULL;
}
int pop() {
if (is_empty()) {
printf("Error: Stack is empty.\n");
exit(1);
}
int data = top->data;
struct node* temp = top;
top = top->next;
free(temp);
return data;
}
void print_stack() {
struct node* temp = top;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
return 0;
}
Output:-
Stack after pushing 10, 20, 30, 40, 50:
50 -> 40 -> 30 -> 20 -> 10 -> NULL
Popped 50 from stack.
Popped 40 from stack.
Stack after popping 50 and 40:
30 -> 20 -> 10 -> NULL
Practical-12
Write a program to implement Bubble sort and Insertion
Sort.
Bubble sort
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
return 0;
}
Output:-
Unsorted array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90
Insertion Sort.
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
Output:-
Sorted array:
5 6 11 12 13
Practical-13
Selection sort
#include <stdio.h>
int main() {
int arr[] = {21, 11, 31, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:-
Sorted array:
6 7 11 21 31
Quick Sort.
#include <stdio.h>
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:-
Sorted array:
1 5 7 8 9 10
Practical-13
Write a program to implement Merge sort.
#include <stdio.h>
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
int main() {
int arr[] = {24, 31, 73, 5,86, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:-
Sorted array:
5 7 24 31 73 86