You are on page 1of 67

Computer science

TARUN SINGH XII-C

:
Q: To create table staff and salary & design the given constraints?

A: Input
SQL> create table staff_table 2 ( 3 id number(3) primary key, 4 name char(30) not null, 5 department char(15) check(department in('sales','finance','research')), 6 gender char(1) check(gender in('M','F')), 7 expirience number(2) not null 8 );

Table created.

SQL> create table salary 2 ( 3 id number(3) references staff_table(id), 4 basic number(4) check(basic>1000), 5 allowence number(4) check(allowence>500), 6 commision number(2) check(commision in(5,10,15,20)) 7 );

Table created. SQL> insert into staff_table 2 values(101,'Mayank','research','M',13);

1 row created.

SQL> insert into staff_table 2 values(102,'sdfgjh','sales','F',4);

1 row created.

SQL> insert into staff_table 2 values(103,'dhsgfh','finance','M',8);

1 row created.

SQL> insert into salary

2 values(103,4546,655,5);

1 row created.

SQL> insert into salary 2 values(104,3657,654,10);

1 row created.

SQL> insert into staff_table 2 values(104,'jhdgfj','sales','F',10);

1 row created.

SQL> insert into salary 2 values(101,9999,8765,20);

1 row created.

SQL> insert into salary 2 values(102,6567,5470,5);

1 row created.

A: Output
SQL> describe staff_table Name Null? Type

----------------------------------------- -------- ---------------------------ID NAME DEPARTMENT GENDER EXPIRIENCE NOT NULL NOT NULL NOT NULL NUMBER(3) CHAR(30) CHAR(15) CHAR(1) NUMBER(2)

SQL> select * from staff_table; ID NAME DEPARTMENT G EXPIRIENCE

---------- ------------------------------ --------------- - ------------------------101 Mayank 102 sdfgjh 103 dhsgfh 104 jhdgfj research sales finance sales M F M F 13 4 8 10

SQL> select * from salary; ID BASIC ALLOWENCE COMMISION

---------- ---------- ---------- ---------101 102 103 104 9999 6567 4546 3657 8765 5470 655 654 20 5 5 10 6

SQL> select name,department from staff_table; NAME DEPARTMENT

------------------------------ --------------Mayank sdfgjh dhsgfh jhdgfj research sales finance sales

SQL> select * from staff_table where department='research'; ID NAME DEPARTMENT G EXPIRIENCE

---------- ------------------------------ --------------- - ---------101 Mayank research M 13

SQL> select * from staff_table where gender='F'; ID NAME DEPARTMENT G EXPIRIENCE

---------- ------------------------------ --------------- - ---------102 sdfgjh 104 jhdgfj sales sales F F 4 10

SQL> select id,name from staff_table where expirience>5; ID NAME ---------- -----------------------------101 Mayank 103 dhsgfh

104 jhdgfj

Q: To create table items & trans & design the given constraints?

A: Input
SQL> create table items 2 ( 3 itemno number(3) primary key, 4 name char(15) not null, 5 Qoh number(5) not null, 6 price number(7) check(price>1000) 7 );

Table created.

SQL> create table trans 2 ( 3 itemno number(3) references items(itemno), 4 transtype char(1) check(transtype in('p','s')), 5 tqty number(3) not null, 6 tdate date not null 7 );

Table created.

SQL> insert into items 2 values(100,'shoes',2500,3400);

1 row created.

SQL> insert into items 2 values(101,'pen drive',1334,1001);

1 row created.

SQL> insert into items 2 values(102,'jackets',1000,4500);

1 row created.

SQL> insert into trans 2 values(100,'s',2,'24-Mar-2012');

1 row created.

SQL> insert into trans 2 values 3 (101,'p',5,'2-feb-2012');

1 row created.

SQL> insert into trans 2 values(102,'s',6,'19-apr-2012');

1 row created.

A: Output
SQL> set linesize 120; SQL> select * from items,trans 2 ;

ITEMNO NAME

QOH

PRICE ITEMNO T TQTY TDATE

