class xii c++ practical projects

45
XII - D KRMWS

Upload: nikunj-jain

Post on 28-Jan-2016

28 views

Category:

Documents


7 download

DESCRIPTION

This is a practical file for Class XII CBSE

TRANSCRIPT

Page 1: Class XII C++ Practical Projects

XII - D KRMWS

Page 2: Class XII C++ Practical Projects

INDEXS.No. Practical Sign

1.) SQL (1)2.) SQL (2)3.) SQL (3)4.) SQL (4)5.) SQL (5)6.) Ascending Order7.) Menu8.) Class Publication9.) Merging of Arrays

10.) Sum of Row and Column11.) Series 112.) Series 213.) Series 314.) Series 415.) Size of File16.) Searching of a Record in a File17.) Deletion of a Record in File18.) Selection Sort19.) Structure20.) Insertion in a Linked List21.) Deletion in a Linked List22.) Stack as Linked List23.) Static Stack24.) Queue as Linked List25.) Circular Queue

Page 3: Class XII C++ Practical Projects

QUES 1- Given the following tables for a DatabaseLIBRARY.

Table BOOKSBook_

IdBookna

meAuthor_Na

mePublishe

rsPric

eType

Qty

F001 The Tears

William Hopkins

First Publ.

750 Fic-Tion

10

F002 Thunder Bolts

Anna Roberts

First Publ.

700 Fic-Tion

5

T001 My First C++

Brian & Brooke

EPB 250 Text 10

T002 C++ Brainwor

k

A.W. Rossaine

TDH 325 Text 5

C001 Fast Cook

LataKapoor EPB 350 Cook-

ery

8

Table ISSUEDBook_Id Quantity Issued

F001 3T001 1C001 5

Write SQL queries for (a) to (f):

a) To show Book name, Author Name and Price of Books of EPB Publishers.Ans. SELECT Bookname from Books where Publisher = “EPB”;

b) To list the names of books of Fiction type.Ans. SELECT Bookname from Books where type = “Fiction”;

c) To display the Names and Price of the books in descending order of their Price.Ans. SELECT Bookname, Price from Books Order by Price DESC;

d) To increase the Price of all books of First Publ by 50.

Page 4: Class XII C++ Practical Projects

Ans. UPDATE Books set Price = Price + 50 where Publisher = “First Publ”;

e) To display the Bookid,Bookname and Quantity Issued for all books which have been issued.

Ans. SELECT Book_Id, Bookname, Quantity Issued from Books, Issued where

Books.Book_Id=Issued.Book_Id;

f) To insert a new row in the table Issued having the following data : “F002”,4.Ans. INSERT into Issued values(“F002”,4);

Give the output of the following queries based on the above tables:

1) SELECT COUNT(DISTINCT Publishers) FROM Books;

Ans.

2) SELECT SUM(PRICE) FROM Books where Price<500;

Ans.

3)

SELECT Book_Name, Author_Name FROM Bookswhere Price<500;

Ans.

4) SELECT COUNT(*) FROM Books;

Ans.

COUNT(DISTINCT Publishers)

3

SUM(Price)1350

Bookname Author_nameMy First C++ Brian & Brooke

C++ Brainworks A.W. RossaineFast Cook LataKapoor

COUNT5

Page 5: Class XII C++ Practical Projects

QUES 2 - Given the following tables for a Database SHOP.

Table INTERIORSS.No.

ITEMNAME

TYPE DATE OF

STOCK

PRICE

DISCOUNT

1 Red Rose Double Bed

23/02/02 32000 15

2 Soft Touch Baby Cot

20/11/02 9000 10

3 Jerry’s House

Baby Cot

19/02/02 8500 10

4 Rough Wood

Office Table

01/01/02 20000 20

5 Comfort Zone

Double Bed

12/01/02 15000 20

6 Jerry Look Baby Cot

24/02/02 7000 19

7 Lion King Office Table

20/02/02 16000 20

8 Royal Tiger

Sofa 22/02/02 30000 25

9 Park Sofa 13/12/01 9000 15

Page 6: Class XII C++ Practical Projects

Sitting10 Dine

ParadiseDinin

g Table

19/02/02 11000 15

Table NEWONES

Write SQL queries for (a) to (f):

