Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- //This function traverses the array accordingly to the input guven by user
- void traverse(int i,int a[], int len) {
- printf("\nTotal number of elements are : %d",len);
- printf("\nElements are : ");
- for(i = 0 ; i < len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- //This function inserts element at a desired location in the array.
- void insert_ele(int i, int a[], int *len) {
- int ele,loc;
- printf("\nEnter element to be inserted :" );
- scanf("%d",&ele);
- printf("\nEnter loc");
- scanf("%d",&loc);
- if(i==19) printf("\Overflow");
- else {
- while(i>=loc) {
- a[i+1]=a[i];
- i--;
- }
- a[loc]=ele;
- (*len)++;
- printf("\nNew Array :");
- for(i = 0 ; i < *len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- //This functions deletes an element from a desired location in the array.
- void delete_ele(int i, int a[], int *len) {
- int loc;
- printf("\nEnter location of element to be deleted");
- scanf("%d",&loc);
- if(*len==0) { printf("\nUnderflow"); }
- else {
- for(i = loc ; i < (*len-1) ; i++) {
- a[i]=a[i+1];
- }
- (*len)--;
- printf("\nNew Array :");
- for(i = 0 ; i < *len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- //This function searches an element from the array.
- void find_ele(int i, int a[], int len) {
- int ele, count=0;
- printf("\nEnter element to be searched :");
- scanf("%d",&ele);
- for(i = 0 ; i < len ; i++) {
- if(a[i]==ele) { count++;}
- }
- if(count == 0) {
- printf("\nElement does not exist");
- }
- else {
- printf("\nElement found %d times",count);
- }
- }
- //This function includes taking and printing user input and switch case to select from different option what user want to do.
- void main() {
- int a[20], i, len, ch;
- char choice;
- clrscr();
- printf("\nEnter number of elements you want to enter");
- scanf("%d",&len);
- printf("\nEnter the elements :");
- for(i = 0 ; i < len ; i++) {
- scanf("%d",&a[i]);
- }
- printf("\nElements inserted are :");
- for(i = 0 ; i < len ; i++) {
- printf("%d",a[i]);
- }
- i--;
- label:
- printf("\nPress 1 to insert, 2 to delete,3 to find any element");
- printf("\n or 4 to traverse the array");
- scanf("%d",&ch);
- switch(ch) {
- case 1:insert_ele(i, a, &len);
- break;
- case 2:delete_ele(i, a, &len);
- break;
- case 3:find_ele(i, a, len);
- break;
- case 4:traverse(i, a, len);
- break;
- default:printf("\nInvalid option");
- break;
- }
- printf("\nPress y to continue or any other key to quit :");
- scanf("%s",&choice);
- if(choice == 'y' || choice == 'Y') {
- goto label;
- }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- int i,j,n,pivot,a[20];
- void quick(int a[], int left, int right);
- void swap(int a[], int i, int j);
- void main() {
- int n,a[20];
- textcolor(15);
- clrscr();
- printf("\n\nQUICK SORT");
- printf("\n\nEnter the limit : ");
- scanf("%d",&n);
- printf("\n\nEnter the element\n\n");
- for(i = 0 ; i < n ; i++) {
- scanf("%d",&a[i])
- }
- quick(a,0,n-1);
- printf("\n\nThe sorted list is \n\n");
- for(i = 0 ; i < n ; i++) {
- printf("%d ",a[i]);
- }
- getch();
- }
- void quick(int a[], int first, int last) {
- if(first<last) {
- pivot=a[first];
- i=first;
- j=last;
- while(i < j) {
- while(a[i] <= pivot && i < last)
- i++;
- while(a[j] >= pivot && j > first)
- j--;
- if(i<j){ swap(a,i,j); }
- }
- swap(a,first,j);
- quick(a,first,j-1);
- quick(a,j+1,last);
- }
- }
- void swap(int a[], int i, int j) {
- int temp;
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- #include<stdio.h>
- #include<conio.h>
- void bsort(int [], int);
- void main() {
- int a[20], n, i;
- clrscr();
- printf("Enter number of elements");
- scanf("%d",&n);
- printf("Enter the array");
- for(i = 0 ; i < n ; i++) {
- scanf("%d",&a[i]);
- }
- bsort(a,n);
- printf("sorted elements\n");
- for(i = 0 ; i < n ; i++) {
- printf("%d\n",a[i]);
- }
- getch();
- }
- void bsort(int a[],int n) {
- int temp, i=1, j, flag=0;
- while(i <= n-1 && flag == 0) {
- flag=1;
- for(j = 0 ; j <= n-i-1 ; j++) {
- if(a[j] > a[j+1]) {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- flag=0;
- }
- i=i+1;
- }
- }
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #define NULL 0
- struct queuenode {
- int info;
- struct queuenode *link;
- };
- typedef struct queuenode node;
- void enqueue(node** front, node** rear, int item) {
- node *new;
- new=(node*) malloc(sizeof(node));
- new->info=item;
- if(*front==NULL) *front=new; *rear=new;
- else { (*rear)->link=new;
- (*rear)=new;
- }
- new->link=NULL;
- }
- void dequeue(node** front) {
- node *save;
- if(*front==NULL) printf("undeflow: queue empty");
- else {
- printf("element deleted=%d",(*front)->info);
- save=*front;
- *front=save->link;
- free(save);
- }
- }
- void display(node *front) {
- node *ptr=front;
- printf("\n traversing queue:");
- while(ptr!=NULL) {
- printf("%d",ptr->info);
- ptr=ptr->link;
- }
- }
- void main() {
- node *front=NULL;
- node *rear=NULL;
- int choice,item;
- char ch;
- clrscr();
- printf("1:ENQUEUE\n2.DEQUEUE\n3.Display\n4.Another to exit");
- do {
- printf("\nEnter your choice:");
- scanf("%d",&choice);
- switch(choice) {
- case 1:
- printf("Enter value to be inserted:\n");
- scanf("%d",&item);
- enqueue(&front,&rear,item);
- break;
- case 2:
- dequeue(&front);
- break;
- case 3:
- display(front);
- break;
- default:
- printf("\nend of operation \n");
- }
- printf("want to continue:(y or Y)=");
- fflush(stdin);
- ch=getchar();
- } while((ch == 'y')||(ch == 'n'));
- printf("\n end of program");
- getch();
- #include<stdio.h>
- #include<conio.h>
- void traverse(int i, int a[], int len) {
- printf("\nTotal number of elements are : %d",len);
- printf("\nElements are : ");
- for(i=0; i<len; i++) {
- printf("\t%d",a[i]);
- }
- }
- void insert_ele(int i, int a[], int *len) {
- int ele,loc;
- printf("\nEnter element to be inserted :" );
- scanf("%d",&ele);
- printf("\nEnter loc");
- scanf("%d",&loc);
- if(i==19) printf("\Overflow");
- else {
- while(i>=loc) {
- a[i+1]=a[i];
- i--;
- }
- a[loc]=ele;
- (*len)++;
- printf("\nNew Array :");
- for(i = 0 ; i < *len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- void delete_ele(int i, int a[], int *len) {
- int loc;
- printf("\nEnter location of element to be deleted");
- scanf("%d",&loc);
- if(*len==0) printf("\nUnderflow");
- else {
- for(i = loc ; i < (*len-1) ; i++) {
- a[i]=a[i+1];
- }
- (*len)--;
- printf("\nNew Array :");
- for(i = 0 ; i < *len ; i++) {
- printf("\t%d",a[i]);
- }
- }
- }
- void find_ele(int i, int a[], int len) {
- int ele, count=0;
- printf("\nEnter element to be searched :");
- scanf("%d",&ele);
- for(i = 0 ; i < len ; i++) {
- if(a[i]==ele) count++;
- }
- if(count == 0) {
- printf("\nElement does not exist");
- }
- else {
- printf("\nElement found %d times",count);
- }
- }
- void main() {
- int a[20], i, len, ch;
- char choice;
- clrscr();
- printf("\nEnter number of elements you want to enter");
- scanf("%d",&len);
- printf("\nEnter the elements :");
- for(i = 0 ; i < len ; i++) {
- scanf("%d",&a[i]);
- }
- printf("\nElements inserted are :");
- for(i = 0 ; i < len ; i++) {
- printf("%d",a[i]);
- }
- i--;
- label:
- printf("\nPress 1 to insert, 2 to delete,3 to find any element");
- printf("\n or 4 to traverse the array");
- scanf("%d",&ch);
- switch(ch) {
- case 1:insert_ele(i, a, &len);
- break;
- case 2:delete_ele(i, a, &len);
- break;
- case 3:find_ele(i, a, len);
- break;
- case 4:traverse(i, a, len);
- break;
- default:printf("\nInvalid option");
- break;
- }
- printf("\nPress y to continue or any other key to quit :");
- scanf("%s",&choice);
- if(choice == 'y' || choice =='Y') {
- goto label;
- }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- #define max 5
- int top=-1;
- int stack[max];
- //This function push an element into the stack.
- void push() {
- int item;
- if(top == (max-1)) printf("\n stack is full oro verflow \n");
- else {
- printf("\nEnter the item:");
- scanf("%d",&item);
- stack[top]=item;
- top=top+1;
- }
- }
- //This function pop an element from the stack
- void pop() {
- int temp;
- if(top==-1) { printf("\n stack is empty or underflow \n"); }
- else {
- temp=stack[top];
- printf("item deleted is %d",temp);
- top=top-1;
- }
- }
- //This function is used to desipla the current elements in stack
- void display() {
- int i;
- if(top==-1) {
- printf("\nstack is empty\n");
- }
- else {
- printf("Elements of stack are:\n");
- for(i = -1 ; i <= top-1 ; i++) {
- printf("%d\n",stack[i]);
- }
- }
- }
- //This function contains the switch statement to choose from different options
- void main() {
- int choice;
- clrscr();
- while(1) {
- printf("1:Push\n 2:Pop\n 3:Display\n 4:Exit");
- printf("\nEnter your choice 1-4:");
- scanf("%d",&choice);
- switch(choice) {
- case 1:push();
- break;
- case 2:pop();
- break;
- case 3:display();
- break;
- case 4:exit(0);
- }
- }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- void merge_split(int a[], int first, int last);
- void merge(int a[], int f1, int l1, int f2, int l2);
- int a[25],b[25];
- void main() {
- int i,n;
- clrscr();
- printf("\n\nMERGE SORT");
- printf("\n\nEnter the limit : ");
- scanf("%d",&n);
- printf("\nEnter the elements\n");
- for(i = 0 ; i < n ; i++) {
- scanf("%d",&a[i]);
- }
- merge_split(a,0,n-1);
- printf("\n\nSorted list : ");
- for(i = 0 ; i < n ; i++) {
- printf("\n %d",a[i]);
- }
- getch();
- }
- void merge_split(int a[], int first, int last) {
- int mid;
- if(first<last) {
- mid=(first+last)/2;
- merge_split(a,first,mid);
- merge_split(a,mid+1,last);
- merge(a,first,mid,mid+1,last);
- }
- }
- void merge(int a[], int f1, int l1, int f2, int l2) {
- int i,j,k=0;
- i=f1;
- j=f2;
- while(i <= l1 && j <= l2) {
- if(a[i]<a[j])
- b[k]=a[i++];
- else
- b[k]=a[j++];
- k++;
- }
- while(i<=l1)
- b[k++]=a[i++];
- while(j<=l2)
- b[k++]=a[j++];
- i=f1;
- j=0;
- while(i<=l2 && j<k)
- a[i++]=b[j++];
- }
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int a[10], i, n, m, c=0;
- clrscr();
- printf("Enter the size of an array");
- scanf("%d",&n);
- printf("\nEnter the elements of the array");
- for(i = 0; i <=n-1 ; i++) {
- scanf("%d",&a[i]);
- }
- printf("\nThe elements of an array are");
- for(i = 0 ; i <= n-1 ; i++) {
- printf(" %d",a[i]);
- }
- printf("\nEnter the number to be search");
- scanf("%d",&m);
- for(i = 0 ; i <= n-1 ; i++) {
- if(a[i]==m) {
- c=1;
- break;
- }
- }
- if(c==0) printf("\nThe number is not in the list");
- else{ printf("\nThe number is found"); }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int a[10], i, n, m, c=0;
- clrscr();
- printf("Enter the size of an array");
- scanf("%d",&n);
- printf("\nEnter the elements of the array");
- for(i = 0; i <=n-1 ; i++) {
- scanf("%d",&a[i]);
- }
- printf("\nThe elements of an array are");
- for(i = 0 ; i <= n-1 ; i++) {
- printf(" %d",a[i]);
- }
- printf("\nEnter the number to be search");
- scanf("%d",&m);
- for(i = 0 ; i <= n-1 ; i++) {
- if(a[i]==m) {
- c=1;
- break;
- }
- }
- if(c==0) printf("\nThe number is not in the list");
- else{ printf("\nThe number is found"); }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int n, a[30], item, i, j, mid, top, bottom;
- clrscr();
- printf("Enter how many elements you want:\n");
- scanf("%d", &n);
- printf("Enter the %d elements in ascending order\n", n);
- for (i = 0 ; i < n ; i++) {
- scanf("%d", &a[i]);
- }
- printf("\nEnter the item to search\n");
- scanf("%d", &item);
- bottom = 1;
- top = n;
- do {
- mid = (bottom + top) / 2;
- if (item < a[mid])
- top = mid - 1;
- else if (item > a[mid])
- bottom = mid + 1;
- } while (item != a[mid] && bottom <= top);
- if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
- else {
- printf("\n Search failed\n %d not found\n", item);
- }
- getch();
- }
- #include<stdio.h>
- #include<conio.h>
- void main() {
- int n, a[30], item, i, j, mid, top, bottom;
- clrscr();
- printf("Enter how many elements you want:\n");
- scanf("%d", &n);
- printf("Enter the %d elements in ascending order\n", n);
- for (i = 0 ; i < n ; i++) {
- scanf("%d", &a[i]);
- }
- printf("\nEnter the item to search\n");
- scanf("%d", &item);
- bottom = 1;
- top = n;
- do {
- mid = (bottom + top) / 2;
- if (item < a[mid])
- top = mid - 1;
- else if (item > a[mid])
- bottom = mid + 1;
- } while (item != a[mid] && bottom <= top);
- if (item == a[mid]) { printf("Binary search successfull!!\n"); printf("\n %d found in position: %d\n", item, mid + 1) }
- else {
- printf("\n Search failed\n %d not found\n", item);
- }
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment