Thursday, April 30, 2009

C++ - Scientific Calculator

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<process.h>
#include<graphics.h>
#include<ctype.h>
double rfact(double);
void main()
{
int ch;
int k,n,choice;
float sinx,cosx,tanx,cotx,secx,cosecx;
float x,j,i;
clrscr();
cin>>ch;
if(ch==4444)
{
int gdriver=DETECT,gmode, errorcode;
initgraph(&gdriver,&gmode,"e:\\tc\\bgi");
setbkcolor(LIGHTRED);
settextstyle(7,0,9);
outtextxy(45,160,"..WELCOME!..");//page1
getch();
closegraph();
}
getch();
clrscr();
textcolor(YELLOW+128);
gotoxy(30,12);
cprintf("COMPUTER EXHIBITION\n\r");//page2
textcolor(GREEN);
gotoxy(30,14);
cprintf("  Dhanoop Bhaskar \n\r");
textcolor(BLUE);
gotoxy(30,15);
cprintf("      C2 Batch\n\r");
textcolor(CYAN);
gotoxy(30,16);
cprintf("   CARDINAL .HSS\n\r ");
textcolor(RED+128);
gotoxy(33,19);
cprintf("INSTRUCTIONS:\n\r");
textcolor(YELLOW);
gotoxy(22,20);
cprintf("\(The Scientific Calculator enables to\n\r");
gotoxy(22,21);
cprintf("Find the values of trigonometric funtions\)\n\r ");
textcolor(LIGHTGREEN);
gotoxy(22,22);
cprintf(":Please try to give appropiate values only\n\r ");
gotoxy(22,23);
cprintf(":Follow the instructions given in brackets\n\r");
gotoxy(22,24);
cprintf(":Donot attempt malpractices\n\r");
gotoxy(22,25);
cprintf(":Use only numerical values\n\r");
getch();
do
{
clrscr();
gotoxy(30,9);
textcolor(YELLOW+128) ;textbackground(RED) ;
cprintf(" SCIENTIFIC CALCULATOR \n\r");//page3
textcolor(YELLOW) ;textbackground(BLACK) ;
gotoxy(30,13);
cprintf("       MAIN MENU       \n\r" );
gotoxy(30,15);
cprintf("1.SINE:               \n\r");
gotoxy(30,16);
cprintf("2.COSINE:             \n\r");
gotoxy(30,17);
cprintf("3.TANGENT:            \n\r");
gotoxy(30,18);
cprintf("4.COTANGENT:          \n\r");
gotoxy(30,19);
cprintf("5.SECANT:             \n\r");
gotoxy(30,20);
cprintf("6.COSECANT:           \n\r");
gotoxy(30,21);
cprintf("7.EXIT:               \n\r");
cout<<"\n Enter your choice ";
cin>>choice;
if(choice<7)
{
cout<<"\n Enter the angle in degrees ";
cout<<"\n (Please enter angle between -360&+360 ) ";
cin>>i;
if(i>360||i<-360)
{
cout<<"\n Sorry!!!!... cannot find the values at present ";
getch();
exit(0);
}
x=i*0.01745;
}

switch(choice)
{
case 1:sinx=0;//sine
       for(j=1,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=-1;
       else
       n=1;
       sinx=(sinx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==0||i==180||i==360)
       cout<<"\n sinx="<<0;
       else if(i==90)
       cout<<"\n sinx="<<1;
       else if(i==270)
       cout<<"\n sinx="<<-1;
       else
       cout<<"\n sin x="<<sinx;
       getch();
       break;
case 2:cosx=1;//cosine
       for(j=2,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=1;
       else
       n=-1;
       cosx=(cosx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==0||i==360)
       cout<<"\n cosx="<<1;
       else if(i==180)
       cout<<"\n cosx="<<-1;
       else if(i==90||i==270)
       cout<<"\n cosx="<<0;
       else
       cout<<"\n cos x="<<cosx;
       getch();
       break;
case 3:sinx=0;//tangent
       for(j=1,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=-1;
       else
       n=1;
       sinx=(sinx)+((n*pow(x,j))/(rfact(j)));
       }
       cosx=1;
       for(j=2,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=1;
       else
       n=-1;
       cosx=(cosx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==90||i==270||cosx==0)
       {
       cout<<"\n tan x is not defined ";
       cout<<"\n\( cosx="<<0<<" appro\)";
       }
       else if(i==0||i==180||i==360)
       cout<<"\n tanx="<<0;
       else
       {
       tanx=sinx/cosx;
       cout<<"\n tan x="<<tanx;
       getch();}
       break;
case 4:sinx=0;//cotangent
       for(j=1,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=-1;
       else
       n=1;
       sinx=(sinx)+((n*pow(x,j))/(rfact(j)));
       }
       cosx=1;
       for(j=2,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=1;
       else
       n=-1;
       cosx=(cosx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==0||i==90||i==180||i==270||i==360||sinx==0)
       {
       cout<<"\n cot x is not defined ";
       cout<<"\n\( sinx="<<0<<" appro\)";
       }
       else
       {
       cotx=cosx/sinx;
       cout<<"\n cot x="<<cotx;
       getch();
       }
       break;
case 5:cosx=1;//secant
       for(j=2,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=1;
       else
       n=-1;
       cosx=(cosx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==90||i==270||cosx==0)
       {
       cout<<"\n sec x is not defined ";
       cout<<"\n\( cosx="<<0<<" appro\)";
       }
       else if(i==0||i==360)
       cout<<"\n secx="<<1;
       else if(i==180)
       cout<<"\n secx="<<-1;
       else
       {
       secx=1/cosx;
       cout<<"\n sec x="<<secx;
       getch();
       }
       break;
case 6:sinx=0;//cosecant
       for(j=1,k=1;j<50;j+=2,k++)
       {
       if(k%2==0)
       n=-1;
       else
       n=1;
       sinx=(sinx)+((n*pow(x,j))/(rfact(j)));
       }
       if(i==0||i==180||i==360||sinx==0)
       {
       cout<<"\n cosec x is not defined ";
       cout<<"\n\( sinx="<<0<<" appro\)";
       }
       else if(i==90)
       cout<<"\n cosecx="<<1;
       else if(i==270)
       cout<<"\n cosecx="<<-1;
       else
       {
       cosecx=1/sinx;
       cout<<"\n cosec x="<<cosecx;
       getch();
       }
       break;
case 7:exit(0);
default:cout<<"\n Invalid choice ";
}
getch();
}while(1);
}

double rfact(double d)
{
if(d<2)
return (1);
else
return (d*rfact(d-1));
}

Review Questions

Chapter I: Principles of Object-Oriented Programming

1. Which of the following languages is not a procedure-oriented programming language?
a) ALGOL
b) COBOL
c) FORTRAN
d) None of the above

2. Which of the following programming approach used functions as a key concept to perform action-oriented tasks?
a) Structured programming
b) Modular programming
c) Procedure-oriented programming
d) Object-oriented programming

3. Identify the drawback of using procedure-oriented programming, if any:
a) Data is hidden from external functions
b) New functions can be added whenever necessary
c) Does not reflect real world problems
d) All of the above

4. Which is not associated with Object-oriented programming?
a) Data abstraction
b) Automatic initialization
c) Dynamic binding
d) None

5. The term operator overloading in C++ refers to:
a) Inheritance
b) Message passing
c) Polymorphism
d) None

6. Which one of the following OOP concepts enables reusability of components?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) All of the above

7. The concept of hierarchical classification is related to:
a) Abstraction
b) Inheritance
c) Function overloading
d) None

8. Object-based programming languages do not support:
i. Inheritance
ii. Dynamic binding
iii. Encapsulation
iv. All of the above
a) Both i and ii
b) iii only
c) iv only
d) i, ii, and iii

9. C++ does not support
i. Genericity
ii. Early binding
iii. Garbage collection
iv. Multiple Inheritance
a) i only
b) ii only
c) iii only
d) ii, iii and iv

10. Which of the following is an Object-oriented programming language?
i. Smalltalk
ii. Object Pascal
iii. Java
iv. All of the above
a) Both ii and iii
b) i only
c) iii only
d) iv only


Chapter I: Principles of Object-Oriented Programming

1. d) None of the above
2. c) Procedure-oriented programming
3. c) Does not reflect real world problems
4. d) None
5. c) Polymorphism
6. a) Inheritance
7. b) Inheritance
8. a) Both i and ii
9. a) i only
10. d) iv only




Chapter II: Beginning With C++

1. Which one of the following options is true on the topic of Simula67?
i. It is the first Object-oriented programming language
ii. It’s a predecessor to C++
iii. It was designed for doing simulations¬
iv. All of the above
a) Both i and ii
b) i only
c) iii only
d) iv only

2. Which of the following features that distinguish object oriented programming from other conventional programming?
i. Structural design
ii. Inheritance
iii. Modular programming
iv. bottom-up
a) i only
b) Both ii and iii
c) Both ii and iv
d) iv only

3. Comments in C++ start with _______ symbol.
a) //
b) \\
c) **
d) None of the above

4. The insertion operator is another name for
a) input operator
b) output operator
c) extraction operator
d) None of the above

5. Which header file in C++ does contain function prototypes for memory allocation?
a) < iostream.h>
b) < stdio.h>
c) < stdlib.h>
d) < limits.h>

6. What is the output of the following code?
int n=10;
while (n< 10)
cout<< “Number:”<< n+1;
a) 10
b) 11
c) No output
d) None of the above

7. Which of the following statements require the header file to be included?
a) a=b/c;
b) cout << a;
c) c=sqrt(a);
d) Both a and c

8. Identify the error, if any: char str_name ‘a‘;
a) str_name is not a valid variable name
b) Variables cannot be initialized at the time of declaration
c) Missing = sign between str_name and ‘a’
d) No error

9. Name the library that must be included while using cin.
a) < iomanip.h>
b) < iostream.h>
c) < ctype.h>
d) < stdlib.h>

10. A C++ program structure is based on the concept of
a) Client-server model
b) N-Tier model
c) Both a and b
d) None of the above


Chapter II: Beginning With C++
11. d) iv only
12. c) Both ii and iv
13. a) //
14. b) output operator
15. c)
16. c) No output
17. d) Both a and c
18. c) Missing = sign between str_name and ‘a’
19. b)
20. a) Client-server model


Chapter III: Tokens, Expressions and Control Structures
1. return is an example of a
a) Keyword
b) Function
c) Statement
d) Comment
2. Which of the following is not a keyword?
i. for
ii. friend
iii. virtual
iv. private
a) i only
b) Both ii and iii
c) Both i and iv
d) ii, iii and iv
3. Identify the valid variable name from the following:
i. char
ii. var_name
iii. _varname
iv. str_name2
a) Both i and iii
b) Both ii and iv
c) ii, iii, and iv
d) i only
4. Which of the following is not a user-defined data type?
a) array
b) structure
c) union
d) class
5. Write the equivalent C++ statement for the following expression,
X= *c/d
a) X=sqrt(a+b) *(c/d);
b) X=(squareroot(a+b)*c)/d;
c) X=( * c)/d;
d) None of the above
6. The statement int main() is a ___________.
a) function prototype
b) function call
c) function header line
d) None of the above
7. The declaration of global variables must be made
a) inside the function
b) outside the function
c) in a function header line
d) None of the above
8. Identify the error, if any:
double avg=tot/n;
a) Declaration should not contain any expression
b) Initialization cannot be done in the declaration statement
c) Dynamic initialization is not possible
d) No error
9. Which of the following is true about scope resolution operator?
a) Qualifies a namespace member to its namespace
b) Allows you to access a global variable
c) Qualifies the hidden variable
d) All of the above
10. Which of the following is not a member-dereferencing operator?
a) ::*
b) ::
c) *
d) *
11. Which of the following is true about new operator?
i. While using new operator, sizeof() operator is not needed
ii. It is also not necessary to use cast operator
iii. new operator can be overloaded
iv. All of the above
a) Both i and ii
b) Both i and iii
c) ii only
d) iv only
12. Identify the manipulators in C++:
a) endl
b) setw
c) Both a and b
d) None of the above
13. a = (b = 5); The C++ statement is an example of
a) Compound assignment
b) Embedded assignment
c) Chained assignment
d) Multiple assignment
14. Water-fall model is associated with
a) Control structures
b) Type conversions
c) Manipulators
d) None of the above
15. Selection structure is also known by
a) branching
b) straight line
c) iteration
d) None of the above
16. Consider the following code:
cout<< “Welcome”;
This is an example of
a) C++ statement
b) Return type
c) Function
d) Both a and c


Chapter III: Tokens, Expressions and Control Structures

1. a) Keyword
2. d) ii, iii and iv
3. c) ii, iii, and iv
4. a) array
5. a) X=sqrt(a+b) *(c/d);
6. c) function header line
7. b) outside the function
8. d) No error
9. d) All of the above
10. b) ::
11. d) iv only
12. c) Both a and b
13. c) Chained assignment
14. b) Type conversions
15. a) branching
16. a) C++ statement






Chapter IV: Functions in C++
1. Which of the following is true about a function call in a C++ program?
a) A function must be called atleast once
b) A function cannot be called from other functions
c) A function may be called whenever it is necessary
d) Both a and c
2. Function prototyping defines
a) The return type of the function
b) The identifier of the function
c) The number and type of arguments
d) All of the above
3. Find if the following function prototype contains any error:
double area(int )
a) No error
b) Variable name is not included in the argument list
c) Semicolon is not found
d) None of the above
4. Identify which function prototype exhibits the following: Name of the function is sample_calc, which receives two values of type double and returns no value;
a) sample_calc(double, double);
b) void sample_calc(double, double);
c) double sample_calc(void);
d) void sample_calc(double, double)
5. When you call a function by passing the address of a data variable, it is called ________.
a) Call by reference
b) Call by value
c) Call by two directions
d) Both b and c
6. Which of the following function calls is correct while providing default arguments:
I. double calc(int a, float b=12.0);
II. double calc(int a=3, float b=12.0, int c);
III. double calc(int a=3, float b, int c=8);
IV. double calc(int a, float b=12.0, int c=8);
a) I only
b) II only
c) Both I and IV
d) Both II, and III
7. Which function call does invoke the following function prototype?
float sub1(int a, float b);
a) X=sub1(5.0,6.5);
b) X=sub1(5,6.5);
c) X=sub1(5,6);
d) Both b and c
8. Identify the variables, which are local to the following function:
int calc(int p, int n)
{
int q;
q=pow(p,n);
return(q);
}
a) p and n
b) p,n, and q
c) q
d) Cannot be determined without the main() function
9. Which of the following statements is true about the function that contains the const argument?
a) The function should not modify the const argument
b) Const declaration is necessary only when the arguments are passed by reference
c) Both b and c
d) None of the above
10. Which of the following functions in C++ replace the usage of macros in C?
a) friend function
b) virtual function
c) inline function
d) All of the above


Chapter IV: Functions in C++

1. c) A function may be called whenever it is necessary
2. d) All of the above
3. c) Semicolon is not found
4. b) void sample_calc(double, double);
5. a) Call by reference
6. c) Both I and IV
7. b) X=sub1(5,6.5);
8. b) p,n, and q
9. c) Both b and c
10. c) inline function