a) To list the ITEMNAME which are priced at more than 1000 from INTERIORS table.Ans. SELECT ITEMNAME from INTERIORS where price>1000;

b) To list ITEMNAME and TYPE of those items in which DATE OF STOCK is before 22/01/02 from INTERIORS Table in descending order of ITEMNAME.Ans. SELECT ITEMNAME, TYPE from INTERIORS where DATE OF STOCK<{22/01/01} order by ITEMNAME desc;

c) To show all information about the Sofas from the INTERIORS table .Ans. SELECT * from INTERIORS where TYPE = ‘Sofa’;

d) To display ITEMNAME and DATE OF STOCK of those items in which the discount percentage is more than 15 from INTERIORS table.Ans. SELECT ITEMNAME, DATE OF STOCK from INTERIORSwhere DISCOUNT>15;

e) To count the number of items, whose type is “Double Bed”from INTERIORS table.

S.No.

ITEMNAME

TYPE DATE OF

STOCK

PRICE

DISCOUNT

11 White Wood

Double Bed

23/02/02

20000

20

12 James 007 Sofa 20/02/03

15000

15

13 Tom Look Baby Cot

21/02/03

7000 10

Page 7: Class XII C++ Practical Projects

Ans. SELECT COUNT(*) from INTERIORS where TYPE=“Double Bed” ;

f) To insert a new row in the NEWONES table with the following data :

14, “True India”, “Office Table”, 23/03/03, 15000, 20.Ans. INSERT into NEWONES VALUES (14, “True Indian”,“Office Table”, 28/08/03, 15000, 20) ; Give the output of the following queries based on the above tables:

1) SELECT COUNT (DISTINCT TYPE) from Interiors;

Ans.

2) SELECT AVG (DISCOUNT) from Interiors where TYPE =“Baby Cot”;

Ans.

3) SELECT SUM (PRICE) from Interiors where DATE OFSTOCK<{12/02/02};

Ans.

4) SELECT MAX (PRICE) from Interiors, Newones ;

Ans.

COUNT (DISTINCT TYPE)

5

AVG (DISCOUNT)

13

SUM (PRICE)44000

MAX (PRICE)32000

Page 8: Class XII C++ Practical Projects

QUES 3 - Given the following tables for a Database SPORTS.

Table SPORTSStudentNo.

Class

Name Game 1 Grade Game 2 Grade 2

10 7 Sammer

Cricket B Swimm-ing

A

11 8 Sujit Tennis A Skating C12 7 Kamal Swimmin

gB Football B

13 7 Venna Tennis C Tennis A14 9 Arehan

aBasketball A Cricket A

15 10 Arpit Cricket A Athletics C

Write SQL queries for (a) to (g):a) Display the names of the students who have grade C in either Game 1 or Game 2 or both.Ans. SELECT Name from Sports where Grade = ‘C’ or Grade 2= ‘C’ ;

b) Display the numbers of students getting A in Cricket.Ans. SELECT Count (*) from Sports where Game 1 =“Cricket” and Grade = ‘A’ or Game 2 = “Cricket” and Grade 2 = ‘A’ ;

c) Display the names of students who have same game for Game 1 and Game 2.Ans. SELECT Name from Sports where Game 1 = Game 2;

d) Display the games taken up by the students, whose name starts with ‘A’.Ans. SELECT Name, Game 1, Game 2 from Sports where Name = ‘A%’;

e) Assign a value 200 for Marks for all those who are getting grade ‘B’ or grade ‘A’ in both game 1 or game 2.Ans. UPDATE Sports set Marks = 200 where Game 1 in (‘A’,‘B’) and Game 2 in (‘A’, ‘B’);

f) Arrange the whole table in the alphabetical order of Name.Ans. SELECT * from Sports order by Name;

Page 9: Class XII C++ Practical Projects

g) Add new column named ‘Marks’.Ans. ALTER TABLE Sports add (Marks number);

QUES 4 - Given the following tables for a Database EMPLOYEE.

Table EMPLOYEEEid Name Deptid Qualificati

onSex

1 Deepali Gupta 101 M.C.A. F2 RajatTyagi 101 B.C.A. M3 Hari Mohan 102 B.A. M4 Harry 102 M.A. M5 Sumit Mittal 103 B.Tech. M6 Jyoti 101 M.Tech. F

