随笔分类 - 数据结构
摘要:1 #include 2 using namespace std; 3 4 static int daytable[2][13] ={ 5 {0,31,28,31,30,31,30,31,31,30,31,30,31}, 6 {0,31,29,31,30,31,30,31,31,30,31,30,31} 7 }; 8 //返回某年某月的某天是该年的第多少天 9 int day_of_year(int year ,int month,int day)10 {11 int i,leap;12 leap = ((year %4 ==0 && year%100 !=...
阅读全文
摘要:#include using namespace std;//快速排序算法实现//挖坑+分治法void quick_sort(int v[] ,int left,int right){ //int x = v[left]; if (left = x )//先从后往前找比x小的数 { j--; } if ( i < j ) //找到,填充前面那个坑 { v[i++] = v[j]; } //从前...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 //在数组v中查找x,返回x在v中的下标,找不到返回-1 5 int binsearch(int v[],int n,int x) 6 { 7 int low = 0 , high = n - 1; 8 int mid ;//= (low + high)/2; 9 while (low v[mid])18 {19 low = mid +1;20 }21 else22 {23 r...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 #define MAXLINE 1000 5 //读取一行输入 6 int getline(char s[] ,int limit) 7 { 8 int c,i; 9 i = 0;10 while ((c = getchar()) != EOF && c != '\n' && --limit > 0 )11 {12 s[i++] = c;13 }14 15 if (c == '\n')16 {17 s[i] = '\0';18...
阅读全文
摘要:1 #include 2 #include 3 using namespace std; 4 void shellsort(int v[],int n) 5 { 6 int gap,i,j,temp; 7 for (gap = n/2 ; gap >0 ; gap /= 2) 8 { 9 for (i = gap; i =0 && v[j] > v[j+gap] ; j -= gap)12 {13 temp = v[j];14 v[j] = v[j + gap]...
阅读全文
摘要:1 #include 2 using namespace std; 3 void shellsort(int v[],int n) 4 { 5 int gap,i,j,temp; 6 for (gap = n/2 ; gap >0 ; gap /= 2) 7 { 8 for (i = gap; i =0 && v[j] > v[j+gap] ; j -= gap)11 {12 temp = v[j];13 v[j] = v[j + gap];14 ...
阅读全文
摘要:1 #include 2 3 4 void ReverseArray(int A[],int n); 5 void PrintArray(int A[],int n); 6 7 #define NUM 10 8 9 void main()10 {11 int a[NUM];12 13 for (int i=0;i<NUM;i++)14 {15 a[i]=i+1;16 }17 18 ReverseArray(a,NUM);19 PrintArray(a,NUM);20 21 getchar();22 }...
阅读全文
摘要:1 #include 2 //#include 3 4 bool BubbleSort(int A[],int n) 5 { 6 // assert(A != NULL); 7 // assert(n != 0 ); 8 if (A==NULL || nA[j+1])18 {19 int temp = A[j];20 A[j] = A[j+1];21 A[j+1] = temp;22 }23 }24 }2...
阅读全文
摘要:#includeusing namespace std;struct student{ long num; float score; struct student *next;};int n;//初始化一个链表void create(struct student **head){ struct student *p1,*p2; n = 0; p1 = p2 = new struct student; cin>>p1->num; cin>>p1->score; while (p1->num != 0 ) { n = n+1...
阅读全文
摘要:1 #include "iostream" 2 using namespace std; 3 4 #define MAXLENGTH 20 5 typedef struct Queue 6 { 7 int array[MAXLENGTH]; 8 //first指向将要被删除的元素的下标,tail指向可以被插入元素的位置 9 int first,tail; 10 }Queue; 11 12 void initial(Queue *queue) 13 { 14 queue->first = 0; 15 queue->tail = 0;...
阅读全文
摘要:1 #include 2 using namespace std; 3 #define STACKLENGTH 20 4 typedef struct Stack 5 { 6 int s[STACKLENGTH]; 7 int t; 8 }Stack; 9 10 void initial(Stack *stack) 11 { 12 //第一个元素舍去不用 13 stack->s[0] = 0; 14 stack->t = 0; 15 } 16 //入栈 17 bool push(Stack *stack,int elem) 18 {...
阅读全文
摘要:1 #include 2 using namespace std; 3 #define MAXLENGTH 20 4 5 typedef struct LineList 6 { 7 int Array[MAXLENGTH]; 8 int curLength; 9 }LineList; 10 11 bool initial(LineList* elem) 12 { 13 elem->Array[0] = 0; 14 elem->curLength = 0; 15 return true; 16 } 17 18 //-------...
阅读全文
浙公网安备 33010602011771号