---------- --------------- ---------- ---------- ------------- - ---------- --------100 shoes 100 shoes 100 shoes 101 101 101 pen drive pen drive pen drive 2500 2500 2500 1334 1334 1334 1000 1000 1000 3400 3400 3400 1001 1001 1001 4500 4500 4500 100 101 102 100 101 102 100 101 102 s p s s p s s p s 2 5 6 2 5 6 2 5 6 24-MAR-12 02-FEB-12 19-APR-12 24-MAR-12 02-FEB-12 19-APR-12 24-MAR-12 02-FEB-12 19-APR-12

102 jackets 102 jackets 102 jackets

9 rows selected.

10

Q: To create table sender & recipient & design the given constraints?

A: Input
SQL> create table sender 2 ( 3 senderid char(4) primary key, 4 sname char(30) not null, 5 saddr char(30) not null, 6 scity char(10) check(scity in('new delhi','mumbai')) 7 );

Table created.

SQL> create table recipient 2 ( 3 recid char(4) primary key, 4 senderid char(4) references sender(senderid), 5 recname char(30) not null, 6 recaddr char(30) not null, 7 reccity char(10) check(reccity in('kolkata','new delhi','mumbai')) 8 );

Table created.

11

SQL> insert into sender 2 values('nd01','R jain','2,abc appts','new delhi');

1 row created.

SQL> insert into sender 2 values('mu02','H sinha','12,newton','mumbai');

1 row created.

SQL> insert into sender 2 values('mu15','S haj','27/a,park street','new delhi');

1 row created.

SQL> insert into sender 2 values('nd50','T prasad','122-k,sda','mumbai');

1 row created. SQL> insert into recipient 2 values('ko05','nd01','R bajpayee','5,central avenue','kolkata');

1 row created.

12

SQL> insert into recipient 2 values('nd08','mu02','S mahajan','116, A vihar','new delhi');

1 row created.

SQL> insert into recipient 2 values('mu19','nd01','H sing','2A andheru west','mumbai');

1 row created.

SQL> insert into recipient 2 values('mu32','mu15','P K swami','B5,CS terminus','mumbai');

1 row created.

SQL> insert into recipient 2 values('nd48','nd50','S tirpathi','13,B1 D,mayur vihar','new delhi');

1 row created .

A: Output

SQL> select sname from sender where scity='mumbai';

SNAME 13

-----------------------------H sinha T prasad

SQL> select * from sender;

SEND SNAME

SADDR

SCITY

---- ------------------------------ ------------------------------ ---------nd01 R jain mu02 H sinha mu15 S haj nd50 T prasad 2,abc appts 12,newton 27/a,park street 122-k,sda new delhi mumbai new delhi mumbai

SQL> set linesize 120; SQL> select * from recipient;

RECI SEND RECNAME

RECADDR

RECCITY

---- ---- ------------------------------ ------------------------------ ---------ko05 nd01 R bajpayee nd08 mu02 S mahajan mu19 nd01 H sing mu32 mu15 P K swami nd48 nd50 S tirpathi 5,central avenue 116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar kolkata new delhi mumbai mumbai new delhi

14

SQL> set linesize 200; SQL> select recid,sname,saddr,recname,recaddr from sender,recipient;

RECI SNAME

SADDR

RECNAME

RECADDR

---- ------------------------------ ------------------------------ ------------------------------ -ko05 R jain nd08 R jain mu19 R jain mu32 R jain nd48 R jain ko05 H sinha nd08 H sinha mu19 H sinha mu32 H sinha nd48 H sinha ko05 S haj 2,abc appts 2,abc appts 2,abc appts 2,abc appts 2,abc appts 12,newton 12,newton 12,newton 12,newton 12,newton 27/a,park street R bajpayee S mahajan H sing P K swami S tirpathi R bajpayee S mahajan H sing P K swami S tirpathi R bajpayee 5,central avenue 116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar 5,central avenue 116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar 5,central avenue

RECI SNAME

SADDR

RECNAME

RECADDR

---- ------------------------------ ------------------------------ ------------------------------ -nd08 S haj mu19 S haj mu32 S haj nd48 S haj ko05 T prasad 27/a,park street 27/a,park street 27/a,park street 27/a,park street 122-k,sda S mahajan H sing P K swami S tirpathi R bajpayee 116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar 5,central avenue

15

nd08 T prasad mu19 T prasad mu32 T prasad nd48 T prasad

122-k,sda 122-k,sda 122-k,sda 122-k,sda