Table SALARYEid Basic D.A. H.R.A. Bonus1 6000 2000 2300 2002 2000 300 300 303 1000 300 300 404 1500 390 490 305 8000 900 900 806 10000 300 490 89

Write SQL queries for (a) to (d) :a) To display the frequency of employees department wise.Ans. SELECT Count (*) from Employee group by Department;

b) To display the names of those employees whose names start with H.Ans. SELECT Name from Employee where Name = ‘H%’;

c) To add a new column in Salary table. The column name istotal_sal.Ans. ALTER TABLE Salary add (total_sal number (5));

d) To store the corresponding values in the total_sal column.Ans. UPDATE Salary set total_sal = Basic + D.A. + H.R.A.+Bonus;

Page 10: Class XII C++ Practical Projects

Give the output of the following queries based on the above tables:1) SELECT Name from Employee where Eid = (SELECT Eidfrom Salary where Basic = (SELECT MAX (Basic) from Salary));

Ans.

2) SELECT MAX (Basic) from Salary where Bonus>40;

Ans.

3) SELECT COUNT (*) from Employee GROUP BY Sex;

Ans.

4) SELECT DISTINCT Deptid from Employee;

Ans.

QUES 5 - Given the following tables for a Database HOSPITAL.

Table HOSPITALNo.

Name Age

Department

Date of Adm

Charges

Sex

1 Sandeep

65 Surgery 23/02/98 300 M

2 Ravina 24 Orthopedic 20/01/98 200 F3 Karan 45 Orthopedic 19/02/98 200 M4 Tarun 12 Surgery 01/01/98 300 M5 Zubin 36 E.N.T. 12/01/98 250 M6 Ketaki 16 E.N.T. 24/02/98 300 F7 Leena 29 Cardiology 20/02/98 800 F8 Zareen 45 Gynecology 22/02/98 300 F9 Kush 19 Gynecology 13/01/98 800 M

NameJyoti

MAX (Basic)10000

COUNT (*) Sex2 F4 M

DISTINCT Deptid101102103

Page 11: Class XII C++ Practical Projects

10 Shailya 31 Nuclear Medicine

19/02/98 400 F

Write SQL queries for (a) to (f) :

a) To show all information about the patients of Cardiology Department. Ans. SELECT * from Hospital where Department =“Cardiology”;

b) To list the names of Female patients who are inOrthopedic department.Ans. SELECT Name from Hospital where Sex = ‘F’ and Department = “Orthopedic”;

c) To list names of all patients with their dates of admission in ascending order.Ans. SELECT Name, Date of Adm from Hospital order by Date of Adm;

d) To display patient’s Name, Charges, Age for Male patients only.Ans. SELECT Name, Charges, Age from Hospital where Sex =‘M’;

e) To count the number of patients with Age greater than 30.Ans. SELECT COUNT (*) from Hospital where Age>30;

f) To insert a new row in the Hospital table with the following data :

11, “Nipan”, 26, ‘E.N.T.’, ‘25/02/98’, 50, ‘M’.Ans. INSERT into Hospital values (11, “Nipan”, 26, ‘E.N.T.’,‘25/02/98’, 50,‘M’);

Give the output of the following queries based on the above tables:1) SELECT COUNT (DISTINCT Department) from Hospital;

Ans.COUNT (DISTINCT Department)

6

Page 12: Class XII C++ Practical Projects

2) SELECT MAX (AGE) from Hospital where Sex = ‘M’;

Ans.

3) SELECT AVG (Charges) from Hospital where Sex = ‘F’;

Ans.

4) SELECT SUM (Charges) from hospital where Date of Adm< ‘2/08/98’;

Ans.

QUES . Write a Program to accept an Array and print it in Ascending Order.

Ans.

#include<iostream.h>void main(){ int i,j,t; int a[10]; for(i=0;i<10;i++) cin>>a[i]; for(j=0;j<10;j++) { for(i=0;i<10;i++) if(x[i]>x[i+1]) {t=x[i]; x[i]=x[i+1]; x[i+1]=t; } } for(i=0;i<10;i++) cout<<x[i];}

MAX (AGE)65

AVG (Charges)

400

SUM (Charges)3850

Page 13: Class XII C++ Practical Projects

QUES – Write a Menu Driven Program to implement Addition, Subtraction and Multiplication to Arrays.