Chapter V: Classes and Objects
1. The declaration of a class includes
a) Declaration of data members
b) Declaration of function prototype
c) Return statements of functions
d) Both a and b
2. By default, the members of a class are
a) private
b) public
c) protected
d) static
3. Which OOP feature can be enabled by using private declaration for the class members?
a) Data abstraction
b) Polymorphism
c) Encapsulation
d) Modularity
4. Which of the following will assign the value to the class member variable num?
void getnum(int a)
a) {num=a};
b) {num=a;}
c) {a=num};
d) {a=num;}
5. Which of the following is not true about member functions?
a) Can access private data variables
b) Can call another function directly without using dot operator
c) Both a and b
d) None of the above
6. Identify all the members of the following class xyz:
class xyz
{
int x,y;
public:
xyz();
void calc(int a, int b);
void output_calc(void);
};
a) Data members x and y
b) Data members x and y, Constructor, and member functions calc() and output_calc()
c) Data members x and y, and member functions calc() and output_calc()
d) Constructor and member functions calc() and output_calc()
7. Which of the following is not true about static member variable?
a) Only one instance of static member can be created
b) Visible only within the class
c) It can be initialized whenever necessary
d) Both a and b
8. What is the general format of calling a static member function using a class name?
a) class-name :: function-name
b) function-name :: class-name
c) class-name :: function-name;
d) function-name :: class-name;
9. Which of the following is true while passing objects as function arguments? It is possible
a) to pass copy of the entire object to the function
b) to pass only the address of the object to the function
c) to pass the objects to a non-member function
d) All of the above
10. A friend function
I. Can be invoked similar to other functions without using objects
II. Cannot access to other member functions directly
III. Cannot be called using the object of the class to which it has been declared as friend
IV. Can be declared only in the public part of a class
a) I, II and III
b) I, II and IV
c) I, II, III and IV
d) IV only


Chapter V: Classes and Objects

1. d) Both a and b
2. a) private
3. c) Encapsulation
4. b) {num=a;}
5. d) None of the above
6. b) Data members x and y, Constructor, and member functions calc() and
output_calc()
7. c) It can be initialized whenever necessary
8. c) class-name :: function-name;
9. d) All of the above
10. c) I, II, III and IV

Chapter VI: Constructors and Destructors

1. Which of the following is not true about constructors and destructors?
a) They must have the same name as class
b) They cannot return values
c) They cannot be called more than once in a program
d) They are called automatically
2. The default constructor for class A is
a) A :: A()
b) A :: A(int)
c) A :: A(int);
d) A :: A();
3. Which of the following statements do correctly describe the characteristics of constructors?
I. They cannot be inherited
II. They cannot be virtual
III. They need not be declared in the public section
IV. We cannot refer to their addresses
a) Both I and II
b) I, II, and IV
c) Both II and III
d) I, III and IV
4. Which of the following is called an implicit constructor for the class xyz?
a) xyz(){ }
b) xyz(int){}
c) xyz(){ };
d) None of the above
5. Consider the following code segment:
class simple
{
int a,b;
public:
simple();
simple(char[]);
};
Which of the following would assigns the string “welcome” to the second constructor?
a) simple s(“welcome”);
b) simple s(welcome);
c) simple s=”welcome”;
d) None of the above
6. Identify if any error in the following code segment
1. class example
2. {
3. float x;
4. public:
5. void example();
6. example(int, float);
7. };
a) Line 7 should not include the semicolon
b) Line 6 is an incorrect statement
c) Line 5 cannot include void
d) No error
7. Which of the following statements are true about copy constructors?
I. It declares and initializes an object from another object
II. It will not be useful when arguments are passed by value
III. It can be used to pass arguments by reference
IV. The compiler provides its own copy constructor, if it is not defined.
a) I, II, IV
b) All are correct
c) Both II and III
d) III only
8. The following statement creates a constant object of a class simple:
const simple m(a,b);
Which the following is true regarding the above statement?
a) The compiler generates compile-time error if you try to change the values of ‘a’ and ‘b’
b) Const member is a function prototype
c) The compiler generates an error if m tries to invoke non-const member functions
d) All of the above
9. Which of the following statements do incorrectly describe the characteristics of destructors?
I. A destructor is a member function, which has the same name as class name
II. A destructor receives only one argument
III. A destructor does not return any value
IV. Usage of destructors in a program clean up memory space that is not used
a) Both I and II
b) All are incorrect
c) Both II and III
d) I, II and IV
10. Which of the following is a correct description of the destructor ‘sample’ and includes output statements (if possible)?
a) sample :: ~sample() {cout << “Welcome”; }
b) sample :: ~sample() { };
c) void ~sample() { }
d) ~sample() { };




Chapter VI: Constructors and Destructors

1. c) They cannot be called more than once in a program
2. a) A :: A()
3. b) I, II, and IV
4. a) xyz(){ }
5. a) simple s(“welcome”);
6. c) Line 5 cannot include void
7. b) All are correct
8. d) All of the above
9. a) Both I and II
10. a) sample :: ~sample() {cout << “Welcome”; }


Chapter VII: Operator Overloading and Type Conversions

1. Which of the following operators cannot be overloaded?
a) ?:
b) ::
c) .*
d) All of the above
2. Operator Overloading is also known by the term
a) runtime polymorphism
b) compile-time polymorphism
c) Both a and b
d) None of the above
3. Operator overloading is necessary because
a) C++ attempts to make the user-defined classes act like built-in types
b) To provide new definitions for most of the C++ operators
c) To add user-defined data type like basic data types
d) All of the above
4. Which of the following is required to overload an operator in C++?
a) operator function
b) built-in function
c) Both a and b
d) None of the above
5. Identify the correct definition of an operator function for the class sample, which overloads a unary minus operator and returns no values.
a) sample :: oper-() { };
b) void sample :: operator-() { }
c) void sample :: operator-() { };
d) sample :: operator-() { }
6. Which of the following is not true about the code segment given below?
float sample :: operator +(float x) {// code }
a) It receives only one float type argument explicitly
b) It returns a float type value
c) It is a member function of sample
d) None of the above
7. Which of the following operators cannot use friend functions for overloading?
I. ==
II. ( )
III. [ ]
IV. ->
a) I only
b) II only
c) II, III and IV
d) I, II and III
8. Which of the following is not true about operator overloading?
a) Only existing operators can be overloaded
b) Binary arithmetic operators (+,-,*,/) need not return a value
c) It is impossible to redefine an operator
d) All of the above
9. Which of the following code segments will convert a class object namely sample to type double?
a) sample :: operator double() { }
b) operator double() { }
c) sample :: operator double();
d) operator double();
10. Which of the following statements does correctly describe the casting operator function?
a) It must not specify return type
b) It must be a class member
c) It must not have any arguments
d) All of the above


Chapter VII: Operator Overloading and Type Conversions

1. d) All of the above
2. b) compile-time polymorphism
3. d) All of the above
4. a) operator function
5. b) void sample : : operator-() { }
6. d) None of the above
7. c) II, III and IV
8. b) Binary arithmetic operators (+,-,*,/) need not return a value
9. a) sample :: operator double() { }
10. d) All of the above



Chapter VIII: Inheritance: Extending Classes

1. Which of the following OOP concepts is supported by Inheritance?
a) Abstraction
b) Encapsulation
c) Reusability
d) None of the above
2. Which of the following does a derived class inherit from a base class?
a) public and protected class members
b) public and private class members
c) only public data
d) Everything
3. Which of the following statement(s) is true, if a derived class is publicly inherited from a base class?
I. The public members of the base class become public members of the derived class
II. The public members of the base class become private members of the derived class
III. The public members of the base class are inaccessible to the objects of the derived class
IV. All of the above
a) I only
b) Both I and II
c) Both I and III
d) IV only
4. Consider the following code segment:
class A
{
int a;
public:
int b;
void inp();
}
class B : A
{
// members of B
}
Which of the following statement(s) is NOT true regarding the above code?
a) The public members, namely ‘b’ and inp() of class A become private members of class B
b) The public members, namely ‘b’ and inp() of class A can be accessed by the member functions of class B
c) The public members, namely ‘b’ and inp() of class A are inaccessible to the objects of class B
d) None of the above
5. According to the following code, which of the following class members does the derived class inherit from the base class?
class base
{
float x;
int y;
public:
int a;
void get_a();
void put_a();
}
class derived : public base
{
// members of B
}
a) float x
b) int y
c) get_a(), void put_a()
d) All of the above
6. Which of the following statement(s) is true about the visibility of inherited members?
a) When a class is inherited in protected mode, the private members of the base class become protected members of the derived class
b) When a class is inherited in private mode, only the public members of the base class can be inherited to the derived class
c) When a class is inherited in public mode, the private members of the base class cannot be inherited
d) None of the above
7. What are the visibility modifiers available in C++?
a) public
b) private
c) protected
d) All of the above
8. Which of the following functions can have access to the private and protected members of a class?
a) A member function of a class that is a friend of the class
b) A member function of a derived class
c) A function that is a friend of the class
d) All of the above
9. Consider the following code segment:
class B : public A
{
int a;
public:
int b;
void a_inp();
void a_out();
}
class C : public A
{
private:
float x;
public:
double get_calc();
double put_calc();
}
Which of the following statement(s) is true regarding the above code?
a) Class A is the parent class of both the classes B and C; and class B inherits get_calc() from class C
b) Class A is the parent class of both the classes B and C, which do not have any relationship between them
c) Two classes cannot be the child classes of the same base class
d) Class A is the parent class of both the classes B and C; and class C inherits a_out() from class B
10. Consider the following code segment:
class Book {……..};
class Prose : public Book {……….};
class Poetry : public Prose {……….};
The above code is an example of
a) Multiple Inheritance
b) Multilevel Inheritance
c) Hierarchical Inheritance
d) Hybrid Inheritance
11. Consider a class D, which is inherited from two classes B and C. The classes B and C inherited the members from class A. Which feature of C++ does allow you to handle such kind of mulitpath inheritance?
a) Abstract class
b) Virtual base class
c) Duplicate class
d) None of the above
12. If a class contains the objects of another class as its members, then it is known as
a) Containership
b) Private inheritance
c) Dynamic notation
d) None of the above

Chapter VIII: Inheritance: Extending Classes

1. c) Reusability
2. a) public and protected class members
3. a) I only
4. d) None of the above
5. c) get_a(), void put_a()
6. c) When a class is inherited in public mode, the private members of the base class cannot be inherited
7. d) All of the above
8. d) All of the above
9. b) Class A is the parent class of both the classes B and C, which do not have any relationship between them
10. b) Multilevel Inheritance
11. b) Virtual base class
12. a) Containership

Chapter IX: Pointers, Virtual Functions and Polymorphism
1. Which of the following is true about pointers?
a) A pointer is a data type
b) A pointer is a keyword
c) A special type of integer variable
d) None of the above
2. Which of the following statement(s) is true according to the following statement?
p=*ptr;
a) p must be a pointer variable
b) The value of ptr is assigned to the variable p
c) The address of the pointer ptr is assigned to the variable p
d) The value of the variable that the pointer ptr is pointing to is assigned to the variable p
3. Compile time polymorphism is also known as
a) early binding
b) static binding
c) static linking
d) All of the above
4. C++ supports a mechanism virtual function to achieve
a) compile time polymorphism
b) function overloading
c) run time polymorphism
d) operator overloading
5. Which of the following is NOT true about virtual functions?
I. They cannot be static members
II. A virtual function can be a friend of another class
III. We can have virtual constructors, but we cannot have virtual destructors
IV. They are accessed by using object pointers
a) II only
b) III only
c) Both II and III
d) I, II and IV
6. Consider the following code segment:
int main()
{
int x, *x_ptr=&x;
x=5;
x_ptr=NULL;
cout<< x << “ “ << *x_ptr;
return 0;
}
What is the output of the above code?
a) 0
b) but addresses are same
c) but having different addresses
d) 5
7. Consider the following code segment:
int main()
{
double f, *f_ptr=&f;
f=5.25;
f_ptr=4.5;
cout<< “Value of f is:” << “ “ << f;
return 0;
}
What is the output of the above code?
a) Value of f is: 5.25
b) Value of f is:
c) Value of f is: 4.5
d) Compiler error
8. How will you assign value ‘5’ to the variable ‘x’ inside a member function using this pointer?
a) this->x=5;
b) this.x=5;
c) x=5;
d) None of the above
9. Consider a class X, which includes a virtual function called X_output. The virtual function receives a float value and returns nothing. Find out the function prototype for the same.
a) virtual void X_output(float );
b) X:: virtual void X_output(float );
c) virtual X:: void X_output(float );
d) virtual :: void X_output(float );
10. What would be the output of the following code?
#include< iostream.h>
#include< conio.h>
class A
{
public:
virtual void disp()
{
cout<<"This is from class A";
}
};

class B : public A
{
public:
void disp()
{
cout<<"This is from class B";
}
};
void main()
{
clrscr();
A *s;
s = new B();
s->disp();
}
a) This is from class A
b) This is from class B
c) This is from class A This is from class B
d) None of the above
11. Which of the following is NOT true about pure virtual function?
a) It is also called do-nothing function
b) It cannot declare its own objects
c) A virtual function, which is not equated to zero is called a pure virtual function
d) None of the above

Chapter IX: Pointers, Virtual Functions and Polymorphism

1. a) A pointer is a data type
2. d) The value of the variable that the pointer ptr is pointing to is assigned to the variable p
3. d) All of the above
4. c) run time polymorphism
5. b) III only
6. a) < hexadecimal address> 0
7. d) Compiler error
8. a) this->x=5;
9. a) virtual void X_output(float );
10. b) This is from class B
11. c) A virtual function, which is not equated to zero is called a pure virtual function

Chapter X: Managing Console I/O Operations

1. Which of the following is not an input/output stream class?
a) ios
b) istream
c) ostream
d) None of the above
2. Which class does define the member function put()?
a) istream
b) ostream
c) streambuf
d) None of the above
3. Which of the following code would read a line of text from char type variable, book[20]?
a) cin.getline(book,20);
b) cin.getline(book[20]);
c) cin.getln(book,20);
d) None of the above
4. What would be the output of the following code?
char *str= “Managing Console I/O Operations”;
cout.write(str,15);
a) Managing Console I/O Operations
b) Managing C
c) Managing Consol
d) None of the above
5. Which of the following feature does allow you to format the output in C++?
a) Manipulators
b) ios class functions and flags
c) User-defined output functions
d) All of the above
6. Which of the following functions allow you clear the specified flags?
a) fill()
b) unsetf()
c) cflag()
d) None of the above
7. What would be the output of the following code?
cout.width(5);
cout.precision(2);
cout<< 846.209;
a) 846.20
b) 846.21
c) 846.209
d) 846.2
8. Identify the equivalent ios function for the manipulator ‘setw’.
a) width()
b) setwidth()
c) swidth()
d) None of the above
9. What would be the output of the following code?
cout.fill(‘$’);
cout.setf(ios::left, ios:: adjustfield);
cout.width(20);
cout<< “I/O Operations”;
a) Operations$$$
b) I/O Operations
c) I/O Operations$$$$$$
d) I/O Operations$$$$$$$
10. Which of the following flags do not have any bit fields?
a) showpoint
b) showpos
c) showbase
d) Both a and b