S mahajan H sing P K swami S tirpathi

116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar

20 rows selected

SQL> set linesize 205; SQL> select recid,sname,saddr,recname,recaddr from sender,recipient where sender.senderid=recipient. senderid;

RECI SNAME

SADDR

RECNAME

RECADDR

---- ------------------------------ ------------------------------ ------------------------------ -ko05 R jain nd08 H sinha mu19 R jain mu32 S haj nd48 T prasad 2,abc appts 12,newton 2,abc appts 27/a,park street 122-k,sda R bajpayee S mahajan H sing P K swami S tirpathi 5,central avenue 116, A vihar 2A andheru west B5,CS terminus 13,B1 D,mayur vihar

SQL> select * from sender 2 order by sname;

SEND SNAME

SADDR

SCITY

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

16

mu02 H sinha nd01 R jain mu15 S haj nd50 T prasad

12,newton 2,abc appts 27/a,park street 122-k,sda

mumbai new delhi new delhi mumbai

SQL> select count(*) from recipient group by reccity;

COUNT(*) ---------1 2 2

SQL> select reccity,count(*) from recipient group by reccity; RECCITY COUNT(*)

---------- ---------kolkata new delhi mumbai 1 2 2

SQL> select sname,recname from sender,recipient where sender.senderid=recipient.senderid and reccity ='mumbai'; SNAME RECNAME

------------------------------ -----------------------------R jain H sing

17

S haj

P K swami

CLASSES:
Q:1) Define a class flight with following description;
int - flightno string dest float dof, fuel. A function feed info to allow users to enter value for all data members & a function to show delayed flights?

#include<iostream.h> #include<string.h> #include<conio.h> #include<stdio.h> class competition{ int eventno,score; char qual,desc[30]; public: competition() { eventno=101; strcpy(desc,"state level"); score=50; qual='n'; }

18

void input() { cout<<"enter event number: "; cin>>eventno; cout<<"enter score: "; cin>>score; cout<<"enter description: "; gets(desc); } void award(int score) { if(score>50) qual='y'; else if(score<50) qual='N'; } void show() { award(score); cout<<endl<<endl<<eventno<<endl<<desc<<endl<<score<<endl<<qual; } };

void main() {

19

clrscr(); competition c; c.input(); c.show(); getch(); };

20

Q: 2) Define a class competition in C++ with the following descriptions:


int eventno, score. char desc[ ],qual. Member Functions: A constructor to assign initial values eventno as 101, desc as state level, score is 50 & qual an n. Input( ), to take the input for eventno, desc & score. Award(int), to award qualified as y, if score is more than the cutoff score passed as argument to the function else n. Show( ), to display all the details.
#include<iostream.h> #include<conio.h> #include<stdio.h> class flight{ int flightno; char dest[20]; float dof,fuel; public: void feedinfo() { cout<<"enter flight no: "; cin>>flightno; cout<<"enter destination: "; 21

gets(dest); cout<<"enter distance of flight: "; cin>>dof; cout<<"enter fuel: "; cin>>fuel; } void delayedflights() { cout<<"delayed flights going to respective destinations are:"; cout<<flightno<<dest; } void showdata() { cout<<flightno<<endl<<dof<<endl<<dest<<endl<<fuel; } };

void main() { clrscr(); flight f; f.feedinfo(); f.delayedflights(); f.showdata(); };

22

23

FILES:
#include<iostream.h> #include<fstream.h> #include<conio.h> #include<string.h> #include<stdio.h> #include<stdlib.h> class IPL { int team_no; char team_name[30]; char coach_name[20]; int team_points; public: void getdata(); void displaydata(); char * returnteamname() { return team_name; } int returnteamno() { return team_no; } };

24

void IPL::getdata() { clrscr(); cout<<"\n Team Number:";cin>>team_no; cout<<"\n Team name:";gets(team_name); cout<<"\n Coach name:";gets(coach_name); cout<<"\n Team points:";cin>>team_points; } void IPL::displaydata() { cout<<team_no<<"\t\t"<<team_name<<"\t\t"<<coach_name<<"\t\t"<<team_points<<"\n"; }

//independent functions to work with file void write_file() { fstream fl("ipl.dat",ios::app,ios::binary); IPL ip; ip.getdata(); fl.write((char *)&ip,sizeof(ip)); fl.close(); } void read_file() { fstream fl("ipl.dat",ios::in,ios::binary); IPL ip;

