الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Data Strucures Code(اكواد لأهم مواضيع الداتا ستركشر)

بدأه Don juan في 10 يوليو 2009 · 10 رد · 4,749 مشاهدة · في قسم المواضيع الهامة في قسم السي /سي++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله ...

يسعدني ان اقدم لكم اول مشاركاتي في هذا المنتدى الرائع ...

I Don't have a new thing,but maybe it's usful for someone

امممم بقدم لكم بعض الدوال الخاصة في مواضيع الدتاء ستركشر مثل stack ,linked list ,double linked list,queue,hash tabe,tree and sort Algorithm

طبعا بدون شرح بكتفي بوضع الدوال مع ذكر بعض التعليقات (مسوي فاهم :lol: ) جميع الاكواد مكتوبة باستخدام فيجوال ستوديو ..

so let's begin

الكود الاول : عبارة عن دوال الترتيب inset-select-merge-Quick sorting ومثل ماهو واضح انو يحسب زمن الترتيب التنفيذ لكل دالة ..

  1.  
  2. #include<ctime>
  3. #include<iostream>
  4. using namespace std;
  5. ////////////////////////////////// InsertionSort ////////////////////////////////////////
  6. void InsertionSort(int numbers[], int array_size)
  7. {
  8. int i, j, key;
  9. for (i=1; i < array_size; i++)
  10. {
  11. key = numbers[i];
  12. j = i;
  13. while (j > 0 && numbers[j-1] > key)
  14. {
  15. numbers[j] = numbers[j-1];
  16. j = j - 1;
  17. }
  18. numbers[j] = key;
  19. }
  20. }
  21. ////////////////////////////////// SeclectionSort ////////////////////////////////////////
  22. void SeclectionSort(int numbers[], int array_size)
  23. {
  24. int temp;
  25. for(int i=0;i<array_size;i++){
  26. for(int s=0;s<array_size-1;s++){
  27. if(numbers[i]<numbers[s]){
  28. temp=numbers[i];
  29. numbers[i]=numbers[s];
  30. numbers[s]=temp;}}}
  31. }
  32. /////////////////////////////////// mergeSort ////////////////////////////////////////
  33. void mergeSort(int array[], int start, int end)
  34. {
  35. int n = end - start + 1;
  36. if (n > 1)
  37. {
  38. int n1 = n / 2;
  39.  
  40. mergeSort(array, start, start + n1 - 1);
  41. mergeSort(array, start + n1, end);
  42.  
  43. int *tmp = new int[ end - start + 1 ];
  44. int *dest = tmp,
  45. *src1 = array + start,
  46. *src2 = array + start + n1;
  47. while ((src1 < array + start + n1) && (src2 <= array + end))
  48. {
  49. if (*src1 <= *src2)
  50. *dest++ = *src1++;
  51. else
  52. *dest++ = *src2++;
  53. }
  54.  
  55. while (src1 < array + start + n1 )
  56. *dest++ = *src1++;
  57. while (src2 <= array + end)
  58. *dest++ = *src2++;
  59.  
  60. memcpy( array + start, tmp, n*sizeof(int) );
  61. delete[] tmp;
  62. }
  63. }
  64. /////////////////////////////////// Quick Sort ////////////////////////////////////////
  65.  
  66. void q_sort(int numbers[], int left, int right)
  67. {
  68. int pivot;
  69. int l_hold;
  70. int r_hold;
  71.  
  72. l_hold = left;
  73. r_hold = right;
  74. pivot = numbers[left];
  75. while (left < right)
  76. {
  77. while ((numbers[right] >= pivot) && (left < right))
  78. right--;
  79. if (left != right)
  80. {
  81. numbers[left] = numbers[right];
  82. left++;
  83. }
  84. while ((numbers[left] <= pivot) && (left < right))
  85. left++;
  86. if (left != right)
  87. {
  88. numbers[right] = numbers[left];
  89. right--;
  90. }
  91. }
  92. numbers[left] = pivot;
  93. pivot = left;
  94. left = l_hold;
  95. right = r_hold;
  96. if (left < pivot)
  97. q_sort(numbers, left, pivot-1);
  98. if (right > pivot)
  99. q_sort(numbers, pivot+1, right);
  100. }
  101. /////////////////////////////////// main ////////////////////////////////////////
  102. int main()
  103. {
  104. clock_t start;
  105. int inset[5000],select[5000],merge[5000],Quick[5000];
  106. for(int i=0;i<5000;i++){
  107. Quick[i]=inset[i]=select[i]=merge[i]=rand();}
  108. start=clock();
  109. cout<<"////////////////// InsertionSort ///////////////////////////"<<endl;
  110. start=clock();
  111. InsertionSort(inset,5000);
  112. cout<<"The time is = "<<start-clock()<<endl;
  113. cout<<"////////////////// SeclectionSort///////////////////////////"<<endl;
  114. start=clock();
  115. SeclectionSort(select,5000);
  116. cout<<"The time is = "<<start-clock()<<endl;
  117. cout<<"/////////////////// mergeSort //////////////////////////////"<<endl;
  118. start=clock();
  119. mergeSort(merge,0,5000);
  120. cout<<"The time is = "<<start-clock()<<endl;
  121. cout<<"/////////////////// Quick Sort /////////////////////////////"<<endl;
  122. start=clock();
  123. q_sort(Quick, 0,5000);
  124. cout<<"The time is = "<<start-clock()<<endl;
  125. cout<<"////////////////////////////////////////////////////////////"<<endl;
  126. system("pause");
  127. return 0;
  128. }
  129.  

تم تعديل هذه المشاركة بواسطة Don juan في 10 يوليو 2009 في 15:35

#2