Ans.

#include<iostream.h>#include<conio.h>#include<process.h>int i,j,k,x[3][3],y[3][3];void add(){int a[3][3]; for(i=0;i<3;i++) {for(j=0;j<3;j++) a[i][j]=x[i][j]+y[i][j]; } cout<<"The Added Matrices is \n"; for(i=0;i<3;i++) {for(j=0;j<3;j++) {cout<<a[i][j]<<" "; } cout<<endl; }}void subtract(){int b[3][3];

Page 14: Class XII C++ Practical Projects

for(i=0;i<3;i++) {for(j=0;j<3;j++) b[i][j]=x[i][j]-y[i][j]; } cout<<"The Subtracted Matrices is \n"; for(i=0;i<3;i++) {for(j=0;j<3;j++) {cout<<b[i][j]<<" "; } cout<<endl; }}void multiply(){int c[3][3]; for(i=0;i<3;i++) {for(j=0;j<3;j++) {for(k=0;k<3;k++) c[i][j]=x[i][k]*y[k][j]; } } cout<<"The Product of Matrices is \n"; for(i=0;i<3;i++) {for(j=0;j<3;j++) {cout<<c[i][j]<<" "; } cout<<endl; }}void menu(){clrscr(); cout<<"MENU\n"; cout<<"Enter the choice\n"; cout<<"1.Add Matrices\n2.Subtract Matrices\n3.Multiply Matrices\n4.Exit\n"; int n; cin>>n; switch(n) {case 1:add(); break; case 2:subtract(); break; case 3:multiply(); break; case 4:exit(0); default:cout<<"Wrong Choice"; break;

Page 15: Class XII C++ Practical Projects

}}void main(){clrscr(); cout<<"Enter Matrices\n"; cout<<"Enter the 1st Matrice\n"; for(i=0;i<3;i++) {for(j=0;j<3;j++) cin>>x[i][j]; } cout<<"Enter the 2nd Matrice\n"; for(i=0;i<3;i++) {for(j=0;j<3;j++) cin>>y[i][j]; } cout<<"\n"; menu(); getch();}

QUES : Write a Class with the following specification: PRIVATE MEMBERSTitle as Title of the BookPrice as Price of the BookPUBLIC MEMBERSFunction getdata() to accept DataFunction putdata() to display DataAns.

#include<iostream.h>#include<conio.h>#include<stdio.h>class publication{char title[25][10]; float price[10]; public: int n; void getdata(); void putdata();}a;void publication::putdata()

Page 16: Class XII C++ Practical Projects

{cout<<"\nData-->"; for(int j=0;j<n;j++) {cout<<"\nBook-"<<j+1; cout<<"\nTitle-"<<title[j]; cout<<"\nPrice-"<<price[j]; } }void publication::getdata() {cout<<"Enter the Data"; for(int i=0;i<n;i++) {cout<<"\nBook-"<<i+1; cout<<"\nTitle-"; gets(title[i]); cout<<"\nPrice-"; cin>>price[i]; } }void main(){clrscr(); cout<<"\nNo. of Books "; cin>>a.n; a.getdata(); a.putdata(); getch();}

QUES – Write a Program to Merge two Arrays?Ans.

#include<iostream.h>#include<conio.h>void merge(int a[50],int m,int b[50],int n,int c[50]){int x,y,z; for(x=0,y=n-1,z=0;x<m&&y>=0;) {if(a[x]<=b[y]) c[z++]=a[x++]; else c[z++]=b[y--]; } if(n>m) {while(n>m) c[z++]=a[x++]; } else {while(b>=0)

Page 17: Class XII C++ Practical Projects

c[z++]=b[y--]; }}void main(){int a[50],b[50],c[50],mn=0,m,n,i; cout<<"Enter the size of 1st array - \n"; cin>>m; cout<<"Enter 1st array element in ascending order - \n"; for(i=0;i<n;i++) cin>>a[i]; cout<<"Enter 2nd array - \n"; cin>>n; mn=m+n; cout<<"Enter 2nd array element in descending order - \n"; for(i=0;i<n;i++) cin>>b[i]; merge(a,m,b,n,c); cout<<"In the merged array B shown : \n"; for(i=0;i<mn;i++) cout<<c[i]; cout<<"\n";}

