Professional Documents
Culture Documents
Mohammad Haseebuddin Mohammad Haseebuddin Mohammad Haseebuddin Mohammad Haseebuddin
Mohammad Haseebuddin Mohammad Haseebuddin Mohammad Haseebuddin Mohammad Haseebuddin
INFORMATION SYSTEMS
JAZAN UNIVERSITY
LAB MANUAL
MOHAMMAD HASEEBUDDIN
1
I. Pointers
A pointer is a variable that is used to store a memory address. The address is the location of the variable
in the memory. Pointers help in allocating memory dynamically.
type *variable_name;
type is the base type of the pointer and variable_name is the name of the variable of the pointer. For
example,
int *x;
Pointer Operators
There are two important pointer operators such as ‘*’ and ‘&’. The ‘&’ is a unary operator. The unary
operator returns the address of the memory where a variable is located. For example,
int x*;
int c;
x=&c;
variable x is the pointer of the type integer and it points to location of the variable c. When the
statement
x=&c;
is executed, ‘&’ operator returns the memory address of the variable c and as a result x will point to the
memory location of variable c.
The ‘*’ operator is called the indirection operator. It returns the contents of the memory location
pointed to. The indirection operator is also called deference operator. For example,
int *x;
int c=100;
x=&c;
p=*x;
2
variable x is the pointer of integer type. It points to the address of the location of the variable c. The
pointer x will contain the contents of the memory location of variable c. It will contain value 100. When
statement
p=*x;
is executed, ‘*’ operator returns the content of the pointer x and variable p will contain value 100 as the
pointer x contain value 100 at its memory location.
void main ()
int *x, p;
int c=200;
x=&c;
p=*x;
cout << " The address of the memory location of x : " << x << endl;
cout << " The contents of the pointer x : " << *x << endl;
cout << " The contents of the variable p : " << p << endl;
getch();
Output:
3
Program - 1
Write a program in C++ to enter age of 5 students and calculate sum of age using pointers.
#include<iostream>
#include<conio.h>
void main()
int age[5];
int *p;
int sum=0,i;
char yes='y';
p=age;
for(i=0;i<5;i++)
sum=sum+*p;
p++;
p=age;
cout << "The sum of the ages" << sum << endl;\
cout << "The age of the last student is : " << *(p + 4) << endl;
getch();
4
II. Structures
A structure is a form of compound data type. It is an aggregate of data items of different data types. It
acts as a cluster of variables of different data types. The variables that form the structure are called
members. For example, you can have a structure of type employee which stores name, age,
qualification, and telephone no and other information about the employee under one data type.
struct Employee
char name[50];
int age;
char quali[70];
int teleno;
char info[80];
};
The keyword struct declares that Employee is a structure. The type name of the structure is Employee.
The variables declared are the members of the structure Employee. Every variable of type Employee will
contain members name, age, quali, teleno and info. The declaration of the structure is terminated by a
semicolon. The data types of the members can be of any type except the type of the structure being
defined. The amount of memory need to store a structure type is the total of the memory required by
the data types of the members of the structure.
5
Program – 2
Write a program in Visual C++ which gets and display the information of employee using
structures.
#include<iostream>
using namespace std;
struct Employee
char name[50];
int age;
char quali[70];
int teleno;
char info[80];
}emp1;
int main ()
Employee emp2;
cout << endl << "Enter the age of the employee" << endl;
cout << endl << "Enter the qualification of the employee" << endl;
cout << endl << "Enter the telephone no of the employee" << endl;
cout << endl << "Enter other information about the employee" << endl;
6
cin >> emp1.info;
emp2=emp1;
cout << endl << "The name of the employee : " << emp2.name << endl;
cout << "The age of the employee : " << emp2.age << endl;
getch();
Output :
7
III. Binary Trees
A binary tree is either:
• An empty tree, or
• A tree consisting of a root node and at most two non-empty binary subtrees.
Eg:
Program – 3
Write Visual C++ program to insert elements into a binary tree and display the preorder
traversal of the binary tree.
#include<iostream>
# include <conio.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}*tree;
int count=1;
void main()
{
int n,ele;
cout<<"\n Enter the no. of elements :";
8
cin>>n;
cout<<"\nEnter the elements::";
for(int i=1;i<=n;i++)
{
cin>>ele;
tree=insert(tree,ele);
}
cout<<"\nThe pre oder taversal of the binary tree is :";
preorder(tree);
getch();
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
//Function for preorder traversal of the binary tree.
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
9
Program – 4
Write Visual C++ program to insert elements into a binary tree and display the inorder
traversal of the binary tree.
#include<iostream>
# include <conio.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}*tree;
int count=1;
void main()
{
int n,ele;
cout<<"\n Enter the no. of elements :";
cin>>n;
cout<<"\nEnter the elements::";
for(int i=1;i<=n;i++)
{
cin>>ele;
tree=insert(tree,ele);
}
cout<<"\nThe in oder taversal of the binary tree is :";
inorder(tree);
getch();
10
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
count++;
}
else if(count%2==0)
tree->left=insert(tree->left,ele);
else
tree->right=insert(tree->right,ele);
return(tree);
}
//Function for inorder traversal of the binary tree.
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
Program – 5
Write Visual C++ program to insert elements into a binary tree and display the postrder
traversal of the binary tree.
#include<iostream>
# include <conio.h>
using namespace std;
struct node
{
int data;
11
node *left;
node *right;
}*tree;
int count=1;
void main()
{
int n,ele;
cout<<"\n Enter the no. of elements :";
cin>>n;
cout<<"\nEnter the elements::";
for(int i=1;i<=n;i++)
{
cin>>ele;
tree=insert(tree,ele);
}
cout<<"\nThe post oder taversal of the binary tree is :";
postorder(tree);
getch();
12
tree->right=insert(tree->right,ele);
return(tree);
}
//Function for postorder traversal of the binary tree.
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
13
IV. Binary Search Tree
A binary search tree (BST) is a binary tree that is empty or that satisfies the BST ordering property:
1. The key of each node is greater than each key in the left subtree, if any, of the node.
2. The key of each node is less than each key in the right subtree, if any, of the node.
Examples:
Program – 6
Write Visual C++ program to insert elements into a binary search tree and search an element.
#include<iostream>
# include <conio.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}*tree;
void main()
{
int n,ele,key;
cout<<"\n Enter the no. of elements :";
cin>>n;
cout<<"\nEnter the elements::";
for(int i=1;i<=n;i++)
14
{
cin>>ele;
tree=insert(tree,ele);
}
cout<<"\nEnter the search key element";
cin>>key;
if(search(tree,key)==1)
cout<<"Element found";
else
cout<<"Element not found";
getch();
}
node *insert(node *tree,int ele)
{
if(tree==NULL)
{
tree=new node;
tree->left=tree->right=NULL;
tree->data=ele;
}
else
{
if(ele < tree->data)
tree->left = insert(tree->left,ele);
else
tree->right = insert(tree->right,ele);
}
return(tree);
}
15
}
V. Graphs
A simple graph G = (V, E) consists of a non-empty set V, whose members are called the vertices
of G, and a set E of pairs of distinct vertices from V, called the edges of G.
Graphs Representation :
An array or a linked list can be used for representing vertices of the graph.
Program – 7
Write a Visual C++ program to create a graph using adjacency matrix representation and
implement the breadth first search traversal.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rear,v,visit[10],visited[10];
void main()
{
int m;
cout <<"Enter the no. of vertices";
cin >> n;
cout <<"Enter the no. of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
16
}
Program – 8
Write a Visual C++ program to create a graph using adjacency matrix representation and
implement the Depth first search traversal.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
void main()
{
int m;
cout <<"\n Enter no of Vertices: ";
cin >> n;
cout <<"\n Enter no of Edges: ";
cin >> m;
17
cout <<"\n EDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"\n Enter Initial Vertex: ";
cin >>v;
cout <<"\n The Depth First Search Traversal of verticies are \n";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
getch();
}
Program – 9
Write a Visual C++ program for creation of a graph using adjacency list representation.
#include<conio.h>
#include<iostream>
#include<stdlib.h>
18
{
int data,status;
};
};
int main()
create();
getch();
int dat,flag=0;
start=NULL;
while(1)
19
{
cin>>dat;
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
start=p;
q=p;
flag++;
else
q->next=p;
q=p;
p=start;
while(p!=NULL)
20
cout<<"Enter the links to "<<p->data<<" (0 to end) : ";
flag=0;
while(1)
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
p->adj=k;
l=k;
flag++;
else
l->adj=k;
l=k;
q=start;
while(q!=NULL)
if(q->data==dat)
21
k->next=q;
q=q->next;
p=p->next;
cout<<"-------------------------";
return;
Program – 10
#include<conio.h>
#include<iostream>
#include<stdlib.h>
int data,status;
};
22
struct link // Structure for adjacency list
};
int main()
int choice;
create();
bfs();
getch();
return 0;
int dat,flag=0;
start=NULL;
while(1)
cin>>dat;
23
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
start=p;
q=p;
flag++;
else
q->next=p;
q=p;
p=start;
while(p!=NULL)
flag=0;
24
while(1)
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
p->adj=k;
l=k;
flag++;
else
l->adj=k;
l=k;
q=start;
while(q!=NULL)
if(q->data==dat)
k->next=q;
q=q->next;
25
}
p=p->next;
cout<<"-------------------------";
return;
void bfs()
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
p->status=0;
p=p->next;
p=start;
qu[0]=p->data;
p->status=1;
while(1)
if(qu[j]==0)
break;
p=start;
26
while(p!=NULL)
if(p->data==qu[j])
break;
p=p->next;
k=p->adj;
while(k!=NULL)
q=k->next;
if(q->status==0)
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
k=k->adj;
j++;
j=0;
cout<<"---------------------------";
27
while(qu[j]!=0)
cout<<qu[j]<<" ";
j++;
getch();
return;
Program – 11
#include<conio.h>
#include<iostream>
#include<stdlib.h>
int data,status;
};
28
{
};
int main()
int choice;
dfs();
getch();
return 0;
int dat,flag=0;
start=NULL;
while(1)
cin>>dat;
if(dat==0)
break;
29
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
start=p;
q=p;
flag++;
else
q->next=p;
q=p;
p=start;
while(p!=NULL)
flag=0;
while(1)
30
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
p->adj=k;
l=k;
flag++;
else
l->adj=k;
l=k;
q=start;
while(q!=NULL)
if(q->data==dat)
k->next=q;
q=q->next;
31
p=p->next;
cout<<"-------------------------";
return;
void dfs()
int stack[25],top=1;
cout<<"---------------------------";
p=start;
while(p!=NULL)
p->status=0;
p=p->next;
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
if(stack[top]==0)
break;
32
p=start;
while(p!=NULL)
if(p->data==stack[top])
break;
p=p->next;
cout<<stack[top]<<" ";
top--;
k=p->adj;
while(k!=NULL)
q=k->next;
if(q->status==0)
{ top++;
stack[top]=q->data;
q->status=1; }
k=k->adj;
getch();
return;
33