25

clrscr(); cout<<"Team No \t Team name \t Coach Name \t Team points\n"; while(!fl.eof()) { fl.read((char*)&ip,sizeof(ip)); if(fl.eof()) break; ip.displaydata(); } fl.close(); getch(); } void search_file() { fstream fl("ipl.dat",ios::in,ios::binary); IPL ip; char nm[30]; cout<<"\n Enter Team Name \n"; gets(nm); while(!fl.eof()) { fl.read((char *)&ip,sizeof(ip)); if(fl.eof()) break; if (strcmpi(nm,ip.returnteamname())==0) { ip.displaydata();

26

break; } } getch(); fl.close(); } void delete_file() { fstream fl1("ipl.dat",ios::in,ios::binary); fstream fl2("temp.dat",ios::out,ios::binary); IPL ip; int tno; cout<<"\n Enter Team No."; cin>>tno; while(!fl1.eof()) { fl1.read((char *)&ip,sizeof(ip)); if(fl1.eof()) break; if(ip.returnteamno()!=tno) { fl2.write((char *)&ip,sizeof(ip)); } } fl1.close(); fl2.close(); remove("ipl.dat");

27

rename("temp.dat","ipl.dat"); } void update_file() { fstream fl("ipl.dat",ios::in|ios::out,ios::binary); IPL ip; int tno; long x; cout<<"Enter Team Number"; cin>>tno; while(!fl.eof()) { x=fl.tellg(); fl.read((char *)&ip,sizeof(ip)); if (fl.eof()) break; if (tno==ip.returnteamno()) { fl.seekp(x,ios::beg); ip.getdata(); fl.write((char *)&ip,sizeof(ip)); break; } } fl.close(); } void box()

28

{ clrscr(); int i=0; int j=0; textcolor(CYAN); for(i=0;i<50;i++) { for(j=0;j<80;j++) { gotoxy(j,i); cprintf(""); } } } void main() { clrscr(); int ch=0; while(ch!=6) { clrscr(); box(); gotoxy(25,5); cout<<"IPL Information System"; gotoxy(10,10); cout<<"1. Insert Record"; gotoxy(10,11);

29

cout<<"2. Read Record"; gotoxy(10,12); cout<<"3. Modify Record"; gotoxy(10,13); cout<<"4. Delete Record"; gotoxy(10,14); cout<<"5. Search Record"; gotoxy(10,15); cout<<"6. EXIT"; gotoxy(10,18); cout<<"Your Ch.........(1-5)"; cin>>ch; switch(ch) { case 1: write_file();break; case 2: read_file();break; case 3: update_file();break; case 4: delete_file();break; case 5: search_file();break; } } }

30

Main Screen

Entering Record

31

Reading Record

Modifying Record
32

Modifying Final

Deleting Record
33

Deleting Final

Searching Record
34

POINTERS:
Q: Program to find the occurrence of character in the string
#include<iostream.h> #include<string.h> #include<conio.h> # include <stdio.h> class strn { char *a; char ch; int i; public: void read(); void find(); }; //END OF CLASS void strn::read() { cout << "\n\tThe number of times "<< ch << " occur in " << a << " is " << count; } void main() { clrscr(); strn x; cout << "\n\n\n\t "; x.read(); x.find(); getch(); };

35

cout << "\n\t"; cout << "\n\tEnter the first string "; gets(a); } void strn::find() { i = 0; int count = 0; cout << "\n\t Enter the character "; cin >> ch; while (*(a+i) != '\0') { if (*(a + i) == ch) count++; i++; }

Output

36

Q: Program to check whether the two strings are equal or not


37

#include<iostream.h> #include<string.h> #include<conio.h> #include <stdio.h> //CLASS DECLARATION class strn { char *a, *b, flag; int i, j, k; public: void read(); //MEMBER FUNCTIONS void compare(); strn() //USE OF CONSTRUCTOR { flag = 'y'; } }; //END OF CLASS void strn::read() { cout << "\n\t"; cout << "\n\tEnter the first string "; gets(a); //TO READ THE STRING cout << "\n\tEnter the second string "; gets(b); //TO READ THE STRING } void strn::compare() { i = 0; j = 0; while (*(a+i) != '\0') i++; while(*(b+j) != '\0') j++; if (i != j) { cout << "\n\t Strings are not equal ";

return; } i = 0; while ((*(a+i) != '\0') && (*(b+i) != '\0')) { if(*(a + i) != *( b + i)) { flag = 'n'; break; } i++; } if(flag == 'n') cout << "\n\tStrings are not equal "; else cout << "\n\tStrings are equal "; } //M A I N P R O G R A M void main() { strn x; //DECLARATION OF OBJECT clrscr(); cout << "\n\n\n\t "; x.read(); //CALLING MEMBER FUNCTIONS x.compare(); cout << "\n\n\t\t bye!"; getch(); }

