Está en la página 1de 60

1 | Page

(RIPHAH INTERNATIONAL UNIVERSTY FAISALABAD CAMPUS)


Department: Computer Science
Lab Work Manual of Data-Structure

Submitted By:

(BSCS 3rd)

Muhammad Habib

Submitted To:
Mr.Syed Muddasir

Submission Date: January 22, 2015

Data Structure and Algorithms

2 | Page

SR#

Data Structure and Algorithms

Topic

Page #

3 | Page

Display array elements


using
Pointer
Implement the
concepts of
inheritance
The concepts of
Polymorphism

04

Power Function

07

Structure Phone

08

Reverse String

09

Transpose

11

Implement a stack

12

Allocate the memory


dynamically

15

10

Queue

16

11

Singly linked list

19

12

Doubly linked list

28

13

Find Factorial

33

14

Array Values Are


Palindrome
or not

34

Data Structure and Algorithms

05

06

4 | Page

15

Tower of Hanoi

35

16

Bubble Sort

36

17

Merge Sort

38

18

Quick Sort

40

19

Insertion Sort

43

20

Build Max Heap

44

21

Radix Sort

47

22

Binary Search

49

23

Linear Search

51

24

Binary Tree Operation

53

25

Swapping

57

Write a code to display array elements using pointer.


#include "stdafx.h"
#include "iostream"
using namespace std;

Data Structure and Algorithms

