Social Icons

banner image

DATA STRUCTURES PROGRAMS




QUESTION#2:- 



WRITE A COMPLETE C++ PROGRAMME WHICH ASK USER TO ENTER A 

VALUE,THEN TO SEARCH THE VALUE FROM STACK AND THEN REMOVE THAT VALUE 

FROM STACK?

NOTE:-MAKE SURE DO NOT VOILATE LIFO?

#include "stdafx.h"

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;



void insert(int,int);

void delte(int);

void display(int);

int search(int);

int search1(int,int);

int t[40],t=1,s,x,i;



int main()



int ch,y; 

for(i=1;i<40;i++) 

t[i]=-1; 

while(1) 

{

cout<<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:"; 

cin>>ch; 

switch(ch) 



case1: 

cout<<"enter the element to insert"; 

cin>>ch;

insert(1,ch);

break;

case2: 

cout<<"enter the element to delete"; 

cin>>x;

y=search(1);

if(y!=-1)delte(y);

else 

break;

case3: 

display(1);

cout<<"\n";

case4: 

for(int i=0;i<=32;i++) 

cout<<i; 

cout<<"\n";

break;

cout<<"enter the element to search:"; 

cin>> x; 

y=search(1); 

if(y ==-1)cout<<"no such element in t"; 

else 

break; 

case5: 

exit(0); 



}

}



void insert(int s,int ch)



int x; 

if(t==1) 



t[t++]=ch; 

return; 



x=search1(s,ch); 

if(t[x]>ch) 

else 

t++;

}

void delte(int x)



if( t[2*x]==-1&& t[2*x+1]==-1) 

else 

{ t[x]=t[2*x+1]; 



else 

{ t[x]=t[2*x]; 

t[2*x]=ch; 

t[2*x+1]=ch; 

t[x]=-1; 

if(t[2*x]==-1) 

t[2*x+1]=-1; 

if(t[2*x+1]==-1) 

t[2*x]=-1; 



else 



  t[x]=t[2*x]; 

delte(2*x); 



t--;

}



int search(int s)

{

if(t==1)

{

cout<<"no element in t";

return-1;

}

if(t[s]==-1)

return t[s];

if(t[s]>x)

search(2*s);

else

if(t[s]<x)

search(2*s+1);

else

return s;

}



void display(int s)

{

if(t==1)

{cout<<"no element in t:";

return;}

for(int i=1;i<40;i++)

if(t[i]==-1)

cout<<" ";

else

cout<<t[i];

return;

}



int search1(int s,int ch)

{

if(t==1)

{

cout<<"no element in tree";

return-1;

}

if(t[s]==-1)

return s/2;

if(t[s]>ch)

search1(2*s,ch);

else 

search1(2*s+1,ch);

}

QUESTION#2:-



 WRITE A COMPLETE C++ PROGRAMME TO CALCULATE  THE FACTORIAL OF A GIVEN 

NUMBER BY USING STACK??????????

NOTE:- MUST SIMULATE THE POP AND PUSH OPERATION BY DRAWING STACK AS 

OUTPUT??????????


#include <iostream.h>

#include <conio.h>

#include <math.h>

#include<stdlib.h>

class stack

{

private:

 int top;

  int para_n[100];

 public:

   void push(stack *,int);

    void pop(stack *,int* );

    double fact(int);

}s;

void stack::push(stack *s,int d)

{

 s->top++;

s->para_n[s->top]=d;

}

void stack::pop(stack *s,int *d)



 *d=s->para_n[s->top];

s->top--;

}

double stack::fact(int n)



 double res;

int d;

d=n;

s.top=-1;

lab1:

if(d==0)

{

 res=1;

goto lab2;

}

    else

{

 push(&s,d);

d--;

goto lab1;

}

lab2:

if(s.top>-1)

{

 pop(&s,&d);

res*=d;

goto lab2;

}

    else

return res;

}

void main()



 int num ,c;

stack k;

clrscr();

while(1)

{

 clrscr();

do

{

  clrscr();

cout<<"\nTo Find the factorial of a number\n\n";

cout<<"------------------------------------------\n";

cout<<"1.Enter a number";

cout<<"\n2.Display factorial";

cout<<"\n3.Exit from the program";

cout<<"\n\nSelectyourchoice _ ";

cin>>c;

}

while((c<1)||(c>3));

if(c==3)exit(1);

switch(c)

{

case 1 :

clrscr();

cout<<"\nEnter your number ";

cin>>num;

break;

case 2 :

clrscr();

cout<<num<<"!="<<k.fact(num);

break;

}

cout<<"\n\nPress any key to continue...........";

getch();

}

}
DATA STRUCTURES PROGRAMS DATA STRUCTURES PROGRAMS Reviewed by Marketing Thrills on October 30, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.