QUES – Write a Program to calculate the Sum of Row and Column of Array?Ans.

#include<iostream.h>#include<conio.h>void main(){clrscr(); int i,j,a[3][3],r=0,c=0; cout<<"Enter the Matrice"; for(i=0;i<3;i++) {for(j=0;j<3;j++) cin>>a[i][j]; } for(j=0;j<3;j++) {i=0;r=0; r+=a[i][j];

Page 18: Class XII C++ Practical Projects

cout<<"The sum of row "<<"is "<<r<<"\n"; i++; }

for(i=0;i<3;i++) {j=0;c=0; c+=a[i][j]; cout<<"The sum of column "<<"is "<<c<<"\n"; j++; } getch();}

Ques. WAP to print the result for the following series : 12+22+32+42+……….+n2

Ans.

#include<iostream.h>#include<conio.h>#include<math.h>void main(){clrscr ();inta,b=0,i;cout<<"Enter the value of n \n";cin>>a;for (i=1;i<=a;i++)b+=pow(i,2);cout<<b;

Page 19: Class XII C++ Practical Projects

getch();}

Ques. WAP to print the result of the following series: x+x/2!+x/3!......+x/n!Ans.

#include<iostream.h>#include<conio.h>void main(){ clrscr ();floata,b,c,d=1,e=0;cout<<"To print the series: x+x/2!+x/3!......+x/n!\n"<<"Enter the values of x and n \n" ;cin>>a>>b;for (c=1;c<=b;c++){d*=c;e+=a/d;

Page 20: Class XII C++ Practical Projects

}cout<<e;getch();}

Ques. WAP to print the following series : x+x2/2!+x3/3!.....+xn/n!Ans.

#include<iostream.h> #include<conio.h> #include<math.h>void main () {clrscr ();cout<<"Enter the value of x \n";floatx,z,v=-1,d=0;cin>>x;

Page 21: Class XII C++ Practical Projects

cin>>n;int a=0;for (z=1;z<=n;z++){a=y*pow(x,z);v*=z;d+=a/v;}cout<<d;getch ();}

Ques. WAP to print the result of the following series :1/x-3!/x3+5!/x5......n!/xn

Ans.

#include<iostream.h>#include<conio.h>#include<math.h>void main (){clrscr ();Float x,n,c,d=1,e,f=1,g;cout<<"Enter the value of x and n\n";

Page 22: Class XII C++ Practical Projects

cin>>x>>n;g=x;for (c=1;c<=n;c=c+2){ d*=-1;e=d*pow(x,c);f*=c*(c-1);g+=f/e;}cout<<g;getch ();}

QUES – Write a Program to calculate the Size of File?Ans.

#include<fstream.h>#include<conio.h>#include<stdio.h>void main(){clrscr(); char filename[20];

Page 23: Class XII C++ Practical Projects

cout<<"Enter filename: \n"; gets(filename); ifstream fin("size.txt",ios::in|ios::ate); while(fin.eof()) {long bytes=fin.tellg(); cout<<"File Size is: "<<bytes<<"Bytes"; } getch();}

QUES – Write a Program to Search a Record in File?Ans.

#include<fstream.h>#include<conio.h>#include<stdio.h>#include<string.h>

Page 24: Class XII C++ Practical Projects

class student{int age; char name[20]; public: void getname() {cout<<"Enter Name \n"; gets(name); } void getage() { cout<<"Enter Age \n"; cin>>age; } void putdata() {cout<<"Name: "<<name<<"\nAge: "<<age; }char sname()(return name;}}s;void main(){clrscr(); ifstream fin("student.txt"); char code[50]; gets(code); s.getname(); s.getage(); while(fin.eof()) {fin.read((char*)&s,sizeof(s)); if(strcmp(s.sname(),code)) {s.putdata(); break; } if(strcmp(s.sname()!,code)) cout<<"The entered Age is invalid"; } fin.close();}

QUES – Write a Program to Delete a Record in a File?Ans.

#include<fstream.h>#include<conio.h>

Page 25: Class XII C++ Practical Projects

#include<stdio.h>class student{int age; char name[20]; public: void getdata() {cout<<"Enter Name \n"; gets(name); cout<<"Enter Age \n"; cin>>age; } void putdata() {cout<<"Name: "<<name<<"\nAge: "<<age; }}s;void main(){clrscr(); ifstream fin("student.txt"); ofstream fout("temp.txt"); int code; cin>>code;

