4. WAP using recursive function for the following. a. To calculate GCD and LCM of 2 integer numbers. b. To solve Tower of Honoi problem.c. To search an elements in a list using binary search.
WAP to construct a stack of integer and to perform the following operations on it
#include#include #include #define MAX 100 int stk[MAX]; int top=-1; int size; void push(); void pop(); void display(); void main() { int ch; clrscr(); printf("Enter the size of stack:\n"); scanf("%d",&size); while(1) { clrscr(); printf("\n\n******** stack operation **********"); printf("\n----------------\n"); printf("\n1.push \n2.pop \n3.display \n4.exit"); printf("\n\nenter your choice:"); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:display(); break; case 4:exit(0); default:printf("invalid choice"); break; } } getch(); } void push() { if(top==size-1) { printf("\noverflow"); return; } else { printf("\nenter the element to push:"); scanf("%d",&stk[++top]); } } void pop() { if(top==-1) { printf("\nunderflow"); return; } else { printf("\ndeleted item=%d",stk[top--]); } } void display() { int i; if(top==-1) { printf("\nstack is empty."); getch(); return; } else { printf("\n stack elements are:"); for(i=top;i>=0;i--) printf("%d\t",stk[i]); } getch(); }
program to simulate the working of a Normal queue.
#include#include #include #define MAX 5 struct queue { int arr[MAX]; int front,rear; }; void initqueue(struct queue *q) { q->front=-1; q->rear=-1; } void insert_queue(struct queue *q) { int x; printf("Enter the element:"); scanf("%d",&x); if(q->rear==MAX-1) { printf("QUEUE IS FULL..!"); getch(); return; } q->rear++; q->arr[q->rear]=x; if(q->front==-1) q->front=0; } void display(struct queue *q) { int i; printf("Queue elements are:"); for(i=q->front;i<=q->rear;i++) printf("%d\t",q->arr[i]); getch(); } int delete_queue(struct queue *q) { int data; if(q->front==-1) { printf("QUEUE IS EMPTY..!"); getch(); return(NULL); } data=q->arr[q->front]; if(q->front==q->rear) q->rear=q->front=-1; else q->front++; return(data); } void main() { int ch; struct queue p; initqueue(&p); clrscr(); while(1) { clrscr(); printf("\n************* QUEUE OPERATION ****************\n"); printf("1.INSERT\n2.DELETE\n3.DISPLAY\n4.EXIT\n"); scanf("%d",&ch); switch(ch) { case 1:insert_queue(&p); break; case 2:delete_queue(&p); break; case 3:display(&p); break; case 4: exit(0); default:printf("INVLAID INPUT...."); } } getch(); }
#include<stdio.h> #include<conio.h> #include<ctype.h> int prec(char); char stack[10]; int top=-1; void push(char); char pop(); void main() { int i,j=0; char infix[25],post[25]; clrscr(); printf("enter the infix expression:"); gets(infix); push('('); for(i=0;infix[i]!='\0';i++) { if(isalpha(infix[i])) { post[j]=infix[i]; j++; } else { switch(infix[i]) { case '(':push('('); break; case ')':while(stack[top]!='(') { post[j]=pop(); j++; } pop(); break; default:while(prec(infix[i])<prec(stack[top])) { post[j]=pop(); j++; } push(infix[i]); break; }}} post[j]='\0'; printf("\nPOSTFIX EXPRESSION:"); puts(post); getch(); } void push(char ch) { top++; stack[top]=ch; } char pop() { char ch; ch=stack[top]; top--; return(ch); } int prec(char ch) { switch(ch) { case '(':return 0; case '+': case '-': return 1; case '*': case '/': return 2; }}
#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<graphics.h> struct llist { int sid,sem; char sname[25]; struct llist *next; }; typedef struct llist node; node *head=NULL; int count=0; void insert_front(); void insert_rear(); void insert_pos(); void delete_record(); void update_record(); void display(); void drawscreen(void); void refreshscreen(void); void main() { int ch; clrscr(); while(1) { clrscr(); //printf("************ CONSTRUCT SINGLY LINK LIST AND PERFORM ACTION *************\n"); refreshscreen(); drawscreen(); printf("\n 1.Insert at front \n 2.Insert at rear\n 3.Insert at pos\n 4.Delete record\n 5.Update record\n 6.Display \n 7.Exit\n"); gotoxy(2,12); printf("Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: insert_front(); break; case 2: insert_rear(); break; case 3: insert_pos(); break; case 4: delete_record(); break; case 5:update_record(); break; case 6:display(); break; case 7: exit(0); } } getch(); } void insert_front() { node *new1; new1=((node*)malloc(sizeof(node))); printf("enter name:"); scanf("%s",new1->sname); printf("enter id:"); scanf("%d",&new1->sid ); printf("enter the semester:"); scanf("%d",&new1->sem); new1->next=NULL; count++; if(head==NULL) head=new1; else { new1->next=head; head=new1; } } void insert_rear() { node *new1, *pos; new1=(node *)malloc(sizeof(node)); printf("enter id:"); scanf("%d",&new1->sid); printf("enter name:"); scanf("%s",new1->sname); printf("enter he semester:"); scanf("%d",&new1->sem); new1->next=NULL; count++; if(head==NULL) head=new1; else { pos=head; while(pos->next!=NULL) pos=pos->next; pos->next=new1; }} void insert_pos() { node *new1,*prv,*curr; int i , pos; printf("enter the node postiton:"); scanf("%d",&pos); if(pos>count) printf("ilegal position.."); else { new1=(node*)malloc(sizeof(node)); printf("enter id:"); scanf("%d",&new1->sid); printf("eneter the name:"); scanf("%s",new1->sname); printf("enter the semester:"); scanf("%d",&new1->sem); new1->next=NULL; curr=head; for(i=1;inext; } if(curr==NULL) { head=new1; count++; } else if((head==curr)&&(curr->next!=NULL)) { new1->next=curr; head=new1; count++; } else { new1->next=prv->next; prv->next=new1; count++; }}} void delete_record() { node *prv,*curr; int sid, flag=0; printf("enter the student id to be deleted:"); scanf("%d",&sid); curr=head; if(curr==NULL) printf("linked list os empty.."); else { while((sid!=curr->sid)&&(curr!=NULL)) { prv=curr; curr=curr->next; } if((sid==head->sid)&&(head->next==NULL)) { count--; head=NULL; free(curr); flag=1; } else if((sid==head->sid)&&(head->next!=NULL)) { count--; head=head->next; free(curr); flag=1; } else if(sid==curr->sid) { count--; prv->next=curr->next; free(curr); flag=1; }} if(flag==0) printf("deleted record not found\n"); } void update_record() { node *curr=head; int sid,flag=0; printf("ente the sid to be updated:"); scanf("%d",&sid); printf("To update record:"); if(curr==NULL) { gotoxy(1,25); cprintf("link list is empty\n"); } else { while(curr!=NULL) { if(sid!=curr->sid) curr=curr->next; else { printf("enter the id:"); scanf("%d",&curr->sid); printf("enter the name:"); scanf("%s",curr->sname); printf("enter he semester:"); scanf("%d",&curr->sem); flag=1; break; }}} if(flag==0) { gotoxy(1,25); cprintf("record not found:"); getch(); } else { gotoxy(1,25); cprintf("updated succesfull..!"); getch(); } } void display() { node *ptr=head; if(ptr==NULL) printf("linked list empty:"); else { gotoxy(2,15); printf("ID \tNAME\t SEMESTER\n"); while(ptr!=NULL) { gotoxy(2,16); printf("%d\t%s\t%5d\n",ptr->sid,ptr->sname,ptr->sem); ptr=ptr->next; } getch(); }} void drawscreen(void) { gotoxy(1,1); cprintf("-------------------------------------------------------------------------------"); gotoxy(1,2); cprintf(" *** SINGLY LINK LIST BY AASHIK *** "); gotoxy(1,3); cprintf("-------------------------------------------------------------------------------"); } /*--------------------------------------------------------------------------- Refreshscreen function ---------------------- used to refresh colour display. ----------------------------------------------------------------------------*/ void refreshscreen(void) { clrscr(); textcolor(WHITE); textbackground(BLACK); gotoxy(1,25); cprintf(" "); clrscr(); textcolor(WHITE); textbackground(BLUE); gotoxy(1,25); cprintf(" "); }
#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #define max 10 int r=-1, f=-1; int cq[max]; int size; void screen() { printf("\n**** circular queue *******\n"); printf("\n 1.insert\n2.delete\n3.display\n4.exit\n"); } void cq_insert() { int item; if((f==0)&&(r==size-1)||(r==f-1)&&(f==r+1)) { printf("queue is overflow"); } else { printf("\nenter the data:"); scanf("%d",&item); if(r==-1) { r=0; f=0; } else { if((f!=0)&&(r==size-1)) r=0; else r=r+1; } cq[r]=item; }} void cq_delete() { int item; if(f==-1) printf("queue underflow\n"); else { item=cq[f]; if(f==r) { f=-1; r=-1; } else if(f==size-1) f=0; else f=f+1; printf("%d item deleted",item); }} void display() { int i; if(f==-1) { printf("queue is empty\n"); } else { for(i=f;i<=r;i++) printf("%d\n",cq[i]); if(f>r) { for(i=f;i
#include<stdio.h> #include<conio.h> #define max 5 void pqinsert(int qno,int q[10],int *front,int *rear,int item) { if(*rear==max-1) { printf("que %d is full\n",qno); return; } else if(*front==-1) *front=*rear=0; else *rear=*rear+1; q[*rear]=item; } int pqdelete(int q[10],int *front,int *rear) { if(*front==-1) return(0); else { printf("deleted item is %d\n",q[*front]); if(*front==*rear) *rear=*front=-1; else *front=*front+1; return(1); } } void pqdisplay(int qno,int q[10],int *front,int *rear) { int i; if(*front==-1) printf("%d quee is empty \n",qno); else { printf("element in %d quee is:",qno); for(i=*front;i<=*rear;i++) printf("%d\t",q[i]); } } void main() { int q1[max],q2[max],q3[max]; int f1=-1,r1=-1,f2=-1,r2=-1,f3=-1,r3=-1; int item,priority,flag,ch; while(1) { printf("**********priority quee operation***************\n"); printf("1-insert\n2-delete\n3-display\n4-exit\n"); printf("enter your chice!"); scanf("%d",&ch); switch(ch) { case 1:printf("enter item & priority no\n"); scanf("%d%d",&item,&priority); if(priority==1) pqinsert(1,q1,&f1,&r1,item); if(priority==2) pqinsert(2,q2,&f2,&r2,item); if(priority==3) pqinsert(3,q3,&f3,&r3,item); break; case 2:flag=pqdelete(q1,&f1,&r1); if(flag==1) { printf("delete from 1st priority\n"); break; } else printf("1st priority is empty\n"); flag=pqdelete(q2,&f2,&r2); if(flag==1) { printf("delete from 2nd priority\n"); break; } else printf("2nd priority qno is empty\n"); flag=pqdelete(q3,&f3,&r3); if(flag==1) { printf("delete from 3rd priority\n"); break; } else printf("3rd priority qno is empty\n"); break; case 3:printf("que elements are \n"); pqdisplay(1,q1,&f1,&r1); pqdisplay(2,q2,&f2,&r2); pqdisplay(3,q3,&f3,&r3); break; case 4:exit(0); } } }
#include#include #include void liner(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int i); void shadow(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int x6,int y6,int x7,int y7); void rightlead( int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int y9,int x10,int y10,int x11, int y11); void leftlead(); void identifier(); void bibin(); void main() { int gd=DETECT, gm,i=3; initgraph(&gd,&gm,"c:\tc\bgi"); setbkcolor(6); cleardevice(); setcolor(i); line(263,192,108,151); line(108,151,345,40); line(263,192,507,68); line(507,68,345,40); setfillstyle(1,9); floodfill(347,49,i);//for upper part setcolor(i); line(263,192,269,231); line(269,231,89,179); line(89,179,108,151); setfillstyle(1,9); floodfill(193,194,i);//for part left light blue setcolor(i); line(269,231,524,93); line(524,93,506,69); floodfill(454,113,i);//for part side light blue setcolor(i); line(269,231,264,260); line(264,260,101,215); line(101,215,89,179); setfillstyle(1,1);//part left for blue floodfill(193,234,i); setcolor(i); line(264,260,514,125); line(525,94,514,125); setfillstyle(1,1); floodfill(489,126,i); setcolor(i);//part side blue //end of upperpart line(102,315,292,383); line(294,419,292,383); line(294,419,101,348); line(102,315,101,348); setfillstyle(1,1);//lower left floodfill(154,360,i); line(542,222,292,383); line(542,257,542,222); line(542,257,294,419); setfillstyle(1,1); floodfill(480,282,i);//lower side line(102,315,222,249); line(407,183,542,222); setfillstyle(1,1); floodfill(206,274,i); line(147,331,146,350); line(259,390,146,350); line(258,371,259,390); line(147,331,411,184); line(411,184,503,210); line(503,210,258,371); setfillstyle(1,13); floodfill(273,297,i); //end of base of low part // start of holes line(130,311,138,313); line(138,313,147,307); line(147,307,139,305); line(139,305,130,311); setfillstyle(1,8); floodfill(139,309,i); //left 1 liner(176,285,183,287,193,282,186,280,185,284,i); liner(221,260,229,262,239,257,232,255,230,258,i); liner(299,362,308,365,315,358,309,357,309,361,i); liner(347,332,354,335,363,330,357,327,357,330,i); liner(389,305,396,306,405,301,399,299,399,303,i); liner(428,280,436,282,444,277,438,276,437,279,i); liner(467,255,475,257,482,252,476,250,475,255,i); liner(505,233,511,234,521,229,514,227,513,231,i); delay(1500); rightlead(283,225,297,226,301,215,314,220,298,280,317,280,305,282,313,282, 305,346,312,343,309,240); shadow(282,226,294,231,294,281,300,285,301,346,305,346,296,280); delay(500); rightlead(333,197,344,200,348,189,360,196,346,253,362,252,350,256,358,255, 351,316,358,314,353,221); shadow(333,197,342,206,343,254,348,258,348,316,351,316,349,315);//for second lead delay(500); rightlead(373,175,385,178,391,166,402,172,388,228,404,225,393,232,400,231, 394,290,400,287,397,197); shadow(373,175,383,182,385,229,390,233,391,291,394,290,392,280); delay(500); rightlead(417,152,428,155,432,143,442,148,430,205,446,202,435,208,441,207, 436,265,441,263,436,178); shadow(417,152,426,161,426,205,431,211,431,267,436,265,425,155); delay(500); rightlead(456,130,466,133,471,122,479,125,469,181,483,180,473,184,479,184, 474,239,480,238,475,158); shadow(456,130,464,137,465,183,471,187,472,241,474,239,473,220); delay(500); rightlead(493,110,501,114,506,102,517,107,504,160,519,159,508,163,515,162, 509,216,515,216,513,137); shadow(493,110,499,117,499,162,505,165,505,218,509,216,506,213); delay(500); leftlead(); identifier(); getch(); bibin(); } void liner(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int i) { line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x4,y4,x1,y1); setfillstyle(1,8); floodfill(x5,y5,i);//x1,y1 left most then go anticlockwise x5,y5 for fill } void rightlead( int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int x6,int y6,int x7,int y7,int x8,int y8,int x9,int y9,int x10,int y10,int x11,int y11) { setcolor(15); line(x1,y1,x3,y3);//for starting line line(x1,y1,x2,y2); line(x3,y3,x4,y4); line(x2,y2,x5,y5); line(x4,y4,x6,y6); line(x5,y5,x7,y7); line(x6,y6,x8,y8); line(x7,y7,x9,y9); line(x10,y10,x8,y8); line(x10,y10,x9,y9); setfillstyle(1,7); floodfill(x11,y11,15); } void shadow(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int x5,int y5,int x6,int y6,int x7,int y7) { setcolor(15); line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); line(x5,y5,x4,y4); line(x5,y5,x6,y6); setfillstyle(1,8); floodfill(x7,y7,15); } void leftlead() { setcolor(15); line(132,224,133,232); line(133,232,139,236); line(139,236,140,297); line(140,297,146,296); line(146,296,146,237); line(146,237,152,232); line(152,232,152,229); line(152,229,132,224); setfillstyle(1,7); floodfill(142,244,15); line(129,224,128,234); line(128,234,135,240); line(135,240,137,298); line(137,298,140,297); line(132,224,129,224); setfillstyle(1,8); floodfill(131,236,15); //end of first lead line(181,237,181,273); line(181,273,188,272); line(188,272,188,241); line(181,237,188,241); setfillstyle(1,7); floodfill(185,255,15); line(177,237,181,239); line(177,237,179,275); line(179,275,181,273); setfillstyle(1,8); floodfill(179,256,15); line(221,249,220,253); line(220,253,226,252); line(226,252,225,250); line(225,250,221,249); floodfill(223,251,15); } void identifier() { setcolor(6); arc(196,177,350,160,26); line(171,168,222,181); setfillstyle(1,1); floodfill(200,170,6); } void bibin() { int midx=320,midy=230; cleardevice(); setbkcolor(RED); settextjustify(CENTER_TEXT,CENTER_TEXT); setcolor(GREEN); settextstyle(5, VERT_DIR, 4); return ; }
#include#include #include #include void main() { int gd=DETECT,gm; int x=320,y=240,r=200,i,h,m,s,thetamin,thetasec; struct time t; char n[12][3]={"3","2","1","12","11","10","9","8","7","6","5","4"}; initgraph(&gd,&gm,"f:\arun\tc");\put the directory which contains egavga.bgi circle(x,y,210); setcolor(4); settextstyle(4,0,5); for(i=0;i<12;i++) { if(i!=3) outtextxy(x+(r-14)*cos(M_PI/6*i)-10,y-(r-14)*sin(M_PI/6*i)-26,n[i]); else outtextxy(x+(r-14)*cos(M_PI/6*i)-20,y-(r-14)*sin(M_PI/6*i)-26,n[i]); } gettime(&t); printf("The current time is: %2d:%02d:%02d.%02d ",t.ti_hour, t.ti_min, t.ti_sec, t.ti_hund); while(!kbhit()) { setcolor(5); setfillstyle(1,5); circle(x,y,10); floodfill(x,y,5); gettime(&t); if(t.ti_min!=m) { setcolor(0); line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180 ))); circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180)) ,10); line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h -((m/2)*(M_PI/180)))); circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-( (m/2)*(M_PI/180))),10); } if(t.ti_hour>12) t.ti_hour=t.ti_hour-12; if(t.ti_hour<4) h=abs(t.ti_hour-3); else h=15-t.ti_hour; m=t.ti_min; if(t.ti_min<=15) thetamin=(15-t.ti_min)*6; else thetamin=450-t.ti_min*6; if(t.ti_sec<=15) thetasec=(15-t.ti_sec)*6; else thetasec=450-t.ti_sec*6; setcolor(4); line(x,y,x+(r-110)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-110)*sin(M_PI/6*h -((m/2)*(M_PI/180)))); circle(x+(r-130)*cos(M_PI/6*h-((m/2)*(M_PI/180))),y-(r-130)*sin(M_PI/6*h-( (m/2)*(M_PI/180))),10); line(x,y,x+(r-60)*cos(thetamin*(M_PI/180)),y-(r-60)*sin(thetamin*(M_PI/180 ))); circle(x+(r-80)*cos(thetamin*(M_PI/180)),y-(r-80)*sin(thetamin*(M_PI/180)) ,10); setcolor(15); line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180 ))); delay(1000); setcolor(0); line(x,y,x+(r-70)*cos(thetasec*(M_PI/180)),y-(r-70)*sin(thetasec*(M_PI/180 ))); } }
Add two long positive intergers
#include#include #include #include struct node { int data; struct node*next; }; void insert(struct node**p,int num) { struct node*temp; if(*p==NULL) { (*p)=(struct node*)malloc(sizeof(struct node)); (*p)->next=NULL; (*p)->data=num; } else { temp=(struct node*)malloc(sizeof(struct node)); temp->next=(*p); (*p)=temp; (*p)->data=num; } } void add_in(struct node*a,struct node*b,struct node**c) { int d,carry; carry=0; struct node*t; while(a!=NULL&&b!=NULL) { d=(a->data+b->data+carry)%10; insert(c,d); if( (a->data+b->data+carry) >= 10) { carry=1; } else carry=0; a=a->next; b=b->next; } if(a==NULL&&b==NULL) { return; } else { if(a!=NULL&&b==NULL) { t=a; } else { t=b; } while(t!=NULL) { d=(carry+t->data)%10; if((carry+t->data)>=10) carry=1; else carry=0; insert(c,d); t=t->next; } if(carry==1) insert(c,carry); } } void numin(struct node**p) { *p=NULL;char c='c'; while(c!='n') { c=getch(); if(!isdigit(c)) return; else { putch(c); insert(p,c-'0'); } } } void disp(struct node*p) { if(p==NULL) return; else { printf("%d",p->data); disp(p->next); } } void main() { struct node *a,*b,*c; clrscr(); a=b=c=NULL; printf("Enter the first number.... "); numin(&a); printf(" Enter the second number.... "); numin(&b); printf(" The added result is... "); add_in(a,b,&c); disp(c); getch(); }
Reversal of a singly linklist by recursion #include#include #include struct node { int data; struct node*next; }; void insert(struct node**p,int num) /*Function for inserting an element into a list */ { if(*p==NULL) { (*p)=(struct node*)malloc(sizeof(struct node)); (*p)->next=NULL; (*p)->data=num; } else { insert(&((*p)->next),num); } } void display(struct node*p) /*Function for displaying the list*/ { while(p!=NULL) { printf("%d ",p->data); p=p->next; } } void reverse(struct node**p) /*Function for reversing the list by recursion */ { struct node*q,*r,*x; int d; q=(*p); /*stores the address of the first element */ x=q; /*also stores the element of the first element for counter pourpose */ d=q->data; /*stores the data of the first element*/ r=q->next; /*stores the address of the second element in the list */ free(q); /*deletes the first element of the list*/ if(x==NULL) return ; else { reverse(&(r));/*Recursive call*/ insert(p,d); /*This function is put in the stack so the first will be taken as last element for the new list */ } } void main() { clrscr(); struct node*p=NULL; int n,d,i=0; printf("How many...? "); scanf("%d",&n); while(i++!=n) { scanf("%d",&d); insert(&p,d); } display(p); reverse(&p); printf(" The reversed list is..."); display(p); getch(); }
10:48 AM |
Category: |