Output
38

Q: Program to find the length of the string


#include<iostream.h> #include<string.h> } void main()
39

#include<conio.h> #include <stdio.h> //CLASS DECLARATION class strn { char *a; int i; public: void read(); void calculate(); }; //END OF CLASS void strn::read() { cout << "\n\t"; cout << "\n\tEnter your name "; gets(a); } void strn::calculate() { i = 0; while (*(a+i) != '\0') i++; cout << "\n\tThe length of your name is : " << i;;

{ strn x; clrscr(); cout<< "\n\n\n\t "; x.read(); x.calculate(); getch(); }

Output

ARRAYS:
40

Q: Search an element in an array using linear search.


#include<iostream.h> #include<conio.h> #include<stdio.h> const val = 100; int linear(float AR[val], float data ,int n) { int i, flag = 0, pos = 1; i= 0; while ( i < n) { if (AR[i] == data) { flag = 1; break; } i++; } if (flag == 1) return(1); else return(0); } void main() { float AR[100],data; int n,x; clrscr(); cout << "Enter n"; cin >>n; for(int i=0;i<n;i++) cin >>AR[i]; cout << "Enter number to be searched "; cin >>data; x = linear(AR,data,n); if (x ==1) cout << "Number exists in the list at"<<i; else cout << "Number does not exists in the list"; getch(); };

Output:

41

Q: A program for bubble sort


#include <iostream.h> #include <conio.h> #include <stdio.h> #include<iomanip.h> #include<string.h> void bubble(int a[], int N); int mark[20]; char name[20][20]; void main() { int i , j , n , temp; char c[20]; clrscr(); putchar('\n'); cout << "Enter How many students \n"; cin >> n; cout << "Enter the marks and name of students ..\n"; for (i = 0; i < n; i++) { cout<< "Enter marks "; cin >> mark[i]; cout<< "Enter name "; gets(name[i]); } for (i = 0; i < n; i++) for (j = 0 ; j < n; j++) if (mark[j] > mark[j+1]) { temp = mark[j]; mark[j+1] = mark[j]; mark[j] = temp; strcpy (c , name[j]); strcpy(name[j+1], name[j]); strcpy(name[j], c); } cout << "\t\tSorted List...\n"; cout << setw(10) << " Marks " << setw(15) << "Name" << endl; for (i = 0; i < n; i++) { cout << setw(10) << mark[i] << setw(15) << name[i] << endl; } getch(); }

Output:

42

Q: Function to arrange 10 elements using selection sort


#include <iostream.h> #include <conio.h> #include <stdio.h> #include<iomanip.h> #include<string.h> void SelectionSort() { int range[10], loc, lowest, T, N, i, j, x; N = 10; cout << "Enter the array elements : \n"; for (i = 0; i < N; i++) { cout << "Location " << i + 1 << ". Value : "; cin >> range[i]; } for (i = 0; i < N - 1; i++) { lowest = range[i]; loc = i; for (j = i + 1; j < N; j++) { if (lowest > range[j]) { loc = j; lowest = range[j]; } } T = range[i]; range[i] = range[loc]; range[loc] = T; } cout << "\nThe sorted list is ... \n"; for (i = 0; i < N; i++) cout << range[i] << endl; } void main() { clrscr(); SelectionSort(); getch(); }

Output

43

