Sunteți pe pagina 1din 38

XII - D

KRMWS

INDEX
S.No.
1.)
2.)
3.)
4.)
5.)
6.)
7.)
8.)
9.)
10.)
11.)
12.)
13.)
14.)
15.)
16.)
17.)
18.)
19.)
20.)
21.)
22.)
23.)
24.)
25.)

Practical
SQL (1)
SQL (2)
SQL (3)
SQL (4)
SQL (5)
Ascending Order
Menu
Class Publication
Merging of Arrays
Sum of Row and Column
Series 1
Series 2
Series 3
Series 4
Size of File
Searching of a Record in a File
Deletion of a Record in File
Selection Sort
Structure
Insertion in a Linked List
Deletion in a Linked List
Stack as Linked List
Static Stack
Queue as Linked List
Circular Queue

Sign

QUES 1- Given the following tables for a


DatabaseLIBRARY.
Table BOOKS
Book_
Id
F001
F002
T001
T002
C001

Bookna
me
The
Tears
Thunder
Bolts
My First
C++
C++
Brainwor
k
Fast
Cook

Author_Na Publishe Pric


me
rs
e
William
First
750
Hopkins
Publ.
Anna
First
700
Roberts
Publ.
Brian &
EPB
250
Brooke
A.W.
TDH
325
Rossaine

Typ
e
FicTion
FicTion
Text

Qty

Text

LataKapoor

Coo
kery

EPB

350

Table ISSUED
Book_Id
F001
T001

Quantity Issued
3
1

C001

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.

10
5
10

Ans. UPDATE Books set Price = Price + 50 where Publisher =


First Publ;
COUNT(DISTINCT
e) To display the
Publishers)
Bookid,Bookname and Quantity
3
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;
SUM(Price)
1350

Ans.

Bookname
Author_name
My First C++
Brian & Brooke
C++ Brainworks
A.W. Rossaine
Fast Cook
LataKapoor
SELECT Book_Name, Author_Name FROM Books
where Price<500;
3)

Ans.

4) SELECT COUNT(*) FROM Books;


COUNT
5

Ans.

QUES 2 - Given the following tables for


a Database SHOP.
Table INTERIORS
S.N
o.

ITEMNA
ME

Red Rose

2
3
4
5
6
7
8
9

TYPE

Doubl
e Bed
Soft Touch Baby
Cot
Jerrys
Baby
House
Cot
Rough
Office
Wood
Table
Comfort
Doubl
Zone
e Bed
Jerry Look Baby
Cot
Lion King Office
Table
Royal
Sofa
Tiger
Park
Sofa
Sitting

DATE
OF
STOCK
23/02/02

PRIC
E

DISCOU
NT

32000

15

20/11/02

9000

10

19/02/02

8500

10

01/01/02

20000

20

12/01/02

15000

20

24/02/02

7000

19

20/02/02

16000

20

22/02/02

30000

25

13/12/01

9000

15

10

Dine
Paradise

Dinin
g
Table

19/02/02

11000

15

Table NEWONES
S.
No.
11

ITEMNA
ME

TYPE

12

White
Double
Wood
Bed
James 007
Sofa

13

Tom Look

Baby
Cot

DATE PRIC DISCOU


OF
E
NT
STOCK
23/02/0 2000
20
2
0
20/02/0 1500
15
3
0
21/02/0 7000
10
3

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 INTERIORS
where DISCOUNT>15;
e) To count the number of items, whose type is Double
Bedfrom INTERIORS table.
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;
COUNT (DISTINCT
TYPE)
5

Ans.

2) SELECT AVG (DISCOUNT) from Interiors where TYPE


=Baby Cot;
AVG
(DISCOUNT)
13

Ans.

3) SELECT SUM (PRICE) from Interiors where DATE OF


STOCK<{12/02/02};
SUM
(PRICE)
44000

Ans.

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


MAX
(PRICE)
32000

Ans.

QUES 3 - Given the following tables for a


Database SPORTS.
Table SPORTS
Stude
ntNo.

Clas
s

Name

Game 1

10

Cricket

11
12

8
7

Samm
er
Sujit
Kamal

13
14

7
9

Tennis
Swimmin
g
Tennis
Basketball

15

10

Cricket

Venna
Areha
na
Arpit

Grade Game 2

Grade
2

A
B

Swimming
Skating
Football

A
C
B

C
A

Tennis
Cricket

A
A

Athletics

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;

g) Add new column named Marks.


Ans. ALTER TABLE Sports add (Marks number);

QUES 4 - Given the following tables for a


Database EMPLOYEE.
Table EMPLOYEE
Eid

Name

Deptid

1
2
3
4
5
6

Deepali Gupta
RajatTyagi
Hari Mohan
Harry
Sumit Mittal
Jyoti

101
101
102
102
103
101

Qualificati
on
M.C.A.
B.C.A.
B.A.
M.A.
B.Tech.
M.Tech.

Sex
F
M
M
M
M
F

Table SALARY
Eid
1
2
3
4
5
6

Basic
6000
2000
1000
1500
8000
10000

D.A.
2000
300
300
390
900
300

H.R.A.
2300
300
300
490
900
490

Bonus
200
30
40
30
80
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 is
total_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;

Give the output of the following queries based


on the above tables:

COUNT (*)
2
4
Name
Jyoti

Sex
F
M

1) SELECT Name from Employee

where Eid = (SELECT Eid


from Salary where Basic = (SELECT
MAX (Basic) from Salary));
Ans.

MAX (Basic)
10000

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;


DISTINCT Deptid
101
102
103

Ans.

QUES 5 - Given the following tables for a


Database HOSPITAL.
Table HOSPITAL
N
o.
1
2
3
4
5
6
7
8

Name

Ag
e
Sandee 65
p
Ravina 24
Karan 45
Tarun 12
Zubin 36
Ketaki 16
Leena 29
Zareen 45

Departme
nt
Surgery

Date of
Adm
23/02/98

Charg
es
300

Se
x
M

Orthopedic
Orthopedic
Surgery
E.N.T.
E.N.T.
Cardiology
Gynecology

20/01/98
19/02/98
01/01/98
12/01/98
24/02/98
20/02/98
22/02/98

200
200
300
250
300
800
300

F
M
M
M
F
F
F

9
Kush
10 Shailya

19
31

Gynecology 13/01/98
Nuclear
19/02/98
Medicine

800
400

M
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 patients 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;
COUNT (DISTINCT
Department)
6

Ans.

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


MAX (AGE)
65

Ans.

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


AVG
(Charges)
400

Ans.

4) SELECT SUM (Charges) from hospital where Date of Adm<


2/08/98;
SUM (Charges)
3850

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];

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];
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;
}
}
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 MEMBERS
Title as Title of the Book
Price as Price of the Book

PUBLIC MEMBERS
Function getdata() to accept Data
Function putdata() to display Data

Ans.
#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()
{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)
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];
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;
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;
}
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;
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";


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];
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>
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>
#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.

#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?

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();
}

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;
}

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;
}
}

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;
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();
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--;
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;
}
}

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****************";
}
goto x;
case 2:
if(rear==-1)
{rear=0;}
if(front==-1)
{cout<<"*************** UNDER FLOW
**********"<<endl;goto x;}
else
if(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; }}

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
{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];
}
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();
}

S-ar putea să vă placă și