Stack using array

  1. // Done By Don Juan
  2.  
  3.  
  4. #include<iostream>
  5. using namespace std;
  6. //////////////////////////////// stack ////////////////////////
  7. class stack
  8. {
  9. private:
  10. int size;
  11. int *ary;
  12. int top;
  13. public:
  14. stack(int x)
  15. {
  16. size=x;
  17. ary=new int[size];
  18. top=0;
  19. }
  20. bool isEmpty();
  21. bool isfull();
  22. void push(int s);
  23. int pop();
  24. };
  25. //////////////////////////////// isEmpty ////////////////////////
  26. bool stack::isEmpty(){
  27. if(top==0)
  28. return true;
  29. return false ;
  30. }
  31. //////////////////////////////// isFull ////////////////////////
  32. bool stack::isfull(){
  33. if(top<size)
  34. return false;
  35. return true ;
  36. }
  37. //////////////////////////////// push ////////////////////////
  38. void stack::push(int x){
  39.  
  40. if(!isfull()){
  41. cout<<"push "<<x<<endl;
  42. top++;
  43. ary[top]=x;
  44. }
  45. }
  46. //////////////////////////////// pop ////////////////////////
  47. int stack::pop(){
  48. int trevel=0;
  49. if(!isEmpty()){
  50. cout<<endl;
  51. cout<<"pop ";
  52. trevel=ary[top];
  53. top--;}
  54. return trevel;
  55. }
  56.  
  57.  
  58. //////////////////////////////// main ////////////////////////
  59. int main()
  60. {
  61. stack *student=new stack(6);
  62. student->push(9);
  63. student->push(7);
  64. student->push(33);
  65. student->push(21);
  66. student->push(66);
  67. student->push(77);
  68. student->push(88);
  69. student->push(99);
  70. cout<<student->pop()<<endl;
  71. cout<<student->pop()<<endl;
  72. cout<<student->pop()<<endl;
  73. cout<<student->pop()<<endl;
  74. cout<<student->pop()<<endl;
  75. cout<<student->pop()<<endl;
  76. cout<<student->pop()<<endl;
  77. cout<<student->pop()<<endl;
  78.  
  79. cout<<endl;
  80.  
  81. system("pause");
  82. return 0;
  83.  
  84.  
  85. }
  86.  

Stack using linked list

  1. //done by Don juan // stack :::LIFO //
  2.  
  3. #include<iostream>
  4. using namespace std;
  5. ////////////////////////////////////// node /////////////////////////////
  6. struct node
  7. {
  8. node *next;
  9. int data;
  10. node(int d)
  11. {
  12. next=NULL;
  13. data=d;
  14. }
  15. };
  16. //////////////////////////////////// stack //////////////////////////////
  17. class stack
  18. {
  19. private:
  20. node *top;
  21. public:
  22. stack()
  23. {
  24. top=NULL; }
  25.  
  26. void push(int d);
  27. int pop();
  28. bool isempty();
  29. void printstack(stack *s);
  30. void printinverse(stack *s);
  31. float avarege(stack *s);
  32. void deleteitem(stack *s,int f);
  33. };
  34.  
  35.  
  36. //////////////////////////////////// push //////////////////////////////
  37. void stack::push(int x)
  38. {
  39. node *newn=new node(x);
  40. if(top==NULL){
  41. top=newn; }
  42. else
  43. {
  44. newn->next=top;
  45. top=newn;
  46. }
  47. }
  48.  
  49. ////////////////////////////////// print stack uing pointer / in stack class /////////////
  50. void stack::printstack(stack *s)
  51. {
  52. stack *s1=new stack;
  53. while(!s->isempty()){
  54. int d=s->pop();
  55. cout<<d<<endl;
  56. s1->push(d);}
  57. while(!s1->isempty()){
  58. s->push(s1->pop());}
  59. }
  60. ////////////////////////////////// print inverse uing pointer / in stack class /////////////
  61. void stack::printinverse(stack *s)
  62. {
  63. stack *s1=new stack;
  64. while(!s->isempty()){
  65. int d=s->pop();
  66. s1->push(d);}
  67. while(!s1->isempty()){
  68. int d=s1->pop();
  69. cout<<d<<endl;
  70. s->push(d);}
  71. }
  72. ///////////////////////////// printstack without using pointer /out stack class ///////////
  73. void printstack1(stack s) /// call this function in function main like this//printstack1(*s)
  74. {
  75. stack s1;
  76. while(!s.isempty()){
  77. int d=s.pop();
  78. cout<<d<<endl;
  79. s1.push(d);}
  80. while(!s1.isempty()){
  81. s.push(s1.pop());}
  82. }
  83.  
  84. //////////////////////////////////// IsEmpty ///////////////////////////
  85. bool stack::isempty()
  86. {
  87.  
  88. return top==NULL;
  89. }
  90.  
  91. //////////////////////////////////// pop //////////////////////////////
  92.  
  93. int stack::pop()
  94. {
  95. if(isempty())
  96. return -1;
  97. int d=top->data;
  98. node *temp=top;
  99. top=top->next;
  100. delete temp;
  101. return d;
  102. }
  103. ////////////////////////////////// avrage /////////////////////////////////
  104. float stack::avarege(stack *s)
  105. {
  106. stack *s1=new stack;
  107. int sum=0;
  108. int count=0;
  109. int d;
  110. while(!s->isempty()){
  111. d=s->pop();
  112. sum=sum+d;
  113. count++;
  114. s1->push(d);}
  115. while(!s1->isempty()){
  116. s->push(s1->pop());}
  117. return sum/count;
  118. }
  119. //////////////////////////////////// delete item //////////////////////////////
  120. void stack::deleteitem(stack *s,int f)
  121. {
  122. stack *s1=new stack;
  123. while(!s->isempty()){
  124. int d=s->pop();
  125. if(d!=f){
  126. s1->push(d);}}
  127. while(!s1->isempty()){
  128. s->push(s1->pop());}
  129.  
  130. }
  131. ////////////////////////////////////// main /////////////////////////////
  132. int main()
  133. {
  134. stack *s=new stack;
  135. for(int i=0;i<5;i++){
  136. s->push(rand()%100);}
  137. s->printstack(s);
  138. cout<<"averge is "<<s->avarege(s)<<endl;
  139. s->printinverse(s);
  140. s->deleteitem(s,34);
  141. s->printstack(s);
  142. system("pause");
  143.  
  144. return 0;
  145. }
  146.  

#3

