Skip to content

Commit eab37be

Browse files
data structure
0 parents  commit eab37be

29 files changed

+2442
-0
lines changed

Diff for: APPEND.C

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
void main()
4+
{
5+
int a[10],b[10],i,j,m,n;
6+
clrscr();
7+
printf("Enter the size of first array: ");
8+
scanf("%d",&m);
9+
printf("\nEnter %d elements into first array:\n",m);
10+
for(i=0;i<m;i++)
11+
scanf("%d",&a[i]);
12+
printf("\nEnter the size of second array: ");
13+
scanf("%d",&n);
14+
printf("\nEnter %d elements into second array:\n",n);
15+
for(i=0;i<n;i++)
16+
scanf("%d",&b[i]);
17+
for(i=m,j=0;j<n;i++,j++)
18+
a[i]=b[j];
19+
printf("\nFirst array after appending second array:\n");
20+
for(i=0;i<m+n;i++)
21+
printf("\n%d",a[i]);
22+
getch();
23+
}

Diff for: BINSEARC.C

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
int search(int [],int,int);
5+
6+
void main()
7+
{
8+
int a[10],i,n,x,loc;
9+
clrscr();
10+
printf("Enter the size : ");
11+
scanf("%d",&n);
12+
printf("\nEnter %d elements into the array in ascending order:\n",n);
13+
for(i=0;i<n;i++)
14+
scanf("%d",&a[i]);
15+
printf("\nEnter the element to be searched : ");
16+
scanf("%d",&x);
17+
loc=search(a,n,x);
18+
if(loc==-1) printf("\n%d is not found.",x);
19+
else printf("\n%d is found at position %d in the array.",x,loc);
20+
getch();
21+
}
22+
23+
int search(int a[],int n,int v)
24+
{
25+
int lb,ub,middle;
26+
lb=0;
27+
ub=n-1;
28+
while(lb<=ub)
29+
{
30+
middle=(lb+ub)/2;
31+
if(a[middle]==v) return middle;
32+
if(v<a[middle]) ub=middle-1;
33+
else lb=middle+1;
34+
}
35+
return -1;
36+
}

Diff for: BSERREC.C

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
int search(int [],int,int);
5+
int binsearch(int [],int,int,int);
6+
7+
void main()
8+
{
9+
int a[10],i,n,x,loc;
10+
clrscr();
11+
printf("Enter the size : ");
12+
scanf("%d",&n);
13+
printf("\nEnter %d elements into the array in ascending order:\n",n);
14+
for(i=0;i<n;i++)
15+
scanf("%d",&a[i]);
16+
printf("\nEnter the element to be searched : ");
17+
scanf("%d",&x);
18+
loc=search(a,n,x);
19+
if(loc==-1) printf("\n%d is not found.",x);
20+
else printf("\n%d is found at position %d in the array.",x,loc+1);
21+
getch();
22+
}
23+
24+
int search(int a[],int n,int v)
25+
{
26+
return binsearch(a,0,n-1,v);
27+
}
28+
int binsearch(int a[],int lb,int ub,int v)
29+
{
30+
int middle=(lb+ub)/2;
31+
if(lb>ub) return -1;
32+
else if(a[middle]==v) return middle;
33+
else
34+
{
35+
if(v<a[middle]) ub=middle-1;
36+
else lb=middle+1;
37+
return binsearch(a,lb,ub,v);
38+
}
39+
}

