1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <stdio.h> #include <stdlib.h>
typedef struct Node { int data; struct Node *pre; struct Node *next; int freq; } Node, *pNode; void sort(const pNode,int); void initial(const pNode, int); void Locate_(const pNode, char); void print_List(const pNode);
int main(void) { pNode pa = (pNode)malloc(sizeof(Node)); int size, l_size; char ch; scanf("%d %d", &size, &l_size); initial(pa, size); while(l_size--) { scanf("%c",&ch); while(ch<='A'||ch>='z') { scanf("%c",&ch); } Locate_(pa,ch); } sort(pa,size); print_List(pa); return 0; } void print_List(pNode p) { pNode r; r = p->next; while(r != p) { printf("%c%c",r->data,(r->next==NULL)?'\n':' '); r = r->next; } } void sort(const pNode p,int size) { pNode head,pa,pb; int i,j; for(i=0;i<size-1;i++) { head = p; pa = head->next; pb = pa->next; j = size-i-1; while(j--) { if(pa->freq<pb->freq) { pa->next = pb->next; pb->next = pa; head->next = pb; } head = head->next; pa = head->next; pb = pa->next; } } } void Locate_(const pNode p, char ch) { pNode r = p->next; while (r != p) { if (r->data == ch) { (r->freq)++; break; } else { r = r->next; } } } void initial(const pNode p, int size) { pNode s, r; r = p; int ch; while (size--) { s = (pNode)malloc(sizeof(Node)); scanf("%c",&ch); while(ch<='A'||ch>='z') { scanf("%c",&ch); } s->data = ch; s->freq = 0; s->pre = r; r->next = s; p->pre = s; s->next = p; r = s; } }
|