Professional Documents
Culture Documents
Stacks&queues
Stacks&queues
numbers.
#include<iostream.h>
void push(int s1[5],int &t)
{
if(t==4)
cout<<"overflow";
else
{
cout<<"\n enter the element to be inserted \n";
int n;
cin>>n;
t=t+1;
s1[t]=n;
}
}
void pop(int s1[5],int &t)
{
if(t==-1)
cout<<"underflow";
else
{
cout<<"the element to be deleted : "<<s1[t]<<endl;
t=t-1;
}
}
void traversal(int s1[5],int t)
{
if(t==-1)
cout<<"Empty stack";
else
{
for(int i=t;i>=0;i--)
cout<<s1[i]<<"->";
cout<<endl;
}
}
void main()
{
int stack[5],top=-1;
char ch='y';
int choice;
do
{
cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{
case 1: push(stack,top);
break;
case 2: pop(stack,top);
break;
case 3: traversal(stack,top);
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
Write a function in C++ to perform Push , Pop, Traversal operation on a dynamically allocated stack
containing real numbers(using class).
#include<iostream.h>
struct node
{
float data;
node *next;
};
class stack
{
node *top;
public:
stack()
{top=NULL;}
void push();
void pop();
void traversal();
};
void stack::push()
{
node * temp;
temp=new node;
cout<<"\n enter the data \n";
cin>>temp->data;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{temp->next=top;
top=temp;}
}
void stack::pop()
{ node *temp;
if(top==NULL)
cout<<"underflow";
else
{ temp=top;
cout<<"\n the element deleted is \n"<<temp->data;
top=top->next;
temp->next=NULL;
delete temp; }}
void stack::traversal()
{ node *temp;
temp=top;
if(temp==NULL)
cout<<\n stack is empty \n;
else
{
cout<<"\n The stack contents are \n";
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
} }}
void main()
{
char ch='y';
int choice;
stack s1;
do
{ cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{ case 1: s1.push();
break;
case 2: s1.pop();
break;
case 3: s1.traversal();
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
(without class)
#include<iostream.h>
struct node
{
float data;
node *next;
} *top=NULL;
void push()
{
node * temp;
temp=new node;
cout<<"\n enter the data \n";
cin>>temp->data;
temp->next=top;
top=temp;
}
void pop()
{
node *temp;
if(top==NULL)
cout<<"underflow";
else
{ temp=top;
cout<<"\n the element deleted is \n"<<temp->data;
top=top->next;
temp->next=NULL;
delete temp;
}
}
void traversal()
{
node *temp;
temp=top;
if(temp==NULL)
cout<<"\n stack is empty \n";
else
{
cout<<"\n The stack contents are \n";
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
} }}
void main()
{
char ch='y';
int choice;
do
{ cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{ case 1: push();
break;
case 2: pop();
break;
case 3: traversal();
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
Write a function in C++ to perform Insert, delete and traversal operation in a static queue containing
integer elements.
#include<iostream.h>
const int size=5;
void insert(int q1[5],int &f,int &r)
{
if(r==size-1)
cout<<"overflow";
else
{
cout<<"\n enter the element to be inserted \n";
int n;
cin>>n;
if(r==-1)
f=r=0;
else
r=r+1;
q1[r]=n;
}
}
void delet(int q1[5],int &f,int &r)
{
if(f==-1)
cout<<"underflow";
else
{
cout<<"the element to be deleted : "<<q1[f]<<endl;
if(f==r)
f=r=-1;
else
f=f+1;
}
}
void traversal(int q1[5],int f,int r)
{
if(f==-1)
cout<<"Empty Queue";
else
{
for(int i=f;i<=r;i++)
cout<<q1[i]<<"->";
cout<<endl;
}
}
void main()
{ int queue[size],front=-1,rear=-1;
char ch='y';
int choice;
do
{ cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{
case 1: insert(queue,front,rear);
break;
case 2: delet(queue,front,rear);
break;
case 3: traversal(queue,front,rear);
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
Write a function in C++ to perform Insert, delete and traversal operation in a dynamically allocated queue
containing names of students(with class).
#include<iostream.h>
#include<stdio.h>
struct node
{ char name[20];
node *next;
};
class queue
{
node *front,*rear;
public:
queue()
{front=rear=NULL;}
void insert();
void delet();
void traversal();
};
void queue::insert()
{
node * temp;
temp=new node;
cout<<"\n enter the Name \n";
gets(temp->name);
temp->next=NULL;
if(rear==NULL)
front=rear=temp;
else
{ rear->next=temp;
rear=temp;
}}
void queue::delet()
{
node *temp;
if(front==NULL)
cout<<"underflow";
else
{ temp=front;
cout<<"\n the element deleted is \n"<<temp->name;
if(front==rear)
front=rear=NULL;
else
front=front->next;
delete temp;
}}
void queue::traversal()
{
node *temp;
temp=front;
if(temp==NULL)
cout<<"\n queue is empty \n";
else
{ cout<<"\n The queue contents are \n";
while(temp!=NULL)
{
cout<<temp->name<<"\t";
temp=temp->next;
}}}
void main()
{
char ch='y';
int choice;
queue q1;
do
{ cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{ case 1: q1.insert();
break;
case 2: q1.delet();
break;
case 3: q1.traversal();
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
(without class)
#include<iostream.h>
#include<stdio.h>
struct node
{ char name[20];
node *next;
}*front=NULL,*rear=NULL;
void insert()
{
node * temp;
temp=new node;
cout<<"\n enter the Name \n";
gets(temp->name);
temp->next=NULL;
if(rear==NULL)
front=rear=temp;
else
{ rear->next=temp;
rear=temp;
}}
void delet()
{
node *temp;
if(front==NULL)
cout<<"underflow";
else
{ temp=front;
temp->next=NULL;
cout<<"\n the element deleted is \n"<<temp->name;
front=front->next;
if(front==NULL)
rear==NULL;
delete temp;
}}
void traversal()
{
node *temp;
temp=front;
if(temp==NULL)
cout<<"\n queue is empty \n";
else
{ cout<<"\n The queue contents are \n";
while(temp!=NULL)
{
cout<<temp->name<<"\t";
temp=temp->next;
}}}
void main()
{
char ch='y';
int choice;
do
{ cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{ case 1: insert();
break;
case 2: delet();
break;
case 3: traversal();
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
Circular Queue: Are the queues implemented in circle form rather than a straight line. It overcomes the
disadvantage of static queue.
Write a function in C++ to perform Insert, delete and traversal operation in a static circular queue
containing integer numbers.
#include<iostream.h>
const int size=5;
void insert(int q1[5],int &f,int &r)
{
if((f==0 && r==(size-1))||(f==r+1))
{cout<<f<<r<<endl;
cout<<"overflow";}
else
{
cout<<"\n enter the element to be inserted \n";
int n;
cin>>n;
if(r==-1)
f=r=0;
else if (r==size-1)
r=0;
else
r=r+1;
q1[r]=n;
}
}
void delet(int q1[5],int &f,int &r)
{
if(f==-1)
cout<<"underflow";
else
{
cout<<"the element to be deleted : "<<q1[f]<<endl;
if(f==r)
f=r=-1;
else if (f==size-1)
f=0;
else
f=f+1;
}
}
void traversal(int q1[5],int f,int r)
{
if(f==-1)
cout<<"Empty Queue";
else
{
if(f<=r)
{
for(int i=f;i<=r;i++)
cout<<q1[i]<<"->";
cout<<endl;
}
else
{ int i=f;
while(i<=size-1)
cout<<q1[i++]<<"->";
i=0;
while(i<=r)
cout<<q1[i++]<<"->";
cout<<endl;
}
}
}
void main()
{
int queue[size],front=-1,rear=-1;
char ch='y';
int choice;
do
{
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. traversal"<<endl;
cin>>choice;
switch(choice)
{
case 1: insert(queue,front,rear);
break;
case 2: delet(queue,front,rear);
break;
case 3: traversal(queue,front,rear);
break;
}
cout<<"\n Want to continue";
cin>>ch;
}while(ch=='y');
}
Deque(double ended queues): are the refined queues in which elements can be added or removed at either end
but not in the middle. There are two types of deque:
(a) Input restricted deque: It allows insertions at only one end but allows deletions at both ends of the list.
(b) Output restricted Deque: It allows deletions at only one end of the list but allows insertions at both ends of
the list.
Insertion in a sorted list
node * insertat(node *first)
{
node *newptr,*temp,*back;
newptr=new node;
if(!newptr)
{
cout<<"Not sufficient Memory"; exit(1);
}
cout<<"Enter the age to be inserted\n";
cin>>newptr->data;
newptr->next=NULL;
//find the appropriate position
if(!first) //list is empty
first=newptr;
else if(newptr->data<first->data)
{ temp=first;
first=newptr;
newptr->next=temp;
}
else
{//find the exact position
temp=first;
while(temp)
{
if(newptr->data>temp->data)
{ back=temp;
temp=temp->next;
}
else
{ back->next=newptr;
newptr->next=temp;
break;
}
}
if(!temp)
back->next=newptr;
}
return(first); }
Console I/O Functions:
These are the library functions available for I/O. Its function is to receive input from keyboard and write output
to VDU. Console I/O functions can be further classified into two categories: formatted and unformatted console
I/O functions.
Formatted console I /O functions allow the input read from the keyboard or the output displayed on the VDU to
be formatted as per our requirements.
Unformatted Functions
Type Input Output
char getch( ) putch( )
getche( ) putchar( )
getchar( )
string gets( ) puts( )
getch ( ) and getche( )- We often want a function that will read a single character the instant it is typed without
waiting for the Enter key to be hit. getch( ) and getche( ) are two functions which server this purpose. The e in
getche( ) function means it echoes(displays) the character that you have typed to the screen. getch( ) just returns
the character read from the keyboard. getch reads a single character directly from the keyboard, without echoing
to the screen.Include the header file <conio.h> for both of them.
getchar( )- Gets character from stdin. It works similarly and echoes the character that you typed on the screen,
but requires enter key to be pressed following the character you have typed. Include <stdio.h>
gets( ) and puts( )- gets( ) receives a string from the keyboard (including white spaces). It is terminated when an
enter key is hit. gets( ) gets a newline (\n) terminated string of characters from the keyboard and replaces the \n
with a \0.
puts( ) outputs the string to the screen. Include<stdio.h> for both of them.
e.g.
puts("puja gupta");
puts("srishti gupta");
Output:
puja gupta
srishti gupta
cout.write("vivek gupta",11);
cout.write("manish garg",11);
Output:
vivek guptamanish garg
iostream.h stdio.h conio.h
Getting Character
cin.get(ch) char ch=getchar() char ch=getch()
cout.put(ch) putchar(ch) putch(ch)
char ch=getc(stdin) char ch=getche()
putc(ch,stdout)
Getting String
cin.getline(name,80,$) gets(name)
cin.getline(name,80) puts(name)
cout.write(name,80)
Database Notes for class XII D
Degree: Degree of a table is total number of attributes in a relation.
Primary Key: An attribute or set of attributes which are used to uniquely identify a tuple is known as Primary
Key.
Candidate Key: If a table has more than one such attributes which identify a tuple uniquely than all such
attributes are known as candidate Keys.
Relation: A relation is a table having atomic values, unique rows and columns. i.e. Data arranged in rows and
columns and having certain properties.
Foreign key: Non-key attribute , whose values are derived from the primary key of some other table, is known as
foreign key in its current table..
Alternate Key: A candidate key that is not the primary key is termed as alternate key.
Domain: It is a pool of values from which the actual values appearing in a given column are drawn.
Data Independence: The ability to modify a scheme definition in one level without affecting a scheme definition
in the next higher level is called Data Independence.
There are two levels of data independence.
Physical Data Independence: refers to the ability to modify the scheme followed at the physical level without
affecting the scheme followed at the conceptual level. That is the application programs remain the same even
though the scheme at physical level gets modified (to improve performance of the system).
Logical Data Independence: refers to the ability to modify the conceptual scheme without causing any changes
in the schemes followed at view levels. Modifications are necessary whenever logical structures of the database
get altered because of some unavoidable reasons(for e.g. adding one field in the table)
It is more difficult to achieve logical data independence than the physical data independence. The reason being
that the application programs are heavily dependent on the logical structure of the database.
Conceptual Level
Physical Level
Relation Algebra: It is a collection of operations on relations. Operations include select, project, cartesian
product, union, se difference, set intersection, natural join, division etc. Select and project are unary operations
since they operate on one relation. The others are binary operations.
(a) Select Operation: It selects tuples (horizontal subset) from a relation that satisfy a given condition. Denoted
by symbol e.g. price > 14.00 (items)
From table items, select the tuples satisfying the condition price >14.00.
(b) Project Operation: It yields a vertical subset of a given relation. It lets you select specified attributes in a
specified order. The result is also a relation, the duplicating tuples are automatically removed. It is denoted by .
e.g supp_name , city (suppliers)
from table suppliers project the attributes supp_name and city
(c) Cartesian Product: It is a binary operation and is denoted by x. The Cartesian product of two relations A and
B is A x B, it yields a new relation which has a degree (no. of attributes) equal to the sum of the degrees of the
two relations operated upon. The no. of tuples(cardinality) of the new relation is the product of the number of
tuples of the two relations operated upon. The cartesian product of two relations yields a relation with all possible
combinations of the tuples of the two relations operated upon.
(d) Union Operation: It is a binary operation that requires two operands. It produces the third relation that
contains tuples from both the operand relations. It is denoted by . For union to be valid, the following two
conditions must be satisfied by the two operands A and B.
(i) The relations A and B must be of same degree. That is, they must have the same number of attributes.
(ii) The domains of the ith attributes of A and the ith attribute of B must be the same.
(e) Set Difference Operation: It is denoted by symbol (minus) allows us to find tuples that are in one relation
but not in another. The expression A B results in a relation containing those tuples in A but not in B.
(f) Set Intersection operation- It finds tuples that are common to the two operand relations. It is denoted by .
i.e AB will yield a relation having tuples common to A and B.
Views
A view is a virtual table that does not really exist in its own right but is instead derived from one or more
underlying base table(s). The view is a kind of table whose contents are taken upon other tables depending upon a
given query condition. No stored file is created to store the contents of a view rather its definition is stored only.
Use: It gives a way to people to access some not all of the information in a table.
DDL(Data Definition Language) It provides commands for defining relation schemas, deleting relations, creating
indexes and modifying relation schemas. E.g Create Table, Drop table
DML(Data manipulation language) It includes a query language to insert, delete and modify tuples in the
database and perform complex queries on these tables.
.
Difference between DDL and DML: DML is used to put values and manipulate them in tables and other
database objects and DDL is used to create tables and other database objects.
Difference between Drop table and Drop View: Drop Table removes a table from a database. Drop view
removes a view from the database but table remains.
Difference between WHERE and HAVING clause: The HAVING clause places conditions on groups in contrast
to WHERE clause, which places conditions on individual rows. In Having clause we can use aggregate functions
whereas in WHERE clause we cannot use aggregate functions.
Stdio.h String.h Math.h Stdlib.h ctype.h
getc, gets strten, strlwr, abs, pow10 exit, atoi, atol, tolower, toupper,
get, putc, strupr, strcpy, sqrt, log10, atof, free, isalnum, isascii,
putchar, putw, strcat, strchr, tan, tanh, itoa ,qsort, isdigit, iscntrl,
getw , remove, strcmp, strcmpi frexp, atan, randomize, isgraph, islower,
rename, fflush, ceil, cabs, rand, div, ispunct, isupper,
fopen, fclose, cos, exp, abort toascii, tolower,
remind, fread, floor, log, toupper
fscanf, fseek, poly, pow, sin
ftell, fwrite
#include<iostream.h>
const int size=50;
void get_array(int a[],int &n)
{ cout<<"\n enter the number of elelments <=50\n ";
cin>>n;
cout<<"\n enter the elements \n";
for(int i=0;i<n;i++)
cin>>a[i];
}
void display_array(int a[],int n)
{
cout<<" \n The elements are: \n";
for(int i=0;i<n;i++)
cout<<a[i]<<'\t';
}
void binary_search(int a[],int n)
{
int beg=0,last=n-1,mid,elem,found=0;;
cout<<"\n enter the elemnet to be searched \n";
cin>>elem;
while(beg<=last)
{
mid=(beg+last)/2;
if(a[mid]==elem)
{
cout<<"element found at "<< (mid+1) <<" positon"<<endl;
found=1;
break;
}
else
{
if(elem>a[mid])
beg=mid+1;
else
last=mid-1;
}
}
if(found==0)
cout<<"\n element not found \n";
}
void insert_array(int a[],int &n)
{
if(n==size)
cout<<"\n Sorry! no more elements can be added \n";
else
{
cout<<"\n Enter the element to be inserted in a sorted array \n";
int elem;
cin>>elem;
for(int i=0;i<size;i++)
if(elem<a[i])
break;
int pos=i;
int last=n-1;
while(last>=pos)
{
a[last+1]=a[last];
last--;
}
a[pos]=elem;
n++;
}
}
void delete_array(int a[],int &n)
{
int elem,pos,found=0;;
cout<<"\n enter the element to be deleted \n";
cin>>elem;
for(int i=0;i<n;i++)
{ if(elem==a[i])
{ found=1;
pos=i;
break;
}
}
if(found==0)
cout<<"\n ellement to de deleted not found \n";
else
{
while(pos<(n-1))
{
a[pos]=a[pos+1];
pos++;
}
n--;
}}
void main()
{
int a[size],n,choice;
char ch;
do
{ cout<<"\n 1. Get the array \n";
cout<<"\n 2. Binary search \n";
cout<<"\n 3. Insert an element \n";
cout<<"\n 4. delete an element \n";
cout<<"\n 5. Traversal \n";
cout<<"Enter choice: ";
cin>>choice;
switch(choice)
{
case 1:get_array(a,n);
break;
case 2:binary_search(a,n);
break;
case 3:insert_array(a,n);
break;
case 4:delete_array(a,n);
break;
case 5: display_array(a,n);
break;
}
cout<<"\n enter choice (y):";
cin>>ch;
}while(ch=='y');}