queue using linked list

  1. //done by Don juan // queue :::FIFO //
  2.  
  3. #include<iostream>
  4. using namespace std;
  5. ////////////////////////////////////// node /////////////////////////////
  6. struct node
  7. {
  8. node *next;
  9. int data;
  10. node(int d)
  11. {
  12. next=NULL;
  13. data=d;
  14. }
  15. };
  16. //////////////////////////////////// stack //////////////////////////////
  17. class queue
  18. {
  19. private:
  20. node *first;
  21. public:
  22. queue()
  23. {
  24. first=NULL; }
  25.  
  26. void enqueue(int d);
  27. int dequeue();
  28. bool isempty();
  29. void printqueue(queue *q);
  30. float avrege(queue *q);
  31. };
  32.  
  33.  
  34. //////////////////////////////////// enqueue //////////////////////////////
  35. void queue::enqueue(int x)
  36. {
  37. node *newn=new node(x);
  38. if(first==NULL){
  39. first=newn; }
  40. else
  41. {
  42. node *temp=first;
  43. while(temp->next!=NULL)
  44. temp=temp->next;
  45. temp->next=newn;
  46.  
  47. }
  48. }
  49.  
  50. ////////////////////////////////// printqueue /////////////
  51. void queue::printqueue(queue *q)
  52. {
  53. queue *q1=new queue;
  54. while(!q->isempty()){
  55. int d=q->dequeue();
  56. cout<<d<<endl;
  57. q1->enqueue(d);}
  58. while(!q1->isempty()){
  59. q->enqueue(q1->dequeue());}
  60. }
  61.  
  62. //////////////////////////////////// IsEmpty ///////////////////////////
  63. bool queue::isempty()
  64. {
  65.  
  66. return first==NULL;
  67. }
  68.  
  69. //////////////////////////////////// dequeue //////////////////////////////
  70.  
  71. int queue::dequeue()
  72. {
  73. if(isempty())
  74. return -1;
  75. int d=first->data;
  76. node *temp=first;
  77. first=first->next;
  78. delete temp;
  79. return d;
  80. }
  81. ////////////////////////////////// avrage /////////////////////////////////
  82. float queue::avrege(queue *q)
  83. {
  84. queue *q1=new queue;
  85. int sum=0;
  86. int count=0;
  87. int d;
  88. float avg;
  89. while(!q->isempty()){
  90. d=q->dequeue();
  91. sum=sum+d;
  92. count++;
  93. q1->enqueue(d);}
  94. while(!q1->isempty()){
  95. q->enqueue(q1->dequeue());}
  96. avg=sum/count;
  97. return avg;
  98. }
  99. ////////////////////////////////////// main /////////////////////////////
  100. int main()
  101. {
  102. queue *q=new queue;
  103. for(int i=0;i<9;i++){
  104. q->enqueue(rand()%100);}
  105. q->printqueue(q);
  106. cout<<q->dequeue()<<endl;
  107. cout<<q->dequeue()<<endl;
  108. cout<<q->dequeue()<<endl;
  109. cout<<q->dequeue()<<endl;
  110. cout<<q->avrege(q);
  111. system("pause");
  112. return 0;
  113. }
  114.  

Binary Search

  1.  
  2. #include<iostream>
  3. using namespace std;
  4. bool search(int a[], int key, int low, int high) {
  5. if (high < low) {
  6. return 0;
  7. }
  8. int mid = (low + high) / 2;
  9. if (key>a[mid]) {
  10. return 0;
  11. } else if (key<a[mid]) {
  12. return 23;
  13. } else {
  14. return 3;
  15. }
  16. return 1;
  17. }
  18. int main()
  19. {
  20. int a[4];
  21. for(int i=0;i<4;i++){
  22. a[i]=i;}
  23. cout<<search(a,7,0,4)<<endl;
  24. system("pause");
  25. return 0;
  26. }
  27.  

#4

Hash table ,هذا مثال يوضح سرعة الهاش تيبل في البحث

  1. //Done By Don juan // hash table
  2.  
  3. #include<iostream>
  4. using namespace std;
  5. /////////////////////////////// Node ///////////////////////////////
  6. struct node
  7. {
  8. int data;
  9. node *next;
  10. node(int d)
  11. {
  12. data=d;
  13. next=NULL;
  14. }};
  15. /////////////////////////////// linkedlist ///////////////////////////////
  16. class linkedlist
  17. {
  18. private:
  19. node *head;
  20. public:
  21. linkedlist()
  22. {
  23. head=NULL;
  24. }
  25. node *gethead()
  26. {
  27. return head;}
  28. void addlast(int s);
  29. int seach(int s);
  30.  
  31. };
  32. //////////////////////////////// addlast //////////////////////////////
  33. void linkedlist::addlast(int n)
  34. {
  35. node *newn=new node(n);
  36. if(head==NULL){
  37. head=newn; }
  38. else {
  39. node *temp=head;
  40. while(temp->next!=NULL)
  41. {
  42. temp=temp->next;
  43. }
  44. temp->next=newn;
  45. }}
  46. //////////////////////////////// search /////////////////////////////
  47. int linkedlist::seach(int s)
  48. {
  49. int count=0;
  50. if(head==NULL){
  51. return 0; }
  52. node *temp=head;
  53. while(temp!=NULL){
  54. count++;
  55. if(temp->data==s){
  56. return count ;}
  57. temp=temp->next;}
  58. return count;
  59. }
  60.  
  61. //////////////////////////////// hash //////////////////////////////
  62. class hash
  63. {
  64. public:
  65. linkedlist **table;
  66.  
  67. hash()
  68. {
  69. table=new linkedlist *[500];
  70. for(int i=0;i<500;i++)
  71. table[i]=new linkedlist();
  72. }
  73. int hashfunction(int x)
  74. {
  75. return x%500;
  76. }
  77. void insert(int x){
  78. int pos=hashfunction(x);
  79. table[pos]->addlast(x);}
  80. int seach(int x)
  81. {
  82. int pos=hashfunction(x);
  83. int s=table[pos]->seach(x);
  84. return s;
  85. }
  86.  
  87. };
  88. //////////////////////////////// main //////////////////////////////
  89. int main()
  90. {
  91.  
  92. hash *h=new hash;
  93. for(long i=0;i<100000;i++){
  94. h->insert(rand());}
  95. double sum=0;
  96. int pos;
  97. for(long i=0;i<80000;i++){
  98. pos=h->seach(rand());
  99. sum=sum+pos;}
  100. float avarege=sum/80000;
  101. cout<<"the avarege = "<<avarege<<endl;
  102. system("pause");
  103. return 0;
  104. }
  105.  

#5

