标签:小代码
#ifndef _LINK_QUEUE_H_ #define _LINK_QUEUE_H_ typedef int elem_type; typedef struct _NODE { elem_type data; struct _NODE *next; }NODE; typedef struct _LINK_QUEUE { NODE *head; NODE *tail; }LINK_QUEUE; bool InitLinkQueue(LINK_QUEUE *p); bool DestroyLinkQueue(LINK_QUEUE *p); bool IsEmpty(LINK_QUEUE *p); bool PUSH(LINK_QUEUE *p,elem_type e); bool POP(LINK_QUEUE *p,elem_type *e); int GetLength(LINK_QUEUE *p); void Display(LINK_QUEUE *p); #endif #include "LINKQUEUE.h" #include<stdio.h> #include<stdlib.h> #include<assert.h> bool InitLinkQueue(LINK_QUEUE *p) { if(p == NULL) { return false; } p->head = NULL; p->tail = NULL; return true; } void InitLinkQueue(LINK_QUEUE **p) { LINK_QUEUE *tmp = (LINK_QUEUE *)malloc(sizeof(LINK_QUEUE)*1); assert(tmp != NULL); tmp->head = NULL; tmp->tail = NULL; *p = tmp; } bool DestroyLinkQueue(LINK_QUEUE *p) { if(p == NULL) { return false; } elem_type e; int n = GetLength(p); for(int i=1;i<=n;i++) { POP(p,&e); } return true; } bool IsEmpty(LINK_QUEUE *p) { assert(p != NULL); return p->head == NULL; } bool PUSH(LINK_QUEUE *p,elem_type e) { if(p == NULL) { return false; } NODE *tmp = (NODE *)malloc(sizeof(NODE)*1); assert(tmp != NULL); tmp->data = e; tmp->next = NULL; if(p->head == NULL) { p->head = tmp; p->tail = tmp; } else { p->tail->next = tmp; p->tail = tmp; } return true; } bool POP(LINK_QUEUE *p,elem_type *e) { if(p == NULL) { return false; } if(IsEmpty(p)) { return false; } NODE *tmp = p->head; *e = tmp->data; p->head = tmp->next; free(tmp); return true; } int GetLength(LINK_QUEUE *p) { assert(p != NULL); NODE *tmp = p->head; int count = 0; while(tmp != NULL) { count++; tmp = tmp->next; } return count; } void Display(LINK_QUEUE *p) { if(p == NULL) { return ; } NODE *tmp = p->head; int n = GetLength(p); for(int i=1;i<=n;i++) { printf("%d ",tmp->data); tmp = tmp->next; } printf("\n"); }
main
#include"LQ.cpp" #include"c++.h" void radixt_sort(int *arr, int len, int n) { int rex; int m = 1; int j; LINK_QUEUE queue[10]; for(int i=0;i<10;i++) { InitLinkQueue(&queue[i]); } for(int i=0;i<n;i++) { for(int k=0;k<len;k++) { rex = (arr[k]/m)%10; PUSH(&queue[rex],arr[k]); } j = 0; for(int r=0;r<10;r++) { while(!IsEmpty(&queue[r])) { POP(&queue[r],&arr[j]); j++; } } m *= 10; } for(int i=0;i<10;i++) { DestroyLinkQueue(&queue[i]); } } void radix_sort(int *arr, int len) { int max = arr[0]; int n = 0; for(int i=1;i<len;i++) { if(arr[i] > max) { max = arr[i]; } } while(max != 0) { max /= 10; n++; } radixt_sort(arr,len,n); } int main() { int x[10]={11,13,21,6,7,8,1}; radix_sort(x,10); for(int i=0;i<10;i++) cout<<x[i]<<" "; return 0; }
标签:小代码
原文地址:http://wzsts.blog.51cto.com/10251779/1759416