samroar04

Untitled

Feb 11th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void traverse(int i,int a[],int len) {
  4. printf("\nTotal number of elements are : %d",len);
  5. printf("\nElements are : ");
  6. for(i=0; i<len; i++) {
  7. printf("\t%d",a[i]);
  8. }
  9. }
  10. void insert_ele(int i, int a[], int *len) {
  11. int ele,loc;
  12. printf("\nEnter element to be inserted :" );
  13. scanf("%d",&ele);
  14. printf("\nEnter loc");
  15. scanf("%d",&loc);
  16. if(i==19) {printf("\Overflow");}
  17. else {
  18. while(i>=loc) {
  19. a[i+1]=a[i];
  20. i--;
  21. }
  22. a[loc]=ele;
  23. (*len)++;
  24. printf("\nNew Array :");
  25. for(i=0 ; i<*len ; i++) {
  26. printf("\t%d",a[i]);
  27. }
  28. }
  29. }
  30. void delete_ele(int i,int a[], int *len) {
  31. int loc;
  32. printf("\nEnter location of element to be deleted");
  33. scanf("%d",&loc);
  34. if(*len==0) { printf("\nUnderflow"); }
  35. else {
  36. for(i=loc ; i<(*len-1) ; i++) {
  37. a[i]=a[i+1];
  38. }
  39. (*len)--;
  40. printf("\nNew Array :");
  41. for(i=0 ; i<*len ; i++) {
  42. printf("\t%d",a[i]);
  43. }
  44. }
  45. }
  46. void find_ele(int i,int a[], int len) {
  47. int ele, count=0;
  48. printf("\nEnter element to be searched :");
  49. scanf("%d",&ele);
  50. for(i=0 ; i<len ; i++) {
  51. if(a[i]==ele) { count++;}
  52. }
  53. if(count == 0) {
  54. printf("\nElement does not exist");
  55. }
  56. else {
  57. printf("\nElement found %d times",count);
  58. }
  59. }
  60. void main() {
  61. int a[20], i, len, ch;
  62. char choice;
  63. clrscr();
  64. printf("\nEnter number of elements you want to enter");
  65. scanf("%d",&len);
  66. printf("\nEnter the elements :");
  67. for(i=0 ; i<len; i++) {
  68. scanf("%d",&a[i]);
  69. }
  70. printf("\nElements inserted are :");
  71. for(i=0 ; i<len; i++) {
  72. printf("%d",a[i]);
  73. }
  74. i--;
  75. label:
  76. printf("\nPress 1 to insert, 2 to delete,3 to find any element");
  77. printf("\n or 4 to traverse the array");
  78. scanf("%d",&ch);
  79. switch(ch) {
  80. case 1:insert_ele(i, a, &len);
  81. break;
  82. case 2:delete_ele(i, a, &len);
  83. break;
  84. case 3:find_ele(i, a, len);
  85. break;
  86. case 4:traverse(i, a, len);
  87. break;
  88. default:printf("\nInvalid option");
  89. break;
  90. }
  91. printf("\nPress y to continue or any other key to quit :");
  92. scanf("%s",&choice);
  93. if(choice== 'y' || choice=='Y') {
  94. goto label;
  95. }
  96. getch();
  97. }
  98.  
  99.  
  100.  
  101. #include<stdio.h>
  102. #include<conio.h>
  103. void main() {
  104. int n, a[30], item, i, j, mid, top, bottom;
  105. clrscr();
  106. printf("Enter how many elements you want:\n");
  107. scanf("%d", &n);
  108. printf("Enter the %d elements in ascending order\n", n);
  109. for (i = 0; i < n; i++) {
  110. scanf("%d", &a[i]);
  111. }
  112. printf("\nEnter the item to search\n");
  113. scanf("%d", &item);
  114. bottom = 1;
  115. top = n;
  116. do {
  117. mid = (bottom + top) / 2;
  118. if (item < a[mid])
  119. top = mid - 1;
  120. else if (item > a[mid])
  121. bottom = mid + 1;
  122. } while (item != a[mid] && bottom <= top);
  123.  
  124. if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
  125. else {
  126. printf("\n Search failed\n %d not found\n", item);
  127. }
  128. getch();
  129. }
  130.  
  131.  
  132.  
  133. #include<stdio.h>
  134. #include<conio.h>
  135. void bsort(int [], int);
  136. void main() {
  137. int a[20],n,i;
  138. clrscr();
  139. printf("Enter number of elements");
  140. scanf("%d",&n);
  141. printf("Enter the array");
  142. for(i=0;i<n;i++) {
  143. scanf("%d",&a[i]);
  144. }
  145. bsort(a,n);
  146. printf("sorted elements\n");
  147. for(i=0;i<n;i++) {
  148. printf("%d\n",a[i]);
  149. }
  150. getch();
  151. }
  152. void bsort(int a[],int n) {
  153. int temp, i=1,j,flag=0;
  154. while(i<=n-1 && flag==0) {
  155. flag=1;
  156. for(j=0;j<=n-i-1;j++) {
  157. if(a[j]>a[j+1]) {
  158. temp=a[j];
  159. a[j]=a[j+1];
  160. a[j+1]=temp;
  161. }
  162. flag=0;
  163. }
  164. i=i+1;
  165. }
  166. }
  167.  
  168.  
  169.  
  170. #include<stdio.h>
  171. #include<conio.h>
  172. void insertsort(int a[], int n) {
  173. int i,j,target;
  174. for(i=1;i<n;i++) {
  175. target=a[i];
  176. j=i-1;
  177. while((j>=0) && (target<=a[j])) {
  178. a[j+1]=a[j];
  179. j=j-1;
  180. }
  181. a[j+1]=target;
  182. }
  183. }
  184. void display(int a[], int n) {
  185. int i;
  186. for(i=0;i<n;i++) {
  187. printf("%d\n",a[i]);
  188. }
  189. }
  190. void main() {
  191. int a[20],n,i;
  192. clrscr();
  193. printf("Enter the total number of elements");
  194. scanf("%d",&n);
  195. printf("Enterthe elements\n");
  196. for(i=0;i<n;i++) {
  197. scanf("%d",&a[i]);
  198. }
  199. insertsort(a,n);
  200. printf("\n Array elements after sorting\n");
  201. display(a,n);
  202. getch();
  203. }
  204.  
  205.  
  206. #include<stdio.h>
  207. #include<conio.h>
  208. void insertsort(int a[], int n) {
  209. int i,j,target;
  210. for(i=1;i<n;i++) {
  211. target=a[i];
  212. j=i-1;
  213. while((j>=0) && (target<=a[j])) {
  214. a[j+1]=a[j];
  215. j=j-1;
  216. }
  217. a[j+1]=target;
  218. }
  219. }
  220. void display(int a[], int n) {
  221. int i;
  222. for(i=0;i<n;i++) {
  223. printf("%d\n",a[i]);
  224. }
  225. }
  226. void main() {
  227. int a[20],n,i;
  228. clrscr();
  229. printf("Enter the total number of elements");
  230. scanf("%d",&n);
  231. printf("Enterthe elements\n");
  232. for(i=0;i<n;i++) {
  233. scanf("%d",&a[i]);
  234. }
  235. insertsort(a,n);
  236. printf("\n Array elements after sorting\n");
  237. display(a,n);
  238. getch();
  239. }
  240.  
  241.  
  242.  
  243. #include<stdio.h>
  244. #include<conio.h>
  245. void main() {
  246. int a[10],i,n,m,c=0;
  247. clrscr();
  248. printf("Enter the size of an array");
  249. scanf("%d",&n);
  250. printf("\nEnter the elements of the array");
  251. for(i=0;i<=n-1;i++) {
  252. scanf("%d",&a[i]);
  253. }
  254. printf("\nThe elements of an array are");
  255. for(i=0;i<=n-1;i++) {
  256. printf(" %d",a[i]);
  257. }
  258. printf("\nEnter the number to be search");
  259. scanf("%d",&m);
  260. for(i=0;i<=n-1;i++) {
  261. if(a[i]==m) {
  262. c=1;
  263. break;
  264. }
  265. }
  266. if(c==0) { printf("\nThe number is not in the list"); }
  267. else{ printf("\nThe number is found"); }
  268. getch();
  269. }
  270.  
  271.  
  272.  
  273. #include<stdio.h>
  274. #include<conio.h>
  275. void traverse(int i,int a[],int len) {
  276. printf("\nTotal number of elements are : %d",len);
  277. printf("\nElements are : ");
  278. for(i=0; i<len; i++) {
  279. printf("\t%d",a[i]);
  280. }
  281. }
  282. void insert_ele(int i, int a[], int *len) {
  283. int ele,loc;
  284. printf("\nEnter element to be inserted :" );
  285. scanf("%d",&ele);
  286. printf("\nEnter loc");
  287. scanf("%d",&loc);
  288. if(i==19) {printf("\Overflow");}
  289. else {
  290. while(i>=loc) {
  291. a[i+1]=a[i];
  292. i--;
  293. }
  294. a[loc]=ele;
  295. (*len)++;
  296. printf("\nNew Array :");
  297. for(i=0 ; i<*len ; i++) {
  298. printf("\t%d",a[i]);
  299. }
  300. }
  301. }
  302. void delete_ele(int i,int a[], int *len) {
  303. int loc;
  304. printf("\nEnter location of element to be deleted");
  305. scanf("%d",&loc);
  306. if(*len==0) { printf("\nUnderflow"); }
  307. else {
  308. for(i=loc ; i<(*len-1) ; i++) {
  309. a[i]=a[i+1];
  310. }
  311. (*len)--;
  312. printf("\nNew Array :");
  313. for(i=0 ; i<*len ; i++) {
  314. printf("\t%d",a[i]);
  315. }
  316. }
  317. }
  318. void find_ele(int i,int a[], int len) {
  319. int ele, count=0;
  320. printf("\nEnter element to be searched :");
  321. scanf("%d",&ele);
  322. for(i=0 ; i<len ; i++) {
  323. if(a[i]==ele) { count++;}
  324. }
  325. if(count == 0) {
  326. printf("\nElement does not exist");
  327. }
  328. else {
  329. printf("\nElement found %d times",count);
  330. }
  331. }
  332. void main() {
  333. int a[20], i, len, ch;
  334. char choice;
  335. clrscr();
  336. printf("\nEnter number of elements you want to enter");
  337. scanf("%d",&len);
  338. printf("\nEnter the elements :");
  339. for(i=0 ; i<len; i++) {
  340. scanf("%d",&a[i]);
  341. }
  342. printf("\nElements inserted are :");
  343. for(i=0 ; i<len; i++) {
  344. printf("%d",a[i]);
  345. }
  346. i--;
  347. label:
  348. printf("\nPress 1 to insert, 2 to delete,3 to find any element");
  349. printf("\n or 4 to traverse the array");
  350. scanf("%d",&ch);
  351. switch(ch) {
  352. case 1:insert_ele(i, a, &len);
  353. break;
  354. case 2:delete_ele(i, a, &len);
  355. break;
  356. case 3:find_ele(i, a, len);
  357. break;
  358. case 4:traverse(i, a, len);
  359. break;
  360. default:printf("\nInvalid option");
  361. break;
  362. }
  363. printf("\nPress y to continue or any other key to quit :");
  364. scanf("%s",&choice);
  365. if(choice== 'y' || choice=='Y') {
  366. goto label;
  367. }
  368. getch();
  369. }
  370.  
  371.  
  372. #include<stdio.h>
  373. #include<conio.h>
  374.  
  375. void merge_split(int a[],int first,int last);
  376. void merge(int a[],int f1,int l1,int f2,int l2);
  377. int a[25],b[25];
  378.  
  379. void main() {
  380. int i,n;
  381. clrscr();
  382. printf("\n\nMERGE SORT");
  383. printf("\n\nEnter the limit : ");
  384. scanf("%d",&n);
  385. printf("\nEnter the elements\n");
  386. for(i=0;i<n;i++) {
  387. scanf("%d",&a[i]);
  388. }
  389. merge_split(a,0,n-1);
  390. printf("\n\nSorted list : ");
  391. for(i=0;i<n;i++) {
  392. printf("\n %d",a[i]);
  393. }
  394. getch();
  395. }
  396.  
  397. void merge_split(int a[],int first,int last) {
  398. int mid;
  399. if(first<last) {
  400. mid=(first+last)/2;
  401. merge_split(a,first,mid);
  402. merge_split(a,mid+1,last);
  403. merge(a,first,mid,mid+1,last);
  404. }
  405. }
  406.  
  407. void merge(int a[],int f1,int l1,int f2,int l2) {
  408. int i,j,k=0;
  409. i=f1;
  410. j=f2;
  411. while(i<=l1 && j<=l2) {
  412. if(a[i]<a[j])
  413. b[k]=a[i++];
  414. else
  415. b[k]=a[j++];
  416. k++;
  417. }
  418. while(i<=l1)
  419. b[k++]=a[i++];
  420. while(j<=l2)
  421. b[k++]=a[j++];
  422. i=f1;
  423. j=0;
  424. while(i<=l2 && j<k)
  425. a[i++]=b[j++];
  426. }
  427.  
  428.  
  429. #include<stdio.h>
  430. #include<conio.h>
  431. #include<string.h>
  432. #define NULL 0
  433. struct queuenode {
  434. int info;
  435. struct queuenode *link;
  436. };
  437. typedef struct queuenode node;
  438. void enqueue(node** front,node** rear,int item) {
  439. node *new;
  440. new=(node*) malloc(sizeof(node));
  441. new->info=item;
  442. if(*front==NULL) { *front=new; *rear=new }
  443. else { (*rear)->link=new;
  444. (*rear)=new;
  445. }
  446. new->link=NULL;
  447. }
  448. void dequeue(node** front) {
  449. node *save;
  450. if(*front==NULL) { printf("undeflow: queue empty");}
  451. else {
  452. printf("element deleted=%d",(*front)->info);
  453. save=*front;
  454. *front=save->link;
  455. free(save);
  456. }
  457. }
  458. void display(node *front) {
  459. node *ptr=front;
  460. printf("\n traversing queue:");
  461. while(ptr!=NULL) {
  462. printf("%d",ptr->info);
  463. ptr=ptr->link;
  464. }
  465. }
  466. void main() {
  467. node *front=NULL;
  468. node *rear=NULL;
  469. int choice,item;
  470. char ch;
  471. clrscr();
  472. printf("1:ENQUEUE\n2.DEQUEUE\n3.Display\n4.Another to exit");
  473. do {
  474. printf("\nEnter your choice:");
  475. scanf("%d",&choice);
  476. switch(choice) {
  477. case 1:
  478. printf("Enter value to be inserted:\n");
  479. scanf("%d",&item);
  480. enqueue(&front,&rear,item);
  481. break;
  482. case 2:
  483. dequeue(&front);
  484. break;
  485. case 3:
  486. display(front);
  487. break;
  488. default:
  489. printf("\nend of operation \n");
  490. }
  491. printf("want to continue:(y or Y)=");
  492. fflush(stdin);
  493. ch=getchar();
  494. } while((ch=='y')||(ch=='n'));
  495. printf("\n end of program");
  496. getch();
  497.  
  498.  
  499.  
  500.  
  501. #include<stdio.h>
  502. #include<conio.h>
  503. int i,j,n,pivot,a[20];
  504. void quick(int a[],int left,int right);
  505. void swap(int a[],int i,int j);
  506. void main() {
  507. int n,a[20];
  508. textcolor(15);
  509. clrscr();
  510. printf("\n\nQUICK SORT");
  511. printf("\n\nEnter the limit : ");
  512. scanf("%d",&n);
  513. printf("\n\nEnter the element\n\n");
  514. for(i=0;i<n;i++) {
  515. scanf("%d",&a[i])
  516. }
  517. quick(a,0,n-1);
  518. printf("\n\nThe sorted list is \n\n");
  519. for(i=0;i<n;i++) {
  520. printf("%d ",a[i]);
  521. }
  522. getch();
  523. }
  524. void quick(int a[],int first,int last) {
  525. if(first<last) {
  526. pivot=a[first];
  527. i=first;
  528. j=last;
  529. while(i<j) {
  530. while(a[i]<=pivot&&i<last)
  531. i++;
  532. while(a[j]>=pivot&&j>first)
  533. j--;
  534. if(i<j){ swap(a,i,j); }
  535. }
  536. swap(a,first,j);
  537. quick(a,first,j-1);
  538. quick(a,j+1,last);
  539. }
  540. }
  541. void swap(int a[],int i,int j) {
  542. int temp;
  543. temp=a[i];
  544. a[i]=a[j];
  545. a[j]=temp;
  546. }
  547.  
  548. #include<stdio.h>
  549. int main() {
  550. int s,i,j,temp,a[20];
  551. clrscr();
  552. printf("Enter total elements: ");
  553. scanf("%d",&s);
  554. printf("Enter %d elements: ",s);
  555. for(i=0;i<s;i++) {
  556. scanf("%d",&a[i]);
  557. }
  558. for(i=0;i<s;i++) {
  559. for(j=i+1;j<s;j++) {
  560. if(a[i]>a[j]) {
  561. temp=a[i];
  562. a[i]=a[j];
  563. a[j]=temp;
  564. }
  565. }
  566. }
  567. printf("After sorting is: ");
  568. for(i=0;i<s;i++) {
  569. printf(" %d",a[i]);
  570. }
  571. getch();
  572. }
  573.  
  574.  
  575.  
  576. #include<stdio.h>
  577. #include<conio.h>
  578. #define max 5
  579. int top=-1;
  580. int stack[max];
  581. void push() {
  582. int item;
  583. if(top==(max-1)) { printf("\n stack is full oro verflow \n");}
  584. else {
  585. printf("\nEnter the item:");
  586. scanf("%d",&item);
  587. stack[top]=item;
  588. top=top+1;
  589. }
  590. }
  591. void pop() {
  592. int temp;
  593. if(top==-1) { printf("\n stack is empty or underflow \n"); }
  594. else {
  595. temp=stack[top];
  596. printf("item deleted is %d",temp);
  597. top=top-1;
  598. }
  599. }
  600. void display() {
  601. int i;
  602. if(top==-1) {
  603. printf("\nstack is empty\n");
  604. }
  605. else {
  606. printf("Elements of stack are:\n");
  607. for(i=-1;i<=top-1;i++) {
  608. printf("%d\n",stack[i]);
  609. }
  610. }
  611. }
  612. void main() {
  613. int choice;
  614. clrscr();
  615. while(1) {
  616. printf("1:Push\n 2:Pop\n 3:Display\n 4:Exit");
  617. printf("\nEnter your choice 1-4:");
  618. scanf("%d",&choice);
  619. switch(choice) {
  620. case 1:push();
  621. break;
  622. case 2:pop();
  623. break;
  624. case 3:display();
  625. break;
  626. case 4:exit(0);
  627. }
  628. }
  629. getch();
  630. }
Advertisement
Add Comment
Please, Sign In to add comment