singal linked list

  1. // Done By Don Juan //
  2.  
  3. #include<iostream>
  4. using namespace std;
  5. /////////////////////////////// Node ///////////////////////////////
  6. struct node
  7. {
  8. int data;
  9. node *next;
  10. node(int d)
  11. {
  12. data=d;
  13. next=NULL;
  14. }};
  15. /////////////////////////////// linkedlist ///////////////////////////////
  16. class linkedlist
  17. {
  18. private:
  19. node *head;
  20.  
  21. public:
  22. linkedlist()
  23. {
  24. head=NULL;
  25. }
  26. void addfirst(int s);
  27. void addlast(int s);
  28. void addafter(int d,int s);
  29. void addbefor(int d,int s);
  30. void addbefor1(int s,int j); // another way for addbefor
  31. void deletfirst();
  32. void deletlast();
  33. int getlength();
  34. int getinfolast();
  35. void deletedlink();
  36. int getmax();
  37. int getmaxp();
  38. void display();
  39. void dd();
  40.  
  41. };
  42. /////////////////////////////// ffffffffff ////////////////////////////////
  43. void linkedlist::dd()
  44. {
  45. if(head==NULL)
  46. return;
  47. node *p=head;
  48. node *q=head;
  49. while(p!=NULL){
  50. if(p->next!=NULL){
  51. q=p->next;
  52. p->next=p->next->next;
  53. delete q;
  54. }
  55. else
  56. break ;
  57.  
  58. }}
  59.  
  60. /////////////////////////////// add as first ////////////////////////////////
  61. void linkedlist::addfirst(int d)
  62. {
  63. node *nw=new node(d);
  64. nw->next=head;
  65. head=nw;
  66.  
  67. }
  68. ///////////////////////////// display /////////////////////////////////
  69. void linkedlist::display()
  70. {
  71. if(head==NULL){
  72. cout<<"The linked list is empaty"<<endl;return ;}
  73. node *temp;
  74. temp=head;
  75. while(temp!=NULL)
  76. {
  77. cout<<" The numbers is "<<temp->data<<endl;
  78. temp=temp->next;
  79. }
  80. }
  81. ///////////////////////////// get length /////////////////////////////////
  82. int linkedlist::getlength()
  83. {
  84. node *temp;
  85. int n=0;
  86. temp=head;
  87. while(temp!=NULL)
  88. {
  89.  
  90. temp=temp->next;
  91. n++;
  92. }
  93. return n;
  94. }
  95. ///////////////////////////// get_info_from_last /////////////////////////////////
  96. int linkedlist::getinfolast()
  97. {
  98. node *temp;
  99. temp=head;
  100. while(temp->next!=NULL)
  101. {
  102.  
  103. temp=temp->next;
  104. }
  105. return temp->data;
  106. }
  107. //////////////////////////////// addlast //////////////////////////////
  108. void linkedlist::addlast(int n)
  109. {
  110. node *newn=new node(n);
  111. if(head==NULL){
  112. head=newn; }
  113. else {
  114. node *temp=head;
  115.  
  116. while(temp->next!=NULL)
  117. {
  118. temp=temp->next;
  119. }
  120. temp->next=newn;
  121.  
  122. }}
  123. //////////////////////////////// addafter //////////////////////////////
  124. void linkedlist::addafter(int n,int d)
  125. {
  126. if(head==NULL)
  127. return ;
  128. node *newn=new node(n);
  129. node *temp=head;
  130.  
  131. while(temp!=NULL&&temp->data!=d)
  132. {
  133. temp=temp->next;
  134. }
  135. if(temp!=NULL){
  136. newn->next=temp->next;
  137. temp->next=newn;
  138.  
  139. }}
  140. //////////////////////////////// addbefor1 //////////////////////////////
  141. void linkedlist::addbefor1(int d,int f )
  142. {
  143. if(head==NULL)
  144. return ;
  145. node *newn=new node(d);
  146. if(head->data==f){
  147. newn->next=head;
  148. head=newn;}
  149.  
  150. else {
  151. node *temp=head;
  152. node *beforlast=head;
  153. while(temp!=NULL&&temp->data!=f){
  154. beforlast=temp;
  155. temp=temp->next;}
  156. if(temp!=NULL){
  157. newn->next=temp;
  158. beforlast->next=newn;}}}
  159.  
  160. //////////////////////////////// addbefor //////////////////////////////
  161. void linkedlist::addbefor(int n,int d)
  162. {
  163. if(head==NULL)
  164. return ;
  165. node *newn=new node(n);
  166. node *temp=head;
  167. if(head->data==d){
  168. newn->next=head;
  169. head=newn;}
  170. else
  171. {
  172. node *z=head;
  173. while(temp!=NULL&&temp->data!=d)
  174. {
  175. temp=temp->next;
  176. }
  177. if(temp!=NULL){
  178. while(z->next!=temp)
  179. {
  180. z=z->next;
  181. }
  182. z->next=newn;
  183. newn->next=temp;
  184.  
  185. }}}
  186. //////////////////////////////// deletefirst //////////////////////////////
  187.  
  188. void linkedlist::deletfirst()
  189. {
  190. if(head==NULL)
  191. return ;
  192. node *temp=head;
  193. head=head->next;
  194. delete temp;
  195. }
  196. //////////////////////////////// deletelast //////////////////////////////
  197.  
  198. void linkedlist::deletlast()
  199. {
  200. node *temp,*temp2;
  201. if(head==NULL){
  202. cout<<"it is Empty"<<endl;}
  203. else {
  204. temp=head;
  205. if(temp->next==NULL){
  206. delete temp;
  207. head=NULL;}
  208. else {
  209. while(temp->next!=NULL){
  210. temp2=temp;
  211. temp=temp->next;}
  212. delete temp;
  213. temp2->next=NULL;}}
  214. }
  215.  
  216. //////////////////////////////// deletedlink //////////////////////////////
  217.  
  218. void linkedlist::deletedlink()
  219. {
  220. node *q=head;
  221.  
  222. while(head!=NULL){
  223. q=head;
  224. head=head->next;
  225. delete q;
  226.  
  227. }
  228.  
  229. }
  230. //////////////////////////////// getmax //////////////////////////////
  231.  
  232. int linkedlist::getmax()
  233. {
  234. if(head==NULL)
  235. return 0 ;
  236. node *temp=head;
  237. node *max=head;
  238. while(temp!=NULL){
  239. if(max->data<temp->data)
  240. max=temp;
  241. temp=temp->next;}
  242. return max->data ;
  243.  
  244. }
  245. //////////////////////////////// get position of max //////////////////////////////
  246.  
  247. int linkedlist::getmaxp()
  248. {
  249. if(head==NULL)
  250. return 0 ;
  251. node *temp=head;
  252. int max=head->data;
  253. int count=1,maxp=1;
  254. while(temp!=NULL){
  255. if(max<temp->data){
  256. max=temp->data;
  257. maxp=count;}
  258. count++;
  259. temp=temp->next;}
  260. return maxp ;
  261.  
  262. }
  263. //////////////////////////////////// main //////////////////////////
  264. int main()
  265. {
  266. linkedlist *l=new linkedlist ;
  267. l->addlast(55);
  268. l->addlast(111);
  269. l->addlast(432);
  270. l->addlast(111);
  271. l->addlast(324);
  272. l->addlast(2222);
  273.  
  274. l->display();
  275. l->dd();
  276. cout<<"fffffffffffffffffffffff"<<endl;
  277. l->display();
  278.  
  279. system("pause");
  280. return 0;
  281. }
  282.  