Chapter X: Managing Console I/O Operations

1. d) None of the above
2. b) ostream
3. a) cin.getline(book,20);
4. c) Managing Consol
5. d) All of the above
6. b) unsetf()
7. b) 846.21
8. a) width()
9. c) I/O Operations$$$$$$
10. d) Both a and b


Chapter XI: Working with Files

1. All file stream classes are derived from
a) ifstream
b) fstreambase
c) ifstreambase
d) ofstreambase
2. Which of the following stream classes provide support for simultaneous input and output operations?
a) ifstream
b) ofstream
c) fstream
d) None of the above
3. Which of the following statement(s) creates an output stream object called out_strobj and opens a file named “sample”?
a) ofstream out_strobj(“sample”);
b) ofstream out_strobj(sample);
c) ostream out_strobj(sample);
d) ostream out_strobj(“sample”);
4. Which of the following function(s) opens a file named “sample” for writing only?
a) ofstream_object.open(“sample”, ios::app);
b) ifstream_object.open(sample, ios::out);
c) ofstream_object.open(“sample”, ios::out);
d) ifstream_object.open(sample, ios::app);
5. Consider the following code segment:
1: void main()
2: {
3: char line[80],str[80];
4: str = "Working with C++ files";
5: ofstream fout;
6: fout.open("sample.txt",ios::out);
7: fout << str ;
8: fout.close();
9: }
The above code will not compile. Assume that all the header files are included in the program. Identify which line should be changed to fix the error?
a) Line 6 & Line 7
b) Line 4
c) Line 3
d) Line 8
6. Consider a file named ‘sample.txt’ contains the string “Working with C++ files”. Now the following code attempts to read the file.
void main()
{
char line[80];
ifstream fin;
fin.open(“sample.txt”,ios::in);
fin>>line;
cout<< line;
fin.close();
}
What would be the output of the following code?
a) Working with C++ files
b) Working
c) Working w
d) None of the above
7. Consider the following code segment:
void main()
{
char c,line[50];
clrscr();
fstream fout;
fout.open("sample.txt",ios::out|ios::in);
fout <<"Working with C++ Files";
fout.seekg(0,ios::beg);
fout.seekg(5,ios::cur);
fout.get(c);
cout<< c;
fout.close();
}
What would be the output of the above code?
a) g
b) i
c) n
d) Working
8. Assume that the file pointer points to the byte number 22. Now consider the following code segment:
fout.seekg(2,ios::beg);
fout.seekg(-4,ios::beg);
int k=fout.tellg();
cout<< k;
What would be the value of k?
a) 24
b) 20
c) 22
d) None of the above
9. Which of the following function(s) does allow you to read data in binary form?
a) write()
b) read()
c) get()
d) Both a and b
10. Which of the following input function reads the variable ‘aval’ of type float with input file stream object ‘file_obj’?
a) file_obj.read((float *) & aval, sizeof (aval));
b) file_obj.read((char *) & aval, sizeof (aval));
c) file_obj.read(& aval, sizeof (aval));
d) file_obj.read(float aval, &aval);

Chapter XI: Working with Files

1. b) fstreambase
2. c) fstream
3. a) ofstream out_strobj(“sample”);
4. c) ofstream_object.open(“sample”, ios::out);
5. c) Line 3
6. b) Working
7. c) n
8. b) 20
9. d) Both a and b
10. b) file_obj.read((char *) & aval, sizeof (aval));

Chapter XII: Templates

1. C++ Templates support the concept of
I. modular programming
II. generic programming
III. structural programming
IV. None of the above
2. Which of the following is NOT true about Templates?
I. They allow you to define generic classes
II. Template type can be substituted by any data-type
III. They eliminate code duplication for different data types
IV. None of the above
3. Consider the following code segment:
template < class temp >
class sample
{
……..//code
};
Identify the correct syntax for declaring a dynamic array of characters using the above template.
I. sample < char> characterArray;
II. sample < datatype> characterArray;
III. temp < char> characterArray;
IV. temp < datatype> characterArray;
4. Which of the following statement(s) defines more than one generic data type in a class template?
I. template < class temp1, class temp2…>
II. template < class temp1; class temp2…>
III. template < class temp1, temp2…>
IV. template < class temp1; temp2…>
5. Consider the following code segment:
template < class temp>
void sample(temp &x)
{
…….//code
};
Which of the following is true about the above code?
I. Declares a sample() class template that receives the given data type for a single value and returns no value
II. Declares a temp function template that receives the given data type for a single value
III. Declares a sample() function template that receives a value of given data type and returns no value
IV. None of the above
6. Which of the following are not the advantages of class templates in C++?
I. One C++ class template can handle different types of parameters
II. Testing and debugging efforts are reduced
III. It allows the process of instantiation
IV. None of the above
7. Consider the following code segment:
1: template
2: void show(temp1 a, temp2 b)
3: {
4: cout<<”a”;
5: }
Assume that there is no logic error. Identify if there is any declaration error:
a) Line1
b) Line 4
c) Both a and b
d) No error
8. Consider the following code segment:
#include< iostream>
template< class temp>
int powerofTwo(temp &x)
{
return(!(x-1)&x);
}
void main()
{
int b=powerofTwo(100);
cout<< b;
}
What is the output of the above code?
a) 0
b) 1
c) 100
d) None of the above
9. Which of the following statement(s) is similar to the declaration of non-type template parameters?
a) Pointer to member
b) Pointer to object or pointer to function
c) Reference to object or reference to function
d) All of the above
10. Consider the declaration of a template non-type argument in the following code snippet:
template < class temp, int max> class sample
{
temp a[max];
static int b=50;
// ………
}
Which of the following statement(s) does not invoke the above template correctly?
I. sample< float, 10*10> x;
II. sample< float, 30>20> x;
III. sample< float, (30>20)> x;
IV. sample< float, 10.0> x;
a) I only
b) III only
c) Both II and IV
d) Both I and III


Chapter XII: Templates

1. b) generic programming
2. d) None of the above
3. a) sample < char> characterArray;
4. a) template < class temp1, class temp2…>
5. c) Declares a sample() function template that receives a value of given data type and
returns no value
6. d) None of the above
7. a) Line1
8. a) 0
9. d) All of the above
10. c) Both II and IV


Chapter XIII: Exception Handling

13. Which of the following error occur due to poor understanding of the language?
a) Logical error
b) Run-Time error
c) Syntax error
d) Program error
14. Which block handles the exception?
a) Finally block
b) Catch block
c) Try block
d) None of the above
15. Which of the following statement(s) is true about try block?
I. The try block is immediately followed by the catch block.
II. Try statement can have only one catch statement.
III. A program segment can have more than one catch statement with a try statement.
IV. All of the above
a) I only
e) Both I and II
f) Both I and III
g) IV only
16. What happens when the try block does not throw any exception?
e) The program is aborted
f) Normal execution is completed
g) Cannot be predicted
h) None of the above
17. What will be the output of the following code segment?

void function();
int main()
{
try
{
function();
}
catch (char)
{
cout<<"Caught a Char<< endl";
}
return 0;
}
void function()
{
throw 2;
}
}
a) Error in execution
b) Caught a Char
c) Abort() function is invoked
d) None of the above
18. Which of the following statement(s) is NOT true about catch (…) statement?
I. All the throws are not caught by catch (…) statement
II. Catch (…) statement should always be placed last in the list of handlers. Placing it before other catch blocks would prevent those blocks from catching exceptions
III. All the throws are caught by catch (…) statement
IV. Catch (…) statement should always be placed first in the list of handlers. Placing it before other catch blocks would prevent those blocks from catching exceptions
a) I only
b) Both II and III
b) Both III and IV
c) Both I and IV
19. Which of the following statement(s) is used to rethrow an exception?
a) throw(exception);
b) throw exception;
c) throw;
d) None of the above
20. Consider the following code segment:
void divide(int x, int y)
{
try
{
if(y==0)
throw y;
else
cout<< endl<<"Division result is: " << x/y<< endl;
}
catch(int)
{
throw;
cout<< endl<<"Caught Divide by zero error inside function";
}
}
int main()
{
try
{
divide(20,0);
}
catch(int)
{
cout<< endl<<"Caught Divide by zero error inside main";
}
return 0;
}
What will be the output of the above code?
e) Caught Divide by zero error inside main
f) Caught Divide by zero error inside main
Caught Divide by zero error inside function
g) Caught Divide by zero error inside function
h) None of the above
21. Which of the following is the general form of using an exception specification?
a) type function(arg-list) throw(type-list)
{
........
........ Function body
........
}
b) type function(arg-list) throw(arg-list)
{
........
........ Function body
........
}
c) type function(type-list) throw(type-list)
{
........
........ Function body
........
}
d) None of the above
22. What will happen if an error is not handled?
a) Error in compilation
b) Abrupt program termination
c) Error in execution
d) None of the above

Chapter XIII: Exception Handling

11. c) Syntax error
12. b) Catch block
13. c) Both I and III
14. b) Normal Execution is completed
15. c) Abort() function is invoked
16. d) Both I and IV
17. c) throw;
18. a) Caught Divide by zero error inside main
19. a) type function(arg-list) throw(type-list)
{
........
........ Function body
........
}
20. b) Abrupt program termination


Chapter XIV: Introduction to the Standard Template Library

1. Which of the following is not a sequence container available in STL?
a) vector
b) deque
c) array
d) list
2. Consider the following code snippet:
int main()
{
vector < int> v1(10);
vector < int> v2(10);
v1.at(0)=24;
v1.at(1)=20;
v1.at(2)=38;
for(int i=0;i <3; i++)
{
cout<< v1[i]<<" ";
}
v2=v1;
cout<< v2[0];
return 0;
}
What is the output of the above program?
a) Error as vectors cannot be copied
b) 24 20 38 24
c) 24 20 38
d) No output
3. Identify the correct combination for the following data:
I. Associative container : A. Random
II. Derived container : B. multiset
III. Sequence container : C. priority_queue
IV. STL iterator : D. deque

a) I-D, II-A, III-C, IV-B
b) I-D, II-C, III-A, IV-B
c) I-B, II-D, III-C, IV-A
d) I-B, II-C, III-D, IV-A
4. Consider the following code segment:
void main()
{
list < int> l1;
list < int> l2;
l1.push_front(6);
l1.push_back(8);
l1.push_front(4);
l1.push_back(5);
l1.push_front(3);
l1.reverse();
l1.front()=l1.front()*5;
l2=l1;
cout<< l1.front()<<" ";
cout<< l2.back();
}
What will be the output of the above code if there is no compile error?
a) 25 3
b) 16 4
c) 9 8
d) 36 5
5. Which of the following is true about Iterators in C++?
a) The functioning of Iterators is similar to pointers
b) Only the derived containers are traversable with iterators
c) The input and output iterators support the most functions of iterators
d) None of the above
6. Consider the following code segment:
1. void main()
2. {
3. list < char> l;
4. list < char>:: iterator i;
5. l.push_back('k');
6. l.push_back('i');
7. l.push_back('n');
8. i=l.end();
9. cout<< i;
10.}
The above code will not compile. Identify which line should be changed to fix the error.
a) Line 4
b) Line 3 & Line 4
c) Line 8
d) Line 9
7. Pick out the correct usage of the list function called, splice().
a) Deletes a list from the invoking list
b) Arranges the list elements as specified
c) Inserts a list into the invoking list
d) Gives the size of the list
8. If map_test is a map,
map k = map_test;
Which of the following statement(s) does illustrate the above code correctly?
I. It will create a new map k whose elements are logical copies of the elements of map_test.
II. It is equivalent to: mapmap_test(k);
III. It will create a new map map_test whose elements are logical copies of the elements of k.
IV. It is equivalent to: mapk(map_test);
a) I only
b) IV only
c) Both II and III
d) Both I and IV
9. Consider ‘i’ is an iterator that accesses the elements of a map. Which of the following will be used to access the two entries, namely key and value of the map?
a) i.first, i.second
b) (*i).first, (*i).second
c) *i.first, *i.second
d) *i.first, (*i).second
10. Which of the following is NOT true about function objects?
a) The class, which creates a function object, contains only one overloaded operator() function.
b) Function objects can be used to perform arithmetic and logical operations.
c) For using function objects, header file is needed.
d) None of the above

Chapter XIV: Introduction to the Standard Template Library

1. c) array
2. b) 24 20 38 24
3. d) I-B, II-C, III-D, IV-A
4. a) 25 3
5. a) The functioning of Iterators is similar to pointers
6. d) Line 9
7. c) Inserts a list into the invoking list
8. d) Both I and IV
9. b) (*i).first, (*i).second
10. d) None of the above


Chapter XV: Manipulating Strings

23. string is a
a) data-type
b) class
c) namespace
d) function
24. Which of the following function is used to read a line of text with blanks?
a) inputline( )
b) getline( )
c) putline( )
d) None the above
25. Which of the following function is not supported by the string class?
a) count( )
b) Assign( )
c) resize( )
d) empty( )
26. Consider the following code segment:
void main()
{
string s1("example");
string s2("2468");
s1.insert(2,s2);
s1.erase(3,5);
s1.replace(2,3,s2);
cout<< s1;
cout<< endl;
}
What will be the output of the above code?

h) ex2468ample
i) ex2ple
j) ex2468e
k) None of the above
27. Consider the following code segment:
void main()
{
int a,b;
string s1("aabbcc");
string s2("bbccaa");
a = s1.compare(4,2,s2,2,2);
b = s1.compare(0,2,s2,0,2);
if(a == 0)
cout<<"Equal , ";
else
cout<< "Not equal ,";
if(b ==0)
cout<<"Equal";
else
cout<<"Not Equal";
}
What will be the output of the above code?
i) Not Equal , Equal
j) Not Equal , Not Equal
k) Equal , Equal
l) Equal , Not Equal
28. Which of the following function does exchange the contents of two strings?
a) exchange( )
b) change( )
c) swap( )
d) None of the above
29. Which of the following function can retrieve the character stored at a specified location?
a) at( )
b) find_first_of( )
c) find( )
d) find_last_of( )
30. Consider the following code segment:
void main()
{
string s1("Time and tide wait for none");
int f;
f=s1.find("t");
cout<< endl<< f;
f=s1.find_first_of("t");
cout<<"\t"<< f;
f=s1.find_last_of("t");
cout<<"\t"<< f;
f=s1.find_first_not_of("t");
cout<<"\t"<< f;
f=s1.find_last_not_of("t");
cout<<"\t"<< f;
}
What will be the output of the above code?

a) 0 0 17 1 26
b) 0 9 17 0 26
c) 9 0 17 0 26
d) 9 9 17 0 26
31. Consider the following statement:
s1.compare (s2);
Which of the following statement is true about compare( ) function?
I. Returns 0 if s1 and s2 are equal
II. Returns -1 if s1 is greater
III. Returns -1 if s2 is greater
IV. Returns 1 if s1 and s2 are equal
a) Both I and III
b) Both III and IV
c) Both I and II
d) I only
32. Which of the following operator does represent concatenation assignment?
i) &=
j) +
k) +=
l) =

