‏إظهار الرسائل ذات التسميات برمجة. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات برمجة. إظهار كافة الرسائل
الجمعة، 16 مايو 2014

quick_sort

#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
class quicksort
{
public:
int no_of_elements;
int elements[10];
public:
void getarray();
void sortit(int[], int, int);
void partition(int[], int, int,int &);
void display();
};
void quicksort::getarray(){
cout << "how many elements ?: ";
cin >> no_of_elements;
cout << "insert array of element to sort : ";
for (int i = 0; i < no_of_elements; i++)
cin >> elements[i];
}
void quicksort::sortit(int x[], int lb, int ub)
{
int j;
if (lb >= ub)
return;
display();
partition(x, lb, ub, j);
sortit(x, lb, j - 1);
sortit(x, j + 1, ub);
}
void quicksort::partition(int x[], int lb, int ub, int &pj)
{
int a, down, temp, up;
a = x[lb];
up = ub;
down = lb;
while (down < up)
{
while (x[down] <= a)
down++;
while (x[up]>a)
up--;
if (down < up)
{
temp = x[down];
x[down] = x[up];
x[up] = temp;
}
}
x[lb] = x[up];
x[up] = a;
pj = up;
}
void quicksort::display()
{
for (int i = 0; i < no_of_elements; i++)
cout << elements[i] << " ";
cout << endl;
}

void main()
{
quicksort qs;
qs.getarray();
cout << "sort is given in step by step showing each iteration" << endl;
qs.sortit(qs.elements, 0, qs.no_of_elements - 1);
qs.display();
_getch();
}

merge_sort


#include "stdafx.h"
#include<iostream>
#include<conio.h>
using namespace std;
void sort(int left, int right);
void merge(int left, int mid, int right);
int a[9] = { 75, 40, 10, 90, 50, 95, 55, 15, 65};
int tmp[9];
void sort(int left, int right)
{

if (left < right)
{
int mid = (left + right) / 2;
sort(left, mid);
sort(mid + 1, right);
merge(left, mid, right);
}
}

void merge(int left, int mid, int right)
{
int i = left;
int j = left;
int k = mid + 1;
while (j <= mid&&k <= right)
{
if (a[j] < a[k])
tmp[i++] = a[j++];
else
tmp[i++] = a[k++];
}
while (j <= mid)
tmp[i++] = a[j++];
for (i = left; i < k; i++)
a[i] = tmp[i];
}
void main()
{
sort(0, 8);
cout << "sort array:"<<"\n";
for (int i = 0; i < 9; i++)
cout << a[i] << " ";
_getch();

}
الثلاثاء، 1 أبريل 2014

hashtable in c++

// record taqble data base.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#define null -1
using namespace std;
struct emprecord
{
int empcode;
};
class hashtable
{
private:
emprecord *hasharray;
int arraysize;
public:
hashtable(int size)
{
hasharray = new emprecord[size];
arraysize = size;
for (int i = 0; i < arraysize; i++)
hasharray[i].empcode = null;
}
int hashfunc(int key)
{
return key%arraysize;
}
void insert(emprecord item)
{
int key = item.empcode;
int hasval = hashfunc(key);
while (hasharray[hasval].empcode != null)
{
++hasval;
hasval %= arraysize;
}
hasharray[hasval] = item;
}
void display()
{
cout << "key" << "\t" << "hashfunc" << endl;
cout << "---" << "\t" << "--------" << endl;
for (int i = 0; i < arraysize; i++)
cout << hasharray[i].empcode << "\t" << hashfunc(hasharray[i].empcode) << endl;
}

};

void main()
{
int a[] = { 12, 17, 21, 28, 35, 40, 43, 52, 66, 69 };
emprecord reckey[10];
for (int i = 0; i < 10; i++)
reckey[i].empcode=a[i];
hashtable h(10);
for (int i = 0; i < 10; i++)
h.insert(reckey[i]);
h.display();
_getch();

}

الاثنين، 24 مارس 2014

priority queues in heap

