0% found this document useful (0 votes)
20 views1 page

DS Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views1 page

DS Part 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Selection sort Insertion sort Tower of hanoi Reverse string Binary search

1]#include<stdio.h> 2]#include<stdio.h> ]#include<stdio.h> 4]#include<stdio.h> 5]#include<stdio.h>


#include<conio.h> #include<conio.h> #include<conio.h> #include<conio.h> #include<conio.h>
void main() void main() void #include<string.h> int bsearch(int a[],int
{ { towers(int,char,char,char); #include<stdlib.h> b,int e,int ele){
int int a[25],n,j,i,temp; int main() #define MAX 20 if(b<=e){
a[25],pass,small,n,j,tem clrscr(); { int top=-1; int mid=(b+e)/2;
p,i; printf("enter the number int n; char stack[MAX]; if(a[mid]==ele)
clrscr(); of elements:"); clrscr(); void pop(); return mid;
printf("enter the size of scanf("%d",&n); printf("enter the number of void push(); else if(a[mid]>ele)
the array:\n"); printf("enter %d disks:"); void main(){ return bsearch(a,b,mid-
scanf("%d",&n); elements:\n",n); scanf("%d",&n); char str[10]; 1,ele);
printf("enter the array for(i=0;i<n;i++) printf("sequence of moves int len,i; else
elements:\n"); { involved are:\n"); clrscr(); return
for(i=0;i<n;i++) scanf("%d",&a[i]); towers(n,'A','B','C'); printf("\n enter the string to be bsearch(a,mid+1,e,ele)}
{ } getch(); reversed:"); return -1;}
scanf("%d",&a[i]); for(i=1;i<=n;i++) return 0; gets(str); void main(){
} { } for(i=0;i<strlen(str);i++) int i,n,a[20],ele,res;
for(pass=0;pass<n- temp=a[i]; void towers(int n,char push(str[i]); clrscr();
1;pass++){ j=i-1; s,char m,char d) for(i=0;i<strlen(str);i++) printf("\n enter the size
small=pass; while(j>=0&&a[j]>temp) { pop(); :");
for(i=pass+1;i<n;i++){ { if(n==1) getch();} scanf("%d",&n);
if(a[small]>a[i]){ a[j+1]=a[j]; { void push(char x){ printf("\nenter the
small=i;}} j--; printf("\n move disk 1 if(top==(MAX-1)){ elements:\n");
if(small!=pass){ } from peg %c to peg printf("stack overflow\n"); for(i=0;i<n;i++){
temp=a[small]; a[j+1]=temp; %c",s,d); return;} scanf("%d",&a[i]);}
a[small]=a[pass]; } return; top++; printf("\nenter the search
a[pass]=temp;}} printf("sorted array } stack[top]=x;} element:");
printf("sorted array is:\n"); towers(n-1,s,d,m); void pop(){ scanf("%d",&ele);
is:\n"); for(i=0;i<n;i++) printf("\n move disk %d if(top==-1){ res=bsearch(a,0,n-1,ele);
for(i=0;i<n;i++){ printf("%d\t",a[i]); from peg %c to peg printf("stack underflow\n"); if(res==-1)
printf("%d\t",a[i]);} getch(); %c",n,s,d); return;} printf("\n element not
getch();} } towers(n-1,m,s,d); printf("%c",stack[top]); found");
} top--;} else
printf("\nelement found
at %d position",res+1);
getch();}
Stack operation Queue operation
using array using array Smallest & largest
6]#include<stdio.h> printf("%d is break; 8]
#include<conio.h> popped",S[TOP]); default:printf("invalid #include<conio.h>
#define MAX 3 TOP--;} choice\n"); #include<stdio.h>
int TOP=-1,item,i; void display(){ void main(){
int S[MAX]; if(TOP==-1){
break;}}} int arr_size,i,*arr,max,min;
void push(); printf("Stack is empty"); void insert(){ clrscr();
void pop(); return;} int item; printf("\nenter the size of array:");
void display(); for(i=TOP;i>=0;i--){ if(rear==MAX-1) scanf("%d",arr_sixe);
int main(){ printf("%d\t",S[i]);}} printf("enter the arr=(int*)calloc(arr_size,sizeof(int));
int choice; elements:"); printf("\nenetr the array:");
while(1){ 7]#include<stdio.h> scanf("%d",&item); for(i=0;i<arr_size;i++){
printf("\n1.PUSH\n"); #include<conio.h> scanf("%d",(arr+i));}
printf("2.POP\n");
rear++; printf("\nthe array is:");
#include<stdlib.h> a[rear]=item;
printf("3.DISPLAY\n"); for(i=0;i<arr_size;i++){
printf("4.EXIT\n");
#define MAX 3 printf("%d is (i!=arr_size1)?printf("%d",*(arr+1)):
printf("enter your void insert(); inserted\n",item);} printf("%d\n",*(arr+1));}
choice:\n"); void delet(); void delet(){ max=arr[0];
scanf("%d",&choice); void display(); if(front==- for(i=0;i<arr_size;i++){
switch(choice){ int a[MAX]; 1||front>rear){ if(max<arr[i]){
case 1:push(); int rear=-1; max=arr[i];}}
break;
printf("queue min=arr[0];
int front=0; underflow\n");
case 2:pop(); for(i=0;i<arr_size;i++){
break;
void main(){ return;} if(min>arr[i]){
case 3:display(); int choice; else if(rear==front){ min=arr[i];}}
break; clrscr(); printf("%d is deleted printf("\nlargest number in the array
case 4:exit(0); while(1){ \n",a[front]); is:%d\nsmallest number in the
default:printf("invalid printf("\n1.Insert into front=0; array:%d\n",max,min);
choice"); queue\n"); getch();}
break;}}}
rear=-1;}
printf("2.Delete from else{
void push(){
if(TOP==MAX-1){
queue\n"); printf("%d is removed
printf("Stack overflow"); printf("Display all from
return;} items\n"); queue\n",a[front]);
printf("Enter an integer printf("4.Exit\n"); front++;}}
value:\n"); printf("Enter your void display(){
scanf("%d",&item); choice:");
TOP++;
int i;
scanf("%d",&choice); if(rear==front-1){
S[TOP]=item;
printf("%d is
switch(choice){ printf("queue is
inserted",item);} case 1:insert(); empty\n");}
void pop(){ break; else{
if(TOP==-1){ case 2:delet(); printf("elements is
printf("Stack break; queue are:\n");
underflow"); case 3:display();
return;}
for(i=front;i<=rear;i++)
break; printf("%d\t",a[i]);}}
case 4:exit(0);

You might also like