5 | Page
int _tmain(int argc, _TCHAR* argv[])
{

int no[5], i;
int *ptr;
cout << " Enter Five Numbers : \n";
for (i = 0; i<5; i++)
{
cout << i + 1 << ":";
cin >> no[i];
}
ptr = no;
cout << " You Entered the Following Numbers :\n";
for (i = 0; i<5; i++)
{
cout << i + 1 << ":";
cout << *ptr++ << "\n";
}
system("pause");
return 0;

Task is to implement the concepts of inheritance


#include "stdafx.h"
#include "iostream"
using namespace std;

Data Structure and Algorithms

6 | Page
class move {
protected: int position;
public:

move() { position = 0; }
int forword()
{
position++;
return 0;
}
int show()
{
cout << "Position = " << position << endl;
return 0;
}

};
class move2 : public move
{
public:
int backword()
{
position--;
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
move2 ob;
ob.show();
ob.forword();
ob.show();
ob.backword();
ob.show();
system("pause");
return 0;
}

Data Structure and Algorithms

7 | Page

Task is to implement the concepts of Polymorphism.


#include "stdafx.h"
#include "iostream"
using namespace std;
class test
{
private:
int n;
public:
int in()
{
cout << "Enter Number ";
cin >> n;
return 0;
}
int out()
{
cout << "The value of n = " << n << endl;
return 0;
}
};

int _tmain(int argc, _TCHAR* argv[])


{
test *ptr;
ptr = new test;

Data Structure and Algorithms

8 | Page
ptr->in();
ptr->out();
system("pause");
return 0;
}

Raising a number n to a power p is the same as multiplying n by


itself P times. Write a
function called power () that takes a double value for n and int value
for p, and returns the
result as double value. Use a default argument of 2 for p, so that
this argument is omitted.
The number will be squared. Write a main function () that gets
values from the user to test
this function.
-------------------------------------------------------------------------------------------------------------------#include "stdafx.h"
#include "iostream"
using namespace std;
int Pow(int x, int power);
int _tmain(int argc, _TCHAR* argv[])

Data Structure and Algorithms

9 | Page
{
int n;
int p;
cout << " Enter The Number : ";
cin >> n;
cout << " Enter The Power To Be Raised : ";
cin >> p;
cout << "\n Result : " << Pow(n, p) << endl;
system("pause");
}
int Pow(int x, int power)
{
int result;
int i;
result = 1;
for (i = 1; i <= power; i++)
{
result = result*x;
}

return(result);

A phone number is consists of three parts. The area code , the


exchange and the number .
write a program that uses a structure to store 3 parts of the number
separately. Call the

Data Structure and Algorithms

10 | P a g e

structure Phone. Create two structure variables of type Phone.


Intialize One, and have the
user to input a number for the other one. Then display both
numbers. Enter your area,
code and exchange number 92-321-7697644 Your number
is .............................
#include "stdafx.h"
#include "iostream"
using namespace std;

struct phone{
int ncode;
int acode;
long number;
};
int _tmain(int argc, _TCHAR* argv[])
{
phone p1;
cout << " Enter National Code : ";
cin >> p1.ncode;
cout << "\n Enter Area Code : ";
cin >> p1.acode;
cout << "\n Enter Phone Number : ";
cin >> p1.number;
cout << "\n Phone Number : +";
cout << p1.ncode << "-" << p1.acode << "-" << p1.number << endl;
system("pause");
return 0;
}

Data Structure and Algorithms

11 | P a g e

Write a function called reverseString() that reverse a string. Use a


for loop that swap first
and last Characters and so on. The string should be passed to
reverseString() as argument.
Enter the string: riphah
#include "stdafx.h"
#include "iostream"
using namespace std;
void reverseString(char name[]);

int _tmain(int argc, _TCHAR* argv[])


{
char name[30];

// get user data


cout << "Enter your name: ";
cin.getline(name, sizeof(name));

cout << "\nYour name reversed is: ";

// function declaration
reverseString(name);
cout << endl;
system("pause");
return 0;
}

Data Structure and Algorithms

The reverse is:hahpir

12 | P a g e

void reverseString(char name[])


{
int nameLength = 0;

//get the length of array


while (name[nameLength] != '\0')
{
++nameLength;
}

//decrease the length of by 1


--nameLength;

// display reversed string


while (nameLength >= 0)
{
cout << name[nameLength];
--nameLength;
}

Take a 3*3 Matrix named as TransposMatrix and gives its equal


transpose matrix by using
2D arrays. Transpose is a matrix which is formed by turning all the
rows of a

Data Structure and Algorithms

13 | P a g e

given matrix into columns and vice-versa.


#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
int arr[3][3], i, j;
cout << " Enter Matrix Values \n";
for (i = 0; i<3; i++)
for (j = 0; j<3; j++)
{
cout << "Enter An Integer ";
cin >> arr[i][j];
}
cout << "\n TransposeMatrix : ";
for (j = 0; j<3; j++)
for (i = 0; i<3; i++)
{
cout << arr[i][j] << "\t";
}
system("pause");
return 0;
}

Data Structure and Algorithms

14 | P a g e

You have to implement a stack by using array with following


function .Push() Pop() Isfull() Is empty()
#include "stdafx.h"
#include "iostream"
using namespace std;

class stack
{
public:
int a[4];
int top;
void display();
void push(int x);
int pop();
void isempty();
stack()
{
top = -1;
}
};
void stack::display()
{
if (top<0)
{
cout << " nothing is available to display ";
}
else
for (int i = 0; i <= top; i++)
{
cout << "\n" << a[i];
}
}
void stack::isempty()

Data Structure and Algorithms

15 | P a g e
{
if (top<0)
{
cout << " Stack is empty ";
}
else
cout << "Stack is not empty";
}
void stack::push(int x)
{
if (top >= 4)
{
cout << " Stack is Full ";

}
else
{
a[++top] = x;
}
}
int stack::pop()
{
if (top>0)

{
int s =
a[--top];
return s; }
else
cout << " Stack is underflow ";
return 0;

Data Structure and Algorithms

16 | P a g e
}
int _tmain(int argc, _TCHAR* argv[])
{
stack ob;
int ch, x, s;

{
cout << "\nSelect Your Number\n";
cout << " 1: Display \n";
cout << " 2: check empty \n";
cout << " 3: Push \n";
cout << " 4: Pop \n";
cin >> ch;
switch (ch)
{
case 1: { ob.display(); break; }
case 2: {ob.isempty(); break; }
case 3: {
cout << " Enter X \n";
cin >> x;
ob.push(x);
break; }
case 4: {
s = ob.pop();
break;
}
}
}
system("pause");
return 0;
}

Data Structure and Algorithms

17 | P a g e

The new operator is used to allocate the memory dynamically. It is


created with the help of
pointers. The delete operator is used to delete dynamic variables at
the time of execution.
You are required to implement these two operators.
#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
int *ptr;
ptr = new int;
cout << "Enter An Integer : ";
cin >> *ptr;
cout << "You Entered : " << *ptr << endl;
cout << " It Is Stored At : " << ptr << endl;
delete ptr;
system("pause");
return 0;
}

Data Structure and Algorithms

18 | P a g e

Queue is a data structure that can be used to store data which can
later be retrieved in the
first in first out (FIFO) order. Queue is an ordered-list in which all
the insertions and
deletions are made at two different ends to maintain the FIFO order.
You have to implement these Functions by using an array.
Add ()

Store onto a Queue

Remove ()

retrieve (delete) from Queue

Is_empty ()
Is_Full

check if the Queue is empty


()

check if the Queue is full

#include "stdafx.h"
#include "iostream"
using namespace std;

class queue
{
int queue1[5];
int rear, front;
public:
queue()

Data Structure and Algorithms

19 | P a g e
{
rear = -1;
front = -1;
}
void insert(int x)
{
if (rear > 4)
{
cout << "queue over flow";
front = rear = -1;
return;
}
queue1[++rear] = x;
cout << "inserted" << x;
}
void delet()
{
if (front == rear)
{
cout << "queue under flow";
return;
}
cout << "deleted" << queue1[++front];
}
void display()
{
if (rear == front)
{
cout << " queue empty";
return;
}
for (int i = front + 1; i <= rear; i++)
cout << queue1[i] << " ";

Data Structure and Algorithms

20 | P a g e

}
};
int _tmain(int argc, _TCHAR* argv[])
{
int ch;
queue qu;
while (1)
{
cout << "\n1.insert 2.delete 3.display 4.exit\nEnter your choice";
cin >> ch;
switch (ch)
{
case 1:

cout << "enter the element : ";


cin >> ch;
qu.insert(ch);
break;

case 2: qu.delet(); break;


case 3: qu.display(); break;
case 4: exit(0);
}
}
return 0;
}

Data Structure and Algorithms

21 | P a g e

You have to implement these operations in singly linked list


Insertion () Deletion () Traversal ()
#include "stdafx.h"
#include "iostream"
using namespace std;

struct node{
int info;
struct node *next;
};
class Link{
node *list;
public:
Link();
void insert_at_begining(int);
void insert_at_end(int);
void insert_before_node();
void insert_after_node();
void delete_at_begining();
void delete_at_end();
void delete_before_node();
void delete_before_after_node(int);
node *find_before_node(node*);

Data Structure and Algorithms

22 | P a g e
void display();
};
Link::Link(){
list = NULL;
}
node *Link::find_before_node(node *p){
int count = 0;
int count1 = 0;
node *temp = new node;
temp = list;
while (temp != p){
count++;
temp = temp->next;
}
temp = list;
while (count1<count - 1){
count1++;
temp = temp->next;
}
return temp;
}
void Link::insert_at_begining(int data){
node *p = new node;
p->info = data;
p->next = list;
list = p;
}
void Link::insert_at_end(int data){
node *p = new node;
node *r = new node;
node *q = new node;
r = list;
p->info = data;

Data Structure and Algorithms

23 | P a g e

p->next = NULL;
if (list == NULL){
list = p;
}
else{
while (r->next != NULL){
r = r->next;
}
r->next = p;
}
}
void Link::insert_before_node(){
node *p = new node;
node *l = new node;
node *r = new node;
int data1, data2;
bool isFound = false;
cout << "Enter the node: ";
cin >> data1;
p = list;
while (p != NULL){
if (p->info == data1){
isFound = true;
break;
}
l = p;
p = p->next;
}
if (isFound){
cout << "Enter the data to insert:";

Data Structure and Algorithms

24 | P a g e
cin >> data2;
r->info = data2;
if (p == list){
insert_at_begining(data2);
}
else{
l->next = r;
r->next = p;
}
}
else{
cout << "\nMember not found\n";
}
}
void Link::insert_after_node(){
node *p = new node;
node *r = new node;
node *l = new node;
int data1, data2;
bool isFound = false;
cout << "Enter the node: ";
cin >> data1;
p = list;
while (p != NULL){
if (p->info == data1){
isFound = true;
break;
}
p = p->next;
}
if (isFound){

Data Structure and Algorithms

25 | P a g e
cout << "Enter the data to insert: ";
cin >> data2;
r->info = data2;
if (p->next == NULL){
insert_at_end(data2);
}
else{
l = p->next;
p->next = r;
r->next = l;
}
}
else{
cout << "\nMember not found\n";
}
}
void Link::delete_at_begining(){
node *p = new node;
if (list == NULL){
cout << "\nList is Empty\n";
}
else{
p = list;
list = list->next;
delete p;
}
}
void Link::delete_at_end(){
node *p = new node;
node *l = new node;
if (list == NULL){
cout << "\nList is Empty\n";
}
else{

Data Structure and Algorithms

26 | P a g e
p = list;
if (p->next == NULL){
list = NULL;
delete p;
}
else{
while (p->next != NULL){
l = p;
p = p->next;
}
l->next = NULL;
delete p;
}
}
}
void Link::delete_before_node(){
node *p = new node;
node *l = new node;
int data1;
bool isFound = false;
p = list;
cout << "Enter the node: ";
cin >> data1;
if (p == NULL){
cout << "\nList is Empty" << endl;
exit(0);
}
while (p != NULL){
if (p->info == data1){

isFound = true;

Data Structure and Algorithms

27 | P a g e
break;
}
l = p;
p = p->next;
}
if (isFound){
if (p == list){
cout << "\nIt is the first element\n";
}
else if (p == list->next){
list = p;
delete l;
}
else{
find_before_node(l)->next = p;
delete l;
}
}
}
void Link::display(){
node *p = new node;
p = list;
if (list == NULL){
cout << "\nNothing to Display\n";
}
else{
cout << "\nThe contents of list\n";
while (p != NULL){
cout << p->info << endl;
p = p->next;
}
}
}
int _tmain(int argc, _TCHAR* argv[])

Data Structure and Algorithms

28 | P a g e
{
int choice;
Link link;
while (1){
int data;
cout << "\n1. Insert at the begining" << endl;
cout << "2. Insert at the end" << endl;
cout << "3. Insert before node" << endl;
cout << "4. Insert After node" << endl;
cout << "5. Delete first element" << endl;
cout << "6. Delete last element" << endl;
cout << "7. Delete before node" << endl;
cout << "8. Delete after node" << endl;
cout << "9. Display" << endl;
cout << "10. Exit" << endl;
cout << "Enter the option: ";
cin >> choice;
switch (choice){
case 1:
cout << "\nEnter data to Insert: ";
cin >> data;
link.insert_at_begining(data);
break;
case 2:
cout << "Enter data to Insert: ";
cin >> data;

link.insert_at_end(data);
break;
case 3:
link.insert_before_node();

Data Structure and Algorithms

29 | P a g e
break;
case 4:
link.insert_after_node();
break;
case 5:
link.delete_at_begining();
break;
case 6:
link.delete_at_end();
break;
case 7:
link.delete_before_node();
break;
case 8:
//link.delete_before_after_node(0);
break;
case 9:
link.display();
break;
case 10:
exit(0);
break;
}
}
system("pause");
return 0;
}

Data Structure and Algorithms

30 | P a g e

You have to implement these operations in Doubly linked list


Insertion () Deletion () Traversal ()
#include "stdafx.h"
#include "iostream"
using namespace std;

int display_menu();

struct link_list
{
int no;
struct link_list *next;
struct link_list *prev;
};
class dqueue
{
link_list *list, *head;
public:
dqueue()
{
head = NULL;
}
void insert_first();

Data Structure and Algorithms

31 | P a g e
void insert_last();
void delete_first();
void delete_last();
void display();
};
void dqueue::insert_first()
{
link_list *newnode;
newnode = new link_list;
cout << "Enter Number :";
cin >> newnode->no;
list = head;
if (list == NULL)
{
head = newnode;
newnode->next = NULL;
newnode->prev = NULL;
return;
}
newnode->next = list;
newnode->prev = NULL;
list->prev = newnode;
head = newnode;
}

void dqueue::insert_last()
{
link_list *newnode;
newnode = new link_list;
cout << "Enter Number :";
cin >> newnode->no;

Data Structure and Algorithms

32 | P a g e

list = head;
if (list == NULL)
{
head = newnode;
newnode->next = NULL;
newnode->prev = NULL;
return;
}
while (list->next != NULL)
{
list = list->next;
}
list->next = newnode;
newnode->prev = list;
newnode->next = NULL;
}

void dqueue::display()
{
cout << endl;
link_list *tail;
list = head;
if (head == NULL)
{
cout << "Empty Queue !!!";
return;
}
cout << endl;
cout << "Forward Display ..." << endl;
while (list != NULL)
{
cout << list->no << "\t";
if (list->next == NULL)

Data Structure and Algorithms

33 | P a g e
{
tail = list;
}
list = list->next;
}

cout << endl << "Backward Display ..." << endl;


while (list != NULL)
{
cout << list->no << "\t";
list = list->prev;
}
}

void dqueue::delete_first()
{
list = head;
list->next->prev = NULL;
head = list->next;
}
void dqueue::delete_last()
{
list = head;
while (list->next->next != NULL)
{
list = list->next;
}
list->next = NULL;

Data Structure and Algorithms

34 | P a g e
int _tmain(int argc, _TCHAR* argv[])
{
dqueue dq1;
while (1)
{
switch (display_menu())
{
case 1: dq1.insert_first();
dq1.display();
break;
case 2: dq1.insert_last();
dq1.display();
break;
case 3: dq1.delete_first();
dq1.display();

break;
case 4: dq1.delete_last();
dq1.display();
break;
case 5: dq1.display();
break;
case 6: exit(0);
}
}
}
int display_menu()
{
int ch;
cout << "[ 1 ] : Insert at First" << endl;
cout << "[ 2 ] : Insert at Last" << endl;
cout << "[ 3 ] : Delete From First" << endl;
cout << "[ 4 ] : Delete From Last" << endl;
cout << "[ 5 ] : Display" << endl;

Data Structure and Algorithms

35 | P a g e
cout << "[ 6 ] : Exit" << endl;
cout << "Enter your choice :";
cin >> ch;
return(ch);
}

Find Factorial Number For an input n.


#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
int n, fact;
int rec(int x);
cout << "Enter the number:->";
cin >> n;
fact = rec(n);
cout << endl << "Factorial Result are:: " << fact << endl;
system("pause");
}
int rec(int x)
{
int f;
if (x == 1)
return(x);

Data Structure and Algorithms

36 | P a g e
else
{
f = x*rec(x - 1);
return(f);
}
}

Check If a Given Array Values Are Palindrome or not


#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome\n";

Data Structure and Algorithms

37 | P a g e
else
cout << " The number is not a palindrome\n";

system("pause");
return 0;
}

Implement the problem of Tower of Hanoi.

#include "stdafx.h"
#include "iostream"
using namespace std;

void tower(int a, char from, char aux, char to){


if (a == 1){
cout << "\t\tMove disc 1 from " << from << " to " << to << "\n";
return;
}
else{
tower(a - 1, from, to, aux);
cout << "\t\tMove disc " << a << " from " << from << " to " << to << "\n";
tower(a - 1, aux, from, to);
}
}

int _tmain(int argc, _TCHAR* argv[])

Data Structure and Algorithms

38 | P a g e
{
int n;
cout << "\n\t\t*****Tower of Hanoi*****\n";
cout << "\t\tEnter number of discs : ";
cin >> n;
cout << "\n\n";
tower(n, 'A', 'B', 'C');
system("pause");
return 0;
}

Bubble Sort
#include "stdafx.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
//declaring array
int array[5];
cout << "Enter 5 numbers randomly : " << endl;
for (int i = 0; i<5; i++)
{ //Taking input in array
cin >> array[i];
}
cout << endl;
cout << "Input array is: " << endl;
for (int j = 0; j<5; j++)
{ //Displaying Array
cout << "\t\t\tValue at " << j << " Index: " << array[j] << endl;

Data Structure and Algorithms

39 | P a g e
}
cout << endl;
// Bubble Sort Starts Here
int temp;
for (int i2 = 0; i2 <= 4; i2++)
{
for (int j = 0; j<4; j++)
{ //Swapping element in if statement
if (array[j]>array[j + 1])
{
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
// Displaying Sorted array
cout << " Sorted Array is: " << endl;
for (int i3 = 0; i3<5; i3++)
{
cout << "\t\t\tValue at " << i3 << " Index: " << array[i3] << endl;
}
system("pause");
return 0;
}

Merge Sort

Data Structure and Algorithms

40 | P a g e
#include "stdafx.h"
#include "iostream"
using namespace std;

int Merge(int A[], int B[], int C[], int N, int M, int &K);

int _tmain(int argc, _TCHAR* argv[])


{

int A[100], B[100], C[200], n, m, k;


cout << "\nEnter number of elements you want to insert in first array ";
cin >> n;
cout << "Enter element in ascending order\n";

for (int i = 0; i<n; i++)


{
cout << "Enter element " << i + 1 << ":";
cin >> A[i];
}

cout << "\nEnter number of elements you want to insert in second array ";
cin >> m;

cout << "Enter element in descending order\n";

for (int i = 0; i<m; i++)


{
cout << "Enter element " << i + 1 << ":";
cin >> B[i];
}

Merge(A, B, C, n, m, k);

Data Structure and Algorithms

41 | P a g e

cout << "The Merged Array in Ascending Order" << endl;


for (int i = 0; i<k; i++)
{
cout << C[i] << " ";
}
system("pause");
return 0;
}

int Merge(int A[], int B[], int C[], int N, int M, int &K)
{
int I = 0, J = M - 1;
K = 0;
while (I<N && J >= 0)
{

if (A[I]<B[J])
C[K++] = A[I++];
else if (A[I]>B[J])
C[K++] = B[J--];
else
{
C[K++] = A[I++];
J--;
}
}

for (int T = I; T<N; T++)


C[K++] = A[T];

Data Structure and Algorithms

42 | P a g e
for (int T = J; T >= 0; T--)
C[K++] = B[T];
return 0;
}

Quick Sort
#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
int srt(int[], int, int);
int a[10], count = 0, n;
cout << "Ener 10 values in unsorted order : \n";

for (n = 0; n<10; n++)


{
cout << "value no.: " << (n + 1) << "\t";
cin >> a[n];
count++;
}
n = 0;
srt(a, n, count - 1);

Data Structure and Algorithms

43 | P a g e
cout << "\t\tThe Sorted order is : \n";

for (n = 0; n<10; n++)


{
cout << "\t\tposition : " << (n + 1) << "\t" << a[n] << "\n";
}
system("pause");
}

int srt(int k[20], int lb, int ub)

{
int i, j, key, flag = 0, temp;
if (lb<ub)
{
i = lb;
j = ub + 1;
key = k[i];
while (flag != 1)
{
i++;
while (k[i]<key)
{
i++;
}
j--;

while (k[j]>key)
{
j--;
}
if (i<j)
{
temp = k[i];

Data Structure and Algorithms

44 | P a g e
k[i] = k[j];
k[j] = temp;

}
else
{
flag = 1;
temp = k[lb];
k[lb] = k[j];
k[j] = temp;
}
}
srt(k, lb, j - 1);
srt(k, j + 1, ub);
}
return 0;
}

Insertion Sort
#include "stdafx.h"
#include "iostream"

Data Structure and Algorithms

45 | P a g e
using namespace std;
int insertion_sort(int x[], int length)
{
int key, i;
for (int j = 1; j<length; j++)
{
key = x[j];
i = j - 1;
while (x[i]>key && i >= 0)
{
x[i + 1] = x[i];
i--;
}
x[i + 1] = key;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int A[25];
int size, i;
int x;
cout << "Enter size of list : ";
cin >> size;
cout << "\n Enter numbers : ";
for (x = 0; x<size; x++)
{
cin >> A[x];
}
cout << "NON SORTED LIST:" << endl;
for (x = 0; x<size; x++)
{
cout << A[x] << endl;
}
insertion_sort(A, size);
cout << endl << "SORTED LIST" << endl;
for (x = 0; x<size; x++)
{
cout << A[x] << endl;
}

Data Structure and Algorithms

46 | P a g e
system("pause");
return 0;
}

You have to implement the function Build Max Heap.


#include "stdafx.h"
#include "iostream"
using namespace std;

int max_heapify(int *a, int i, int n)

{
int j, temp;
temp = a[i];
j = 2 * i;

while (j <= n)
{
if (j < n && a[j + 1] > a[j])
j = j + 1;
if (temp > a[j])
break;

Data Structure and Algorithms

47 | P a g e
else if (temp <= a[j])

{
a[j / 2] = a[j];
j = 2 * j;
}
}
a[j / 2] = temp;
return 0;
}

int build_maxheap(int *a, int n)


{
int i;
for (i = n / 2; i >= 1; i--)
{
max_heapify(a, i, n);
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])

int n, i, x;
cout << "enter no of elements of array\n";

Data Structure and Algorithms

48 | P a g e
cin >> n;
int a[20];

for (i = 1; i <= n; i++)


{
cout << "enter element" << (i) << endl;
cin >> a[i];
}
build_maxheap(a, n);
cout << "Max Heap\n";

for (i = 1; i <= n; i++)


{
cout << a[i] << endl;
}
system("pause");
}

Radix Sort
#include "stdafx.h"
#include "iostream"
using namespace std;

Data Structure and Algorithms

49 | P a g e
class radixsort{
int arr[10], n;
public:
void getdata();
void showdata();
void sortLogic();
};

void radixsort::getdata(){
cout << "How many elements you require : ";
cin >> n;
for (int i = 0; i<n; i++)
cin >> arr[i];
}

void radixsort::showdata(){
cout << "\n--Display--\n";
for (int i = 0; i<n; i++)
cout << arr[i] << "

";

void radixsort::sortLogic(){
//for base 10int temp;
int bucket[10][20], buck_count[10], b[10];
int i, j, k, r, no_of_passes = 0, divisor = 1, largest, pass_no;

largest = arr[0];

for (i = 1; i<n; i++) //Find the largest Number


{
if (arr[i] > largest)
largest = arr[i];
}

Data Structure and Algorithms

50 | P a g e
while (largest > 0) //Find number of digits in largest number
{
no_of_passes++;
largest /= 10;
}

for (pass_no = 0; pass_no < no_of_passes; pass_no++){

for (k = 0; k<10; k++)


buck_count[k] = 0; //Initialize bucket count
for (i = 0; i<n; i++){
r = (arr[i] / divisor) % 10;
bucket[r][buck_count[r]++] = arr[i];
}
i = 0; //collect elements from bucket
for (k = 0; k<10; k++){

for (j = 0; j<buck_count[k]; j++)


arr[i++] = bucket[k][j];
}

divisor *= 10;
}
}

int _tmain(int argc, _TCHAR* argv[]){

cout << "\n*****Radix Sort*****\n";


radixsort obj;

Data Structure and Algorithms

51 | P a g e
obj.getdata();
obj.sortLogic();
obj.showdata();
system("pause");
}

Binary Search
#include "stdafx.h"
#include "iostream"
using namespace std;
int bsearch(int AR[], int N, int VAL);
int _tmain(int argc, _TCHAR* argv[])
{
int AR[100], n, val, found;
cout << "Enter number of elements you want to insert ";
cin >> n;
cout << "Enter element in ascending order\n";
for (int i = 0; i<n; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> AR[i];
}
cout << "\nEnter the number you want to search ";
cin >> val;
found = bsearch(AR, n, val);
if (found == 1)
cout << "\nItem found";
else

Data Structure and Algorithms

52 | P a g e
cout << "\nItem not found";
system("pause");
return 0;
}
int bsearch(int AR[], int N, int VAL)
{
int Mid, Lbound = 0, Ubound = N - 1;
while (Lbound <= Ubound)
{
Mid = (Lbound + Ubound) / 2;
if (VAL>AR[Mid])
Lbound = Mid + 1;
else if (VAL<AR[Mid])
Ubound = Mid - 1;
else
return 1;
} return 0;
}

Linear Search
#include "stdafx.h"
#include "iostream"
using namespace std;

int linear_search(int[], int, int);

Data Structure and Algorithms

53 | P a g e
int _tmain(int argc, _TCHAR* argv[])

const int array_size = 10;

int array[array_size] = { 0 };

cout << "\n Enter the contents of the array are : " << endl;

cout << "\n

Elements :" << "\t\t

Value:" << endl;

for (int count = 0; count<array_size; count++)


{
cout << "\t" << " array [" << count << "]" << "\t\t";
cin >> array[count];
}

int searching_element = 0;
int flag = 0;

cout << "\n\n Enter the element you want to find


cin >> searching_element;

flag = linear_search(array, array_size, searching_element);

if (flag != -1)

Data Structure and Algorithms

";

54 | P a g e
cout << "\n The given element is found at the position
flag << "]" << endl;

else

cout << "\n The given element is not found. " << endl;
system("pause");
return 0;
}
int linear_search(int array[], int size, int element)
{
for (int count = 0; count<size; count++)
{
if (element == array[count])
{
return count;
}
}
return -1;
}

Binary Tree Operation


#include "stdafx.h"

Data Structure and Algorithms

array[" <<

55 | P a g e
#include <iostream>
#include <stdlib.h>
using namespace std;

template <class T>


class bintree
{
bintree<T> *left;
T data;
bintree<T> *right;
public:
bintree()
{
left = right = NULL;
}
void create();
void preorder();
void inorder();
void postorder();
};

template <class T>


void bintree<T> ::create()
{
char opt;
cout << "\n Enter the element : ";
cin >> data;
cout << "\n Is there left child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt == 'y')
{
left = new bintree<T>;
left->create();

Data Structure and Algorithms

56 | P a g e
}
cout << "\n Is there right child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt == 'y')
{
right = new bintree<T>;
right->create();
}
}

template <class T>


void bintree<T> ::preorder()
{
if (this != NULL)
{
cout << data << " ";
left->preorder();
right->preorder();
}
}

template <class T>


void bintree<T> ::inorder()
{
if (this != NULL)
{
left->inorder();
cout << data << " ";
right->inorder();
}
}

Data Structure and Algorithms

57 | P a g e

template <class T>


void bintree<T> ::postorder()
{
if (this != NULL)
{
left->postorder();
right->postorder();
cout << data << " ";
}
}

int _tmain(int argc, _TCHAR* argv[])


{
bintree<char> *x;
x = NULL;
int ch;
do
{
cout << "Binary Tree Operation Menu\n";
cout << "1. Create\n";
cout << "2. Preorder Traversal\n";
cout << "3. Inorder Traversal\n";
cout << "4. Postorder Traversal\n";
cout << "5. Exit\n\n";
cout << "Enter Your Choice [1..5] : ";
cin >> ch;
switch (ch)
{
case 1:
x = new bintree <char>;
x->create();
break;
case 2:

Data Structure and Algorithms

58 | P a g e
x->preorder();
break;
case 3:
x->inorder();
break;
case 4:
x->postorder();
break;
case 5:
break;
default:
cout << "Invalid Choice!";
}
} while (ch != 5);
system("pause");
}

Swapping
#include "stdafx.h"
#include <iostream>
using namespace std;
void swap(int &m, int &n);
int _tmain(int argc, _TCHAR* argv[])

Data Structure and Algorithms

59 | P a g e
{
int N = 6;
int a[6] = { 5, 2, 4, 6, 1, 3, };
// Selection Sort
for (int i = 0; i < (N - 1); i++)
{
int minIndex = i;
// Find the index of the minimum element
for (int j = i + 1; j < N; j++)
{
if (a[j] < a[minIndex])

minIndex = j;
}
}
// Swap if i-th element not already smallest
if (minIndex > i)
{

swap(a[i], a[minIndex]);

}
}

// Print sorted results


for (int i = 0; i < N; i++)
{
cout << i << " " << a[i] << endl;
}
system("pause");
return 0;
}
void swap(int &x, int &y)
{

int temp;

temp = x;
x = y;
y = temp;
}

Data Structure and Algorithms

60 | P a g e

Data Structure and Algorithms

También podría gustarte