// ConsoleApplication46.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string>
#define maxsize 10
using namespace std;
struct node
{
string data;
int prn;
};
class heap_priority
{

node a[maxsize];
int count;

public:
heap_priority()
{
count = 0;
}
bool insert(int key, string name)
{
if (count == maxsize)
return false;
else
a[count].data = name;
a[count].prn = key;
moveup(count++);
return true;
}
void moveup(int index)
{
int parent = (index - 1) / 2;
node bottom = a[index];
while (index > 0 && a[parent].prn < bottom.prn)
{
a[index] = a[parent];
index = parent;
parent = (parent - 1) / 2;
}
a[index] = bottom;

}

void *remove()
{
if (count == 0)
{
cout << "\n heap is empty" << endl;
return NULL;
}
node *root = new node;
root->data = a[0].data;
root->prn = a[0].prn;
a[0] = a[--count];
movedown(0);
return root;
}
void movedown(int index)
{
int larger_child;
node top = a[index];
while (index < count / 2)
{
int left_child = 2 * index + 1;
int right_child = 2 * index + 2;
if (right_child < count&&a[left_child].prn < a[right_child].prn)
larger_child = right_child;
else
larger_child = left_child;
if (top.prn >= a[larger_child].prn)
break;
a[index] = a[larger_child];
index = larger_child;
}
a[index] = top;
}
void display()
{
for (int i = 0; i < count; i++)
cout << "["<<i<<"] [" << a[i].data << "|" << a[i].prn << "]";;
cout << endl;
}



};
void main()
{
heap_priority h;
h.insert(2, "alpha");
h.insert(3, "beta");
h.insert(2, "gamma");
h.insert(4, "delta");
h.insert(1, "omega");
h.display();
h.insert(3, "lambda");
h.display();
h.remove();
h.display();
_getch();
}

الأحد، 16 مارس 2014

heap (complete binary tree)

// Heap.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<iostream>
#include<conio.h>
#define max 7
using namespace std;
class heap
{
private:
int  a[max];
int count;
public:
heap()
{
count = 0;
}
bool insert(int key)
{
if (count == max)
return false;
a[count] = key;
moveup(count++);
return true;
}
void moveup(int index)
{
int parent = (index - 1) / 2;
int bottom = a[index];
while (index > 0 && a[parent] <bottom)
{
a[index] = a[parent];
index = parent;
parent = (parent - 1) / 2;
}
a[index] = bottom;
}
void movedown(int index)
{
int largechild;
int top = a[index];
while (index<count / 2)
{
int leftchild = 2 * index + 1;
int rightchild = 2 * index + 2;
if (rightchild<count && a[leftchild] < a[rightchild])
largechild = rightchild;
else
largechild = leftchild;
if (top >= a[largechild])
break;
a[index] = a[largechild];
index = largechild;
}
a[index] = top;
}

int remove()

{
if (count == 0)
{
cout << "heap is empty" << endl;
return -1;
}
int root = a[0];
a[0] = a[--count];
movedown(0);
return root;
}
void display()
{
for (int i = 0; i <count; i++)
cout << a[i]<<" ";
cout << endl;
}

};


void main()

{
heap h;
h.insert(50);
h.insert(20);
h.insert(30);
h.insert(10);
h.insert(40);
h.insert(70);
h.insert(60);
h.display();
h.remove();
h.display();
_getch();
}








For more advantage in show:-















void display()
{
cout <<"\t\t\t\t    ("<< a[0] <<")" <<endl;
cout << "\t\t\t\t   /" << "    |" << endl;
cout << "\t\t\t\t  /" << "     |" << endl;
cout << "\t\t\t      (" << a[1] << ")\t(" << a[2] << ")" << endl;
cout << "\t\t\t      /" << "  |" <<"     / " << "  | " << endl;
cout << "\t\t\t     /" << "   |" <<"    / " << "   | " << endl;
cout << "\t\t\t  (" << a[3] << ") (" << a[4] << ")  (" << a[5] << ") ("<<a[6]<<")";
cout << endl;
}

tree

// Tree.cpp : create with my friend Murtada //

#include<iostream>
#include<conio.h>
using namespace std;
class Tree{

struct Node{
Node* Left;
Node* Right;
int data;
Node(int N){
data=N;
Left=Right=NULL;
}
};
Node *Root;
void insertp(int item,Node* &root){
if(root==NULL)
root=new Node(item);
else
if(root->data>item)
insertp(item,root->Left);
else
insertp(item,root->Right);

}
void displaypre(Node *root){
if(root!=NULL){
cout<<root->data<<"  ";
if(root->Left!=NULL)
displaypre(root->Left);
if(root->Right!=NULL)
displaypre(root->Right);
}
}
void displayin(Node *root){
if(root->Left!=NULL)
displayin(root->Left);

cout<<root->data<<"  ";

if(root->Right!=NULL)
displayin(root->Right);
}
void displaypost(Node *root){
if(root->Left!=NULL)
displaypost(root->Left);
if(root->Right!=NULL)
displaypost(root->Right);

cout<<root->data<<"  ";
}
public:
Tree(){
Root=NULL;
}
void insert(int i){ insertp(i,Root);}
void buildtree (int A[],int n){
//int n=7;
insert(A[0]);
//insert(A[(i*2)+1]);
//insert(A[(i*2)+2]);
for (int i=0;i<n;i++){
//insert(A[i]);
if((i*2)+1<n)
insert(A[(i*2)+1]);
if((i*2)+2<n)
insert(A[(i*2)+2]);
}
}
void display(){
cout<<"\npreorder : ";
displaypre(Root);
       cout<<"\ninorder : ";
displayin(Root);
cout<<"\npostorder : ";
displaypost(Root);

}

};
void main(){
//cout<<"Murtada and Ahmed ";
Tree t;
t.insert(5);
t.insert(9);
t.insert(7);
t.insert(3);
t.display();
int A[]={5,9,7,3,2,4,6};
t.buildtree(A,7);//n=size of array=7
t.display();
_getch();
}
الأحد، 23 فبراير 2014