Chapter XV: Manipulating Strings

21. b) class
22. b) getline( )
23. a) count( )
24. c) ex2468e
25. d) Equal , Not Equal
26. c) swap( )
27. a) at( )
28. d) 9 9 17 0 26
29. a) Both I and III
30. c) +=


Chapter XVI: New Features of ANSI C++ Standard

1. Which of the following data type(s) is introduced by ANSI C++?
a) bool
b) wchar_t
c) char_t
d) Both a and b
2. Consider the following code snippet:

……………………………..
int p;
string s("Pass");
bool k;
if(s=="pass")
{
k=true;
}
p=true+50+false;
cout<< p;
……………………………..
What will be the output on executing the above code?
a) Error as the Boolean values cannot be added
b) 51
c) 52
d) No output
3. Which of the following character literal(s) does use two bytes of memory?
a) wide_character literal
b) byte_character literal
c) new_character literal
d) None of the above
4. Which of the following casting operator can cast a pointer to any other type of pointer in ANSI C++?
a) const_cast
b) dynamic_cast
c) reinterpret_cast
d) static_cast
5. Consider the following code snippet:
……………….
char *a,b;
if(typeid(a)==typeid(b))
cout<<"a";
else
cout<<"b";
………………….
What will be the output of the above code?
a) b
b) a
c) Compilation error
d) No output
6. Consider the following code snippet:
1. class sample
2. {
3. public:
4. explicit sample(float);
5. int a;………………
5. }

6. sample x=10.5;
………..
The code will not compile. Which of the following option will solve the problem?
a) Line 6 should be changed to sample x(10.5);
b) In line 4, explicit keyword should be removed
c) Both a and b
d) Either a or b
7. Which of the following keyword is used to modify a const object?
a) explicit
b) typeid
c) mutable
d) typename
8. Which of the following option will create a user-defined namespace in ANSI C++?
a) namespace namespace_name{….};
b) namespace(){namespace_name}
c) namespace namespace_name{….}
d) namespace(){namespace_name}
9. Let us define a namespace called ‘samplespace’, which includes a member variable, namely ‘p’. How will you access this member variable from outside the namespace?
I. using namespace samplespace;
II. using samplespace :: p;
III. samplespace.p;
IV. p :: samplespace();
a) I only
b) II only
c) Both III and IV
d) Both I and II
10. Write the equivalent expression using operator keywords for the following.
(a!=b) > (~(a & b)&=(a^b))
a) (a not_eq b) gt (not( a bitand b) and_eq (a xor b))
b) (a not_eq b) > (compl( a bitand b) and_eq (a xor b))
c) (a not_eq b) gt (not( a bitand b) not_eq (a exor b))
d) (a not_eq b) > (compl( a bitand b) not_eq (a exor b))

Chapter XVI: New Features of ANSI C++ Standard

1. d) Both a and b
2. b) 51
3. a) wide_character 2literal
4. c) reinterpret_cast
5. a) b
6. d) Either a or b
7. c) mutable
8. c) namespace namespace_name{….}
9. d) Both I and II
10. b) (a not_eq b) > (compl( a bitand b) and_eq (a xor b))

Chapter XVII: Object-Oriented Systems Development

1. Which model of object-oriented paradigm replaces the classic “water-fall” model of procedure-oriented development?
a) Fountain model
b) Spiral model
c) Throwaway prototyping model
d) None of the above
2. Functional decomposition technique can be used to implement _________.
a) Spiral model
b) Water-fall model
c) Reuse model
d) Fountain model
3. Which of the following is true about object-oriented analysis (OOA) approach?
a) Identifies the objects and their attributes
b) Identifies the services that each object is expected to provide
c) Establishes interconnections between the objects
d) All of the above
4. Data flow diagram is also known as________.
a) Bubble chart
b) Data flow graph
c) Both a and b
d) None of the above
5. Which of the following can be used to identify the services provided and received by objects?
a) Information Flow Diagram
b) Entity-Relationship Diagram
c) Either a or b
d) Neither a nor b
6. Which relationship defines ‘is_a’ relationship in object-oriented design?
a) Containment relationship
b) Inheritance relationship
c) Use relationship
d) None of the above
7. Let us assume that there are two classes, namely M and N. Consider the following statement:
M calls a member of N
Which object-oriented relationship can provide this information?
a) Use relationship
b) Containment relationship
c) Inheritance relationship
d) None of the above
8. Which of the following is associated with the process of a class organization?
a) Single-tree model
b) Forest model
c) Both a and b
d) None of the above
9. Which of the following is NOT true about prototyping process?
a) It is the process of building and testing a working model of the proposed system
b) It is the process of time consuming and expensive
c) Both a and b
d) None of the above
10. Which of the following can be used to identify the objects in object-oriented design?
a) Textual analysis
b) Data flow diagram
c) Both a and b
d) None of the above

Chapter XVII: Object-Oriented Systems Development

1. a) Fountain model
2. b) Water-fall model
3. d) All of the above
4. c) Both a and b
5. c) Either a or b
6. b) Inheritance relationship
7. a) Use relationship
8. c) Both a and b
9. b) It is the process of time consuming and expensive
10. c) Both a and b

Thursday, April 23, 2009

DATA STRUCTURE PROGRAMS IN C++

Aim: Write a C++ program to find the sparse of a matrix


PROGRAM


#include<iostream.h>
#include<conio.h>

void main()
{
int a[50][50],sp[50][50],i,j,k=0,m,n;
clrscr();
cout<<"\n Enter the order of matrix: ";
cin>>m>>n;
cout<<"\n Enter the elements: ";
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
cin>>a[i][j];
if(a[i][j]!=0)
{
k++;
sp[k][0]=i;
sp[k][1]=j;
sp[k][2]=a[i][j];
}
}
}
sp[0][0]=m;
sp[0][1]=n;
sp[0][2]=k;
cout<<"\n The sparse matrix is:\n";
for(i=0;i<=k;i++)
{
for(j=0;j<3;j++)
cout<< sp[i][j]<<" ";
cout<<"\n";
}
getch();
}




OUTPUT


Enter the order of matrix: 3 3

Enter the elements:
0 1 0
2 0 0
0 0 3

The sparse matrix is:
3 3 3
0 1 1
1 0 2
2 2 3




Aim: Write a C++ program to find the transpose of a matrix using the given sparse matrix



PROGRAM


#include<iostream.h>
#include<conio.h>

void main()
{
int a[50][50],sp[50][50],i,j,k=0,m,n,tsp[50][50],p=0;
clrscr();
cout<<"\n Enter the order of matrix: ";
cin>>m>>n;
cout<<"\n Enter the elements: ";
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
cin>>a[i][j];
if(a[i][j]!=0)
{
k++;
sp[k][0]=i;
sp[k][1]=j;
sp[k][2]=a[i][j];
}
}
}
sp[0][0]=m;
sp[0][1]=n;
sp[0][2]=k;
cout<<"\n The sparse matrix is:\n";
for(i=0;i<=k;i++)
{
for(j=0;j<3;j++)
cout<< sp[i][j]<<" ";
cout<<"\n";
}
p=0;
for(j=0;j< n;j++)
{
for(i=1;i<=k;i++)
{
if(sp[i][1]==j)
{
p++;
tsp[p][0]=sp[i][1];
tsp[p][1]=sp[i][0];
tsp[p][2]=sp[i][2];
}
}
}
tsp[0][0]=sp[0][1];
tsp[0][1]=sp[0][0];
tsp[0][2]=p;
cout<<"\n Transpose of sparse \n";
for(i=0;i<=p;i++)
{
for(j=0;j<3;j++)
cout<< tsp[i][j]<<" ";
cout<<"\n";
}

getch();
}

OUTPUT

Enter the order of matrix: 3 3

Enter the elements:
1 0 0
0 0 2
0 3 0

The sparse matrix is:
3 3 3
0 0 1
1 2 2
2 1 3

Transpose of sparse
3 3 3
0 0 1
1 2 3
2 1 2


Aim: Write a C++ program to find transpose of a matrix using fast transpose method



PROGRAM


#include<iostream.h>
#include<conio.h>

void main()
{
int a[50][50],sp[50][50],i,j,k=0,m,n,t,tsp[50][50];
int start[50],size[50];
clrscr();
cout<<"\n Enter the order of matrix: ";
cin>>m>>n;
cout<<"\n Enter the elements: ";
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
cin>>a[i][j];
if(a[i][j]!=0)
{
k++;
sp[k][0]=i;
sp[k][1]=j;
sp[k][2]=a[i][j];
}
}
}
sp[0][0]=m;
sp[0][1]=n;
sp[0][2]=k;
cout<<"\n The sparse matrix is:\n";
for(i=0;i<=k;i++)
{
for(j=0;j<3;j++)
cout<< sp[i][j]<<" ";
cout<<"\n";
}
for(i=0;i< n;i++)
size[i]=0;

for(i=1;i<=k;i++)
{
t=sp[i][1];
size[t]++;
}
start[0]=1;
for(i=1;i< n;i++)
start[i]=start[i-1]+size[i-1];


for(i=1;i<=k;i++)
{
j=sp[i][1];
t=start[j];
tsp[t][0]=sp[i][1];
tsp[t][1]=sp[i][0];
tsp[t][2]=sp[i][2];
start[j]++;
}

tsp[0][0]=sp[0][1];
tsp[0][1]=sp[0][0];
tsp[0][2]=sp[0][2];
cout<<"\n Transpose \n";
for(i=0;i<=k;i++)
{
for(j=0;j<3;j++)
cout<< tsp[i][j]<<" ";
cout<<"\n";
}
getch();
}

OUTPUT

Enter the order of matrix: 3 3

Enter the elements:
0 1 0
2 0 0
0 0 5

The sparse matrix is:
3 3 3
0 1 1
1 0 2
2 2 5

Transpose
3 3 3
0 1 2
1 0 1
2 2 5



Aim: Write a C++ program to convert the given sparse to original matrix



PROGRAM



#include<iostream.h>
#include<conio.h>
#include<process.h>

void main()
{
int sp[50][50],a[40][40],k,i,j,m,n,r,c;
clrscr();
cout<<"\n Enter the order of sparse matrix: ";
cin>>n;
cout<<"\n Enter the sparse matrix: ";
for(i=0;i< n;i++)
for(j=0;j<3;j++)
cin>>sp[i][j];

if(sp[0][2]!=(n-1))
{
cout<<"\n Error ";
getch();
exit(0);
}

for(i=0;i<40;i++)
for(j=0;j<40;j++)
a[i][j]=0;

m=sp[0][0];
n=sp[0][1];
k=sp[0][2];

for(i=1;i<=k;i++)
{
r=sp[i][0];
c=sp[i][1];
a[r][c]=sp[i][2];
}




cout<<"\n The original matrix is: \n";
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
cout<< a[i][j]<<" ";
cout<<"\n";
}
getch();
}



OUTPUT



Enter the order of sparse matrix: 4

Enter the sparse matrix:
3 3 3
0 1 2
1 0 1
2 1 3

The original matrix is:
0 2 0
1 0 0
0 3 0


Aim: Write a C++ program to perform sparse matrix addition



PROGRAM


#include”iostream.h”
#include”conio.h”
void main()
{
int num,sp1[50][50],i,j,k1=0,k2=0,k=0,m1,n1,m2,n2,sp2[50][50],sp[50][50];
int m,n;
clrscr();
cout<<"\n Enter the order of matrix1: ";
cin>>m1>>n1;
cout<<"\n Enter the elements: ";
for(i=0;i< m1;i++)
{
for(j=0;j< n1;j++)
{
cin>>num;
if(num!=0)
{
k1++;
sp1[k1][0]=i;
sp1[k1][1]=j;
sp1[k1][2]=num;
}
}
}
sp1[0][0]=m1;
sp1[0][1]=n1;
sp1[0][2]=k1;

cout<<"\n Enter the order of matrix2: ";
cin>>m2>>n2;
cout<<"\n Enter the elements: ";
for(i=0;i< m2;i++)
{
for(j=0;j< n2;j++)
{
cin>>num;
if(num!=0)
{
k2++;
sp2[k2][0]=i;
sp2[k2][1]=j;
sp2[k2][2]=num;
}
}
}
sp2[0][0]=m2;
sp2[0][1]=n2;
sp2[0][2]=k2;
i=1;
j=1;
k=0;
while(i<=k1&&j<=k2)
{
if(sp1[i][0]==sp2[j][0])
{
if(sp1[i][1]< sp2[j][1])
{
k++;
sp[k][0]=sp1[i][0];
sp[k][1]=sp1[i][1];
sp[k][2]=sp1[i][2];
i++;
}
else if(sp1[i][1]>sp2[j][1])
{
k++;
sp[k][0]=sp2[j][0];
sp[k][1]=sp2[j][1];
sp[k][2]=sp2[j][2];
j++;
}
else
{
k++;
sp[k][0]=sp2[j][0];
sp[k][1]=sp2[j][1];
sp[k][2]=sp2[j][2]+sp1[i][2];
j++;i++;
}
}
else if(sp1[i][0]< sp2[j][0])
{
k++;
sp[k][0]=sp1[i][0];
sp[k][1]=sp1[i][1];
sp[k][2]=sp1[i][2];
i++;
}
else if(sp1[i][0]>sp2[j][0])
{
k++;
sp[k][0]=sp2[j][0];
sp[k][1]=sp2[j][1];
sp[k][2]=sp2[j][2];
j++;
}
}
while(i<=k1)
{
k++;
sp[k][0]=sp1[i][0];
sp[k][1]=sp1[i][1];
sp[k][2]=sp1[i][2];
i++;
}
while(j<=k2)
{
k++;
sp[k][0]=sp2[j][0];
sp[k][1]=sp2[j][1];
sp[k][2]=sp2[j][2];
j++;
}
m=((m1>m2)?m1:m2);
n=((n1>n2)?n1:n2);
sp[0][0]=m;
sp[0][1]=n;
sp[0][2]=k;

cout<<"\n The sparse of sum matrix is:\n";
for(i=0;i<=k;i++)
{
for(j=0;j<3;j++)
cout<< sp[i][j]<<" ";
cout<<"\n";
}
getch();
}



OUTPUT



Enter the order of matrix1: 3 3

Enter the elements:
1 0 0
0 5 0
0 0 6

Enter the order of matrix2: 3 3

Enter the elements:
0 5 0
0 6 0
0 0 3

The sparse of sum matrix is:
3 3 4
0 0 1
0 1 5
1 1 11
2 2 9




Aim: Write a C++ program to perform sparse multiplication



PROGRAM


#include”iostream.h”
#include”conio.h”
#include"process.h"