s.getdata(); while(fin.eof()) {fin.read((char*)&s,sizeof(s)); } fin.close(); fout.close(); remove("student.txt"); rename("temp.txt","student.txt"); getch();}

QUES – Write a Program to implicate Selection Sort on an Array?Ans.

Page 26: Class XII C++ Practical Projects

#include<iostream.h>#include<conio.h>void main(){clrscr(); int i,j,min,minat,temp,a[10]; for(i=0;i<10;i++) cin>>a[i]; for(i=0;i<(10-1);i++) {minat=i; min=a[i]; for(j=i+1;j<10;j++) {if(min>a[j]) {minat=j; min=a[j]; } } temp=a[i]; a[i]=a[minat]; a[minat]=temp; } for(i=0;i<10;i++) cout<<a[i];getch(); }

QUES – Write a Structure Student with Name and Marks and Display the Details in Decreasing Order?

Page 27: Class XII C++ Practical Projects

Ans.

#include<iostream.h>#include<conio.h>#include<stdio.h>struct student{int marks,roll_no; char name[40];}s[100];void main(){clrscr(); student s1; cout<<"Enter no. of students\n"; int n; cin>>n; cout<<"Enter Details"; for(int i=0;i<n;i++) {cout<<"\nStudent-->"<<i+1; cout<<"\nName-->"; gets(s[i].name); cout<<"\nRoll No.-->"; cin>>s[i].roll_no; cout<<"\nMarks"; cin>>s[i].marks; } for(int j=0;j<n;j++) {for(i=0;i<n;i++) if(s[i].marks<s[i+1].marks) {s1=s[i]; s[i]=s[i+1]; s[i+1]=s1; } } cout<<"Result in Decreasing order"; for(i=0;i<n;i++) {cout<<"\nName-->"<<s[i].name; cout<<"\nRoll No.-->"<<s[i].roll_no; cout<<"\nMarks-->"<<s[i].marks; } getch();}

Page 28: Class XII C++ Practical Projects

QUES – Write a Function to Insert a Node in Linked List?Ans.

#include<iostream.h>void insert(){clrscr(); if(head==NULL) {s *temp=new s; cout<<"\nEnter ID : "; cin>>temp->id; cout<<"\nEnter Gpa : "; cin>>temp->gpa; temp->next=NULL; head=temp; } else {s *temp=new s; cout<<"\nEnter ID : "; cin>>temp->id; cout<<"\nEnter Gpa : "; cin>>temp->gpa; temp->next=NULL; s *temp1=head; while(temp1->next!=NULL) { temp1=temp1->next; } temp1->next=temp;}

Page 29: Class XII C++ Practical Projects

QUES – Write a Function to delete a node in a Linked List?Ans.

#include<iostream.h>void delete(){clrscr(); s *temp=head; int no; cout<<"\n\nEnter ID To Be Deleted : "; cin>>no; while(temp!=NULL) { if(head->id==no) head=head->next; else if(temp->id==no) temp=temp->next; }}

Page 30: Class XII C++ Practical Projects

QUES – Write a Program to implement Stack as a Linked List?Ans.

#include<iostream.h>#include<conio.h>#include<process.h>#include<stdio.h>struct node {char name[20]; int age; node *link; }*ptr=NULL,*save=NULL;class stack {node *top; public: stack() {top=NULL; } void stackpush(); void stackpop(); void display(); }st;

void stack::stackpush() {ptr=new node; if(ptr==NULL) {cout<<"ERROR.... "; } else { cout<<"Enter the name "; gets(ptr->name); cout<<"Enter the age "; cin>>ptr->age; ptr->link=NULL; if(top==NULL)

{top=ptr;}

else{ptr->link=top;

Page 31: Class XII C++ Practical Projects

top=ptr;}

} }

void stack::stackpop() {if(top==NULL) {cout<<"Underflow "; } else {save=top; top=top->link; cout<<"Name "; puts(save->name); cout<<"Age "<<save->age; delete save; } }