Deque (Double-ended queue)


#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
node *p = new node(item);
if (isempty())
first = last = new node(item);
else
{
last->next = p;
p->prev = last;
last = p;
}
count++;
}
void addlast(char item)
{
node *p = new node(item);
last->next = p;
p->prev = last;


last = p;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char temp;if(!isempty()){temp=last->data;last=last->prev;last->next=NULL;count--;return temp;}else if(first==last){temp=first->data;first=last=NULL;return temp;}else{cout<<"queue is empty."<<endl;return NULL;}}
void display()
{
node *p = first;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
};
void main()
{
linked_deque d;
d.addfirst('a');
d.addfirst('b');
d.addlast('c');
d.display();
d.removefirst();
cout << endl;
d.display();
d.removelast();
cout << endl;
d.display();

_getch();
}

after correct and for more advantage:


#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
if (isempty())
first = last = new node(item);
else
{
node *p = new node(item);
first->prev = p;
p->next = first;
first = p;


}
count++;
}
void addlast(char item)
{
node *p = new node(item);
p->prev = last;
last->next = p;
last = p;


count++;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char item = last->data;
last = last->prev;
last->next = NULL;
return item;
count--;
}

void display()
{
node *p = first;
cout << "---------------------------\n";
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << "\n---------------------------\n";
}

};
void main()
{
linked_deque d;
int ch;
int k = 1;
while (k == 1)
{
cout << "\n0-insert item from first and display\n1- insert item from last and diplay\n2-delete first item and disply\n3-delete last item and display\n4-exit\n";
cin >> ch;
switch (ch)
{
case 0:
d.addfirst('a');
d.display();
break;
case 1:
d.addlast('b');
d.addlast('c');
d.display();
break;
case 2:
d.removefirst();
cout << endl;
d.display();
break;
case 3:
d.removelast();
cout << endl;
d.display();
break;
case 4:
cout << "good bye";
k = 0;
}
}
_getch();
}



Deque (Double-ended queue)


#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
node *p = new node(item);
if (isempty())
first = last = new node(item);
else
{
last->next = p;
p->prev = last;
last = p;
}
count++;
}
void addlast(char item)
{
node *p = new node(item);
last->next = p;
p->prev = last;


last = p;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char temp;

if(!isempty())
{
temp=last->data;
last=last->prev;
last->next=NULL;
count--;
return temp;
}
else if(first==last)
{
temp=first->data;
first=last=NULL;
return temp;
}
else
{
cout<<"queue is empty."<<endl;
return NULL;
}
}
void display()
{
node *p = first;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
};
void main()
{
linked_deque d;
d.addfirst('a');
d.addfirst('b');
d.addlast('c');
d.display();
d.removefirst();
cout << endl;
d.display();
d.removelast();
cout << endl;
d.display();

_getch();
}

after correct and for more advantage:

#include "stdafx.h"
#include <iostream>
#include<conio.h>
using namespace std;
class linked_deque
{
private:
struct node
{
char data;
node *next;
node *prev;
node(char item)
{
data = item;
next = prev = NULL;
}
};
node *first;
node *last;
int count;
public:
linked_deque()
{
first = last = NULL;
count = 0;
}
bool isempty(){ return (count == 0); }
void addfirst(char item)
{
if (isempty())
first = last = new node(item);
else
{
node *p = new node(item);
first->prev = p;
p->next = first;
first = p;


}
count++;
}
void addlast(char item)
{
node *p = new node(item);
p->prev = last;
last->next = p;
last = p;


count++;
}

char removefirst()
{
if (isempty())
{
cout << "the Queue is Empty";
return NULL;
}
else
{
char item = first->data;
first = first->next;
return item;
}
count--;
}
char removelast()
{
char item = last->data;
last = last->prev;
last->next = NULL;
return item;
count--;
}

void display()
{
node *p = first;
cout << "---------------------------\n";
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << "\n---------------------------\n";
}

};
void main()
{
linked_deque d;
int ch;
int k = 1;
while (k == 1)
{
cout << "\n0-insert item from first and display\n1- insert item from last and diplay\n2-delete first item and disply\n3-delete last item and display\n4-exit\n";
cin >> ch;
switch (ch)
{
case 0:
d.addfirst('a');
d.display();
break;
case 1:
d.addlast('b');
d.addlast('c');
d.display();
break;
case 2:
d.removefirst();
cout << endl;
d.display();
break;
case 3:
d.removelast();
cout << endl;
d.display();
break;
case 4:
cout << "good bye";
k = 0;
}
}
_getch();
}