merge two linked list

  1. // Done By DoN JuAn //
  2.  
  3. #include<iostream>
  4. using namespace std;
  5. /////////////////////////////// Node ///////////////////////////////
  6. struct node
  7. {
  8. int data;
  9. node *next;
  10. node(int d)
  11. {
  12. data=d;
  13. next=NULL;
  14. }};
  15. /////////////////////////////// linkedlist ///////////////////////////////
  16. class linkedlist
  17. {
  18. private:
  19. node *head;
  20.  
  21. public:
  22. linkedlist()
  23. {
  24. head=NULL;
  25. }
  26. void addlast(int s);
  27. void display();
  28. void mwrgey(linkedlist *&l1,linkedlist *&l2);
  29.  
  30. };
  31.  
  32. ///////////////////////////// display /////////////////////////////////
  33. void linkedlist::display()
  34. {
  35. if(head==NULL){
  36. cout<<"The linked list is empaty"<<endl;return ;}
  37. node *temp;
  38. temp=head;
  39. while(temp!=NULL)
  40. {
  41. cout<<" The numbers is "<<temp->data<<endl;
  42. temp=temp->next;
  43. }
  44. }
  45.  
  46. //////////////////////////////// addlast //////////////////////////////
  47. void linkedlist::addlast(int n)
  48. {
  49. node *newn=new node(n);
  50. if(head==NULL){
  51. head=newn; }
  52. else {
  53. node *temp=head;
  54.  
  55. while(temp->next!=NULL)
  56. {
  57. temp=temp->next;
  58. }
  59. temp->next=newn;
  60.  
  61. }}
  62. //////////////////////////////// merge ////////////////////////////
  63. void linkedlist::mwrgey(linkedlist *&l1,linkedlist *&l2)
  64. {
  65. node *p=l1->head;
  66. node *p2=l2->head;
  67. if(p==NULL||p2==NULL)
  68. return ;
  69. node *temp;
  70. if(p->data<p2->data){
  71. temp=p;
  72. p=p->next;
  73. }
  74. else {
  75. temp=p2;
  76. p2=p2->next;}
  77. while(p2!=NULL&&p!=NULL){
  78. if(p->data<p2->data){
  79. temp->next=p;
  80. temp=temp->next;
  81. p=p->next;
  82. }
  83. else {
  84. temp->next=p2;
  85. temp=temp->next;
  86. p2=p2->next;}
  87. }
  88. if(p2!=NULL){
  89. temp->next=p2;}
  90. if(p!=NULL){
  91. temp->next=p;}
  92. }
  93. //////////////////////////////////// main //////////////////////////
  94. int main()
  95. {
  96. linkedlist *L=new linkedlist;
  97. linkedlist *L2=new linkedlist;
  98. L->addlast(3);
  99. L->addlast(5);
  100. L->addlast(7);
  101. L->addlast(12);
  102. L->addlast(19);
  103. cout<<"///////////// first linked list /////////////////"<<endl;
  104. L->display();
  105. L2->addlast(6);
  106. L2->addlast(8);
  107. L2->addlast(10);
  108. L2->addlast(13);
  109. L2->addlast(20);
  110. L2->addlast(33);
  111. cout<<"///////////// second linked list /////////////////"<<endl;
  112. L2->display();
  113. L->mwrgey(L,L2);
  114. cout<<"///////////// after merge in first linked list /////////////////"<<endl;
  115. L->display();
  116. system("pause");
  117. return 0;
  118. }
  119.  
  120.  

#6

Add with priority in linked list

  1.  
  2. #include<iostream>
  3. using namespace std;
  4. ////////////////////////////////////// node /////////////////////////////
  5. struct node
  6. {
  7. node *next;
  8. node *prev;
  9. int data;
  10. int priority;
  11. node(int r,int y)
  12. {
  13. next=NULL;
  14. prev=NULL;
  15. data=r;
  16. priority=y;
  17. }
  18. };
  19. ////////////////////////////////////// linked list /////////////////////////////
  20. class linkedlist
  21. {
  22. private:
  23. node *front;
  24. node *back;
  25. public:
  26. linkedlist()
  27. {
  28. front=NULL;
  29. back=NULL;
  30. }
  31. void display();
  32. void addpiorty(int x,int y);
  33.  
  34. };
  35. ////////////////////////////////////// add piorty /////////////////////////////
  36. void linkedlist::addpiorty(int x,int y)
  37. {
  38. node *newitem=new node(x,y);
  39. if(front==NULL){
  40. front=newitem;
  41. back=newitem;}
  42. else
  43. {
  44. if(back->priority==y){
  45. newitem->prev=back;
  46. back->next=newitem;
  47. back=newitem;}
  48. else {
  49. node *temp=front;
  50. while(temp!=NULL&&temp->priority!=y)
  51. temp=temp->next;
  52. if(temp==NULL){
  53. newitem->prev=back;
  54. back->next=newitem;
  55. back=newitem;}
  56. else {
  57. newitem->next=temp->next;
  58. newitem->prev=temp;
  59. temp->next->prev=newitem;
  60. temp->next=newitem;}
  61. }
  62.  
  63.  
  64. }
  65.  
  66. };
  67.  
  68. ////////////////////////////////////// disply /////////////////////////////
  69. void linkedlist::display()
  70. {
  71. node *temp=back;
  72. while(temp!=NULL)
  73. {
  74. cout<<temp->data<<" "<<temp->priority<<endl;
  75. temp=temp->prev;
  76. }
  77. };
  78.  
  79. ////////////////////////////////////// main /////////////////////////////
  80. int main()
  81. {
  82. linkedlist *student=new linkedlist();
  83. for(int i=0;i<20;i++){
  84. student->addpiorty(rand()%100,rand()%10);}
  85.  
  86.  
  87.  
  88. student->display();
  89. cout<<"///////////////////////////////////"<<endl;
  90.  
  91.  
  92.  
  93. system("pause");
  94. return 0;
  95. }
  96.  