Diff for: BSTSEARC.C

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<alloc.h>
4+
5+
typedef struct treeNode* treePointer;
6+
7+
struct treeNode
8+
{
9+
int data;
10+
treePointer leftChild,rightChild;
11+
};
12+
13+
treePointer root=NULL;
14+
15+
void create();
16+
void search(int);
17+
18+
void main()
19+
{
20+
int choice,n;
21+
do
22+
{
23+
clrscr();
24+
printf("\n\tBINARY SEARCH TREE OPERATIONS\n");
25+
printf("\n\t\t1. Create");
26+
printf("\n\t\t2. Search");
27+
printf("\n\t\t3. Exit");
28+
printf("\n\n\tEnter your choice(1-3) : ");
29+
scanf("%d",&choice);
30+
switch(choice)
31+
{
32+
case 1:
33+
create();
34+
break;
35+
case 2:
36+
if(root==NULL) printf("\nTree is empty...");
37+
else
38+
{
39+
printf("\nEnter the element : ");
40+
scanf("%d",&n);
41+
search(n);
42+
}
43+
break;
44+
default:continue;
45+
}
46+
getch();
47+
}while(choice!=3);
48+
}
49+
50+
void create()
51+
{
52+
int i,n=0,left;
53+
treePointer node,temp,p;
54+
if(root!=NULL)
55+
{
56+
printf("\nTree already exists...");
57+
return;
58+
}
59+
printf("\nEnter the number of nodes : ");
60+
scanf("%d",&n);
61+
printf("\nEnter %d nodes for the binary search tree:\n",n);
62+
root=(treePointer)malloc(sizeof(*root));
63+
scanf("%d",&root->data);
64+
root->leftChild=root->rightChild=NULL;
65+
for(i=0;i<n-1;i++)
66+
{
67+
temp=(treePointer)malloc(sizeof(*temp));
68+
scanf("%d",&temp->data);
69+
temp->leftChild=temp->rightChild=NULL;
70+
node=root;
71+
while(node!=NULL)
72+
{
73+
left=0;
74+
p=node;
75+
if(temp->data<node->data)
76+
{
77+
left=1;
78+
node=node->leftChild;
79+
}
80+
else node=node->rightChild;
81+
}
82+
if(left) p->leftChild=temp;
83+
else p->rightChild=temp;
84+
}
85+
printf("\n\nBinary search tree created with %d nodes...",n);
86+
}
87+
void search(int x)
88+
{
89+
treePointer node;
90+
node=root;
91+
while(node!=NULL)
92+
{
93+
if(node->data==x)
94+
{
95+
printf("\nElement is found");
96+
return;
97+
}
98+
if(x<node->data) node=node->leftChild;
99+
else node=node->rightChild;
100+
}
101+
printf("\nElement is not found");
102+
}

Diff for: BUBBSORT.C

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
void sort(int*,int);
5+
6+
void main()
7+
{
8+
int a[10],n,i;
9+
clrscr();
10+
printf("Enter the size : ");
11+
scanf("%d",&n);
12+
printf("\nEnter %d elements into the array :\n",n);
13+
for(i=0;i<n;i++)
14+
scanf("%d",&a[i]);
15+
printf("\nArray before sorting :");
16+
for(i=0;i<n;i++)
17+
printf(" %d",a[i]);
18+
sort(a,n);
19+
printf("\n\nArray after sorting :");
20+
for(i=0;i<n;i++)
21+
printf(" %d",a[i]);
22+
getch();
23+
}
24+
25+
void sort(int a[],int n)
26+
{
27+
int i,j,temp,swapped=1;
28+
for(i=0;i<n-1 && swapped;i++)
29+
{
30+
swapped=0;
31+
for(j=0;j<n-i-1;j++)
32+
if(a[j]>a[j+1])
33+
{
34+
temp=a[j];
35+
a[j]=a[j+1];
36+
a[j+1]=temp;
37+
swapped=1;
38+
}
39+
}
40+
}
41+

Diff for: CIRQUEUE.C

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
4+
#define LENGTH 5
5+
6+
int queue[LENGTH];
7+
int front=0,rear=0;
8+
9+
void add(int);
10+
void del();
11+
void list();
12+
13+
void main()
14+
{
15+
int ch,n;
16+
do
17+
{
18+
clrscr();
19+
printf("\n\tQUEUE OPERATIONS\n");
20+
printf("\n\t\t1.ADD");
21+
printf("\n\t\t2.DELETE");
22+
printf("\n\t\t3.LIST");
23+
printf("\n\t\t4.EXIT");
24+
printf("\n\n\tEnter your choice(1-4) : ");
25+
scanf("%d",&ch);
26+
switch(ch)
27+
{
28+
case 1:
29+
printf("\nEnter the element : ");
30+
scanf("%d",&n);
31+
add(n);
32+
break;
33+
case 2:
34+
del();
35+
break;
36+
case 3:
37+
list();
38+
break;
39+
default:continue;
40+
}
41+
getch();
42+
}while(ch!=4);
43+
}
44+
45+
void add(int x)
46+
{
47+
if((rear+1)%LENGTH==front)
48+
{
49+
printf("\nSorry, queue is full...");
50+
return;
51+
}
52+
rear=(rear+1)%LENGTH;
53+
queue[rear]=x;
54+
printf("\n%d is added...",x);
55+
}
56+
void del()
57+
{
58+
if(front==rear)
59+
{
60+
printf("\nSorry, queue is empty...");
61+
return;
62+
}
63+
front=(front+1)%LENGTH;
64+
printf("\n%d is removed...",queue[front]);
65+
}
66+
void list()
67+
{
68+
int i;
69+
if(front==rear)
70+
{
71+
printf("\nSorry, queue is empty...");
72+
return;
73+
}
74+
printf("\nThe queue :\n\n");
75+
i=front;
76+
do
77+
{
78+
i=(i+1)%LENGTH;
79+
printf("%d\t",queue[i]);
80+
}
81+
while(i!=rear);
82+
}
83+

0 commit comments

Comments
 (0)