Skip to content

Commit be5cd55

Browse files
authored
Add files via upload
0 parents  commit be5cd55

13 files changed

+993
-0
lines changed

ArrayOperations.c

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include<stdio.h>
2+
int n,ch;
3+
void menu();
4+
void create(int []);
5+
void display(int []);
6+
void insert(int []);
7+
void delete(int []);
8+
void main()
9+
{
10+
int a[10];
11+
menu();
12+
while(ch < 5)
13+
{
14+
switch(ch)
15+
{
16+
case 1: create(a); break;
17+
case 2: display(a) ; break;
18+
case 3: insert(a) ; break;
19+
case 4: delete(a) ; break;
20+
}
21+
menu();
22+
}
23+
}
24+
void menu()
25+
{
26+
printf("1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit\nEnter your choice : ");
27+
scanf("%d", &ch);
28+
}
29+
void create(int a[10])
30+
{
31+
int i;
32+
printf("Enter the size of the array elements : ");
33+
scanf("%d", &n);
34+
printf("Enter the elements for the array : ");
35+
for(i = 0; i < n; i++)
36+
scanf("%d", &a[i]);
37+
}
38+
void display(int a[10])
39+
{
40+
int i;
41+
printf("The array elements are : ");
42+
for(i = 0; i < n; i++)
43+
printf("%d ", a[i]);
44+
printf("\n");
45+
}
46+
void insert(int a[10])
47+
{
48+
int i,pos,ele;
49+
printf("Enter the position(index) for the new element : ");
50+
scanf("%d", &pos);
51+
printf("Enter the element to be inserted : ");
52+
scanf("%d", &ele);
53+
if(pos > n)
54+
{
55+
printf("Invalid position.\n");
56+
}
57+
else
58+
{
59+
for(i = n - 1; i >= pos; i--)
60+
a[i + 1] = a[i];
61+
a[pos] = ele;
62+
n++;
63+
}
64+
}
65+
void delete(int a[10])
66+
{
67+
int i, pos;
68+
printf("Enter the position(index) of the element to be deleted : ");
69+
scanf("%d", &pos);
70+
if(pos > n)
71+
{
72+
printf("Invalid position.\n");
73+
}
74+
else
75+
{
76+
printf("The deleted element is : %d\n", a[pos]);
77+
for(i = pos; i < n - 1; i++)
78+
a[i] = a[i + 1];
79+
n--;
80+
}
81+
}

binarySearch.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include<stdio.h>
2+
void main() {
3+
int a[20], i, j, n, key, flag = 0, low, high, mid, temp;
4+
printf("Enter value of n : ");
5+
scanf("%d", &n);
6+
// Write the code to read an array of elements
7+
for(i = 0; i < n; i++)
8+
{
9+
printf("Enter element for a[%d] : ",i);
10+
scanf("%d", &a[i]);
11+
}
12+
printf("Enter key element : ");
13+
scanf("%d", &key);
14+
// Write the code to sort the elements using any sorting technique
15+
for(i = 0; i < n - 1; i++)
16+
{
17+
for(j = 0; j < n - i - 1; j++)
18+
{
19+
if(a[j] > a[j + 1])
20+
{
21+
temp = a[j];
22+
a[j] = a[j + 1];
23+
a[j + 1] = temp;
24+
}
25+
}
26+
}
27+
printf("After sorting the elements in the array are\n");
28+
// Write the code to display the elements
29+
for(i = 0; i < n; i++)
30+
printf("Value of a[%d] = %d\n", i, a[i]);
31+
low = 0; // Complete the statement
32+
high = n - 1; // Complete the statement
33+
// Write the code to search an element using binary search process
34+
while(low <= high)
35+
{
36+
mid = (low + high) / 2;
37+
if(a[mid] == key)
38+
{
39+
flag = 1;
40+
break;
41+
}
42+
else if(key > a[mid])
43+
low = mid + 1;
44+
else
45+
high = mid - 1;
46+
}
47+
if (flag == 1 ) { // Write the condition part
48+
printf("The key element %d is found at the position %d\n", key, mid); // Complete the statement
49+
} else {
50+
printf("The Key element %d is not found in the array\n", key); // Complete the statement
51+
}
52+
}

bubbleSort.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include<stdio.h>
2+
void main() {
3+
int a[20], i, n, j, temp;
4+
printf("Enter value of n : ");
5+
scanf("%d", &n);
6+
// Write the for loop to read array elements
7+
for(i = 0; i < n; i++)
8+
{
9+
printf("Enter element for a[%d] : ", i);
10+
scanf("%d", &a[i]);
11+
}
12+
printf("Before sorting the elements in the array are\n");
13+
// Write the for loop to display array elements before sorting
14+
for(i = 0; i < n; i++)
15+
printf("Value of a[%d] = %d\n", i, a[i]);
16+
//Write the code to sort elements
17+
for(i = 0; i < n - 1; i++)
18+
{
19+
for(j = 0; j < n - i - 1; j++)
20+
{
21+
if(a[j] > a[j+1])
22+
{
23+
temp = a[j];
24+
a[j] = a[j + 1];
25+
a[j + 1] = temp;
26+
}
27+
}
28+
}
29+
printf("After sorting the elements in the array are\n");
30+
// Write the for loop to display array elements after sorting
31+
for(i = 0; i < n; i++)
32+
printf("Value of a[%d] = %d\n", i, a[i]);
33+
}