#7

Binary Tree

  1.  
  2. #include<iostream>
  3. using namespace std;
  4. struct Node
  5. {
  6. int data;
  7. Node *left;
  8. Node *right;
  9. Node(int d)
  10. {
  11. data=d;
  12. left=NULL;
  13. right=NULL;
  14. }};
  15. class Tree
  16. {
  17. private:
  18. Node *root;
  19. public:
  20. Tree()
  21. { root=NULL;}
  22. bool isEmpt()
  23. {return root==NULL;}
  24. void insert(Node *&p,int d);
  25. bool Search(Node *t, int d);
  26. void print(Node *p);
  27. void insert(int f)
  28. {insert(root,f);}
  29. void print()
  30. {print(root);}
  31. bool Search(int d)
  32. { Search(root,d);}
  33. int roorty(Node *p)
  34. {
  35. if(p==NULL)
  36. return 0;
  37. return p->data;}
  38. int roorty()
  39. {return roorty(root);}
  40. };
  41. //////////////////////////// search//////////////////////////////////////
  42. bool Tree::Search(Node *t, int d)
  43. {
  44. if (t == NULL)
  45. return false;
  46.  
  47. if (t->data == d)
  48. return true;
  49.  
  50. if (d < t->data)
  51. return Search(t->left, d);
  52. else
  53. return Search(t->right, d);
  54. }
  55. //////////////////////////////////// insert //////////////////////////////
  56. void Tree::insert(Node *&p, int d)
  57. {
  58. if(p==NULL)
  59. {
  60. p=new Node(d);
  61. }
  62.  
  63. else{
  64. if(d<p->data)
  65. insert(p->left,d);
  66. else
  67. insert(p->right,d);
  68. }
  69. }
  70. ///////////////////////////////// ptint /////////////////////////////////
  71. void Tree::print(Node *p)
  72. {
  73. if(p==NULL)
  74. return;
  75. cout<<p->data<<endl;
  76. print(p->left);
  77. print(p->right);
  78.  
  79.  
  80.  
  81. }
  82. /////////////////////////////////////////////////////////////////
  83. int main()
  84. {
  85. Tree *t=new Tree;
  86. for(int d=0;d<10;d++){
  87. t->insert(rand()%100);}
  88.  
  89. t->print();
  90. cout<<"welcome anwar"<<endl<<t->roorty();
  91. system("pause");
  92. return 0;
  93. }
  94.  

second max and minim in Tree

  1.  
  2. #include<iostream>
  3. #include<ctime>
  4. using namespace std;
  5. struct Node
  6. {
  7. int data;
  8. Node *left;
  9. Node *right;
  10. Node(int d)
  11. {
  12. data=d;
  13. left=NULL;
  14. right=NULL;
  15. }};
  16. class Tree
  17. {
  18. private:
  19. Node *root;
  20. public:
  21. Tree()
  22. { root=NULL;}
  23. bool isEmpt()
  24. {return root==NULL;}
  25. void insert(Node *&p,int d);
  26. bool Search(Node *t, int d);
  27. void print(Node *p);
  28. void insert(int f)
  29. {insert(root,f);}
  30. void print()
  31. {print(root);}
  32. bool Search(int d)
  33. { Search(root,d);}
  34. int roorty(Node *p)
  35. {
  36. if(p==NULL)
  37. return 0;
  38. return p->data;}
  39. int roorty()
  40. {return roorty(root);}
  41. int max();
  42. int minim();
  43.  
  44. };
  45. //////////////////////////// search//////////////////////////////////////
  46. bool Tree::Search(Node *t, int d)
  47. {
  48. if (t == NULL)
  49. return false;
  50.  
  51. if (t->data == d)
  52. return true;
  53.  
  54. if (d < t->data)
  55. return Search(t->left, d);
  56. else
  57. return Search(t->right, d);
  58. }
  59. //////////////////////////////////// insert //////////////////////////////
  60. void Tree::insert(Node *&p, int d)
  61. {
  62. if(p==NULL)
  63. {
  64. p=new Node(d);
  65. }
  66.  
  67. else{
  68. if(d<p->data)
  69. insert(p->left,d);
  70. else
  71. insert(p->right,d);
  72. }
  73. }
  74.  
  75.  
  76. ///////////////////////////////// second max /////////////////////////////////
  77. int Tree::max()
  78. {
  79. Node *p=root;
  80. bool flag=true;
  81. while(p!=NULL){
  82. ////////////////////////////// if root has right child
  83. if(root->right!=NULL){
  84. if(p->right!=NULL){
  85. if(p->right->right==NULL&&flag==true){
  86. if(p->right->left==NULL)
  87. return p->data;
  88. else {
  89. p=p->right->left; // Exit 1
  90. flag=false;}}}
  91. if(p->right==NULL)
  92. return p->data;
  93. p=p->right;} // heigh way
  94.  
  95. ////////////////////////////// if root has left child just
  96. if(root->right==NULL){
  97. if(flag==true){
  98. p=p->left;
  99. flag=false;}
  100. else if (p->right==NULL)
  101. return p->data;
  102. else p=p->right;}
  103. }
  104. return -1;
  105. }
  106. ///////////////////////////////// second mininum /////////////////////////////////
  107. int Tree::minim(){
  108. Node *p=root;
  109. bool flag=true;
  110. while(p!=NULL){
  111. ////////////////////////////// if root has right child
  112. if(root->left!=NULL){
  113. if(p->left!=NULL){
  114. if(p->left->left==NULL&&flag==true){
  115. if(p->left->right==NULL)
  116. return p->data;
  117. else {
  118. p=p->left->right; // Exit 1
  119. flag=false;}}}
  120. if(p->left==NULL)
  121. return p->data;
  122. p=p->left;} // heigh way
  123.  
  124. ////////////////////////////// if root has left child just
  125. if(root->left==NULL){
  126. if(flag==true){
  127. p=p->right;
  128. flag=false;}
  129. else if (p->left==NULL)
  130. return p->data;
  131. else p=p->left;}
  132. }
  133. return -1;
  134. }
  135. ///////////////////////////////// ptint /////////////////////////////////
  136. void Tree::print(Node *p)
  137. {
  138. if(p==NULL)
  139. return;
  140.  
  141. print(p->left);
  142. cout<<p->data<<endl;
  143. print(p->right);
  144.  
  145.  
  146.  
  147. }
  148. /////////////////////////////////////////////////////////////////
  149. int main()
  150. {
  151. srand(time(0));
  152. Tree *t=new Tree;
  153. for(int i=0;i<90;i++){
  154. t->insert(rand());}
  155.  
  156.  
  157. t->print();
  158.  
  159. cout<<" The second max is "<<t->max()<<endl;
  160. cout<<" The second minim is "<<t->minim()<<endl;
  161.  
  162. cout<<"welcome anwar"<<endl<<t->roorty();
  163. system("pause");
  164. return 0;
  165. }
  166.  
  167.  