Q:Deletion
#include<iostream.h> #include<conio.h> #define len 50 int i,n,arr[len],a,pos=-1; void main() { clrscr(); void del(int); cout<<"Enter the no. of entries to be done : "; cin>>n; cout<<"\n\n"; for(i=0;i<n;i++) { cout<<"\nEnter No. "<<i+1<<" : "; cin>>arr[i]; } cout<<"\n\nThe Entered No.s are :"; for(i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\nEnter the no. to be deleted :"; cin>>a; del(a); cout<<"\n\nNow The Array is: "; if(pos!=-1) {for(i=0;i<n-1;i++) cout<<arr[i]<<" "; } else {for(i=0;i<n;i++) cout<<arr[i]<<" "; } getch();} void del(int) { for(i=0;i<n;i++) { if(a==arr[i]) pos=i; } if( pos==-1) cout<<"\n\nNot Found"; else {for(i=pos;i<n;i++) { arr[i]=arr[i+1]; }}}

Output

44

Q: Traversal in an array
#include<iostream.h> #include<conio.h> #define len 50 void main() { clrscr(); int arr[len]; int n,i; cout<<"How many elemnts do you want to insert in the array :"; cin>>n; cout<<"\n\nEnter array elements :"; for(i=0;i<n;i++) {cout<<"Enter element "<<i+1<<": "; cin>>arr[i]; cout<<endl; } cout<<"\n\nDoubling the Elements.......\n"; cout<<"\nNow the array is :\n\n"; for(i=0;i<n;i++) { arr[i]=arr[i]*2; cout<<"Element "<<i+1<<"= "; cout<<arr[i]; cout<<"\n\n"; } getch(); }

Output

45

Q: Insertion
#include<iostream.h> #include<conio.h> #define len 50 int n; void main() { clrscr(); int i,arr[len],a,pos; cout<<"Enter the no. of entries to be done : "; cin>>n; cout<<"\n\n"; for(i=0;i<n;i++) { cout<<"\nEnter No. "<<i+1<<" : "; cin>>arr[i]; } cout<<"\n\nThe Entered No.s are :"; for(i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\n\nEnter the new no. to be inserted :"; cin>>a; cout<<"\nEnter Its Position : "; cin>>pos; pos--; for(i=n;i>pos;i--) { arr[i]=arr[i-1]; } arr[pos]=a; cout<<"\n\nNow The Array is: "; for(i=0;i<n+1;i++) cout<<arr[i]<<" "; getch(); }

Output

46

Q: Updation
#include<iostream.h> #include<conio.h> #define len 50 int i,n,arr[len],a,nval,pos=-1; void main() { void update(int); clrscr(); cout<<"Enter the no. of entries to be done : "; cin>>n; cout<<"\n\n"; for(i=0;i<n;i++) { cout<<"\nEnter No. "<<i+1<<" : "; cin>>arr[i]; } cout<<"\n\nThe Entered No.s are :"; for(i=0;i<n;i++) cout<<arr[i]<<" "; cout<<"\nEnter the no. to be Updated :"; cin>>a; update(a); cout<<"\n\nNow The Array is: "; for(i=0;i<n;i++) cout<<arr[i]<<" "; getch();} void update(int) { for(i=0;i<n;i++) { if(a==arr[i]) pos=i; } if( pos==-1) cout<<"\n\nNot Found"; else { cout<<"\nEnter the new Value :"; cin>>nval; arr[pos]=nval; } }

Output

47

STACKS:
Q: Write a program to demonstrate array based stack?
#include<iostream.h> #include<conio.h> #include<process.h> class stack { int arr[20]; int top, max; public: stack ( ) { top = -1; max=19; } void push(); void pop( ); void display( ); }; void stack : : push() { if(top= =max) cout<< \n Overflow; else { int d; cout <<enter element to be pushed \n; cin >> d; top++; arr[top]=d; cout << d << PUSHED INTO STACK.; } } int elt=arr[top]; top - -; cout << elt << POPPED FROM STACK.; } } void stack: : display( ) { for (int i = top; i >= 0; i--) cout << \n << arr[i]; } void main( ) { int ch; char c; stack ob; int n; do { clrscr( ); cout<<\n #######MENU#######; cout << \n1. PUSH INTO STACK.; cout << \n 2. POP INTO STACK.; cout << \n 3. DISPLAY THE STACK.; cout << \n 4. EXIT.; cout << \n ENTER YOUR CHOICE?: cin >> ch; switch(ch) { case 1 : cout << \n Enter a number?; cin>>n; ob.push(n); break; case 2: ob.pop( );break;
48