void main()
{
int num,sp1[50][50],mul[50][50],i,j,k1=0,k2=0,k=0,m1,n1,m2,n2,sp2[50][50],sp[50][50];
int m,n,t;
clrscr();
cout<<"\n Enter the order of matrix1: ";
cin>>m1>>n1;
cout<<"\n Enter the elements: ";
for(i=0;i< m1;i++)
{
for(j=0;j< n1;j++)
{
cin>>num;
if(num!=0)
{
k1++;
sp1[k1][0]=i;
sp1[k1][1]=j;
sp1[k1][2]=num;
}
}
}
sp1[0][0]=m1;
sp1[0][1]=n1;
sp1[0][2]=k1;

cout<<"\n Enter the order of matrix2: ";
cin>>m2>>n2;
cout<<"\n Enter the elements: ";
for(i=0;i< m2;i++)
{
for(j=0;j< n2;j++)
{
cin>>num;
if(num!=0)
{
k2++;
sp2[k2][0]=i;
sp2[k2][1]=j;
sp2[k2][2]=num;
}
}
}
sp2[0][0]=m2;
sp2[0][1]=n2;
sp2[0][2]=k2;

if(n1!=m2)
{
cout<<"\n Error ";
getch();
exit(0);
}

k=0;
for(i=1;i<=k1;i++)
{
for(j=1;j<=k2;j++)
{
if(sp1[i][1]==sp2[j][0])
{
k++;
sp[k][0]=sp1[i][0];
sp[k][1]=sp2[j][1];
sp[k][2]=sp1[i][2]*sp2[j][2];
}
}
}

sp[0][2]=k;
sp[0][0]=m1;
sp[0][1]=n2;

t=0;
for(i=0;i< sp[0][0];i++)
{
for(j=0;j< sp[0][1];j++)
{
for(k1=1;k1<=sp[0][2];k1++)
{
if(sp[k1][0]==i&&sp[k1][1]==j)
{
t++;
mul[t][0]=sp[k1][0];
mul[t][1]=sp[k1][1];
mul[t][2]=sp[k1][2];
}
}
}
}

mul[0][2]=k;
mul[0][0]=m1;
mul[0][1]=n2;

cout<<"\n The sparse of product matrix is:\n";
for(i=0;i<=t;i++)
{
for(j=0;j<3;j++)
cout<< mul[i][j]<<" ";
cout<<"\n";
}

getch();
}



OUTPUT

Enter the order of matrix1: 3 3

Enter the elements:
2 0 9
0 1 0
0 0 2

Enter the order of matrix2: 3 3

Enter the elements:
0 0 3
1 0 1
2 1 0

The sparse of product matrix is:
3 3 7
0 0 18
0 1 9
0 2 6
1 0 1
1 2 1
2 0 4
2 1 2


Aim: Write a C++ program to convert an infix expression to postfix and evaluate



PROGRAM

#include"iostream.h"
#include"conio.h"
#include"stdio.h"
#include"ctype.h"
#include"string.h"
#include"process.h"
#include"math.h"

int top=-1;
int stack[50];
int p;
void evaluate(char [],int);

void display(char a[],int n)
{
int i;
for(i=0;i<=n;i++)
{
cout<< a[i];
}
cout<<"\n";
}

void inpst(char q[])
{
char stk[50],tp[50],op,ot;
int s=-1,p=-1,j,k,l,h,flg1,flg2;
char pcdn[6]={'-','+','/','*','^','\0'};
int pval[5]={1,1,2,2,3};

for(int i=0;q[i]!='\0';i++)
{
flg1=flg2=-1;
if(isalpha(q[i]))
{
p++;
tp[p]=q[i];

}

else if(q[i]=='(')
{
s++;
stk[s]=q[i];

}

else if(q[i]==')')
{
for(j=s;j>=0;j--)
{
if(stk[s]=='(')
break;

if(stk[j]!='(')
{
p++;
tp[p]=stk[j];

s--;

}
}

s--;


}

else
{
op=q[i];
flg1=-1;
/* to find precedence value */
for(h=0;pcdn[h]!='\0';h++)
{
if(op==pcdn[h])
{
flg1=pval[h];
break;
}
}

/* check for higher precedence operator */

for(k=s;k>=0;k--)
{
flg2=-1;
ot=stk[k];

for(h=0;pcdn[h]!='\0';h++)
{
if(ot==pcdn[h])
{
flg2=pval[h];
break;
}
}

if(flg2>flg1)
{
p++;
tp[p]=ot;
for(l=k;l {
stk[k]=stk[k+1];
}
s--;

}


}
s++;
stk[s]=op;
}
}

while(s>=0)
{
p++;
tp[p]=stk[s];
s--;
}

cout<<"\n The post fix expression is: ";
display(tp,p);

evaluate(tp,p);
}

int result(int a,int b,char op)
{
int c=0;

switch(op)
{
case '+': c=a+b; break;
case '-': c=a-b; break;
case '*': c=a*b; break;
case '/': if(b!=0)
c=a/b;
else
{
cout<<"\n Error: ";
getch();
exit(0);
}
break;
case '^':c=(int)pow(a,b); break;
case '%':c=a%b; break;
}
return c;
}

void push(int item)
{
stack[++top]=item;
}

int pop()
{
int s;
if(top>=0)
{
s=stack[top];
top--;
return s;
}
else
return 0;
}

void evaluate(char post[],int p)
{
int a,b,v=0,q;
top=-1;

for(int i=0;i<=p;i++)
{
if(isalpha(post[i]))
{
cout<<"\n Enter value for "<< post[i]<<": ";
cin>>q;
push(q);
continue;
}
else
{
a=pop();
b=pop();
v=result(a,b,post[i]);
push(v);
}
a=b=0;
}

}

void main()
{
char q[50];
clrscr();
cout<<"\n Enter the infix expression: ";
gets(q);
inpst(q);
cout<<"\n Value of expression "<< stack[top];
getch();
}



OUTPUT


Enter the infix expression: a*b+c*d

The post fix expression is: ab*cd*+

Enter value for a: 2

Enter value for b: 3

Enter value for c: 4

Enter value for d: 5

Value of expression 26


Aim: Write a C++ program to implement stack operations



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

int stk[50],top=-1,i,j,max,num,ch;

void push()
{
top++;
cout<<"\n Enter the item: ";
cin>>num;
stk[top]=num;
}

void pop()
{
cout<<"\n Popped "<< stk[top];
top--;
}

void display()
{
cout<<"\n ---------------------\n";
cout<<"TOP>> ";
for(i=top;i>=0;i--)
cout<< stk[i]<<" ";
cout<<"\n ---------------------";
}
void main()
{

clrscr();
cout<<"\n Enter the limit: ";
cin>>max;
do
{
cout<<"\n\n STACK\n";
cout<<"\n1.Push\n2.Pop\n3.Display\n4.Exit";
cout<<"\n Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:if(top>=max-1)
{
cout<<"\n Stack full: ";
continue;
}
push();
break;
case 2:if(top==-1)
{
cout<<"\n Stack empty: ";
continue;
}
pop();
break;
case 3:display();
break;
case 4:cout<<".......Thanking you.......";
getch();
exit(0);
default:cout<<"\n INVALID KEY-IN ";
}
}while(1);

}


OUTPUT


Enter the limit: 3


STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 1

Enter the item: 23


STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 1

Enter the item: 96


STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 3

---------------------
TOP>> 96 56 23
---------------------

STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 2

Popped 96

STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 3

---------------------
TOP>> 56 23
---------------------

STACK

1.Push
2.Pop
3.Display
4.Exit
Enter the choice: 4
.......Thanking you.......


Aim: Write a C++ program to reverse a string using stack



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"
#include"string.h"

void revstk(char str[])
{
char stk[50];
int top,i;
for(i=0;str[i]!='\0';i++)
stk[i]=str[i];

top=--i;
cout<<"\n Reversed string: ";
for(i=top;i>=0;i--)
cout<< stk[i];

return;
}

void main()
{
char str[50];
clrscr();
cout<<"\n Enter a string:";
gets(str);
revstk(str);
getch();
}


OUTPUT

Enter a string:english

Reversed string: hsilgne


Aim: Write a C++ program to implement simple queue operations



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

int que[50],rear=-1,front=-1,i,j,max,num,ch;

void insert()
{
rear++;
cout<<"\n Enter the item: ";
cin>>num;
que[rear]=num;
if(front==-1)
front=0;
}

void delet()
{
cout<<"\n Deleted "<< que[front];
if(front==rear)
front=rear=-1;
else
front++;
}

void display()
{
if(front==-1)
{
cout<<"\n\t\t\t QUEUE EMPTY ";
return;
}
cout<<"\n -----------------------------\n";
cout<<"FRONT>> ";
for(i=front;i<=rear;i++)
cout<< que[i]<<" ";
cout<<" << REAR";
cout<<"\n -----------------------------";
}
void main()
{

clrscr();
cout<<"\n Enter the limit: ";
cin>>max;
do
{
cout<<"\n\n SIMPLE QUEUE\n";
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\n Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:if(rear+1>max-1)
{
cout<<"\n\t\t\t Queue full: ";
continue;
}
insert();
break;
case 2:if(front==-1||front>rear)
{
cout<<"\n\t\t\t Queue empty: ";
continue;
}
delet();
break;
case 3:display();
break;
case 4:cout<<".......Thanking you.......";
getch();
exit(0);
default:cout<<"\n\t\t\t INVALID KEY-IN ";
}
}while(1);

}

OUTPUT


Enter the limit: 2


SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 23


SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 96


SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 3

-----------------------------
FRONT>> 23 96 << REAR
-----------------------------


SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 2

Deleted 23

SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 3

-----------------------------
FRONT>> 96 << REAR
-----------------------------

SIMPLE QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 4
.......Thanking you.......


Aim: Write a C++ program to implement circular queue operations



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

int que[50],rear=-1,front=-1,i,j,max,num,ch;

void insert()
{
rear=(rear+1)%max;
cout<<"\n Enter the item: ";
cin>>num;
que[rear]=num;
if(front==-1)
front=0;
}

void delet()
{
cout<<"\n Deleted "<< que[front];
if(front==rear)
front=rear=-1;
else
front=(front+1)%max;
}

void display()
{
if(front==-1)
{
cout<<"\n QUEUE EMPTY ";
return;
}
cout<<"\n -----------------------------\n";
cout<<"FRONT>> ";
if(rear< front)
{
for(i=front;i< max;i++)
cout<< que[i]<<" ";
for(i=0;i<=rear;i++)
cout<< que[i]<<" ";
}
else
for(i=front;i<=rear;i++)
cout<< que[i]<<" ";
cout<<" << REAR";
cout<<"\n -----------------------------";
}
void main()
{
clrscr();
cout<<"\n Enter the limit: ";
cin>>max;
do
{
cout<<"\n\n CIRCULAR QUEUE\n";
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\n Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:if((front==(rear+1)%max))
{
cout<<"\n Queue full: ";
continue;
}
insert();
break;
case 2:if(front==-1)
{
cout<<"\n Queue empty: ";
continue;
}
delet();
break;
case 3:display();
break;
case 4:cout<<".......Thanking you.......";
getch();
exit(0);
default:cout<<"\n INVALID KEY-IN ";
}
}while(1);
}



OUTPUT


Enter the limit: 2


CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 44


CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 56


CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 3

-----------------------------
FRONT>> 44 56 << REAR
-----------------------------







CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 2

Deleted 44

CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 99


CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 3

-----------------------------
FRONT>> 56 99 << REAR
-----------------------------

CIRCULAR QUEUE


1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 4
.......Thanking you.......


Aim: Write a C++ program to implement double ended queue operations



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

int que[50],rear=-1,front=-1,i,j,max,num,ch;

void r_insert()
{
rear++;
cout<<"\n Enter the item: ";
cin>>num;
que[rear]=num;
if(front==-1)
front=0;
}

void f_insert()
{
if(front==-1)
front=rear=0;
else if(front>0)
front--;

cout<<"\n Enter the item: ";
cin>>num;
que[front]=num;

}

void r_delet()
{
cout<<"\n Deleted "<< que[rear];
if(front==rear)
front=rear=-1;
else
rear--;
}

void f_delet()
{
cout<<"\n Deleted "<< que[front];
if(front==rear)
front=rear=-1;
else
front++;
}

void display()
{
if(front==-1)
{
cout<<"\n\t\t\t QUEUE EMPTY ";
return;
}
cout<<"\n -----------------------------\n";
cout<<"FRONT>> ";
for(i=front;i<=rear;i++)
cout<< que[i]<<" ";
cout<<" << REAR";
cout<<"\n -----------------------------";
}
void main()
{

clrscr();
cout<<"\n Enter the limit: ";
cin>>max;
do
{
cout<<"\n\n DE-QUEUE\n";
cout<<"\n1.Insert at rear\n2.Insert at front\n3.Delete at front\n4.Delete at rear\n5.Display\n6.Exit";
cout<<"\n Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:if(rear+1>max-1)
{
cout<<"\n\t\t\t Queue full: ";
continue;
}
r_insert();
break;
case 2:if(front==0)
{
cout<<"\n\t\t\t Cannot insert: ";
continue;
}
f_insert();
break;
case 3:if(front==-1||front>rear)
{
cout<<"\n\t\t\t Queue empty: ";
continue;
}
f_delet();
break;
case 4:if(front==-1||front>rear)
{
cout<<"\n\t\t\t Queue empty: ";
continue;
}
r_delet();
break;
case 5:display();
break;
case 6:cout<<".......Thanking you.......";
getch();
exit(0);
default:cout<<"\n\t\t\t INVALID KEY-IN ";
}
}while(1);

}

OUTPUT



Enter the limit: 3

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit

Enter the choice: 2

Enter the item: 23

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 1

Enter the item: 44

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 1

Enter the item: 96

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 5

-----------------------------
FRONT>> 23 44 96 << REAR
-----------------------------

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 3

Deleted 23

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 4

Deleted 96

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 5

-----------------------------
FRONT>> 44 << REAR
-----------------------------

DE-QUEUE

1.Insert at rear
2.Insert at front
3.Delete at front
4.Delete at rear
5.Display
6.Exit
Enter the choice: 6
.......Thanking you.......


Aim: Write a C++ program to implement priority queue operations



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

int que[50],rear=-1,front=-1,i,j,max,item,ch;
struct p_que
{
int num;
int pr;
};
class pro
{
public:
void insert(p_que ob[]);
void delet(p_que ob[]);
void display(p_que ob[]);
};


void pro::insert(p_que ob[])
{
rear++;
cout<<"\n Enter the item: ";
cin>>item;
ob[rear].num=item;
cout<<"\n Enter the priority: ";
cin>>ob[rear].pr;
if(front==-1)
front=0;
}

void pro::delet(p_que ob[])
{
p_que temp;
int big=ob[front].pr;
int p=0;
for(int i=front+1;i<=rear;i++)
{
if(ob[i].pr>big)
{
big=ob[i].pr;
p=i;
}
}

for(i=p;i<=rear;i++)
{
ob[i].num=ob[i+1].num;
ob[i].pr=ob[i+1].pr;
}

if(front==rear)
front=rear=-1;
else
rear--;
}