another way to get the second max and minim

  1. int Tree::secondmax(Node *p)
  2. {
  3.  
  4. if (root==NULL||root->right==NULL&&root->left==NULL)
  5. return -1;
  6.  
  7. else if (root->right==NULL )
  8. {
  9. Node *q=root->left;
  10.  
  11. while(q ->right!=NULL)
  12.  
  13. q=q->right;
  14. return q->data;
  15. }
  16.  
  17.  
  18.  
  19. else if (root->right->left==NULL&&root->right->right==NULL)
  20. return root->data;
  21.  
  22. else if (root->right ->right==NULL&&root->right ->left!=NULL)
  23. { Node *q=root->right ->left;
  24. while(q ->right!=NULL)
  25. q=q->right;
  26. return q->data;
  27. }
  28.  
  29. else{
  30.  
  31. Node*q=root;
  32.  
  33. while(q ->right!=NULL)
  34.  
  35. q=q->right;
  36. if (q ->left!=NULL)
  37. {
  38. Node *p=q->left;
  39.  
  40. while(q ->right!=NULL)
  41. p=p->right;
  42. return p->data;}
  43.  
  44. else {
  45. Node*p =root;
  46.  
  47. while(p->right->right!=NULL)
  48. p=p->right;
  49. return p-> data;
  50. }
  51.  
  52. }
  53. }
  54.  

#8

Tree project /Read and Write to file

مشروع للقراء والكتابة في ملف بستخدام بينري تري ملاحظة يجب ان يكون الملف في نفس مجلد الكود وان تكون البيانات مخزنة في ملف txt

like this :

12 anwar 101234543

22 moner 101283552

13 fisel 101287131