void stack: : pop( ) { if(top= = -1) cout<< \n Underflow; else {

case 3: ob.display( ) break; } cout << \ndo you want to continue?; cin >> c; }while((c= =y) || (c = = Y )); getch( );}

Output: Push:

Pop:

49

Display:

50

Q: Write a program to demonstrate linked list based stack?


#include<iostream.h> #include<conio.h> #include<process.h> struct NODE { int Data; NODE *Next; }; class Stack { NODE *Top; public: Stack( ) {Top=NULL;} void Push( ); void Pop( ); void Disp( ); ~Stack( ); }; void Stack: :Push( )

51

{ NODE *Temp; Temp=new NODE; if (!Temp) cout <<Overflow\n; else { cout << Data:; cin >> Temp Data; Temp Next = Top; Top=Temp; } } void Stack: :Pop( ) { if (Top != NULL) { NODE *Temp = Top; cout << TopData << Deleted..\n; Top = Top Next; delete Temp; } else cout << Underflow\n;

52

} void Stack : : Disp( ) { if (Top==NULL) cout <<Stack is empty\n; else { NODE *Temp = Top; while (Temp != NULL) { cout<<Temp Data<<endl; Temp=Temp Next; } } } Stack: : ~Stack( ) { while (Top!=NULL) {NODE *Temp=Top; Top=Top Next; delete Temp; } } void main( ) //Destructor Function

53

{ int ch; char c; stack ob; int n; do{ clrscr( ); cout<<\n #######MENU#######; cout << \n1. PUSH INTO STACK.; cout << \n 2. POP INTO STACK.; cout << \n 3. DISPLAY THE STACK.; cout << \n 4. EXIT.; cout << \n ENTER YOUR CHOICE?: cin >> ch; switch(ch) { case 1 : cout << \n Enter a number?; ob.Push(); break; case 2: case 3: ob.Pop( );break; ob.Display()break;

default: cout<<Wrong choice; } cout << \ndo you want to continue?; cin >> c;

54

}while((c= =y) || (c = = Y )); getch( ); }

Output: Push:

Pop:

55

Display:

56

QUEUES:
Q: Write a program to demonstrate array based queue?
# include<iostream.h> # include <conio.h> # include<process.h> class queue{ int q[20]; int front, rear, max; public: queue( ) { front = rear = -1; max = 29; void qinsert( ); void qdelete( ); void display( ); int isfull( ) { return (max = = rear); } int isempty( ) { return (front = = -1); } } void queue : : qinsert( ) { if (isfull ( ) ) cout << \nQUEUE OVERFLOW; else { int elt; if (isempty( ) ) cout << \nQUEUE UNDERFLOW; else { int elt = q[front]; cout << \n << elt << REMOVED FROM QUEUE; if (front = = rear) front = rear = -1; else front ++; } } void queue : : display( ) {for (int i = front; i < = rear; i++) cout << \n << q[i];} void main( ) {int ch;char c;queue ob; do{ clrscr( ); cout << \n#########MENU########; cout << \n1. ADD ELEMENT TO QUEUE; cout << \n 2. REMOVE ELEMENT FROM QUEUE; cout << \n 3. DISPLAY THE QUEUE; cout << \n 4. EXIT; cout << \n ENTER YOUR CHOICE? ; cin >> ch; switch(ch){ case 1 : ob.qinsert( );break;
57

cout << \n ENTER NUMBER: ; cin >> elt; if ( isempty ( ) ) front = rear = 0; else rear ++; q[rear] = elt; }} void queue : : qdelete ( ){

A case 2 : ob.qdelete( );break; case 3 : ob.display( );break; default : cout << \n WRONG CHOICE ! ! ! ; } cout << \n \n DO YOU WANT TO CONTINUE?; cin >> c; } while ( ( c = = y ) | | ( c = = Y ) ); getch ( );}

Output: Insert:

Delete:

58

Display:

59

Q: Write a program to demonstrate linked list queue?


# include<iostream.h> # include <conio.h> # include<process.h> struct NODE {int Data; NODE *Next; }; class Queue { NODE *Rear, *Front; Public: Queue ( ) {Rear= NULL; Front=NULL;} void Qinsert( ); void Qdelete( ); void Qdisplay( ); ~Queue( ); }; void Queue: : Qinsert( ) {NODE *Temp; Temp=new NODE; cout<<Data:;
60

