主函数
main.c
# include <malloc.h>
# include "main.h"
# include <stdio.h>
# ifdef selector
# if ( selector < tree_1)
# include "SeqList.h"
void SeqList ( ) ;
# elif ( selector < hash_1)
# include "Tree.h"
void Tree ( ) ;
# elif ( selector < graph_1)
# include "Hash.h"
void Hash ( ) ;
# elif ( selector < sort_1)
# include "Graph.h"
void Graph ( ) ;
# else
# include "Sort.h"
void Sort ( ) ;
# endif
# else
# include "BinarySearch.h"
# endif
int main ( )
{
# ifdef selector
# if ( selector < tree_1)
SeqList ( ) ;
# elif ( selector < hash_1)
Tree ( ) ;
# elif ( selector < graph_1)
Hash ( ) ;
# elif ( selector < sort_1)
Graph ( ) ;
# else
Sort ( ) ;
# endif
# else
int arr[ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , target = 3 ;
printf ( "target id %d" , binarySearch ( arr, 9 , target) ) ;
# endif
return 0 ;
}
void SeqList ( )
{
# if ( selector == seqList_1)
List list;
seqList_build ( & list, 3 ) ;
seqList_push ( & list, 1 ) ;
seqList_destroy ( & list) ;
# elif ( selector == seqList_2)
ListNode head;
linkList_create ( & head) ;
for ( int i = 1 ; i <= 4 ; i++ ) {
linkList_add ( & head, i, i* 1 ) ;
}
linkList_iterate ( & head) ;
linkList_delete ( & head, 5 ) ;
linkList_iterate ( & head) ;
linkList_delete ( & head, 1 ) ;
printf ( "index for : %d\r\n" , linkList_search ( & head, 4 ) ) ;
printf ( "index for : %d\r\n" , linkList_search ( & head, 2 ) ) ;
printf ( "index for : %d\r\n" , linkList_search ( & head, 6 ) ) ;
printf ( "index of %d\r\n" , linkList_retrieve ( & head, 2 ) ) ;
printf ( "index of %d\r\n" , linkList_retrieve ( & head, 5 ) ) ;
printf ( "index of %d\r\n" , linkList_retrieve ( & head, 1 ) ) ;
printf ( "length: %d" , linkList_length ( & head) ) ;
# elif ( selector == seqList_2_1)
struct doubleListNode_t head;
dLinkList_init ( & head) ;
for ( int i = 1 ; i <= 4 ; i++ )
{
dLinkList_insert ( & head, i, i* 1 ) ;
}
dLinkList_show ( & head) ;
dLinkList_destroy ( & head, 5 ) ;
dLinkList_show ( & head) ;
dLinkList_destroy ( & head, 4 ) ;
dLinkList_show ( & head) ;
dLinkList_destroy ( & head, 1 ) ;
dLinkList_show ( & head) ;
# elif ( selector == seqList_2_2)
struct cycleLinkList_t head;
cLinkList_born ( & head) ;
for ( int i = 1 ; i <= 4 ; i++ )
{
cLinkList_join ( & head, i, i* 1 ) ;
}
cLinkList_scan ( & head) ;
cLinkList_kick ( & head, 1 ) ;
cLinkList_scan ( & head) ;
cLinkList_kick ( & head, 3 ) ;
cLinkList_scan ( & head) ;
# elif ( selector == seqList_3)
ShareStack stack;
stack-> capacity = 3 ;
stack-> arr = malloc ( sizeof ( E) * stack-> capacity) ;
stack-> leftTop = 0 ;
stack-> rightTop = stack-> capacity - 1 ;
pushShareStack ( stack, 1 , 0 ) ;
pushShareStack ( stack, 3 , 1 ) ;
pushShareStack ( stack, 2 , 1 ) ;
pushShareStack ( stack, 4 , 1 ) ;
for ( int i = 0 ; i < stack-> capacity; i++ )
printf ( "%d " , stack-> arr[ i] ) ;
printf ( "\r\n" ) ;
printf ( "%d " , popShareStack ( stack, 0 ) ) ;
printf ( "%d " , popShareStack ( stack, 0 ) ) ;
printf ( "%d " , popShareStack ( stack, 1 ) ) ;
printf ( "%d " , popShareStack ( stack, 1 ) ) ;
# elif ( selector == seqList_4)
rQueue queue;
rQueueInit ( queue, 3 ) ;
for ( int i = 0 ; i < 2 ; i++ )
{
if ( isFull ( queue) ) break ;
inRQueue ( queue, i) ;
}
rQueueIter ( queue) ;
if ( ! isEmpty ( queue) )
{
printf ( "%d " , outRQueue ( queue) ) ;
}
QueueLink queueL;
QueueLinkNodeInit ( queueL) ;
for ( int i = 0 ; i < 5 ; i++ )
{
inQueueLinkNode ( queueL, i * 10 ) ;
}
QueueLinkNodeItr ( queueL) ;
for ( int i = 0 ; i < 6 ; i++ )
printf ( "%d " , outQueueLinkNode ( queueL) ) ;
QueueLinkNodeItr ( queueL) ;
# endif
}
void Tree ( )
{
# if ( selector == tree_1)
BinaryTree root, leftNode, rightNode;
leftNode = BinaryTreeBuild ( NULL , NULL , 'D' ) ;
leftNode = BinaryTreeBuild ( leftNode, NULL , 'B' ) ;
rightNode = BinaryTreeBuild ( NULL , NULL , 'C' ) ;
root = BinaryTreeBuild ( leftNode, rightNode, 'A' ) ;
printf ( "%c " , root-> elem) ;
printf ( "%c " , root-> leftChild-> elem) ;
printf ( "%c " , root-> rightChild-> elem) ;
BinaryTreePreOrderTraversal ( root) ;
printf ( "\n" ) ;
BinaryTreePreOrder ( root) ;
printf ( "\n" ) ;
BinaryTreeInorderTraversal ( root) ;
printf ( "\n" ) ;
BinaryTreeInorder ( root) ;
printf ( "\n" ) ;
BinaryTreePostOrderTraversal ( root) ;
printf ( "\n" ) ;
BinaryTreePostOrder ( root) ;
printf ( "\n" ) ;
BinaryTreeLevelOrder ( root) ;
printf ( "\n" ) ;
# elif ( selector == tree_1_1)
# elif ( selector == tree_1_2)
# elif ( selector == tree_1_3)
Node root = NULL ;
int i = 0 ;
while ( i < 20 )
{
I e;
scanf ( "%d" , & e) ;
root = insert ( root, e) ;
i++ ;
}
# elif ( selector == tree_2)
# elif ( selector == tree_3)
const int size = 5 ;
char alpha[ ] = { 'A' , 'B' , 'C' , 'D' , 'E' } ;
int weights[ ] = { 1 , 11 , 3 , 6 , 2 } ;
HuffmanTree root;
root = buildHuffmanTree ( alpha, weights, size) ;
for ( int i = 0 ; i < size; i++ )
{
printf ( "%s\r\n" , encoder ( root, alpha[ i] ) ) ;
}
# elif ( selector == tree_4)
struct MaxHeap heap;
initHeap ( & heap) ;
insert ( & heap, 0 ) ;
insert ( & heap, 10 ) ;
insert ( & heap, 4 ) ;
insert ( & heap, 9 ) ;
insert ( & heap, 3 ) ;
print ( & heap) ;
delete ( & heap) ;
print ( & heap) ;
# endif
}
void Hash ( )
{
# if ( selector == hash_1)
struct HashTable_t table;
initHash ( & table) ;
insertHash ( & table, createElement ( 0 ) ) ;
insertHash ( & table, createElement ( 10 ) ) ;
insertHash ( & table, createElement ( 70 ) ) ;
insertHash ( & table, createElement ( 54 ) ) ;
printf ( "%d %d " , searchHash ( & table, 54 ) , searchHash ( & table, 5 ) ) ;
insertHash_LD ( & table, createElement ( 154 ) ) ;
printf ( "%d %d " , searchHash_LD ( & table, 154 ) , searchHash_LD ( & table, 54 ) ) ;
# elif ( selector == hash_1_2)
struct HashTable hashTable;
initHashTable ( & hashTable) ;
insertHash ( & hashTable, 10 ) ;
insertHash ( & hashTable, 20 ) ;
insertHash ( & hashTable, 1 ) ;
printf ( "%d %d %d %d" , searchHash ( & hashTable, 10 ) , searchHash ( & hashTable, 20 ) ,
searchHash ( & hashTable, 1 ) , searchHash ( & hashTable, 2 ) ) ;
# endif
}
void Graph ( )
{
# if ( selector == graph_1)
MatrixGraph graph = create ( ) ;
for ( int c = 'A' ; c <= 'D' ; ++ c)
addVertex ( graph, ( char ) c) ;
addEdge ( graph, 0 , 1 ) ;
addEdge ( graph, 1 , 2 ) ;
addEdge ( graph, 2 , 3 ) ;
addEdge ( graph, 3 , 0 ) ;
addEdge ( graph, 2 , 0 ) ;
printGraph ( graph) ;
# elif ( selector == graph_2)
AdjacencyGraph graph = create ( ) ;
for ( int c = 'A' ; c <= 'D' ; ++ c)
addVertex ( graph, ( char ) c) ;
addEdge ( graph, 0 , 1 ) ;
addEdge ( graph, 1 , 2 ) ;
addEdge ( graph, 2 , 3 ) ;
addEdge ( graph, 3 , 0 ) ;
addEdge ( graph, 2 , 0 ) ;
printGraph ( graph) ;
# elif ( selector == graph_3)
AdjacencyGraph graph = create ( ) ;
for ( int c = 'A' ; c <= 'F' ; ++ c)
addVertex ( graph, ( char ) c) ;
addEdge ( graph, 0 , 1 ) ;
addEdge ( graph, 1 , 2 ) ;
addEdge ( graph, 1 , 3 ) ;
addEdge ( graph, 1 , 4 ) ;
addEdge ( graph, 4 , 5 ) ;
printGraph ( graph) ;
int arr[ graph-> vertexCount] ;
for ( int i = 0 ; i < graph-> vertexCount; ++ i) arr[ i] = 0 ;
dfs ( graph, 0 , 5 , arr) ;
# elif ( selector == graph_4)
AdjacencyGraph graph = create ( ) ;
for ( int c = 'A' ; c <= 'F' ; ++ c)
addVertex ( graph, ( char ) c) ;
addEdge ( graph, 0 , 1 ) ;
addEdge ( graph, 1 , 2 ) ;
addEdge ( graph, 1 , 3 ) ;
addEdge ( graph, 1 , 4 ) ;
addEdge ( graph, 4 , 5 ) ;
printGraph ( graph) ;
int arr[ graph-> vertexCount] ;
struct _Queue queue;
_initQueue ( & queue) ;
for ( int i = 0 ; i < graph-> vertexCount; ++ i) arr[ i] = 0 ;
bfs ( graph, 0 , 5 , arr, & queue) ;
# elif ( selector == graph_5)
# elif ( selector == graph_6)
# elif ( selector == graph_7)
# elif ( selector == graph_8)
# endif
}
void Sort ( )
{
int arr[ ] = { 10 , 9 , 8 , 3 , 4 , 9 , 2 } ;
int size = sizeof ( arr) / sizeof ( * arr) ;
# if ( selector == sort_1)
int * res = BubbleSort ( arr, size) ;
# elif ( selector == sort_2)
int * res = insertSort ( arr, size) ;
# elif ( selector == sort_3)
int * res = selectSort ( arr, size) ;
# elif ( selector == sort_4)
int * res = quickSort ( arr, 0 , size- 1 ) ;
# elif ( selector == sort_5)
int * res = shellSort ( arr, size) ;
# elif ( selector == sort_6)
struct MinHeap heap;
initHeap ( & heap, size+ 1 ) ;
for ( int i = 0 ; i < size; ++ i)
insertHeap ( & heap, arr[ i] ) ;
for ( int i = 0 ; i < size; ++ i)
arr[ i] = deleteHeap ( & heap) ;
int * res = arr;
# elif ( selector == sort_7)
int tmp[ size] ;
int * res = mergeSort ( arr, tmp, 0 , size - 1 ) ;
# elif ( selector == sort_8)
# elif ( selector == sort_9)
# elif ( selector == sort_10)
# endif
for ( int i = 0 ; i < size; i++ ) printf ( "%d " , res[ i] ) ;
}
main.h
# ifndef DATA_STRUCT_MAIN_H
# define DATA_STRUCT_MAIN_H
# include <stdio.h>
# define seqList_1 110
# define seqList_2 120
# define seqList_2_1 121
# define seqList_2_2 122
# define seqList_3 130
# define seqList_4 140
# define tree_1 210
# define tree_1_1 211
# define tree_1_2 212
# define tree_1_3 213
# define tree_2 220
# define tree_3 230
# define tree_4 240
# define hash_1 310
# define hash_1_1 311
# define hash_1_2 312
# define graph_1 410
# define graph_2 420
# define graph_3 430
# define graph_4 440
# define graph_5 450
# define graph_6 460
# define graph_7 470
# define graph_8 480
# define sort_1 510
# define sort_2 520
# define sort_3 530
# define sort_4 540
# define sort_5 550
# define sort_6 560
# define sort_7 570
# define sort_8 580
# define sort_9 590
# define sort_10 5100
# define selector sort_7
# endif
顺序表
SeqList.c
# include <stdio.h>
# include <stdbool.h>
# include "SeqList.h"
# if ( selector == seqList_1)
void seqList_build ( List * list, int capacity)
{
list-> capacity = capacity;
list-> array = malloc ( sizeof ( E) * ( list-> capacity + 1 ) ) ;
if ( list-> array == NULL ) return ;
list-> size = 0 ;
}
void seqList_print ( List * list)
{
for ( int i = 0 ; i < list-> size; i++ )
{
printf ( "%d " , list-> array[ i] ) ;
}
printf ( "\r\n" ) ;
}
void seqList_set ( List * list, int index, E param)
{
if ( index < 0 || index >= list-> capacity) return ;
list-> array[ index] = param;
}
void seqList_push ( List * list, E param)
{
if ( list-> size >= list-> capacity)
list-> capacity++ ;
E * newListArray = realloc ( list-> array, sizeof ( E) * ( list-> capacity + 1 ) ) ;
if ( newListArray == NULL ) return ;
list-> array = newListArray;
list-> array[ list-> size++ ] = param;
}
E seqList_get ( List * list, int index)
{
if ( index < 0 || index >= list-> capacity) return NULL ;
return list-> array[ index] ;
}
int seqList_find ( List * list, E param)
{
for ( int i = 0 ; i < list-> size; i++ )
if ( list-> array[ i] == param) return i;
return - 1 ;
}
void seqList_remove ( List * list, int index)
{
if ( index < 0 || index >= list-> capacity) return ;
list-> size-- ;
do
{
list-> array[ index] = list-> array[ index+ 1 ] ;
} while ( index++ < list-> size) ;
}
void seqList_pop ( List * list, int direction)
{
if ( list-> size <= 0 ) return ;
list-> size-- ;
if ( direction == 1 )
{
for ( int i = 0 ; i < list-> size; i++ )
list-> array[ i] = list-> array[ i+ 1 ] ;
}
list-> array[ list-> size] = NULL ;
}
void seqList_destroy ( List * list)
{
list-> capacity = 0 ;
list-> size = 0 ;
free ( list-> array) ;
}
# elif ( selector == seqList_2)
void linkList_create ( ListNode head) {
head-> next = NULL ;
}
void linkList_iterate ( ListNode head) {
while ( head-> next) {
head = head-> next;
printf ( "%d " , head-> elem) ;
}
printf ( "\r\n" ) ;
}
void linkList_add ( ListNode head, int index, int elem) {
if ( index < 1 ) return ;
while ( -- index) {
head = head-> next;
if ( head == NULL ) return ;
}
ListNode node = malloc ( sizeof ( struct ListNode_t ) ) ;
if ( node == NULL ) return ;
node-> elem = elem;
node-> next = head-> next;
head-> next = node;
}
void linkList_delete ( ListNode head, int index) {
if ( index < 1 ) return ;
while ( -- index) {
head = head-> next;
if ( head == NULL ) return ;
}
if ( head-> next == NULL ) return ;
ListNode node = head-> next;
head-> next = node-> next;
free ( node) ;
}
E linkList_retrieve ( ListNode head, int index) {
if ( index < 1 ) return NULL ;
while ( index-- ) {
head = head-> next;
if ( head == NULL ) return NULL ;
}
return head-> elem;
}
int linkList_search ( ListNode head, E elem) {
int index = 0 ;
while ( head-> next) {
head = head-> next;
index += 1 ;
if ( head-> elem == elem) return index;
else if ( head == NULL ) return - 1 ;
}
}
int linkList_length ( ListNode head) {
int index = 0 ;
while ( head-> next) {
head = head-> next;
index++ ;
}
return index;
}
# elif ( selector == seqList_2_1)
void dLinkList_init ( dNode head)
{
head-> next = head-> prev = NULL ;
}
void dLinkList_show ( dNode head)
{
while ( head-> next)
{
head = head-> next;
printf ( "%d " , head-> elem) ;
}
printf ( "\r\n" ) ;
}
void dLinkList_insert ( dNode head, int index, E elem) {
if ( index < 1 ) return ;
while ( -- index) {
head = head-> next;
if ( head == NULL ) return ;
}
dNode node = malloc ( sizeof ( struct doubleListNode_t ) ) ;
if ( node == NULL ) return ;
node-> elem = elem;
if ( head-> next != NULL )
{
node-> next = head-> next;
head-> next-> prev = node;
} else {
node-> next = NULL ;
}
head-> next = node;
node-> prev = head;
}
void dLinkList_destroy ( dNode head, int index)
{
if ( index < 1 ) return ;
while ( -- index) {
head = head-> next;
if ( head == NULL ) return ;
}
if ( head-> next == NULL ) return ;
dNode node = head-> next;
if ( head-> next-> next != NULL ) {
head-> next = node-> next;
node-> next-> prev = head;
} else {
head-> next = NULL ;
}
free ( node) ;
}
# elif ( selector == seqList_2_2)
void cLinkList_born ( cNode head)
{
head-> next = NULL ;
}
void cLinkList_scan ( cNode head)
{
if ( head-> next) {
cNode start = head;
head = head-> next;
while ( head != start) {
printf ( "%d " , head-> value) ;
head = head-> next;
}
printf ( "\r\n" ) ;
}
}
void cLinkList_join ( cNode head, int index, E value)
{
cNode start = head;
if ( index < 1 ) return ;
while ( -- index) {
head = head-> next;
if ( head == start) return ;
}
cNode node = malloc ( sizeof ( struct cycleLinkList_t ) ) ;
node-> value = value;
if ( head-> next != NULL )
{
node-> next = head-> next;
head-> next = node;
} else {
node-> next = head;
head-> next = node;
}
}
void cLinkList_kick ( cNode head, int index)
{
if ( index < 1 ) return ;
cNode start = head;
while ( -- index)
{
head = head-> next;
if ( head == start) return ;
}
if ( head-> next == NULL ) return ;
cNode node = head-> next;
head-> next = node-> next;
free ( node) ;
}
# elif ( selector == seqList_3)
void StackInit ( Stack * stack, int capacity)
{
stack-> arr = malloc ( sizeof ( E) * capacity) ;
if ( stack-> arr == NULL ) return ;
stack-> capacity = capacity;
stack-> top = - 1 ;
}
void pushStack ( Stack * stack, E val)
{
if ( stack-> top + 1 >= stack-> capacity) return ;
stack-> arr[ ++ stack-> top] = val;
}
E popStack ( Stack * stack)
{
if ( stack-> top - 1 == - 1 ) return NULL ;
E val = stack-> arr[ -- stack-> top] ;
stack-> arr[ stack-> top] = NULL ;
return val;
}
void pushShareStack ( ShareStack shareStack, E val, int dir)
{
if ( shareStack-> leftTop > shareStack-> rightTop) return ;
if ( dir == 0 ) {
shareStack-> arr[ shareStack-> leftTop++ ] = val;
} else if ( dir == 1 ) {
shareStack-> arr[ shareStack-> rightTop-- ] = val;
}
}
E popShareStack ( ShareStack shareStack, int dir)
{
if ( dir == 0 && shareStack-> leftTop - 1 >= 0 ) {
return shareStack-> arr[ -- shareStack-> leftTop] ;
} else if ( dir == 1 && shareStack-> rightTop + 1 <= shareStack-> capacity) {
return shareStack-> arr[ ++ shareStack-> rightTop] ;
}
return NULL ;
}
void StackNodeInit ( StackNode stack)
{
stack-> next = NULL ;
}
_Bool isEmpty ( StackNode stack)
{
if ( stack-> next != NULL ) return true;
return false;
}
void pushStackNode ( StackNode stack, E val)
{
StackNode node = malloc ( sizeof ( StackNode) ) ;
node-> val = val;
node-> next = stack-> next;
stack-> next = node;
}
E popStackNode ( StackNode stack)
{
StackNode node = stack-> next;
E val = node-> val;
stack-> next = stack-> next-> next;
free ( node) ;
return val;
}
# elif ( selector == seqList_4)
void rQueueInit ( rQueue queue, int capacity)
{
queue-> arr = malloc ( sizeof ( E) * capacity) ;
if ( queue-> arr == NULL ) return ;
queue-> capacity = capacity;
queue-> front = queue-> rear = 0 ;
}
void rQueueIter ( rQueue queue)
{
int i = queue-> front;
while ( i != queue-> rear)
{
i = ( i + 1 ) % queue-> capacity;
printf ( "%d " , queue-> arr[ i] ) ;
}
printf ( "\r\n" ) ;
}
_Bool isFull ( rQueue queue)
{
if ( ( queue-> rear + 1 ) % queue-> capacity == queue-> front) return true;
return false;
}
_Bool isEmpty ( rQueue queue)
{
if ( ( queue-> front + 1 ) % queue-> capacity == queue-> rear) return true;
return false;
}
void inRQueue ( rQueue queue, int val)
{
queue-> rear = ( queue-> rear + 1 ) % queue-> capacity;
queue-> arr[ queue-> rear] = val;
}
E outRQueue ( rQueue queue)
{
queue-> front = ( queue-> front + 1 ) % queue-> capacity;
E val = queue-> arr[ queue-> front] ;
queue-> arr[ queue-> front] = NULL ;
return val;
}
void QueueLinkNodeInit ( QueueLink queue)
{
QueueNode node = malloc ( sizeof ( QueueNode) ) ;
if ( node == NULL ) return ;
node-> next = NULL ;
queue-> front = queue-> rear = node;
}
void QueueLinkNodeItr ( QueueLink queue)
{
QueueNode node = queue-> front-> next;
while ( 1 )
{
printf ( "%d " , node-> val) ;
if ( node == queue-> rear) break ;
node = node-> next;
}
printf ( "\r\n" ) ;
}
void inQueueLinkNode ( QueueLink queue, E val)
{
QueueNode node = malloc ( sizeof ( QueueNode) ) ;
if ( node == NULL ) return ;
node-> next = NULL ;
node-> val = val;
queue-> rear-> next = node;
queue-> rear = node;
}
E outQueueLinkNode ( QueueLink queue)
{
QueueNode node = queue-> front-> next;
E val = node-> val;
queue-> front-> next = node-> next;
if ( node == queue-> rear) queue-> rear = queue-> front;
free ( node) ;
return val;
}
# endif
SeqList.h
# ifndef DATA_STRUCT_SEQLIST_H
# define DATA_STRUCT_SEQLIST_H
# include <stdlib.h>
# include "main.h"
typedef int E;
# if ( selector == seqList_1)
typedef struct List_t {
E * array;
int capacity;
int size;
} List;
void seqList_build ( List * list, int capacity) ;
void seqList_print ( List * list) ;
void seqList_set ( List * list, int index, E param) ;
void seqList_push ( List * list, E param) ;
E seqList_get ( List * List, int index) ;
int seqList_find ( List * list, E param) ;
void seqList_remove ( List * list, int index) ;
void seqList_pop ( List * list, int direction) ;
void seqList_destroy ( List * list) ;
# elif ( selector == seqList_2)
typedef struct ListNode_t {
E elem;
struct ListNode * next;
} * ListNode;
void linkList_create ( ListNode head) ;
void linkList_add ( ListNode head, int index, E elem) ;
void linkList_delete ( ListNode head, int index) ;
void linkList_iterate ( ListNode head) ;
E linkList_retrieve ( ListNode head, int index) ;
int linkList_search ( ListNode head, E elem) ;
int linkList_length ( ListNode head) ;
# elif ( selector == seqList_2_1)
typedef struct doubleListNode_t {
E elem;
struct doubleListNode_t * next;
struct doubleListNode_t * prev;
} * dNode;
void dLinkList_init ( dNode head) ;
void dLinkList_show ( dNode head) ;
void dLinkList_insert ( dNode head, int index, E elem) ;
void dLinkList_destroy ( dNode head, int index) ;
# elif ( selector == seqList_2_2)
typedef struct cycleLinkList_t {
struct cycleLinkList_t * next;
E value;
} * cNode;
void cLinkList_born ( cNode head) ;
void cLinkList_scan ( cNode head) ;
void cLinkList_join ( cNode head, int index, E value) ;
void cLinkList_kick ( cNode head, int index) ;
# elif ( selector == seqList_3)
typedef struct Stack_t {
E * arr;
int capacity;
int top;
} Stack;
void StackInit ( Stack * stack, int capacity) ;
void pushStack ( Stack * stack, E val) ;
E popStack ( Stack * stack) ;
typedef struct ShareStack_t {
E * arr;
int capacity;
int leftTop;
int rightTop;
} * ShareStack;
void pushShareStack ( ShareStack shareStack, E val, int dir) ;
E popShareStack ( ShareStack shareStack, int dir) ;
typedef struct StackNode_t {
E val;
struct StackNode_t * next;
} * StackNode;
void StackNodeInit ( StackNode stack) ;
_Bool isEmpty ( StackNode stack) ;
void pushStackNode ( StackNode stack, E val) ;
E popStackNode ( StackNode stack) ;
# elif ( selector == seqList_4)
typedef struct Queue_t {
E * arr;
int capacity;
} Queue;
typedef struct RingQueue_t
{
E * arr;
int capacity;
int front, rear;
} * rQueue;
void rQueueInit ( rQueue queue, int capacity) ;
_Bool isFull ( rQueue queue) ;
_Bool isEmpty ( rQueue queue) ;
void rQueueIter ( rQueue queue) ;
void inRQueue ( rQueue queue, int val) ;
E outRQueue ( rQueue queue) ;
typedef struct QueueNode_t
{
E val;
struct QueueNode_t * next;
} * QueueNode;
typedef struct QueueLink {
QueueNode front, rear;
} * QueueLink;
void QueueLinkNodeInit ( QueueLink queue) ;
void QueueLinkNodeItr ( QueueLink queue) ;
void inQueueLinkNode ( QueueLink queue, E val) ;
E outQueueLinkNode ( QueueLink queue) ;
# endif
# endif
队列
_Queue.c
# include "_Queue.h"
# if ( selector < hash_1)
void _initQueue ( _Queue queue)
{
_QueueNode node = malloc ( sizeof ( _QueueNode) ) ;
if ( node == NULL ) return ;
queue-> front = queue-> rear = node;
node-> next = NULL ;
}
void _inQueue ( _Queue queue, T elem)
{
_QueueNode node = malloc ( sizeof ( _QueueNode) ) ;
if ( node == NULL ) return ;
node-> elem = elem;
queue-> rear-> next = node;
queue-> rear = node;
}
T _outQueue ( _Queue queue)
{
_QueueNode node = queue-> front-> next;
T elem = node-> elem;
queue-> front-> next = node-> next;
if ( queue-> rear == node) queue-> rear = queue-> front;
free ( node) ;
return elem;
}
# if ( selector == tree_3)
void _priorityJoinQueue ( _Queue queue, T elem)
{
_QueueNode node = malloc ( sizeof ( _QueueNode) ) ;
if ( node == NULL ) return ;
node-> elem = elem;
node-> next = NULL ;
_QueueNode pre = queue-> front;
while ( pre-> next && pre-> next-> elem-> weights <= elem-> weights)
{
pre = pre-> next;
}
if ( pre == queue-> rear)
{
queue-> rear-> next = node;
queue-> rear = node;
} else {
node-> next = pre-> next;
pre-> next = node;
}
}
# endif
# elif ( selector == graph_4)
_Bool _initQueue ( _LinkedQueue queue) {
_QNode node = malloc ( sizeof ( struct _QueueNode ) ) ;
if ( node == NULL ) return 0 ;
queue-> front = queue-> rear = node;
return 1 ;
}
_Bool _offerQueue ( _LinkedQueue queue, T element) {
_QNode node = malloc ( sizeof ( struct _QueueNode ) ) ;
if ( node == NULL ) return 0 ;
node-> element = element;
queue-> rear-> next = node;
queue-> rear = node;
return 1 ;
}
_Bool _isEmpty ( _LinkedQueue queue) {
return queue-> front == queue-> rear;
}
T _pollQueue ( _LinkedQueue queue) {
T e = queue-> front-> next-> element;
_QNode node = queue-> front-> next;
queue-> front-> next = queue-> front-> next-> next;
if ( queue-> rear == node) queue-> rear = queue-> front;
free ( node) ;
return e;
}
# endif
_Queue.h
# ifndef DATA_STRUCT__QUEUE_H
# define DATA_STRUCT__QUEUE_H
# include <malloc.h>
# include "main.h"
# ifdef selector
# if ( selector < tree_1)
# elif ( selector < hash_1)
# include "Tree.h"
typedef struct _QueueNode_t {
T elem;
struct _QueueNode_t * next;
} * _QueueNode;
typedef struct _Queue_t {
_QueueNode front, rear;
} * _Queue;
void _initQueue ( _Queue queue) ;
void _inQueue ( _Queue queue, T elem) ;
T _outQueue ( _Queue queue) ;
void _priorityJoinQueue ( _Queue queue, T elem) ;
# elif ( selector < graph_1)
# include "Hash.h"
# elif ( selector < sort_1)
# include "Graph.h"
typedef int T;
typedef struct _QueueNode {
T element;
struct _QueueNode * next;
} * _QNode;
typedef struct _Queue {
_QNode front, rear;
} * _LinkedQueue;
_Bool _initQueue ( _LinkedQueue queue) ;
_Bool _offerQueue ( _LinkedQueue queue, T element) ;
_Bool _isEmpty ( _LinkedQueue queue) ;
T _pollQueue ( _LinkedQueue queue) ;
# else
# endif
# endif
# endif
栈
_Stack.c
# include <malloc.h>
# include "_Stack.h"
# if ( selector < hash_1)
void _initStack ( _Stack stack)
{
stack-> next = NULL ;
}
void _pushStack ( _Stack stack, T elem)
{
_Stack node = malloc ( sizeof ( _Stack) ) ;
if ( node == NULL ) return ;
node-> next = stack-> next;
node-> elem = elem;
stack-> next = node;
}
T _popStack ( _Stack stack)
{
_Stack node = stack-> next;
if ( node == NULL ) return NULL ;
T elem = node-> elem;
stack-> next = stack-> next-> next;
free ( node) ;
return elem;
}
T _peekStack ( _Stack stack)
{
return stack-> next-> elem;
}
# endif
_Stack.h
# ifndef DATA_STRUCT__STACK_H
# define DATA_STRUCT__STACK_H
# include "main.h"
# ifdef selector
# if ( selector < tree_1)
# elif ( selector < hash_1)
# include "Tree.h"
typedef struct _StackNode_t {
T elem;
struct _StackNode_t * next;
} * _Stack;
void _initStack ( _Stack stack) ;
void _pushStack ( _Stack stack, T elem) ;
T _popStack ( _Stack stack) ;
T _peekStack ( _Stack stack) ;
# elif ( selector < graph_1)
# include "Hash.h"
# elif ( selector < sort_1)
# include "Graph.h"
# else
# endif
# endif
# endif
树
Tree.c
# include "Tree.h"
# include <math.h>
# include <malloc.h>
# include <string.h>
# include "_Stack.h"
# include "_Queue.h"
# if ( selector == tree_1)
int countBinaryTreeNum ( int nodesNum)
{
int dp[ nodesNum + 1 ] ;
dp[ 0 ] = dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= nodesNum; i++ )
{
dp[ i] = 0 ;
for ( int j = 0 ; j < i; j++ )
{
dp[ i] = dp[ i - j - 1 ] + dp[ j] ;
}
}
return dp[ nodesNum] ;
}
int completeBinaryTreeLeafNodesNum ( int nodesNum)
{
int depth = ( ceil) ( ( int ) log ( nodesNum) ) + 1 ;
int first_to_last2nd_depth_nodesNum = pow ( 2 , ( depth- 1 ) ) - 1 ;
int last_depth_nodesNum = nodesNum - first_to_last2nd_depth_nodesNum;
int last2nd_depth_nodes_Num = pow ( 2 , depth- 2 ) ;
return last2nd_depth_nodes_Num - ( last_depth_nodesNum + 1 ) / 2 + last_depth_nodesNum;
}
int fullBinaryTreeNodesNum ( int depth)
{
return pow ( 2 , depth - 1 ) ;
}
int * BinaryTreeDepth ( int nodesNum)
{
int * res;
res[ 0 ] = log ( nodesNum - 1 ) ;
res[ 1 ] = nodesNum;
return res;
}
BinaryTree BinaryTreeBuild ( BinaryTree leftChild, BinaryTree rightChild, E elem)
{
BinaryTree tree = malloc ( sizeof ( BinaryTree) ) ;
tree-> elem = elem;
tree-> leftChild = leftChild;
tree-> rightChild = rightChild;
return tree;
}
void BinaryTreePreOrderTraversal ( BinaryTree tree)
{
printf ( "%c " , tree-> elem) ;
if ( tree-> leftChild) BinaryTreePreOrderTraversal ( tree-> leftChild) ;
if ( tree-> rightChild) BinaryTreePreOrderTraversal ( tree-> rightChild) ;
}
void BinaryTreePreOrder ( BinaryTree tree)
{
_Stack stack;
_initStack ( & stack) ;
while ( tree && stack-> next)
{
while ( tree)
{
printf ( "%c " , tree-> elem) ;
_pushStack ( & stack, tree) ;
tree = tree-> leftChild;
}
tree = _popStack ( & stack) ;
tree = tree-> rightChild;
}
}
void BinaryTreeInorderTraversal ( BinaryTree tree)
{
if ( tree-> leftChild) BinaryTreeInorderTraversal ( tree-> leftChild) ;
printf ( "%c " , tree-> elem) ;
if ( tree-> rightChild) BinaryTreeInorderTraversal ( tree-> rightChild) ;
}
void BinaryTreeInorder ( BinaryTree tree)
{
_Stack stack;
_initStack ( & stack) ;
while ( tree || stack-> next)
{
while ( tree)
{
_pushStack ( & stack, tree) ;
tree = tree-> leftChild;
}
tree = _popStack ( & stack) ;
printf ( "%c " , tree-> elem) ;
tree = tree-> rightChild;
}
}
void BinaryTreePostOrderTraversal ( BinaryTree tree)
{
if ( tree-> leftChild) BinaryTreeInorderTraversal ( tree-> leftChild) ;
if ( tree-> rightChild) BinaryTreeInorderTraversal ( tree-> rightChild) ;
printf ( "%c " , tree-> elem) ;
}
void BinaryTreePostOrder ( BinaryTree tree)
{
_Stack stack;
_initStack ( & stack) ;
while ( tree || stack-> next)
{
while ( tree)
{
_pushStack ( & stack, tree) ;
tree-> flag = 0 ;
tree = tree-> leftChild;
}
tree = _peekStack ( tree) ;
if ( tree-> flag == 0 )
{
tree-> flag = 1 ;
tree = tree-> rightChild;
}
else
{
printf ( "%c " , tree-> elem) ;
_popStack ( & stack) ;
tree = NULL ;
}
}
}
void BinaryTreeLevelOrder ( BinaryTree tree)
{
struct _Queue_t q;
_initQueue ( & q) ;
_inQueue ( & q, tree) ;
while ( q. front != q. rear)
{
BinaryTree node = _outQueue ( & q) ;
printf ( "%c " , node-> elem) ;
if ( node-> leftChild) { _inQueue ( & q, node-> leftChild) ; }
if ( node-> rightChild) { _inQueue ( & q, node-> rightChild) ; }
}
}
# elif ( selector == tree_1_1)
TreeNode buildNode ( E elem)
{
TreeNode node = malloc ( sizeof ( TreeNode) ) ;
node-> leftChild = node-> rightChild = NULL ;
node-> leftTag = node-> rightTag = 0 ;
node-> elem = elem;
return node;
}
TreeNode pre = NULL ;
TreeNode preInit ( )
{
return NULL ;
}
void preOrderThreaded ( TreeNode root)
{
if ( root == NULL ) return ;
if ( root-> leftChild == NULL ) {
root-> leftChild = pre;
root-> leftTag = 1 ;
}
if ( pre && pre-> rightChild == NULL ) {
pre-> rightChild = root;
pre-> rightTag = 1 ;
}
pre = root;
if ( root-> leftTag == 0 ) preOrderThreaded ( root-> leftChild) ;
if ( root-> rightTag == 0 ) preOrderThreaded ( root-> rightChild) ;
}
void preOrder ( TreeNode root)
{
while ( root) {
printf ( "%c" , root-> elem) ;
if ( root-> leftTag == 0 )
root = root-> leftChild;
else
root = root-> rightChild;
}
}
void inOrderThreaded ( TreeNode root)
{
if ( root == NULL ) return ;
if ( root-> leftTag == 0 ) preOrderThreaded ( root-> leftChild) ;
if ( root-> leftChild == NULL ) {
root-> leftChild = pre;
root-> leftTag = 1 ;
}
if ( pre && pre-> rightChild == NULL ) {
pre-> rightChild = root;
pre-> rightTag = 1 ;
}
pre = root;
if ( root-> rightTag == 0 ) preOrderThreaded ( root-> rightChild) ;
}
void inOrder ( TreeNode root)
{
while ( root) {
while ( root && root-> leftTag == 0 )
root = root-> leftChild;
printf ( "%c" , root-> elem) ;
while ( root && root-> rightTag == 1 ) {
root = root-> rightChild;
printf ( "%c" , root-> elem) ;
}
root = root-> rightChild;
}
}
void postOrderThreaded ( TreeNode root)
{
if ( root == NULL ) return ;
if ( root-> leftTag == 0 ) {
postOrderThreaded ( root-> leftChild) ;
if ( root-> leftChild) root-> leftChild-> parent = root;
}
if ( root-> rightTag == 0 ) {
postOrderThreaded ( root-> rightChild) ;
if ( root-> rightChild) root-> rightChild-> parent = root;
}
if ( root-> leftChild == NULL ) {
root-> leftChild = pre;
root-> leftTag = 1 ;
}
if ( pre && pre-> rightChild == NULL ) {
pre-> rightChild = root;
pre-> rightTag = 1 ;
}
pre = root;
}
void postOrder ( TreeNode root)
{
TreeNode last = NULL , node = root;
while ( node) {
while ( node-> leftChild != last && node-> leftTag == 0 )
node = node-> leftChild;
while ( node && node-> rightTag == 1 ) {
printf ( "%c" , node-> elem) ;
last = node;
node = node-> rightChild;
}
if ( node == root && node-> rightChild == last) {
printf ( "%c" , node-> elem) ;
return ;
}
while ( node && node-> rightChild == last) {
printf ( "%c" , node-> elem) ;
last = node;
node = node-> parent;
}
if ( node && node-> rightTag == 0 ) {
node = node-> rightChild;
}
}
}
# elif ( selector == tree_1_2)
Node buildNode ( E elem) {
Node node = malloc ( sizeof ( Tree) ) ;
node-> left = node-> right = NULL ;
node-> elem = elem;
return node;
}
Node insertNode ( Node root, E elem)
{
if ( root)
{
if ( root-> elem > elem)
root-> left = insertNode ( root-> left, elem) ;
else if ( root-> elem < elem)
root-> right = insertNode ( root-> right, elem) ;
} else {
root = buildNode ( elem) ;
}
return root;
}
Node searchNode ( Node root, E target)
{
while ( root)
{
if ( root-> elem > target)
root = root-> left;
else if ( root-> elem < target)
root = root-> right;
else
return root;
return NULL ;
}
}
Node maxNode ( Node root)
{
while ( root && root-> right)
root = root-> right;
return root
}
Node minNode ( Node root)
{
while ( root && root-> left)
root = root-> left;
return root;
}
Node removeNode ( Node root, E target)
{
if ( root == NULL ) return NULL ;
if ( root-> elem > target) { root = removeNode ( root-> left, target) ; }
if ( root-> elem < target) { root = removeNode ( root-> right, target) ; }
else {
if ( root-> left && root-> right)
{
Node max = maxNode ( root-> left) ;
root-> elem = max-> elem;
root-> left = removeNode ( root-> left, root-> elem) ;
} else {
Node tmp = root;
if ( root-> right) { root = root-> right; }
else { root = root-> left; }
free ( tmp) ;
}
}
return root;
}
# elif ( selector == tree_1_3)
Node makeNode ( I element) {
Node node = malloc ( sizeof ( struct TreeNode ) ) ;
node-> left = node-> right = NULL ;
node-> element = element;
node-> height = 1 ;
return node;
}
I max ( I e1, I e2)
{
return e1 > e2 ? e1 : e2;
}
int getHeight ( Node root) {
if ( root == NULL ) return 0 ;
return root-> height;
}
Node leftRotation ( Node root) {
Node newRoot = root-> right;
root-> right = newRoot-> left;
newRoot-> left = root;
root-> height = max ( getHeight ( root-> right) , getHeight ( root-> left) ) + 1 ;
newRoot-> height = max ( getHeight ( newRoot-> right) , getHeight ( newRoot-> left) ) + 1 ;
return newRoot;
}
Node rightRotation ( Node root) {
Node newRoot = root-> left;
root-> left = newRoot-> right;
newRoot-> right = root;
root-> height = max ( getHeight ( root-> right) , getHeight ( root-> left) ) + 1 ;
newRoot-> height = max ( getHeight ( newRoot-> right) , getHeight ( newRoot-> left) ) + 1 ;
return newRoot;
}
Node leftRightRotation ( Node root) {
root-> left = leftRotation ( root-> left) ;
return rightRotation ( root) ;
}
Node rightLeftRightRotation ( Node root) {
root-> right = rightRotation ( root-> right) ;
return leftRotation ( root) ;
}
Node insert ( Node root, I element) {
if ( root == NULL ) {
root = makeNode ( element) ;
} else if ( root-> element > element) {
root-> left = insert ( root-> left, element) ;
if ( getHeight ( root-> left) - getHeight ( root-> right) > 1 ) {
if ( root-> left-> element > element)
root = rightRotation ( root) ;
else
root = leftRightRotation ( root) ;
}
} else if ( root-> element < element) {
root-> right = insert ( root-> right, element) ;
if ( getHeight ( root-> left) - getHeight ( root-> right) < - 1 ) {
if ( root-> right-> element < element)
root = leftRotation ( root) ;
else
root = rightLeftRightRotation ( root) ;
}
}
root-> height = max ( getHeight ( root-> left) , getHeight ( root-> right) ) + 1 ;
return root;
}
# elif ( selector == tree_2)
# elif ( selector == tree_3)
HuffmanTree createNode ( E elem, int weights)
{
HuffmanTree node = malloc ( sizeof ( HuffmanTree) ) ;
node-> elem = elem;
node-> left = node-> right = NULL ;
node-> weights = weights;
return node;
}
HuffmanTree buildHuffmanTree ( char alpha[ ] , int weights[ ] , int size)
{
struct _Queue_t queue;
_initQueue ( & queue) ;
for ( int i = 0 ; i < size; i++ )
{
_priorityJoinQueue ( & queue, createNode ( alpha[ i] , weights[ i] ) ) ;
}
while ( queue. front-> next != queue. rear)
{
HuffmanTree left = _outQueue ( & queue) ;
HuffmanTree right = _outQueue ( & queue) ;
HuffmanTree node = createNode ( ' ' , left-> weights + right-> weights) ;
node-> left = left;
node-> right = right;
_priorityJoinQueue ( & queue, node) ;
}
return _outQueue ( & queue) ;
}
E * encoder ( HuffmanTree root, E e)
{
if ( root == NULL ) return NULL ;
if ( root-> elem == e) return "" ;
E * str = encoder ( root-> left, e) ;
E * s = malloc ( sizeof ( E) * 10 ) ;
if ( str != NULL )
{
s[ 0 ] = '0' ;
str = strcat ( s, str) ;
}
else
{
str = encoder ( root-> right, e) ;
if ( str != NULL )
{
s[ 0 ] = '1' ;
str = strcat ( s, str) ;
}
}
return str;
}
# elif ( selector == tree_4)
_Bool initHeap ( Heap heap)
{
heap-> size = 0 ;
heap-> capacity = 10 ;
heap-> arr = malloc ( sizeof ( E) * heap-> capacity) ;
return heap-> arr != NULL ;
}
_Bool insert ( Heap heap, E element)
{
if ( heap-> size == heap-> capacity) return 0 ;
int index = ++ heap-> size;
while ( index > 1 && element > heap-> arr[ index / 2 ] ) {
heap-> arr[ index] = heap-> arr[ index / 2 ] ;
index /= 2 ;
}
heap-> arr[ index] = element;
return 1 ;
}
E delete ( Heap heap)
{
E max = heap-> arr[ 1 ] , e = heap-> arr[ heap-> size-- ] ;
int index = 1 ;
while ( index * 2 <= heap-> size) {
int child = index * 2 ;
if ( child < heap-> size && heap-> arr[ child] < heap-> arr[ child + 1 ] )
child += 1 ;
if ( e >= heap-> arr[ child] ) break ;
else heap-> arr[ index] = heap-> arr[ child] ;
index = child;
}
heap-> arr[ index] = e;
return max;
}
void print ( Heap heap)
{
for ( int i = 0 ; i < heap-> size; i++ )
{
printf ( "%d " , heap-> arr[ i] ) ;
}
printf ( "\r\n" ) ;
}
# endif
Tree.h
# ifndef DATA_STRUCT_TREE_H
# define DATA_STRUCT_TREE_H
# include "main.h"
typedef char E;
typedef int I;
# if ( selector != tree_1 && selector != tree_3)
typedef int T;
# endif
int countBinaryTreeNum ( int nodesNum) ;
int completeBinaryTreeLeafNodesNum ( int nodesNum) ;
# if ( selector == tree_1)
typedef struct BinaryTreeNode_t
{
E elem;
struct BinaryTreeNode_t * leftChild;
struct BinaryTreeNode_t * rightChild;
int flag;
} * BinaryTree;
typedef BinaryTree T;
BinaryTree BinaryTreeBuild ( BinaryTree leftChild, BinaryTree rightChild, E elem) ;
void BinaryTreePreOrderTraversal ( BinaryTree tree) ;
void BinaryTreePreOrder ( BinaryTree tree) ;
void BinaryTreeInorderTraversal ( BinaryTree tree) ;
void BinaryTreeInorder ( BinaryTree tree) ;
void BinaryTreePostOrderTraversal ( BinaryTree tree) ;
void BinaryTreePostOrder ( BinaryTree tree) ;
void BinaryTreeLevelOrder ( BinaryTree tree) ;
# elif ( selector == tree_1_1)
typedef struct TreeNode_t {
E elem;
struct TreeNode_t * leftChild;
struct TreeNode_t * rightChild;
struct TreeNode * parent;
int leftTag, rightTag;
} * TreeNode;
TreeNode buildNode ( E elem) ;
TreeNode preInit ( ) ;
void preOrderThreaded ( TreeNode root) ;
void preOrder ( TreeNode root) ;
void inOrderThreaded ( TreeNode root) ;
void inOrder ( TreeNode root) ;
void postOrderThreaded ( TreeNode root) ;
void postOrder ( TreeNode root) ;
# elif ( selector == tree_1_2)
typedef struct TreeNode {
E elem;
struct TreeNode * left;
struct TreeNode * right;
} * Node;
Node buildNode ( E elem) ;
Node insertNode ( Node root, E elem) ;
Node searchNode ( Node root, E target) ;
Node maxNode ( Node root)
Node minNode ( Node root)
# elif ( selector == tree_1_3)
typedef struct TreeNode {
I element;
struct TreeNode * left;
struct TreeNode * right;
int height;
} * Node;
I max ( I e1, I e2) ;
int getHeight ( Node root) ;
Node leftRotation ( Node root) ;
Node rightRotation ( Node root) ;
Node leftRightRotation ( Node root) ;
Node rightLeftRightRotation ( Node root) ;
Node insert ( Node root, I element) ;
# elif ( selector == tree_2)
# elif ( selector == tree_3)
typedef struct HuffmanTree_t {
E elem;
struct HuffmanTree_t * left;
struct HuffmanTree_t * right;
int weights;
} * HuffmanTree;
typedef HuffmanTree T;
HuffmanTree createNode ( E elem, int weights) ;
HuffmanTree buildHuffmanTree ( char alpha[ ] , int weights[ ] , int size) ;
E * encoder ( HuffmanTree root, E e) ;
# elif ( selector == tree_4)
typedef struct MaxHeap {
E * arr;
int size;
int capacity;
} * Heap;
_Bool initHeap ( Heap heap) ;
_Bool insert ( Heap heap, E element) ;
E delete ( Heap heap) ;
void print ( Heap heap) ;
# endif
# endif
散列
Hash.c
# include "Hash.h"
# include <malloc.h>
# if ( selector == hash_1)
void initHash ( HashTable table)
{
table-> table = malloc ( sizeof ( Element) * SIZE) ;
for ( int i = 0 ; i < SIZE; i++ )
table-> table[ i] = NULL ;
}
Element createElement ( int key)
{
Element elem = malloc ( sizeof ( Element) ) ;
elem-> key = key;
return elem;
}
int hash ( int key)
{
return key % SIZE;
}
void insertHash ( HashTable table, Element elem)
{
int hashVal = hash ( elem-> key) ;
table-> table[ hashVal] = elem;
}
Element searchHash ( HashTable table, int key)
{
int hashVal = hash ( key) ;
if ( table-> table[ hashVal] == NULL ) return NULL ;
return table-> table[ hashVal] -> key == key ? table-> table[ hashVal] : NULL ;
}
void insertHash_LD ( HashTable table, Element elem)
{
int hashVal = hash ( elem-> key) ;
int cnt = 0 ;
while ( table-> table[ hashVal] )
{
hashVal = hash ( elem-> key + ++ cnt) ;
if ( cnt >= SIZE) return ;
}
table-> table[ hashVal] = elem;
}
Element searchHash_LD ( HashTable table, int key)
{
int hashVal = hash ( key) ;
int cnt = 0 ;
const int startIndex = hashVal;
do {
if ( table-> table[ hashVal] -> key == key) return table-> table[ hashVal] ;
hashVal = hash ( key + ++ cnt) ;
} while ( startIndex != hashVal && table-> table[ hashVal] ) ;
return NULL ;
}
void deleteHash_LD ( HashTable table, int key)
{
int hashVal = hash ( key) ;
int cnt = 0 ;
const int startIndex = hashVal;
do {
if ( table-> table[ hashVal] -> key == key) { table-> table[ hashVal] -> key = key + SIZE; return ; }
hashVal = hash ( key + ++ cnt) ;
} while ( startIndex != hashVal && table-> table[ hashVal] ) ;
}
# elif ( selector == hash_1_2)
void initHashTable ( HashTable hashTable)
{
hashTable-> table = malloc ( sizeof ( struct ListNode ) * SIZE) ;
for ( int i = 0 ; i < SIZE; ++ i) {
hashTable-> table[ i] . key = - 1 ;
hashTable-> table[ i] . next = NULL ;
}
}
int hash ( int key)
{
return key % SIZE;
}
Node createNode ( int key)
{
Node node = malloc ( sizeof ( Node) ) ;
node-> key = key;
node-> next = NULL ;
return node;
}
void insertHash ( HashTable hashTable, int key)
{
int hashVal = hash ( key) ;
Node head = hashTable-> table + hashVal;
while ( head-> next) head = head-> next;
head-> next = createNode ( key) ;
}
_Bool searchHash ( HashTable hashTable, int key)
{
int hashVal = hash ( key) ;
Node head = hashTable-> table + hashVal;
while ( head-> next && head-> key != key)
head = head-> next;
return head-> key == key;
}
# endif
Hash.h
# ifndef DATA_STRUCT_HASH_H
# define DATA_STRUCT_HASH_H
# include "main.h"
# if ( selector == hash_1)
# define SIZE 100
typedef struct Element_t {
int key;
} * Element;
typedef struct HashTable_t {
Element * table;
} * HashTable;
void initHash ( HashTable table) ;
Element createElement ( int key) ;
int hash ( int key) ;
void insertHash ( HashTable table, Element elem) ;
Element searchHash ( HashTable table, int key) ;
void insertHash_LD ( HashTable table, Element elem) ;
Element searchHash_LD ( HashTable table, int key) ;
# elif ( selector == hash_1_2)
# define SIZE 9
typedef struct ListNode {
int key;
struct ListNode * next;
} * Node;
typedef struct HashTable {
struct ListNode * table;
} * HashTable;
void initHashTable ( HashTable hashTable) ;
int hash ( int key) ;
Node createNode ( int key) ;
void insertHash ( HashTable hashTable, int key) ;
_Bool searchHash ( HashTable hashTable, int key) ;
# endif
# endif
图
Graph.c
# include "Graph.h"
# include <malloc.h>
# if ( selector == graph_1)
MatrixGraph create ( )
{
MatrixGraph graph = malloc ( sizeof ( struct MatrixGraph_t ) ) ;
graph-> vertexCount = 0 ;
graph-> edgeCount = 0 ;
for ( int i = 0 ; i < MaxVertex; ++ i)
for ( int j = 0 ; j < MaxVertex; ++ j)
graph-> matrix[ i] [ j] = 0 ;
return graph;
}
void addVertex ( MatrixGraph graph, E element) {
if ( graph-> vertexCount >= MaxVertex) return ;
graph-> data[ graph-> vertexCount++ ] = element;
}
void addEdge ( MatrixGraph graph, int a, int b)
{
if ( graph-> matrix[ a] [ b] == 0 ) {
graph-> matrix[ a] [ b] = 1 ;
graph-> edgeCount++ ;
}
}
void printGraph ( MatrixGraph graph)
{
for ( int i = - 1 ; i < graph-> vertexCount; ++ i) {
for ( int j = - 1 ; j < graph-> vertexCount; ++ j) {
if ( j == - 1 )
printf ( "%c" , 'A' + i) ;
else if ( i == - 1 )
printf ( "%3c" , 'A' + j) ;
else
printf ( "%3d" , graph-> matrix[ i] [ j] ) ;
}
putchar ( '\n' ) ;
}
}
# elif ( selector == graph_2 || selector == graph_3 || selector == graph_4)
AdjacencyGraph create ( )
{
AdjacencyGraph graph = malloc ( sizeof ( struct AdjacencyGraph_t ) ) ;
graph-> vertexCount = graph-> edgeCount = 0 ;
return graph;
}
void addVertex ( AdjacencyGraph graph, E element)
{
if ( graph-> vertexCount >= MaxVertex) return ;
graph-> vertex[ graph-> vertexCount] . element = element;
graph-> vertex[ graph-> vertexCount] . next = NULL ;
graph-> vertexCount++ ;
}
void addEdge ( AdjacencyGraph graph, int a, int b)
{
Node node = graph-> vertex[ a] . next;
Node newNode = malloc ( sizeof ( struct Node ) ) ;
newNode-> next = NULL ;
newNode-> nextVertex = b;
if ( ! node) {
graph-> vertex[ a] . next = newNode;
} else {
do {
if ( node-> nextVertex == b) return ;
if ( node-> next) node = node-> next;
else break ;
} while ( 1 ) ;
node-> next = newNode;
}
graph-> edgeCount++ ;
}
void printGraph ( AdjacencyGraph graph)
{
for ( int i = 0 ; i < graph-> vertexCount; ++ i) {
printf ( "%d | %c" , i, graph-> vertex[ i] . element) ;
Node node = graph-> vertex[ i] . next;
while ( node) {
printf ( " -> %d" , node-> nextVertex) ;
node = node-> next;
}
putchar ( '\n' ) ;
}
}
void dfs ( AdjacencyGraph graph, int startVertex, int targetVertex, int * visited)
{
visited[ startVertex] = 1 ;
printf ( "%c -> " , graph-> vertex[ startVertex] . element) ;
Node node = graph-> vertex[ startVertex] . next;
while ( node) {
if ( ! visited[ node-> nextVertex] )
dfs ( graph, node-> nextVertex, targetVertex, visited) ;
node = node-> next;
}
}
void bfs ( AdjacencyGraph graph, int startVertex, int targetVertex, int * visited, _LinkedQueue queue) {
_offerQueue ( queue, startVertex) ;
visited[ startVertex] = 1 ;
while ( ! _isEmpty ( queue) ) {
int next = _pollQueue ( queue) ;
printf ( "%c -> " , graph-> vertex[ next] . element) ;
Node node = graph-> vertex[ next] . next;
while ( node) {
if ( ! visited[ node-> nextVertex] ) {
_offerQueue ( queue, node-> nextVertex) ;
visited[ node-> nextVertex] = 1 ;
}
node = node-> next;
}
}
}
extern void bfs ( AdjacencyGraph graph, int startVertex, int targetVertex, int * visited, _LinkedQueue queue) ;
# elif ( selector == graph_5)
# elif ( selector == graph_6)
# elif ( selector == graph_7)
# elif ( selector == graph_8)
# endif
Graph.h
# ifndef DATA_STRUCT_GRAPH_H
# define DATA_STRUCT_GRAPH_H
# include "main.h"
# include "_Queue.h"
# if ( selector == graph_1)
# define MaxVertex 5
typedef char E;
typedef struct MatrixGraph_t {
int vertexCount;
int edgeCount;
int matrix[ MaxVertex] [ MaxVertex] ;
E data[ MaxVertex] ;
} * MatrixGraph;
MatrixGraph create ( ) ;
void addVertex ( MatrixGraph graph, E element) ;
void addEdge ( MatrixGraph graph, int a, int b) ;
void printGraph ( MatrixGraph graph) ;
# elif ( selector == graph_2 || selector == graph_3 || selector == graph_4)
# define MaxVertex 5
typedef char E;
typedef struct Node {
int nextVertex;
struct Node * next;
} * Node;
struct HeadNode {
E element;
struct Node * next;
} ;
typedef struct AdjacencyGraph_t {
int vertexCount;
int edgeCount;
struct HeadNode vertex[ MaxVertex] ;
} * AdjacencyGraph;
AdjacencyGraph create ( ) ;
void addVertex ( AdjacencyGraph graph, E element) ;
void addEdge ( AdjacencyGraph graph, int a, int b) ;
void printGraph ( AdjacencyGraph graph) ;
void dfs ( AdjacencyGraph graph, int startVertex, int targetVertex, int * visited) ;
# elif ( selector == graph_5)
# elif ( selector == graph_6)
# elif ( selector == graph_7)
# elif ( selector == graph_8)
# endif
# endif
排序
Sort.c
# include <malloc.h>
# include "Sort.h"
void swap ( int * a, int * b) {
int tmp = * a;
* a = * b;
* b = tmp;
}
# if ( selector == sort_1)
int * Bubble ( int arr[ ] , int size)
{
for ( int i = 0 ; i < size - 1 ; i++ )
{
_Bool flag = 1 ;
for ( int j = i + 1 ; j < size; j++ )
{
if ( arr[ i] > arr[ j] ) { int temp = arr[ j] ; arr[ j] = arr[ i] ; arr[ i] = temp; flag = 0 ; }
}
if ( flag) break ;
}
return arr;
}
# elif ( selector == sort_2)
void * insertSort ( int arr[ ] , int size)
{
for ( int i = 1 ; i < size; i++ )
{
int j = i, tmp = arr[ i] ;
while ( j > 0 && arr[ j - 1 ] > tmp)
{
arr[ j] = arr[ j - 1 ] ;
j-- ;
}
arr[ j] = tmp;
}
return arr;
}
# elif ( selector == sort_3)
int * selectSort ( int arr[ ] , int size)
{
int left = 0 , right = size - 1 ;
while ( left < right) {
int min = left, max = right;
for ( int i = left; i <= right; i++ ) {
if ( arr[ i] < arr[ min] ) min = i;
if ( arr[ i] > arr[ max] ) max = i;
}
swap ( & arr[ max] , & arr[ right] ) ;
if ( min == right) min = max;
swap ( & arr[ min] , & arr[ left] ) ;
left++ ;
right-- ;
}
return arr;
}
# elif ( selector == sort_4)
int * quickSort ( int arr[ ] , int start, int end)
{
if ( start >= end) return arr;
int left = start, right = end, pivot = arr[ left] ;
while ( left < right) {
while ( left < right && arr[ right] >= pivot) right-- ;
arr[ left] = arr[ right] ;
while ( left < right && arr[ left] <= pivot) left++ ;
arr[ right] = arr[ left] ;
}
arr[ left] = pivot;
quickSort ( arr, start, left - 1 ) ;
quickSort ( arr, left + 1 , end) ;
return arr;
}
# elif ( selector == sort_5)
int * shellSort ( int arr[ ] , int size)
{
int delta = size / 2 ;
while ( delta >= 1 ) {
for ( int i = delta; i < size; ++ i) {
int j = i, tmp = arr[ i] ;
while ( j >= delta && arr[ j - delta] > tmp) {
arr[ j] = arr[ j - delta] ;
j -= delta;
}
arr[ j] = tmp;
}
delta /= 2 ;
}
return arr;
}
# elif ( selector == sort_6)
_Bool initHeap ( Heap heap, int size)
{
heap-> size = 0 ;
heap-> capacity = size;
heap-> arr = malloc ( sizeof ( E) * heap-> capacity) ;
return heap-> arr != NULL ;
}
_Bool insertHeap ( Heap heap, E element)
{
if ( heap-> size == heap-> capacity) return 0 ;
int index = ++ heap-> size;
while ( index > 1 && element < heap-> arr[ index / 2 ] ) {
heap-> arr[ index] = heap-> arr[ index / 2 ] ;
index /= 2 ;
}
heap-> arr[ index] = element;
return 1 ;
}
E deleteHeap ( Heap heap)
{
E max = heap-> arr[ 1 ] , e = heap-> arr[ heap-> size-- ] ;
int index = 1 ;
while ( index * 2 <= heap-> size) {
int child = index * 2 ;
if ( child < heap-> size && heap-> arr[ child] > heap-> arr[ child + 1 ] )
child += 1 ;
if ( e <= heap-> arr[ child] ) break ;
else heap-> arr[ index] = heap-> arr[ child] ;
index = child;
}
heap-> arr[ index] = e;
return max;
}
void makeHeap ( int * arr, int start, int end)
{
while ( start * 2 + 1 <= end) {
int child = start * 2 + 1 ;
if ( child + 1 <= end && arr[ child] < arr[ child + 1 ] )
child++ ;
if ( arr[ child] > arr[ start] )
swap ( & arr[ child] , & arr[ start] ) ;
start = child;
}
}
int * heapSort ( int arr[ ] , int size)
{
for ( int i= size/ 2 - 1 ; i >= 0 ; i-- )
makeHeap ( arr, i, size - 1 ) ;
for ( int i = size - 1 ; i > 0 ; i-- ) {
swap ( & arr[ i] , & arr[ 0 ] ) ;
makeHeap ( arr, 0 , i - 1 ) ;
}
return arr;
}
# elif ( selector == sort_7)
void merge ( int arr[ ] , int tmp[ ] , int left, int leftEnd, int right, int rightEnd)
{
int i = left, size = rightEnd - left + 1 ;
while ( left <= leftEnd && right <= rightEnd) {
if ( arr[ left] <= arr[ right] )
tmp[ i++ ] = arr[ left++ ] ;
else
tmp[ i++ ] = arr[ right++ ] ;
}
while ( left <= leftEnd)
tmp[ i++ ] = arr[ left++ ] ;
while ( right <= rightEnd)
tmp[ i++ ] = arr[ right++ ] ;
for ( int j = 0 ; j < size; ++ j, rightEnd-- )
arr[ rightEnd] = tmp[ rightEnd] ;
}
int * mergeSort ( int arr[ ] , int tmp[ ] , int start, int end)
{
if ( start >= end) return arr;
int mid = ( start + end) / 2 ;
mergeSort ( arr, tmp, start, mid) ;
mergeSort ( arr, tmp, mid + 1 , end) ;
merge ( arr, tmp, start, mid, mid + 1 , end) ;
return arr;
}
# elif ( selector == sort_8)
# elif ( selector == sort_9)
# elif ( selector == sort_10)
# endif
Sort.h
# ifndef DATA_STRUCT_SORT_H
# define DATA_STRUCT_SORT_H
# include "main.h"
# if ( selector == sort_1)
int * Bubble ( int arr[ ] , int size) ;
# elif ( selector == sort_2)
void * insertSort ( int arr[ ] , int size) ;
# elif ( selector == sort_3)
int * selectSort ( int arr[ ] , int size) ;
# elif ( selector == sort_4)
int * quickSort ( int arr[ ] , int start, int end) ;
# elif ( selector == sort_5)
int * shellSort ( int arr[ ] , int size) ;
# elif ( selector == sort_6)
typedef int E;
typedef struct MinHeap {
E * arr;
int size;
int capacity;
} * Heap;
_Bool initHeap ( Heap heap, int size) ;
_Bool insertHeap ( Heap heap, E element) ;
E deleteHeap ( Heap heap) ;
void makeHeap ( int * arr, int start, int end) ;
int * heapSort ( int arr[ ] , int size) ;
# elif ( selector == sort_7)
void merge ( int arr[ ] , int tmp[ ] , int left, int leftEnd, int right, int rightEnd) ;
int * mergeSort ( int arr[ ] , int tmp[ ] , int start, int end) ;
# elif ( selector == sort_8)
# elif ( selector == sort_9)
# elif ( selector == sort_10)
# endif
# endif
二分搜索
_BinarySearch.c
# include <stdio.h>
# include "_BinarySearch.h"
int binarySearch ( int arr[ ] , int len, int target)
{
int max = len, min = 0 ;
while ( max >= min)
{
int index = ( ( max - min) / 2 > min) ? ( ( max - min) / 2 ) : min;
printf ( "%d %d index %d\r\n" , max, min, index) ;
if ( arr[ index] > target) { max = index - 1 ; }
else if ( arr[ index] < target) { min = index + 1 ; }
else return index;
}
return - 1 ;
}
_BinarySearch.h
# ifndef DATA_STRUCT__BINARYSEARCH_H
# define DATA_STRUCT__BINARYSEARCH_H
int binarySearch ( int arr[ ] , int len, int target) ;
# endif
CMakeList.txt
cmake_minimum_required(VERSION 3.20)
project(Data_Struct C)
set(CMAKE_C_STANDARD 99)
add_executable(Data_Struct main.c _BinarySearch.c _BinarySearch.h SeqList.c SeqList.h Tree.c Tree.h _Stack.c _Stack.h _Queue.c _Queue.h Hash.c Hash.h Graph.c Graph.h Sort.c Sort.h)