Professional Documents
Culture Documents
Submitted By:
(BSCS 3rd)
Muhammad Habib
Submitted To:
Mr.Syed Muddasir
2 | Page
SR#
Topic
Page #
3 | Page
04
Power Function
07
Structure Phone
08
Reverse String
09
Transpose
11
Implement a stack
12
15
10
Queue
16
11
19
12
28
13
Find Factorial
33
14
34
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
44
21
Radix Sort
47
22
Binary Search
49
23
Linear Search
51
24
53
25
Swapping
57
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;
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;
}
7 | Page
8 | Page
ptr->in();
ptr->out();
system("pause");
return 0;
}
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);
10 | P a g e
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;
}
11 | P a g e
// function declaration
reverseString(name);
cout << endl;
system("pause");
return 0;
}
12 | P a g e
13 | P a g e
14 | P a g e
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()
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;
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;
}
17 | P a g e
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 ()
Remove ()
Is_empty ()
Is_Full
#include "stdafx.h"
#include "iostream"
using namespace std;
class queue
{
int queue1[5];
int rear, front;
public:
queue()
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] << " ";
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:
21 | P a g e
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*);
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;
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:";
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){
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{
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;
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[])
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();
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;
}
30 | P a g e
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();
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;
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)
33 | P a g e
{
tail = list;
}
list = list->next;
}
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;
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;
35 | P a g e
cout << "[ 6 ] : Exit" << endl;
cout << "Enter your choice :";
cin >> ch;
return(ch);
}
36 | P a g e
else
{
f = x*rec(x - 1);
return(f);
}
}
37 | P a g e
else
cout << " The number is not a palindrome\n";
system("pause");
return 0;
}
#include "stdafx.h"
#include "iostream"
using namespace std;
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;
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
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);
cout << "\nEnter number of elements you want to insert in second array ";
cin >> m;
Merge(A, B, C, n, m, k);
41 | P a g e
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--;
}
}
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;
43 | P a g e
cout << "\t\tThe Sorted order is : \n";
{
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];
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"
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;
}
46 | P a g e
system("pause");
return 0;
}
{
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;
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 n, i, x;
cout << "enter no of elements of array\n";
48 | P a g e
cin >> n;
int a[20];
Radix Sort
#include "stdafx.h"
#include "iostream"
using namespace std;
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];
50 | P a g e
while (largest > 0) //Find number of digits in largest number
{
no_of_passes++;
largest /= 10;
}
divisor *= 10;
}
}
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
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;
53 | P a g e
int _tmain(int argc, _TCHAR* argv[])
int array[array_size] = { 0 };
cout << "\n Enter the contents of the array are : " << endl;
int searching_element = 0;
int flag = 0;
if (flag != -1)
";
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;
}
array[" <<
55 | P a g e
#include <iostream>
#include <stdlib.h>
using namespace std;
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();
}
}
57 | P a g e
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[])
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]);
}
}
int temp;
temp = x;
x = y;
y = temp;
}
60 | P a g e