cout<<Queue Empty..; } void Queue : :Qdisplay( ) { NODE *Temp=Front; While (Temp!=NULL) { cout<<TempData<<endl; Temp=TempNext; } } Queue: : ~Queue (){ while (Front!=NULL) { NODE *Temp= Front; Front = FrontNext; delete Temp; } } void main( ) { Queue ob; char Ch;

cin>>Temp->Data; Temp->Next=NULL; If (Rear ==NULL) {Rear=Temp; Front=Temp; } else { Rear->Next = Temp; Rear=Temp; } } void Queue: : Qdelete( ) { if (Front!=NULL) { NODE *Temp=Front; cout<<Front->Data<<Deleted\n; Front=Front->Next; Delete Temp; If (Front ==NULL) Real=NULL; } else

do { clrscr( ); cout << \n#########MENU########; cout << \n1. ADD ELEMENT; cout << \n 2. REMOVE ELEMENT ; cout << \n 3. DISPLAY THE QUEUE; cout << \n 4. EXIT; cout << \n ENTER YOUR CHOICE? ; cin >> ch; switch(ch) {case 1 : ob.qinsert( );break; case 2 : ob.qdelete( );break; case 3 : ob.display( );break; default : cout << \n WRONG CHOICE ! ! ! ; } cout<<\n\nDO YOU WANT TO CONTINUE?; cin >> ch; } while ( ( c = = y ) | | ( c = = Y ) ); }while (ch!=Q); }

Output: Insert:

61

Delete:

Display:

62

Q: Write a program to demonstrate circular queue?


# include<iostream.h> # include <conio.h> #include<stdio.h> # include<process.h> struct employee { int emp_no; char emp_name[25]; float salary; }; void read(employee &s) { cout<<\nENTER the employee number: :; cin>>s.emp; cout<<\nENTER the employee number: :; gets(s.emp_name); cout<< nENTER THE SALARY: :; cin>>s.salary; } void disp(employee &s)
63

{cout<<\n\NEMPLOYEE NO:<<s.emp_no; cout<<\nEMPLOYEE NAME:; puts(s.emp_name); cout<<SALARY:<<s.salary; } class queue { employee a[20]; int front, rear, max; public: queue( ) { front=rear=-1; max=19; } void add( ); void del( ); void display( ); int isfull( ); int isempty( ); }; int queue: :isfull( ) { if((front= =rear+1) | | (front= =0) &&(rear= =max))) return 1; else return 0; } void queue: :add( ) { if(isfull( )) cout<<\n queue overflow! !; else { employee temp; read(temp); if(isempty( )) front = rear = 0; else if(rear= =max) rear=0; else rear++;
64

a[rear]=temp; } } void queue: :del( ) { if(isempty( )) cout<<\n Queue underflow! !; else { employee temp; temp=a[front]; cout<<\nEmployee number<<temp.emp_no<<remove from queue\n; if(front= = rear) front=rear=-1; else if(front= = max) front=0; else front++; } } void queue : :display( ) { if(isempty( )) cout<<\n Queue underflow!! else { if(front<=rear) { for(int i = front; i<= rear;i++) disp(a[i]); } else { for(int i=front;i<=max;i++) disp(a[i]); for(i=0;i<=rear;i++) disp(a[i]); } } } void main( ) {int ch;
65

char c; queue ob; do { clrscr( ); cout << \n#######MENU######; cout << \n1.ADD A RECORD IN QUEUE.; cout << \n2.REMOVE A RECORD FROM QUEUE.; cout << \n3.DISPLAY THE QUEUE.; cout << \n4.QUIT.; cout << \n ENTER YOUR CHOICE : :; cin >> ch; switch (ch) { case 1 : ob.add( );break; case 2 : ob.del( );break; case 3 :ob.display( );break; default : cout << \n\n WRONG CHOICE !!!; } cout << \n\n\n DO YOU WANT TO CONTINUE?; cin >> c; } while ( ( c = = y ) | | ( c = = Y ) ); getch( ); }

Output: Insert:

66

Delete:

Display:

67

You might also like