مثال على الملف في المرفقات

  1. //Binary Search Tree Program //Done By Anwar//
  2.  
  3. #include <string>
  4. #include <fstream>
  5. #include<iostream>
  6. using namespace std;
  7. //////////////////////////////////// Node //////////////////////////////////////
  8. struct Node
  9. {
  10. long id;
  11. int key;
  12. string name;
  13. Node *left;
  14. Node *right;
  15. Node(long i,int k,string n)
  16. {
  17. id=i;
  18. key=k;
  19. name=n;
  20. left=NULL;
  21. right=NULL;
  22. }};
  23. ///////////////////////////////////// tree Class //////////////////////////////
  24. class Tree
  25. {
  26. private:
  27. Node *root;
  28. public:
  29. Tree()
  30. { root=NULL;}
  31. void insert(Node *&p,long i,int k,string n);
  32. bool Search(Node *t, long d);
  33. void print(Node *p);
  34. void insert(long i,int k,string n)
  35. {insert(root,i, k, n);}
  36. void print()
  37. {print(root);}
  38. bool Search(long d)
  39. { return Search(root,d);}
  40. void deleted(int n);
  41.  
  42. };
  43. //////////////////////////////////// searching //////////////////////////////
  44. bool Tree::Search(Node *t, long i)
  45. {
  46. if (t == NULL){
  47. cout<<" Data not found!"<<endl;
  48. return false;}
  49.  
  50. if (t->key== i){
  51. cout<<" Data found "<<endl;
  52. return true;}
  53.  
  54. if (i < t->key)
  55. return Search(t->left, i);
  56. else
  57. return Search(t->right, i);
  58. }
  59. //////////////////////////////////// delete //////////////////////////////
  60. void Tree::deleted(int dkey)
  61.  
  62. {
  63. //Locate the element
  64. bool found = false;
  65. if(root==NULL)
  66. {
  67. cout<<" This Tree is empty! "<<endl;
  68. return;
  69. }
  70.  
  71. Node* curr;
  72. Node* parent;
  73. curr = root;
  74.  
  75. while(curr)
  76. {
  77. if(curr->key==dkey)
  78. {
  79. found = true;
  80. break;
  81. }
  82. else
  83. {
  84. parent = curr;
  85. if(curr->key<dkey)
  86. curr = curr->right;
  87. else
  88. curr = curr->left;
  89. }
  90. }
  91. if(!found)
  92. {
  93. cout<<" Data not found! "<<endl;
  94. return;
  95. }
  96.  
  97.  
  98. // 3 cases :
  99. // 1. We're removing a leaf node
  100. // 2. We're removing a node with a single child
  101. // 3. we're removing a node with 2 children
  102.  
  103. // Node with single child
  104. if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
  105. && curr->right == NULL))
  106. {
  107. if(curr->left == NULL && curr->right != NULL)
  108. {
  109. if(parent->left == curr)
  110. {
  111. parent->left = curr->right;
  112. delete curr;
  113. }
  114. else
  115. {
  116. parent->right = curr->right;
  117. delete curr;
  118. }
  119. }
  120. else // left child present, no right child
  121. {
  122. if(parent->left == curr)
  123. {
  124. parent->left = curr->left;
  125. delete curr;
  126. }
  127. else
  128. {
  129. parent->right = curr->left;
  130. delete curr;
  131. }
  132. }
  133. return;
  134. }
  135.  
  136. //We're looking at a leaf node
  137. if( curr->left == NULL && curr->right == NULL)
  138. {
  139. if(parent->left == curr)//////////error///////////////////
  140. parent->left = NULL;
  141. else
  142. parent->right = NULL;
  143. delete curr;
  144. return;
  145. }
  146. //Node with 2 children
  147. // replace node with smallest value in right subtree
  148. if (curr->left != NULL && curr->right != NULL)
  149. {
  150. Node *chkr;
  151. chkr = curr->right;
  152. if((chkr->left == NULL) && (chkr->right == NULL))
  153. {
  154. curr = chkr;
  155. delete chkr;
  156. curr->right = NULL;
  157. }
  158. else // right child has children
  159. {
  160. //if the node's right child has a left child
  161. // Move all the way down left to locate smallest element
  162.  
  163. if((curr->right)->left != NULL)
  164. {
  165. Node* lcurr;
  166. Node* lcurrp;
  167. lcurrp = curr->right;
  168. lcurr = (curr->right)->left;
  169. while(lcurr->left != NULL)
  170. {
  171. lcurrp = lcurr;
  172. lcurr = lcurr->left;
  173. }
  174. curr->key = lcurr->key;
  175. delete lcurr;
  176. lcurrp->left = NULL;
  177. }
  178. else
  179. {
  180. Node* tmp;
  181. tmp = curr->right;
  182. curr->key = tmp->key;
  183. curr->right = tmp->right;
  184. delete tmp;
  185. }
  186.  
  187. }
  188. return;
  189. }
  190.  
  191. }
  192.  
  193. //////////////////////////////////// inserting //////////////////////////////
  194. void Tree::insert(Node *&p, long i,int k ,string n)
  195. {
  196. if(p==NULL)
  197. {
  198. p=new Node(i,k,n);
  199. }
  200.  
  201. else{
  202. if(k<p->key)
  203. insert(p->left,i,k,n);
  204. else
  205. insert(p->right,i,k,n);
  206. }
  207. }
  208. ///////////////////////////////// ptint tree /////////////////////////////////
  209. void Tree::print(Node *p)
  210. {
  211. if(root==NULL)
  212. cout<< " File Is Empty ";
  213. if(p==NULL)
  214. return;
  215.  
  216. print(p->left);
  217. cout<<" "<<p->key<<" "<<p->name<<" "<<p->id<<endl;
  218. print(p->right);
  219.  
  220. }
  221. //////////////////////////////// main function /////////////////////////////////
  222. int main()
  223. {
  224. Tree *t=new Tree;
  225. string m,m2,m7;
  226. long r,r2,f,k,k2,r7;
  227. int n,k7;
  228. /////////////////////////////// reading ///////////////////////////////////////
  229. ifstream fin("dp3x.txt");
  230.  
  231. while ( !fin.eof() )
  232. {
  233. fin >>k >> m >> r;
  234. t->insert(r,k,m);
  235.  
  236.  
  237. }
  238. fin.close();
  239. ////////////////////////////// choosing /////////////////////////////////////////
  240. while(1)
  241. {
  242. cout<<endl<<endl;
  243. cout<<" Binary Search Tree Operations "<<endl;
  244. cout<<" ----------------------------- "<<endl;
  245. cout<<" 1. Insertion/Creation "<<endl;
  246. cout<<" 2. Print "<<endl;
  247. cout<<" 3. Find "<<endl;
  248. cout<<" 4. Removal "<<endl;
  249. cout<<" 5. Exit "<<endl;
  250. cout<<" Enter your choice : ";
  251. cin>>n;
  252. switch(n){
  253. case 2:{
  254. cout<<" --------------------"<<endl;
  255. t->print();
  256. break;}
  257. case 1:{
  258. cout<<" --------------------"<<endl;
  259. cout << " Enter key: ";
  260. cin>>k2;
  261. cout << " Enter the name:";
  262. cin>>m2;
  263. cout<<" Enter the number: ";
  264. cin>>r2;
  265. t->insert(r2,k2,m2);
  266. ofstream filestr;
  267. filestr.open ("dp3x.txt",fstream::app);
  268.  
  269. filestr<<k2<<" "<<m2<<" "<<r2<<endl;
  270. filestr.close();
  271. break ; }
  272. case 3:{
  273. cout<<" --------------------"<<endl;
  274. cout<<" Enter key: ";
  275. cin>>f;
  276. t->Search(f);
  277. break ; }
  278. case 5:{
  279. cout<<" --------------------"<<endl;
  280. exit(1);
  281. break ; }
  282.  
  283. case 4:{
  284. cout<<" --------------------"<<endl;
  285. cout<<" Enter key: ";
  286. cin>>f;
  287. t->deleted(f);
  288. /////////////////////////////////deleted from file /////////////////////////
  289.  
  290. fstream filestr;
  291. filestr.open ("outfile.txt",fstream::app);
  292.  
  293. ifstream fin("dp3x.txt");
  294. fin >>k7 >> m7 >> r7;
  295. while ( !fin.eof() )
  296. {
  297. if(k7!=f){
  298. filestr << k7 <<" "<<m7<<" "<<r7<<endl;}
  299. fin >>k7 >> m7 >> r7;
  300. }
  301. fin.close();
  302. filestr.close();
  303. // delete the original file
  304. remove("dp3x.txt");
  305. // rename old to new
  306. rename("outfile.txt","dp3x.txt");
  307. break ; }
  308.  
  309. }
  310. }
  311. system("pause");
  312. return 0;
  313. }
  314.  

dp3x.rar

تم تعديل هذه المشاركة بواسطة Don juan في 11 يوليو 2009 في 02:57

#9

رائع اخي الكريم , مجهود تستحق الشكر عليه , سيتم تعيين الاكواد داخل المواضيع المثبتة بالقسم ليستفاد منها لاحقا .

llback.jpg

اشهد ان لا إله إلا الله وان محمدا ً رسول الله

#10

يعطيك العافية اخوي على المجهود الرائع

الترم الجاي راح ادرس مادهـ الـ Data Strucures

ان شاء الله استفيد من موضوعك

في انتظار جديدك :)

#11

السلام عليكم ورحمة الله وبركاته

مشكور الله يجزاك خير

أنا أدرس هذي المادة صيفي

وراح نستفيد منها إن شاء الله

شكرا مرة أخرى....

programmerssu3.gif

مواضيع مشابهة