void pro::display(p_que ob[])
{
if(front==-1)
{
cout<<"\n\t\t\t QUEUE EMPTY ";
return;
}
cout<<"\n -----------------------------\n\n";
cout<<"FRONT>> Pr ";
for(i=front;i<=rear;i++)
printf("%3d",ob[i].pr);
cout<<" << REAR";
cout<<"\n\n Data";
for(i=front;i<=rear;i++)
printf("%3d",ob[i].num);

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

void main()
{
p_que o[50];
pro pp;
clrscr();
cout<<"\n Enter the limit: ";
cin>>max;
do
{
cout<<"\n\n PRIORITY QUEUE\n";
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\n Enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:if(rear+1>max-1)
{
cout<<"\n\t\t\t Queue full: ";
continue;
}
pp.insert(o);
break;
case 2:if(front==-1||front>rear)
{
cout<<"\n\t\t\t Queue empty: ";
continue;
}
pp.delet(o);
break;
case 3:pp.display(o);
break;
case 4:cout<<".......Thanking you.......";
getch();
exit(0);
default:cout<<"\n\t\t\t INVALID KEY-IN ";
}
}while(1);

}


OUTPUT


Enter the limit: 3


PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 23

Enter the priority: 5


PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 44

Enter the priority: 6


PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 1

Enter the item: 96

Enter the priority: 7


PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit

Enter the choice: 3


-----------------------------

FRONT>> Pr 5 6 7 << REAR

Data 23 44 96
-----------------------------

PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 2


PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 3

-----------------------------

FRONT>> Pr 5 6 << REAR

Data 23 44
-----------------------------

PRIORITY QUEUE

1.Insert
2.Delete
3.Display
4.Exit
Enter the choice: 4
.......Thanking you.......


Aim: Write a C++ program to represent a polynomial



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class poly
{
int i,n;
struct rep
{
int co;
int ex;
}r[10];

public:
void read();
void disp();
};

void poly::read()
{
cout<<"\n Enter the number of terms in the polynomial: ";
cin>>n;
cout<<"\n Enter the coefficient and exponent: ";

for(i=0;i< n;i++)
{
cout<<"\n Term "<< i+1<<": ";
cin>>r[i].co>>r[i].ex;
}
}

void poly::disp()
{
cout<<"\nThe polynomial is:\n\n";
for(i=0;i< n-1;i++)
{
cout<< r[i].co<<"x^"<< r[i].ex<<"+";
}
cout<< r[i].co<<"x^"<< r[i].ex;
}

void main()
{
clrscr();
poly ob;
ob.read();
ob.disp();
getch();
}



OUTPUT

Enter the number of terms in the polynomial: 3

Enter the coefficient and exponent:
Term 1: 5 2

Term 2: 6 1

Term 3: 7 0

The polynomial is:

5x^2+6x^1+7x^0


Aim: Write a C++ program to perform polynomial addition



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class poly
{
int i,n;
struct rep
{
int co;
int ex;
}r[10];

public:
void read();
void sum(poly,poly);
void disp();
};

void poly::read()
{
cout<<"\n Enter the number of terms in the polynomial: ";
cin>>n;
cout<<"\n Enter the coefficient and exponent: ";

for(i=0;i< n;i++)
{
cout<<"\n Term "<< i+1<<": ";
cin>>r[i].co>>r[i].ex;
}
}

void poly::disp()
{
cout<<"\n.........................\n";
for(i=0;i< n-1;i++)
{
cout<< r[i].co<<"x^"<< r[i].ex<<"+";
}
cout<< r[i].co<<"x^"<< r[i].ex;
cout<<"\n\n";
}

void poly::sum(poly c1,poly c2)
{
int i=0,j=0,k=0,t;

while(i< c1.n&&j< c2.n)
{//1
if(c1.r[i].ex==c2.r[j].ex)
{
r[k].co=c1.r[i].co+c2.r[j].co;
r[k].ex=c1.r[i].ex;
i++,j++,k++;
}

else if(c1.r[i].ex>c2.r[j].ex)
{
r[k].co=c1.r[i].co;
r[k].ex=c1.r[i].ex;
i++,k++;
}

else if(c1.r[i].ex< c2.r[j].ex)
{
r[k].co=c2.r[j].co;
r[k].ex=c2.r[j].ex;
j++,k++;
}
}//1

while(i< c1.n)
{
r[k].co=c1.r[i].co;
r[k].ex=c1.r[i].ex;
i++,k++;
}

while(j< c2.n)
{
r[k].co=c2.r[j].co;
r[k].ex=c2.r[j].ex;
j++,k++;
}
n=k--;


}

void main()
{
clrscr();
poly c1,c2,c3;
c1.read();
c2.read();
cout<<"\n Polynomial 1: ";
c1.disp();
cout<<"\n Polynomial 2: ";
c2.disp();
c3.sum(c1,c2);
cout<<"\n Sum polynomial : ";
c3.disp();
getch();
}



OUTPUT



Enter the number of terms in the polynomial: 3

Enter the coefficient and exponent:
Term 1: 4 2

Term 2: 5 1

Term 3: 3 0

Enter the number of terms in the polynomial: 2

Enter the coefficient and exponent:
Term 1: 8 3

Term 2: 5 1


Polynomial 1:
.........................
4x^2+5x^1+3x^0


Polynomial 2:
.........................
8x^3+5x^1


Sum polynomial :
.........................
8x^3+4x^2+10x^1+3x^0



Aim: Write a C++ program to implement a singly linked list



PROGRAM

#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class node
{
int data;
node *next;
public:
void display();
void insertend();
void insertbeg();
void insertsp();
void delbeg();
void delend();
void delsp();
};
node *head,*temp,*start,*t;

void node::display()
{
temp=start;
cout<<"\n Data list: ";
while(temp!=NULL)
{
cout<< temp->data<<" ";
temp=temp->next;
}
}

void node::insertbeg()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;

if(start==NULL)
{
head->next=NULL;
start=head;
}
else
{
head->next=start;
start=head;
}
}

void node::insertend()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;

if(start==NULL)
{
start=head;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
head->next=NULL;
}
}

void node::insertsp()
{
int pos,i;
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
cout<<"\n Enter the position: ";
cin>>pos;
if(pos==1)
insertbeg();
else
{
temp=start;
for(i=1;i< pos-1;i++)
{
temp=temp->next;
}
head->next=temp->next;
temp->next=head;

}
}

void node::delbeg()
{
if(start==NULL)
cout<<"\n LIST EMPTY ";
else
{
//temp=start;
cout<< start->data;
start=start->next;
delete temp;
}
}

void node::delend()
{
temp=start;
if(start==NULL)
cout<<"\n LIST EMPTY ";

else if(temp->next==NULL)
{
cout<< temp->data;
start=NULL;
delete temp;
}

else
{
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
cout<< temp->data;
t->next=NULL;
delete temp;
}
}

void node::delsp()
{

if(start==NULL)
{
cout<<"\n LIST EMPTY ";
return;
}

int num;
cout<<"\n Enter the item: ";
cin>>num;
temp=start;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==start)
delbeg();
else
{
cout<< temp->data;
t->next=temp->next;
delete temp;
}
}
else
{
t=temp;
temp=temp->next;
}

}
}

void main()
{
int ch;
node ob;
start=NULL;
clrscr();
do
{
//clrscr();
cout<<"\n\n\t...SINGLY LINKED LIST...\n";
cout<<"\n1.INSERT AT BEGINNING\n2.INSERT AT END\n3.INSERT AT A SPECIFIED POSITION";
cout<<"\n4.DELETION FROM BEGINNING\n5.DELETION FROM END\n6.DELETION FROM A SPECIFIED POSITION";
cout<<"\n7.DISPLAY\n8.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insertbeg();break;
case 2:ob.insertend();break;
case 3:ob.insertsp();break;
case 4:ob.delbeg();break;
case 5:ob.delend();break;
case 6:ob.delsp();break;
case 7:ob.display();break;
case 8:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}



OUTPUT


...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 23


...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 44


...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 2

Enter the data: 56


...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 3

Enter the data: 99

Enter the position: 2


...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 44 99 23 56



...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 4
44




...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 5
56

...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 6

Enter the item: 23
23

...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 99

...SINGLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 8

....THANKS....



Aim: Write a C++ program to implement a circular linked list



PROGRAM



#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"
#define MAX 25
class node
{
int data;
node *next;
public:
void display();
void insertend();
void insertbeg();
void insertsp();
void delbeg();
void delend();
void delsp();
};
node *head,*temp,*start,*t,*last;

void node::display()
{
temp=start;
cout<<"\n Data list: ";
while(temp!=last)
{
cout<< temp->data<<" ";
temp=temp->next;
}
cout<< last->data;
}

void node::insertbeg()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;

if(start==NULL)
{
start=last=head;
last->next=NULL;
}
else
{
head->next=start;
last->next=head;
start=head;
}
}

void node::insertend()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;

if(start==NULL)
{
start=last=head;
last->next=head;
}
else
{
last->next=head;
head->next=start;
last=head;
}
}

void node::insertsp()
{
int pos,i;
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
cout<<"\n Enter the position: ";
cin>>pos;
if(pos==1)
{
insertbeg();
return;
}
else
{
temp=start;
for(i=1;i< pos-1;i++)
{
temp=temp->next;
}
head->next=temp->next;
temp->next=head;

}
}

void node::delbeg()
{

if(start==NULL)
cout<<"\n LIST EMPTY ";

else if(start->next==start)
{
temp=start;
cout<< temp->data;
start=last=NULL;
delete temp;
}
else
{
temp=start;
cout<< start->data;
start=start->next;
last->next=start;
delete temp;
}
}

void node::delend()
{
temp=start;
if(start==NULL)
cout<<"\n LIST EMPTY ";
else if(start->next==start)
{
temp=start;
cout<< temp->data;
start=last=NULL;
delete temp;
}
else
{
temp=start;
if(temp->next!=last)
{
temp=temp->next;
}
t=last;
cout<< t->data;
temp->next=start;
last=temp;
delete t;
}
}

void node::delsp()
{

if(start==NULL)
{
cout<<"\n LIST EMPTY ";
return;
}

int item;
cout<<"\n Enter the item: ";
cin>>item;
int i=0;
for(temp=start;i< MAX;i++,t=temp,temp=temp->next)
{
if(temp->data==item)
{
if(temp==start)
{
delbeg();
return;
}
else if(temp==last)
{
delend();
return;
}
else
{
t->next=temp->next;
delete temp;
return;
}
}


}
}

void main()
{
int ch;
node ob;
start=last=NULL;
clrscr();
do
{
cout<<"\n\n\t...CIRCULAR LINKED LIST...\n";
cout<<"\n1.INSERT AT BEGINNING\n2.INSERT AT END\n3.INSERT AT A SPECIFIED POSITION";
cout<<"\n4.DELETION FROM BEGINNING\n5.DELETION FROM END\n6.DELETION FROM A SPECIFIED POSITION";
cout<<"\n7.DISPLAY\n8.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insertbeg();break;
case 2:ob.insertend();break;
case 3:ob.insertsp();break;
case 4:ob.delbeg();break;
case 5:ob.delend();break;
case 6:ob.delsp();break;
case 7:ob.display();break;
case 8:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}


OUTPUT


...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 25


...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 47


...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 2

Enter the data: 58


...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 3

Enter the data: 101

Enter the position: 2


...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 47 101 25 58



...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 4
47

...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 5
58

...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 6

Enter the item: 25
25

...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 101

...CIRCULAR LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 8

....THANKS....


Aim: Write a C++ program to implement a doubly linked list



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class node
{
int data;
node *next;
node *prev;
public:
void display();
void insertend();
void insertbeg();
void insertsp();
void delbeg();
void delend();
void delsp();
};

node *head,*temp,*start,*last,*t,*t1,*t2;

void node::display()
{
temp=start;
if(start==NULL)
{
cout<<"\n LIST EMPTY ";
return;
}
cout<<"\n Data list: ";
while(temp!=last)
{
cout<< temp->data<<" ";
temp=temp->next;
}
cout<< last->data;
}

void node::insertbeg()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
head->prev=NULL;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start->prev=head;
start=head;
}
}

void node::insertend()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
head->next=NULL;
if(start==NULL)
{
start=last=head;
head->prev=NULL;
}
else
{
head->prev=last;
last->next=head;
last=head;
}
}

void node::insertsp()
{
int pos,i;
head=new node;
cout<<"\n Enter the position: ";
cin>>pos;
if(pos==1)
insertbeg();
else
{
cout<<"\n Enter the data: ";
cin>>head->data;
temp=start;
for(i=1;i< pos-1;i++)
{
temp=temp->next;
}
if(temp->next==NULL)
last=head;
head->next=temp->next;
temp->next=head;

}
}

void node::delbeg()
{
if(start==NULL)
cout<<"\n LIST EMPTY ";
else if(start==last)
{
cout<< start->data;
temp=start;
start=last=NULL;
delete temp;
}
else
{
temp=start;
cout<< start->data;
start=start->next;
start->prev=NULL;
delete temp;
}
}

void node::delend()
{
temp=start;
if(start==NULL)
cout<<"\n LIST EMPTY ";
else if(start==last)
{
cout<< last->data;
temp=last;
start=last=NULL;
delete temp;
}

else
{
temp=last;
cout<< last->data;
last=last->prev;
last->next=NULL;
delete temp;
}
}

void node::delsp()
{

if(start==NULL)
{
cout<<"\n LIST EMPTY ";
return;
}

int num;
cout<<"\n Enter the item: ";
cin>>num;
temp=start;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==start)
{
delbeg();
return;
}
else if(temp==last)
{
delend();
return;
}

else
{
cout<< temp->data;
t1=temp->prev;
t2=temp->next;
t1->next=t2;
t2->prev=t1;
delete temp;
}
}
else
{
temp=temp->next;
}
}
}