doubleLinkedList.c

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
typedef struct DLL
4+
{
5+
struct DLL *lptr;
6+
int no;
7+
struct DLL *rptr;
8+
}node;
9+
10+
node *left = NULL, *right = NULL;
11+
12+
void insert();
13+
void deletion();
14+
void display();
15+
16+
void main()
17+
{
18+
int ch;
19+
while(1)
20+
{
21+
printf("Operations on doubly linked list\n");
22+
printf("1. Insert \n2.Remove\n3. Display\n0. Exit\n");
23+
printf("Enter Choice 0-4? : ");
24+
scanf("%d", &ch);
25+
switch(ch)
26+
{
27+
case 0: exit(0);
28+
case 1: insert();break;
29+
case 2: deletion();break;
30+
case 3: display();
31+
}
32+
}
33+
}
34+
void insert()
35+
{
36+
node *p,*ptr;
37+
p = malloc(sizeof(node));
38+
printf("Enter number: ");
39+
scanf("%d", &p->no);
40+
if(left == NULL)
41+
{
42+
p -> lptr = p -> rptr = NULL;
43+
left = right = p;
44+
return;
45+
}
46+
if(p -> no <= left -> no)
47+
{
48+
p -> lptr = NULL;
49+
p -> rptr = left;
50+
left -> lptr = p;
51+
left = p;
52+
return;
53+
}
54+
if(p -> no > right -> no)
55+
{
56+
p -> lptr = right;
57+
p -> rptr = NULL;
58+
right -> rptr = p;
59+
right = p;
60+
return;
61+
}
62+
ptr = left -> rptr;
63+
{
64+
while(ptr != right && p -> no > ptr -> no)
65+
{
66+
ptr = ptr -> rptr;
67+
}
68+
p -> rptr = ptr;
69+
p -> lptr = ptr -> lptr;
70+
ptr -> lptr -> rptr = p;
71+
ptr -> lptr = p;
72+
}
73+
}
74+
void deletion()
75+
{
76+
int rno;
77+
node *ptr;
78+
if(left == NULL)
79+
{
80+
printf("Underflow\n");
81+
return;
82+
}
83+
printf("Enter number to delete: ");
84+
scanf("%d", &rno);
85+
if(rno == left -> no)
86+
{
87+
if(left == right)
88+
{
89+
left = right = NULL;
90+
return;
91+
}
92+
ptr = left;
93+
left = left -> rptr;
94+
left -> lptr = NULL;
95+
free(ptr);
96+
return;
97+
}
98+
if(rno == right -> no)
99+
{
100+
ptr = right;
101+
right = right -> lptr;
102+
right -> rptr = NULL;
103+
free(ptr);
104+
return;
105+
}
106+
ptr = left -> rptr;
107+
while(ptr != right && rno > ptr -> no)
108+
{
109+
ptr = ptr -> rptr;
110+
}
111+
if(ptr == right || rno < ptr -> no)
112+
{
113+
printf("%d not found.\n", rno);
114+
return;
115+
}
116+
ptr -> rptr -> lptr = ptr -> lptr;
117+
ptr -> lptr -> rptr = ptr -> rptr;
118+
free(ptr);
119+
}
120+
void display()
121+
{
122+
node *ptr;
123+
if(left == NULL)
124+
{
125+
printf("List is empty\n");
126+
return;
127+
}
128+
ptr = left;
129+
while(ptr != NULL)
130+
{
131+
printf("%d\t", ptr->no);
132+
ptr = ptr -> rptr;
133+
}
134+
printf("\n");
135+
}
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+

insertionSort.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<stdio.h>
2+
void main() {
3+
int a[20], i, n, j, temp;
4+
printf("Enter value of n : ");
5+
scanf("%d", &n);
6+
// Write the for loop to read array elements
7+
for(i = 0; i < n; i++)
8+
{
9+
printf("Enter element for a[%d] : ", i);
10+
scanf("%d", &a[i]);
11+
}
12+
printf("Before sorting the elements in the array are\n");
13+
// Write the for loop to display array elements before sorting
14+
for(i = 0; i < n; i++)
15+
{
16+
printf("Value of a[%d] = %d\n", i, a[i]);
17+
}
18+
//Write the code to sort elements
19+
for(i = 1; i < n; i++)
20+
{
21+
temp = a[i];
22+
for(j = i; j > 0; j--)
23+
{
24+
if(a[j - 1] > temp)
25+
{
26+
a[j] = a[j - 1];
27+
a[j - 1] = temp;
28+
}
29+
}
30+
}
31+
printf("After sorting the elements in the array are\n");
32+
// Write the for loop to display array elements after sorting
33+
for(i = 0; i < n; i++)
34+
{
35+
printf("Value of a[%d] = %d\n", i, a[i]);
36+
}
37+
}

0 commit comments

Comments
 (0)