void stack::display() {if(top==NULL) {cout<<"No elements.."<<endl; } else {ptr=top; while(ptr!=NULL) {cout<<"\nName ";

puts(ptr->name);cout<<"Age "<<ptr->age;ptr=ptr->link;

} } }void main() {clrscr(); int ch; X: cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n"; cin>>ch; switch(ch) {case 1:st.stackpush();

goto X; case 2:st.stackpop();

goto X; case 3:st.display();

Page 32: Class XII C++ Practical Projects

goto X; default:cout<<"Wrong choice ";

goto X; case 4:exit(0); } getch(); }

QUES – Write a Program to implicate Static Stack?Ans.

#include<iostream.h>#include<conio.h>#include<process.h>void main(){clrscr();int ch,i,top=-1,stack[5];x:cout<<endl<<endl;cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;cin>>ch;switch(ch){case 1:top++;if(top<=4){cout<<"Enter The Element"<<endl;cin>>stack[top];cout<<"The Stack is"<<endl;for(i=0;i<=top;i++) cout<<stack[i]<<"--> ";goto x;}else{cout<<" ************* Stack OVERFLOW ********** "<<endl;goto x;}

case 2:if(top>=0){top--;

Page 33: Class XII C++ Practical Projects

cout<<"Stack is"<<endl;for(i=0;i<=top;i++)cout<<stack[i]<<" -> ";goto x;}else{cout<<"************** Stack UNDER FLOW ***********"<<endl;goto x;}case 3:exit(0);default :cout<<"WRONG CHOICE !!!!!!!!!!! "<<endl;goto x;}}

Page 34: Class XII C++ Practical Projects

QUES – Write a Program to implicate Queue as a Linked List?Ans.

#include<iostream.h>#include<conio.h>#include<process.h>void main(){clrscr();int ch,i,rear=-1,front=-1,queue[10];x:cout<<endl<<endl;cout<<"Enter Choice 1> Insert 2> Delete 3>exit "<<endl;cin>>ch;switch(ch){case 1:

if(front==-1){front=0;}rear++;if(rear<=9){cout<<"Enter The Element"<<endl;cin>>queue[rear];cout<<"Queue is"<<endl;for(i=0;i<=rear;i++){cout<<queue[i];}}else{cout<<"****************QUEUE OVERFLOW****************";

Page 35: Class XII C++ Practical Projects

}goto x;

case 2:if(rear==-1){rear=0;}

if(front==-1){cout<<"*************** UNDER FLOW **********"<<endl;goto x;}elseif(rear==front){queue[front]='\o';front=-1;rear=-1;goto x;}else{queue[front]='\o';front++;}cout<<"Queue is"<<endl;for(i=0;i<=rear;i++){cout<<queue[i];}goto x;case 3:exit(0);

default :goto x; }}

Page 36: Class XII C++ Practical Projects

QUES – Write a Program to implicate Circular Queue?Ans.

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

class queue {int data[10]; int front,rear; public: queue() {front=-1; rear=-1; } void add(); void remove(); void display(); };

void queue::add() {if((rear+1==front)||(rear==9&&front==0)) {cout<<"Overflow "; } else

Page 37: Class XII C++ Practical Projects

{if((rear==-1) &&(front==-1)){rear=0; front=0;}

else if(rear==9){rear=0;}

else{rear++;}

cout<<"Enter the element "; cin>>data[rear]; } }

void queue::remove() {if(front==-1&&rear==-1) {cout<<"Underflow "; } else {if(front==9) {front=0; } else if(front==rear) {front=-1;

rear=-1; } else {front++; } } }

void queue::display() {int i=0,n=9; if(rear==-1) {cout<<"No elements.."<<endl; } else { if(rear>front) {for(i=0;i<front;i++)

{cout<<"_";}

for(i=front;i<=rear;i++){cout<<data[i];}

Page 38: Class XII C++ Practical Projects

for(i=rear+1;i<n;i++){cout<<"_";}

} else {for(i=0;i<=rear;i++)

{cout<<data[i];}

for(i=rear+1;i<front;i++){cout<<"_";}

for(i=front;i<n;i++){cout<<data[i];}

} } }

void main() {clrscr(); int ch; queue queue; X: cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n"; cin>>ch; switch(ch) {case 1:queue.add();

goto X; case 2:queue.remove();

goto X; case 3:queue.display();

goto X; case 4:exit(0); } getch(); }