Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

COLLEGE OF COMPUTER SCIENCE AND

INFORMATION SYSTEMS

JAZAN UNIVERSITY

ALGORITHMS & DATA STRUCTURES-


STRUCTURES-2
(COMP -222) LEVEL - 6

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.

The general form of declaring pointer is:-

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;

x is the variable name and it is the pointer of type integer.

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.

Here is a program which illustrates the working of pointers.

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++)

cout << "Enter the age of a student" << endl;

cin >> *p;

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.

A structure can be declared as:-

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 << "Enter the name of the employee" << endl;

cin >> emp1.name;

cout << endl << "Enter the age of the employee" << endl;

cin >> emp1.age;

cout << endl << "Enter the qualification of the employee" << endl;

cin >> emp1.quali;

cout << endl << "Enter the telephone no of the employee" << endl;

cin >> emp1.teleno;

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;

node *insert(node *tree,int ele);


void preorder(node *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;

node *insert(node *tree,int ele);


void inorder(node *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;

node *insert(node *tree,int ele);


void postorder(node *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();

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

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.

Thus, each key in a BST is unique.

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;

node *insert(node *tree,int ele);


int search(node *tree,int key);

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

int search(node *tree,int key)


{
int flag=0;
if(tree!=NULL)
{
if(key==tree->data)
return(1);
else
if(key < tree->data)
search(tree->left,key);
else
search(tree->right,key);
}

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.

And edges can be represented by the following three methods.

• Adjacency Matrix (Two-dimensional array)

• Adjacency List (One-dimensional array of linked lists)

• Linked List (one list only)

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
}

cout <<"Enter the initial vertex: ";


cin >>v;
cout <<"Visitied vertices are : \n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rear++]=j;
}
v=qu[front++];
cout<<" "<< v;
k++;
visit[v]=0; visited[v]=1;
}
getch();
}

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>

using namespace std;

void create(); // For creating a graph

struct node // Structure for elements in the graph

18
{

int data,status;

struct node *next;

struct link *adj;

};

struct link // Structure for adjacency list

struct node *next;

struct link *adj;

};

struct node *start,*p,*q;

struct link *l,*k;

int main()

create();

getch();

void create() // Creating a graph

int dat,flag=0;

start=NULL;

cout<<"Enter the nodes in the graph(0 to end): ";

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

Write Visual C++ program for implementation of Breadth first search(BFS)

#include<conio.h>

#include<iostream>

#include<stdlib.h>

using namespace std;

void create(); // For creating a graph

void bfs(); // For Breadth First Search(BFS) Traversal.

struct node // Structure for elements in the graph

int data,status;

struct node *next;

struct link *adj;

};

22
struct link // Structure for adjacency list

struct node *next;

struct link *adj;

};

struct node *start,*p,*q;

struct link *l,*k;

int main()

int choice;

create();

bfs();

getch();

return 0;

void create() // Creating a graph

int dat,flag=0;

start=NULL;

cout<<"Enter the nodes in the graph(0 to end): ";

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)

cout<<"Enter the links to "<<p->data<<" (0 to end) : ";

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<<"Breadth First Search Results";

cout<<"---------------------------";

27
while(qu[j]!=0)

cout<<qu[j]<<" ";

j++;

getch();

return;

Program – 11

Write a Visual C++ Program for depth first Travsersal (DFS)

#include<conio.h>

#include<iostream>

#include<stdlib.h>

using namespace std;

void create(); // For creating a graph

void dfs(); // For Deapth First Search(DFS) Traversal.

struct node // Structure for elements in the graph

int data,status;

struct node *next;

struct link *adj;

};

struct link // Structure for adjacency list

28
{

struct node *next;

struct link *adj;

};

struct node *start,*p,*q;

struct link *l,*k;

int main()

int choice;

dfs();

getch();

return 0;

void create() // Creating a graph

int dat,flag=0;

start=NULL;

cout<<"Enter the nodes in the graph(0 to end): ";

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)

cout<<"Enter the links to "<<p->data<<" (0 to end) : ";

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<<"Deapth First Search Results";

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

You might also like