编程札记

C

C示例找到单链表大小的程序

C示例找到单链表大小的程序 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; return; } current = head; // move to the end of the list while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } void size_of_list() { int size = 0; if(head==NULL) { printf("List size : %d ", size); return; } current = head; size = 1; while(current->next!=NULL) { current = current->next; size++; } printf("List size : %d ", size); } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); size_of_list(); return 0; }

阅读全文 ↗
C

C示例单链接列表反向打印

C示例单链接列表反向打印 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //display the list void reverse_print(struct node *list) { if(list == NULL) { printf("[null] => "); return; } reverse_print(list->next); printf(" %d =>",list->data); } //Create Linked List void insert(int data) { // Allocate memory for new node; struct node *link = (struct node*) malloc(sizeof(struct node)); link->data = data; link->next = NULL; // If head is empty, create new list if(head==NULL) { head = link; return; } current = head; // move to the end of the list while(current->next!=NULL) current = current->next; // Insert link at the end of the list current->next = link; } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); reverse_print(head); return 0; }

阅读全文 ↗
C

C实例 创建一个简单的链表

C实例 创建一个简单的链表 #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; struct node *current = NULL; //display the list void printList() { struct node *ptr = head; printf("\n[head] =>"); //start from the beginning while(ptr != NULL) { printf(" %d =>",ptr->data); ptr = ptr->next; } printf(" [null]\n"); } //insert link at the first location void insert(int data) { //create a link struct node *link = (struct node*) malloc(sizeof(struct node)); //link->key = key; link->data = data; //point it to old first node link->next = head; //point first to new first node head = link; } int main() { insert(10); insert(20); insert(30); insert(1); insert(40); insert(56); printList(); return 0; }

阅读全文 ↗
C

C实例 Lcm

C实例 Lcm #include<stdio.h> int main() { int a, b, max, step, lcm; a = 3; b = 4; lcm = 0; if(a > b) max = step = a; else max = step = b; while(1) { if(max%a == 0 && max%b == 0) { lcm = max; break; } max += step; } printf("LCM is %d", lcm); return 0; }

阅读全文 ↗
C

C实例 hcf

C实例 hcf #include<stdio.h> int main() { int a, b, i, hcf; a = 12; b = 16; for(i = 1; i <= a || i <= b; i++) { if( a%i == 0 && b%i == 0 ) hcf = i; } printf("HCF = %d", hcf); return 0; }

阅读全文 ↗
C

C实例 数列

C实例 数列 #include <stdio.h> int factorial(int n) { int f; for(f = 1; n > 1; n--) f *= n; return f; } int npr(int n,int r) { return factorial(n)/factorial(n-r); } int main() { int n, r; n = 4; r = 3; printf("%dp%d = %d \n", n, r, npr(n,r)); return 0; }

阅读全文 ↗
C

C实例 立方根

C实例 立方根 #include <stdio.h> double cubeRoot(double n) { double i, precision = 0.000001; for(i = 1; (i*i*i) <= n; ++i); //Integer part for(--i; (i*i*i) < n; i += precision); //Fractional part return i; } int main() { int n = 125; printf("Cube root of %d = %lf", n, cubeRoot(n)); return 0; }

阅读全文 ↗
C

C示例平方根程序

C示例平方根程序 #include <stdio.h> double squareRoot(double n) { double i, precision = 0.00001; for(i = 1; i*i <=n; ++i); //Integer part for(--i; i*i < n; i += precision); //Fractional part return i; } int main() { int n = 24; printf("Square root of %d = %lf", n, squareRoot(n)); return 0; }

阅读全文 ↗
C

C实例 计算模式

C实例 计算模式 #include <stdio.h> int mode(int a[],int n) { int maxValue = 0, maxCount = 0, i, j; for (i = 0; i < n; ++i) { int count = 0; for (j = 0; j < n; ++j) { if (a[j] == a[i]) ++count; } if (count > maxCount) { maxCount = count; maxValue = a[i]; } } return maxValue; } int main() { int n = 5; int a[] = {0,6,7,2,7}; printf("Mode = %d ", mode(a,n)); return 0; }

阅读全文 ↗
C

C实例 计算中位数

C实例 计算中位数 #include <stdio.h> void swap(int *p,int *q) { int t; t=*p; *p=*q; *q=t; } void sort(int a[],int n) { int i,j,temp; for(i = 0;i < n-1;i++) { for(j = 0;j < n-i-1;j++) { if(a[j] > a[j+1]) swap(&a[j],&a[j+1]); } } } int main() { int a[] = {6,3,8,5,1}; int n = 5; int sum,i; sort(a,n); n = (n+1) / 2 - 1; // -1 as array indexing in C starts from 0 printf("Median = %d ", a[n]); return 0; }

阅读全文 ↗
C

C实例 计算平均值

C实例 计算平均值 #include <stdio.h> int main() { float mean; int sum, i; int n = 5; int a[] = {2,6,7,4,9}; sum = 0; for(i = 0; i < n; i++) { sum+=a[i]; } printf("Mean = %f ", sum/(float)n); return 0; }

阅读全文 ↗