void main()
{
int ch;
node ob;
start=last=NULL;
clrscr();
do
{
cout<<"\n\t...DOUBLY LINKED LIST...\n";
cout<<"\n1.INSERT AT BEGINNING\n2.INSERT AT END\n3.INSERT AT A SPECIFIED
POSITION";
cout<<"\n4.DELETION FROM BEGINNING\n5.DELETION FROM END\n6.DELETION
FROM A SPECIFIED POSITION";
cout<<"\n7.DISPLAY\n8.EXIT\n";
cout<<"\n Enter your choice: ";
cin>>ch;

switch(ch)
{
case 1:ob.insertbeg();break;
case 2:ob.insertend();break;
case 3:ob.insertsp();break;
case 4:ob.delbeg();break;
case 5:ob.delend();break;
case 6:ob.delsp();break;
case 7:ob.display();break;
case 8:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}


OUTPUT

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 1

Enter the data: 45

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 1

Enter the data: 25

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 2

Enter the data: 96

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 3

Enter the position: 3

Enter the data: 56

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 7

Data list: 25 45 56 96

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 4
25

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 5
96

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 6

Enter the item: 56

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 7

Data list: 45

...DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 8

....THANKS....


Aim: Write a C++ program to implement a circular doubly linked list



PROGRAM



#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"


class node
{
int data;
node *next;
node *prev;
public:
node(){}
void create();
void display();
void insertend();
void insertbeg();
void insertsp();
void delbeg();
void delend();
void delsp();
};

node *temp,*t,*curr,*t1,*t2;
static node *head;

void node::create()
{
head=new node;
head->data=0;
head->next=head;
head->prev=head;
}

void node::display()
{
if(head->next==head)
cout<<"\n DATA LIST EMPTY ";
else
{
temp=head;
cout<<"\n Data list: ";
while(temp->next!=head)
{
temp=temp->next;
cout<< temp->data<<" ";
}
}

}

void node::insertbeg()
{
curr=new node;
cout<<"\n Enter the data: ";
cin>>curr->data;
if(head->next==head)
{
head->next=curr;
curr->prev=head;
head->prev=curr;
curr->next=head;
return;
}
temp=head->next;
curr->prev=head;
head->next=curr;
curr->next=temp;
temp->prev=curr;

}

void node::insertend()
{
curr=new node;
cout<<"\n Enter the data: ";
cin>>curr->data;
temp=head->prev;
curr->next=head;
head->prev=curr;
curr->prev=temp;
temp->next=curr;
}

void node::insertsp()
{
int pos,k;
cout<<"\n Enter the position: ";
cin>>pos;
temp=head;
if(pos==1)
insertbeg();
else
{
for(k=1;k< pos;k++)
{
temp=temp->next;
}
curr=new node;
cout<<"\n Enter the data: ";
cin>>curr->data;
t=temp->next;
temp->next=curr;
curr->prev=temp;
curr->next=t;
t->prev=curr;
}

}

void node::delbeg()
{
if(head->next==head)
cout<<"\n DELETION IMPOSSIBLE ";

else
{
temp=head->next;
cout<< temp->data;
t=head->next=temp->next;
t->prev=head;
delete temp;
}
}

void node::delend()
{
if(head->prev==head)
cout<<"\n DELETION IMPOSSIBLE ";

else
{
temp=head->prev;
cout<< temp->data;
t=head->prev=temp->prev;
t->next=head;
delete temp;
}
}

void node::delsp()
{
if(head->next==head)
{
cout<<"\n DELETION IMPOSSIBLE ";
return;
}
else
{
int item;
cout<<"\n Enter the item: ";
cin>>item;
temp=head;
do
{
if(temp->data==item)
{
if(temp->prev==head)
{
delbeg();
return;
}
else if(temp->next==head)
{
delend();
return;
}
else
{
t1=temp->prev;
t2=temp->next;
t1->next=t2;
t2->prev=t1;
delete temp;
return;
}
}
else
{
temp=temp->next;
}
}while(temp!=head);
}

}

void main()
{
int ch;
node ob;
clrscr();
ob.create();
do
{
cout<<"\n\n\t...CIRCULAR DOUBLY LINKED LIST...\n";
cout<<"\n1.INSERT AT BEGINNING\n2.INSERT AT END\n3.INSERT AT A SPECIFIED POSITION";
cout<<"\n4.DELETION FROM BEGINNING\n5.DELETION FROM END\n6.DELETION FROM A SPECIFIED POSITION";
cout<<"\n7.DISPLAY\n8.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insertbeg();break;
case 2:ob.insertend();break;
case 3:ob.insertsp();break;
case 4:ob.delbeg();break;
case 5:ob.delend();break;
case 6:ob.delsp();break;
case 7:ob.display();break;
case 8:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);

}




OUTPUT


...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 21


...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 1

Enter the data: 41


...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 2

Enter the data: 51


...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 3

Enter the position: 2

Enter the data: 91


...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 41 91 21 51



...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 4
41

...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 5
51

...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT

Enter your choice: 6

Enter the item: 21
21

...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 7

Data list: 91

...CIRCULAR DOUBLY LINKED LIST...

1.INSERT AT BEGINNING
2.INSERT AT END
3.INSERT AT A SPECIFIED POSITION
4.DELETION FROM BEGINNING
5.DELETION FROM END
6.DELETION FROM A SPECIFIED POSITION
7.DISPLAY
8.EXIT


Enter your choice: 8

....THANKS....


Aim: Write a C++ program to implement a stack using linked list



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class node
{
int data;
node *next;
public:
void display();
void push();
void pop();
};
node *head,*temp,*start,*t;

void node::display()
{
temp=start;
if(temp==NULL)
cout<<"\n STACK EMPTY ";
else
cout<<"\n STACK: ";
while(temp!=NULL)
{
cout<< temp->data<<" ";
temp=temp->next;
}
}


void node::push()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
head->next=NULL;

if(start==NULL)
{
start=head;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
}
}

void node::pop()
{
temp=start;
if(start==NULL)
cout<<"\n STACK EMPTY ";

else if(temp->next==NULL)
{
cout<< temp->data;
start=NULL;
delete temp;
}

else
{
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
cout<< temp->data;
t->next=NULL;
delete temp;
}
}

void main()
{
int ch;
node ob;
start=NULL;
clrscr();
do
{

cout<<"\n\n\t...STACK USING LINKED LIST...\n";
cout<<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.push();break;
case 2:ob.pop();break;
case 3:ob.display();break;
case 4:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}


OUTPUT

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 23

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 45

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 3

STACK: 23 45

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 2
45

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 3

STACK: 23

...STACK USING LINKED LIST...

1.PUSH
2.POP
3.DISPLAY
4.EXIT

Enter your choice: 4

....THANKS....


Aim: Write a C++ program to implement a simple queue using linked list



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

class node
{
int data;
node *next;
public:
void display();
void insert();
void del();
};

node *head,*temp,*start,*t;

void node::display()
{
temp=start;
if(temp==NULL)
cout<<"\n QUEUE EMPTY ";
else
cout<<"\n QUEUE: ";
while(temp!=NULL)
{
cout<< temp->data<<" ";
temp=temp->next;
}
}

void node::insert()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
head->next=NULL;

if(start==NULL)
{
start=head;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;
}
}

void node::del()
{
if(start==NULL)
cout<<"\n QUEUE EMPTY ";
else
{
temp=start;
cout<< start->data;
start=start->next;
delete temp;
}
}

void main()
{
int ch;
node ob;
start=NULL;
clrscr();
do
{
cout<<"\n\t...QUEUE USING LINKED LIST...\n";
cout<<"\n1.INSERTION\n2.DELETION\n3.DISPLAY\n4.EXIT\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insert();break;
case 2:ob.del();break;
case 3:ob.display();break;
case 4:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}



OUTPUT


...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 25

...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 50

...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 3

QUEUE: 25 50

...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 2
25

...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 3

QUEUE: 50

...QUEUE USING LINKED LIST...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 4

....THANKS....


Aim: Write a C++ program to implement a circular queue using linked list



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"


class node
{
int data;
node *next;
public:
node(){}
void display();
void insert();
void del();
};

node *head,*temp,*start,*t,*last;

void node::display()
{
temp=start;
cout<<"\n Data list: ";
while(temp!=last)
{
cout<< temp->data<<" ";
temp=temp->next;
}
cout<< last->data;
}

void node::insert()
{
head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;

if(start==NULL)
{
start=last=head;
last->next=head;
}
else
{
last->next=head;
head->next=start;
last=head;
}

}

void node::del()
{

if(start==NULL)
cout<<"\n LIST EMPTY ";

else if(start->next==start)
{
temp=start;
cout<< temp->data;
start=last=NULL;
delete temp;

}
else
{
temp=start;
cout<< start->data;
start=start->next;
last->next=start;
delete temp;
}
}

void main()
{
int ch;
node ob;
start=last=NULL;

clrscr();
do
{
cout<<"\n\n\t...CIRCULAR QUEUE USING LL...\n";
cout<<"\n1.INSERTION\n2.DELETION\n3.DISPLAY\n4.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insert();break;
case 2:ob.del();break;
case 3:ob.display();break;
case 4:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}


OUTPUT


...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 23

...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 1

Enter the data: 44


...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 3

Data list: 23 44

...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 2
23


...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 3

Data list: 44


...CIRCULAR QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT

Enter your choice: 4

....THANKS....


Aim: Write a C++ program to implement a priority queue using linked list



PROGRAM



#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"


class node
{
int data;
int prty;
node *next;
public:
void display();
void insert();
void del();
void delbeg();
};

node *head,*temp,*start,*t;

void node::display()
{

temp=start;
cout<<"\n Data list: ";

while(temp!=NULL)
{
printf("%3d ",temp->data);
temp=temp->next;
}

temp=start;
cout<<"\n Priority: ";

while(temp!=NULL)
{
printf("%3d ",temp->prty);
temp=temp->next;
}

}


void node::insert()
{

head=new node;
cout<<"\n Enter the data: ";
cin>>head->data;
cout<<"\n Enter the priority: ";
cin>>head->prty;
head->next=NULL;

if(start==NULL)
{
start=head;
}

else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head;

}

}

void node::delbeg()
{
if(start==NULL)
cout<<"\n LIST EMPTY ";
else
{
cout<< start->data;
start=start->next;
delete temp;
}
}


void node::del()
{

if(start==NULL)
{
cout<<"\n LIST EMPTY ";
return;
}

int lp;
temp=start;
lp=temp->prty;

while(temp!=NULL)
{
if(temp->prty>lp)
{
lp=temp->prty;
}
temp=temp->next;
}

temp=start;
while(temp!=NULL)
{
if(temp->prty==lp)
{
if(temp==start)
{
delbeg();
return;
}
else
{
cout<< temp->data;
t->next=temp->next;
delete temp;
return;
}
}
else
{
t=temp;
temp=temp->next;
}

}
}

void main()
{
int ch;
node ob;
start=NULL;
clrscr();
do
{
cout<<"\n\n\t...PRIORITY QUEUE USING LL...\n";
cout<<"\n1.INSERTION\n2.DELETION\n3.DISPLAY\n4.EXIT\n\n";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:ob.insert();break;
case 2:ob.del();break;
case 3:ob.display();break;
case 4:cout<<"\n\t ....THANKS....";
getch();
exit(0);
default:cout<<"\n\t INVALID KEY-IN ";
}
}while(1);
}



OUTPUT



...PRIORITY QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 1

Enter the data: 23

Enter the priority: 5


...PRIORITY QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 1

Enter the data: 96

Enter the priority: 6


...PRIORITY QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 3

Data list: 23 96
Priority: 5 6


...PRIORITY QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 2
96


...PRIORITY QUEUE USING LL...

1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 3

Data list: 23
Priority: 5

...PRIORITY QUEUE USING LL...


1.INSERTION
2.DELETION
3.DISPLAY
4.EXIT


Enter your choice: 4

....THANKS....



Aim: Write a C++ program to perform polynomial addition using linked list



PROGRAM



#include”iostream.h”
#include”conio.h”
#include”process.h”


struct node
{
int co,exp;
node *next;
};

class poly
{
node *start,*temp,*head;
public:
poly()
{
start=NULL;
}
void create();
void display();
void add(poly,poly);
};

void poly::create()
{
int n;
do
{
head=new node;
cout<<"\n Enter values for coefficient and exponent: ";
cin>>head->co>>head->exp;
head->next=NULL;
if(head->co==0)
goto PROCEED;
else if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}

PROCEED:
cout<<"\n Continue? ,then press \"1\":";
cin>>n;
}while(n==1);
}

void poly::display()
{
temp=start;
cout<<"\n\n........................................\n\n\n\t";
while(temp->next!=NULL)
{
cout<< temp->co<<"x^"<< temp->exp<<"+";
temp=temp->next;
}
cout<< temp->co<<"x^"<< temp->exp;
cout<<"\n\n........................................\n";
}

void poly::add(poly r1,poly r2)
{
r1.temp=r1.start;
r2.temp=r2.start;
while(r1.temp!=NULL&&r2.temp!=NULL)
{
if(r1.temp->exp>r2.temp->exp)
{
head=new node;
head->co=r1.temp->co;
head->exp=r1.temp->exp;
head->next=NULL;
if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}
r1.temp=r1.temp->next;
}

else if(r1.temp->expexp)
{
head=new node;
head->co=r2.temp->co;
head->exp=r2.temp->exp;
head->next=NULL;
if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}
r2.temp=r2.temp->next;
}

else
{
head=new node;
head->co=r1.temp->co+r2.temp->co;
head->exp=r1.temp->exp;
head->next=NULL;
if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}
r1.temp=r1.temp->next;
r2.temp=r2.temp->next;
}
}

while(r1.temp!=NULL)
{
head=new node;
head->co=r1.temp->co;
head->exp=r1.temp->exp;
head->next=NULL;
if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}
r1.temp=r1.temp->next;
}

while(r2.temp!=NULL)
{
head=new node;
head->co=r2.temp->co;
head->exp=r2.temp->exp;
head->next=NULL;
if(start==NULL)
start=temp=head;
else
{
temp->next=head;
temp=head;
}
r2.temp=r2.temp->next;
}

}

void main()
{
poly p1,p2,p3;
clrscr();
cout<<"\n First polynomial...";
p1.create();
cout<<"\n Second polynomial...";
p2.create();
p3.add(p1,p2);
cout<<"\n Result of addition: ";
p3.display();
getch();
}



OUTPUT

First polynomial...
Enter values for coefficient and exponent: 4 3

Continue? ,then press "1":1

Enter values for coefficient and exponent: 5 2

Continue? ,then press "1":1

Enter values for coefficient and exponent: 3 1

Continue? ,then press "1":1

Enter values for coefficient and exponent: 6 0

Continue? ,then press "1":0

Second polynomial...
Enter values for coefficient and exponent: 3 3

Continue? ,then press "1":1

Enter values for coefficient and exponent: 2 2

Continue? ,then press "1":1

Enter values for coefficient and exponent: 3 1

Continue? ,then press "1":1

Enter values for coefficient and exponent: 1 0

Continue? ,then press "1":0

Result of addition:

. . . . . . . . . . . . . . . . .


7x^3+7x^2+6x^1+7x^0

. . . . . . . . . . . . . . . . .



Aim: Write a C++ program to implement a binary tree



PROGRAM


#include
#include
#include

struct node
{
char data;
node *lchild,*rchild;
};

node *head;

class tree
{
node *root;
public:
tree()
{
root=NULL;
}
node * read();
node * makenode(char);
void createtree(node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
};

node * tree::read()
{
char item;
cout<<"\n Enter data: ";
cin>>item;
root=makenode(item);
createtree(root);
return root;
}

node * tree::makenode(char x)
{
head=new node;
head->data=x;
head->lchild=head->rchild=NULL;
return head;
}

void tree::createtree(node *root)
{
int ch;
char item;
if(root!=NULL)
{
cout<<"\n Create left child for "<< root->data<<" (if so press \"1\")";
cin>>ch;
if(ch==1)
{
cout<<"\n Enter data: ";
cin>>item;
root->lchild=makenode(item);
createtree(root->lchild);
}

cout<<"\n Create right child for "<< root->data<<" (if so press \"1\")";
cin>>ch;
if(ch==1)
{
cout<<"\n Enter data: ";
cin>>item;
root->rchild=makenode(item);
createtree(root->rchild);
}
}
}

void tree::inorder(node *root)
{
if(root!=NULL)
{
inorder(root->lchild);
cout<< root->data<<" ";
inorder(root->rchild);
}
}

void tree::preorder(node *root)
{
if(root!=NULL)
{
cout<< root->data<<" ";
preorder(root->lchild);
preorder(root->rchild);
}
}

void tree::postorder(node *root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
cout<< root->data<<" ";
}
}


void main()
{
tree ob;
node *root;
int ch;
clrscr();
do
{
cout<<"\n\n.... BINARY TREE ....\n\n";
cout<<"\n1.Creation\n2.Inorder Traversal\n3.Preorder Traversal";
cout<<"\n4.Postorder Traversal\n5.Exit";
cout<<"\n\t Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:root=ob.read();
break;
case 2:ob.inorder(root);
break;
case 3:ob.preorder(root);
break;
case 4:ob.postorder(root);
break;
case 5:cout<<"\n\n\t... Thanking You ...";
getch();
exit(0);
default:cout<<"\n Invalid key-in ";
}

}while(1);

}



OUTPUT



.... BINARY TREE ....


1.Creation
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Exit
Enter your choice: 1

Enter data: A

Create left child for A (if so press "1")1

Enter data: B

Create left child for B (if so press "1")1

Enter data: C

Create left child for C (if so press "1")0

Create right child for C (if so press "1")0

Create right child for B (if so press "1")1

Enter data: D

Create left child for D (if so press "1")0

Create right child for D (if so press "1")0

Create right child for A (if so press "1")1

Enter data: E

Create left child for E (if so press "1")0

Create right child for E (if so press "1")0



.... BINARY TREE ....


1.Creation
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Exit
Enter your choice: 2
C B D A E

.... BINARY TREE ....


1.Creation
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Exit
Enter your choice: 3
A B C D E

.... BINARY TREE ....


1.Creation
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Exit
Enter your choice: 4
C D B E A


.... BINARY TREE ....


1.Creation
2.Inorder Traversal
3.Preorder Traversal
4.Postorder Traversal
5.Exit
Enter your choice: 5


... Thanking You ...


Aim: Write a C++ program to implement Binary Search Tree



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"stdio.h"

struct node
{
int data;
node *left;
node *right;
};

node *root;

class bst
{
public:
bst()
{
root=NULL;
}
node *insert(node *,int);
void delet(node *,node*);
void search(node *,int);
node *find(node *,int);

};

node * bst::insert(node *root,int item)
{
if(root==NULL)
{
root=new node;
root->data=item;
root->left=root->right=NULL;
}
else if(itemdata)
root->left=insert(root->left,item);
else
root->right=insert(root->right,item);

return root;

}
void bst::search(node *root,int item)
{
if(root==NULL)
cout<<"\n Number doesnot exist ";
else if(root->data==item)
cout<<"\n Number is present ";
else if(itemdata)
search(root->left,item);
else
search(root->right,item);

}

node *bst::find(node *root,int item)
{
node *temp;
temp=root;
node *parent;
while(root!=NULL)
{
if(itemdata)
{
parent=root;
root=root->left;
}
else if(item>root->data)
{
parent=root;
root=root->right;
}
else
{
delet(root,parent);
break;
}
}
if(root==NULL)
{
cout<<"\n Item doesnot exist ";
}

return temp;

}

void bst::delet(node *root,node *parent)
{

if(root->left==NULL&&root->right==NULL)//terminal node
{
if(parent->left==root)
parent->left=NULL;
else
parent->right=NULL;

return;
}

else if(root->left!=NULL&&root->right!=NULL)//node with 2 childs
{
node *ptr,*temp;
parent=root;
temp=root->left;
ptr=root->right;
if(ptr->left==NULL)
{
root->data=ptr->data;
}
while(ptr->left!=NULL)
{
parent=ptr;
ptr=ptr->left;
root->data=ptr->data;
}
root->left=temp;
delete ptr;

return;

}

else//node with 1 child
{
if(parent->left==root)
{
if(root->left==NULL)
parent->left=root->right;
else
parent->left=root->left;
}
else if(parent->right==root)
{
if(root->left==NULL)
parent->right=root->right;
else
parent->right=root->left;
}
return;
}

}

void main()
{
clrscr();
bst ob;
int item,ch;
node *temp;
do
{
cout<<"\n\n ... BINARY SEARCH TREE ... ";
cout<<"\n\n1.Insertion\n2.Deletion\n3.Searching\n4.Exit";
cout<<"\n\t Enter your choice: ";
cin>>ch;

switch(ch)
{
case 1: cout<<"\n Enter an item: "; cin>>item;
root=ob.insert(root,item);
break;
case 2: cout<<"\n Enter the item: "; cin>>item;
root=ob.find(root,item);
break;
case 3: cout<<"\n Enter the item: "; cin>>item;
ob.search(root,item);
break;
case 4: cout<<"\n ... Thanking You ...";
getch();
exit(0);
default:cout<<"\n Invalid key-in ";

}

}while(1);

}


OUTPUT


... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 25



... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 10



... BINARY SEARCH TREE ...


1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 20

... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 5

... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 35


... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 1

Enter an item: 32

... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 3

Enter the item: 10

Number is present


... BINARY SEARCH TREE ...

1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 2

Enter the item: 10


... BINARY SEARCH TREE ...


1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 3

Enter the item: 10

Number doesnot exist


... BINARY SEARCH TREE ...


1.Insertion
2.Deletion
3.Searching
4.Exit

Enter your choice: 4

... Thanking You ...


Aim: Write a C++ program to create and evaluate an Expression Tree



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”
#include"ctype.h"
#include"stdio.h"


struct node
{
char data;
node *lchild;
node *rchild;
};

node *head;
char p[50];
node *stack[50];
int top=-1;
float c;
float v;
node *q;

class tree
{
node *root;
public:
tree(){ }
float evaluate(node *);
node *makenode(char);
void createtree();
void push(node *);
node *pop();
float result(float,float,char);
};

void tree::createtree()
{
int i=0;
while(p[i]!='\0')
{
root=makenode(p[i]);
if(!isalpha(p[i]))
{
root->rchild=pop();
root->lchild=pop();
}

push(root);
i++;
}

}

void tree::push(node *root)
{
top++;
stack[top]=root;
}

node *tree::pop()
{
q=stack[top];
top--;
return q;
}

node * tree::makenode(char x)
{
head=new node;
head->data=x;
head->lchild=head->rchild=NULL;
return head;
}

float tree::evaluate(node *root)
{
float a,b;
if(!isalpha(root->lchild->data))
a=evaluate(root->lchild);
else
{
cout<<"\n Enter the value for "<< root->lchild->data<<": ";
cin>>a;
}

if(!isalpha(root->rchild->data))
b=evaluate(root->rchild);
else
{
cout<<"\n Enter the value for "<< root->rchild->data<<": ";
cin>>b;
}


v=result(a,b,root->data);
return v;

}

float tree::result(float a,float b,char op)
{
float c=0;

switch(op)
{
case '+': c=a+b; break;
case '-': c=a-b; break;
case '*': c=a*b; break;
case '/': if(b!=0)
c=a/b;
else
{
cout<<"\n Error: ";
getch();
exit(0);
}
break;
}

return c;
}

void main()
{
clrscr();
float ans;
cout<<"\n Enter a postfix expression: ";
gets(p);
tree ob;
ob.createtree();
ans=ob.evaluate(stack[top]);
cout<<"\n Value of the expression is: "<< ans;
getch();

}



OUTPUT


Enter a postfix expression: ab/cd/*

Enter the value for a: 15

Enter the value for b: 5

Enter the value for c: 20

Enter the value for d: 4

Value of the expression is: 15


Aim: Write a C++ program to implement various Sorting techniques



PROGRAM



#include"iostream.h"
#include"conio.h"
#include"process.h"

int item;

void display(int a[],int n)
{
cout<<"\n Sorted elements are: \n";

for(int i=0;i< n;i++)
cout<< a[i]<<" ";
}

void bubblesort(int a[],int n)
{
int i,j,t;
for(i=0;i< n;i++)
{
for(j=0;j< n-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

}

void seletionsort(int a[],int n)
{
int i,j,t;
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}

}

void insertionsort(int a[],int n)
{
int k,j,t;
for(k=1;k< n;k++)
{
t=a[k];
j=k-1;
while(t< a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
}

}

void quicksort(int a[],int low,int high)
{
int l,h,key,t;
l=low;
h=high;
key=a[(low+high)/2];

do
{
while(key>a[low])
low++;
while(key< a[high])
high--;
if(low<=high)
{
t=a[low];
a[low++]=a[high];
a[high--]=t;
}

}while(low<=high);

if(l< high)
quicksort(a,l,high);

if(low< h)
quicksort(a,low,h);

}

void bucketsort(int a[],int n)
{
int i,j,pass,k,l,div=1,num=0,large=a[0];
int buck[10],q[15][15];

for(i=1;i< n;i++)
{
if(a[i]>large)
large=a[i];
}

while(large>0)
{
num++;
large=large/10;
}

for(pass=0;pass< num;pass++)
{
for(k=0;k<10;k++)
buck[k]=0;

for(i=0;i< n;i++)
{
l=(a[i]/div)%10;
q[l][buck[l]]=a[i];
buck[l]++;
}
i=0;

for(k=0;k<10;k++)
for(j=0;j< buck[k];j++)
{
a[i]=q[k][j];
i++;
}

div=div*10;
}

}

void merge(int a[],int low,int mid,int high)
{
int i,h,j,b[30],k;
i=low;
h=low;
j=mid+1;

while(h<=mid&&j<=high)
{
if(a[h]< a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}

if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}

for(k=low;k<=high;k++)
{
a[k]=b[k];
}

}

void mergesort(int a[],int low,int high)
{
int mid;
if(low< high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}

}
/* for tree sort */
struct node
{
int data;
node *left;
node *right;
};

node *root;

class bst
{
public:
bst()
{
root=NULL;
}
node *insert(node *,int);

};

node * bst::insert(node *root,int item)
{
if(root==NULL)
{
root=new node;
root->data=item;
root->left=root->right=NULL;
}
else if(itemdata)
root->left=insert(root->left,item);
else
root->right=insert(root->right,item);

return root;

}

void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
cout<< root->data<<" ";
inorder(root->right);
}
}

void treesort(int a[],int n)
{
node *root;
bst ob;
for(int i=0;i< n;i++)
{
root=ob.insert(root,a[i]);
}
cout<<"\n Sorted elements are: \n";
inorder(root);
}

/* end of tree sort */

void heapsort(int a[],int n)
{
int i,s,f,item,value;
for(i=0;i< n;i++)
{
item=a[i];
s=i;
f=(s-1)/2;
while(s>0&&a[f]< item)
{
a[s]=a[f];
s=f;
f=(s-1)/2;
}
a[s]=item;
}

for(i=n-1;i>0;i--)
{
value=a[i];
a[i]=a[0];
f=0;
if(i==1)
s=-1;
else
s=1;
if(i>2&&a[2]>a[1])
s=2;

while(s>=0&&value< a[s])
{
a[f]=a[s];
f=s;
s=2*f+1;
if(s+1<=i-1&&a[s] s=s+1;
if(s>i-1)
s=-1;
}

a[f]=value;
}

}


void main()
{

int a[50],num[50],n,i,flag=1,ch,low,high;
clrscr();
do
{
cout<<"\n..... SORTING ....\n\n";
cout<<"\n1.BUBBLE SORT\n2.SELECTION SORT\n3.INSERTION SORT\n4.QUICK SORT";
cout<<"\n5.RADIX SORT\n6.MERGE SORT\n7.TREE SORT\n8.HEAP SORT\n9.EXIT";
cout<<"\n\t Enter your choice: ";
cin>>ch;

if(ch>=1&&ch<=8&&flag==1)
{
cout<<"\n Enter the limit: ";
cin>>n;
cout<<"\n Enter the elements: ";
for(i=0;i< n;i++)
{
cin>>a[i];
}
flag=0;
}

for(i=0;i< n;i++)
num[i]=a[i];

switch(ch)
{
case 1:bubblesort(num,n);
break;
case 2:seletionsort(num,n);
break;
case 3:insertionsort(num,n);
break;
case 4:low=0;high=n-1;
quicksort(num,low,high);
break;
case 5:bucketsort(num,n);
break;
case 6:low=0;high=n-1;
mergesort(num,low,high);
break;
case 7:flag=0;
treesort(num,n);
break;
case 8:heapsort(num,n);
break;
case 9:cout<<"\n\t .....Thanking You .....";
getch();
exit(0);
default:cout<<"\n\t Invalid key-in ";
}

if(ch>=1&&ch<=8&&ch!=7)
{
display(num,n);
}

}while(1);

}


OUTPUT


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 1

Enter the limit: 5

Enter the elements: 99 12 56 3 4

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 2

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 3

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 4

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 5

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 6

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 7

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 8

Sorted elements are:
3 4 12 56 99


..... SORTING ....


1.BUBBLE SORT
2.SELECTION SORT
3.INSERTION SORT
4.QUICK SORT
5.RADIX SORT
6.MERGE SORT
7.TREE SORT
8.HEAP SORT
9.EXIT
Enter your choice: 9

.....Thanking You .....


Aim: Write a C++ program to implement various Searching techniques



PROGRAM


#include”iostream.h”
#include”conio.h”
#include”process.h”

void sequential(int a[],int n,int item)
{
int flag=0,i;
for(i=0;i< n;i++)
{
if(a[i]==item)
{
cout<<"\n Item is found at position "<< i+1;
flag=1;
break;
}
}
if(flag==0) cout<<"\n Item not found ";

}

void binary(int a[],int n,int item)
{
int loc=-1,b=0,e=n-1,mid=-1;
while((b<=e)&&(a[mid]!=item))
{
mid=(b+e)/2;
if(item==a[mid])
{
cout<<"\n Item is found at position "<< mid+1;
loc=mid;
}
else if(item< a[mid])
e=mid-1;
else
b=mid+1;
}
if(loc==-1) cout<<"\n Item not found ";

}


void main()
{
int num[50],n,item,ch,flag=1,i;
clrscr();
do
{
cout<<"\n\n\n .... SEARCHING .... \n\n\n";
cout<<"\n1.Sequential Search\n2.Binary Search\n3.Enter another list\n4.Exit";
cout<<"\n\t Enter your choice: ";
cin>>ch;

if(ch==3) flag=1;

if(ch>=1&&ch<=3&&flag==1)
{
cout<<"\n Enter the limit: ";
cin>>n;
cout<<"\n Enter the elements: ";
for(i=0;i< n;i++)
cin>>num[i];
}
if(ch>=1&&ch<=2)
{
cout<<"\n Enter the element to be searched: ";
cin>>item;
}

switch(ch)
{
case 1:sequential(num,n,item);
break;
case 2:binary(num,n,item);
break;
case 3:break;
case 4:cout<<"\n\t.... Thanking You .... ";
getch();
exit(0);
default:cout<<"\n\t Invalid key-in";
}
flag=0;

}while(1);

}



OUTPUT


.... SEARCHING ....

1.Sequential Search
2.Binary Search
3.Enter another list
4.Exit
Enter your choice: 1

Enter the limit: 5

Enter the elements: 12 56 10 45 96

Enter the element to be searched: 10

Item is found at position 3


.... SEARCHING ....


1.Sequential Search
2.Binary Search
3.Enter another list
4.Exit
Enter your choice: 3

Enter the limit: 5

Enter the elements: 10 20 30 40 50


.... SEARCHING ....

1.Sequential Search
2.Binary Search
3.Enter another list
4.Exit

Enter your choice: 2

Enter the element to be searched: 40

Item is found at position 4


....SEARCHING ....

1.Sequential Search
2.Binary Search
3.Enter another list
4.Exit

Enter your choice: 4

.... Thanking You ....

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...