├── Chapter 1: Introduction ├── README.md ├── .DS_Store ├── 1.8 快速幂取模 │ ├── 算法1.c │ └── 算法2.c ├── 1 最大子序列和 │ ├── 算法4.c │ ├── 算法2.c │ ├── 算法1.c │ └── 算法3.c ├── 1.1 选择问题 │ ├── 算法1.c │ └── 算法2.c ├── 1.6 求各和 │ └── 算法.c └── 1.2 字谜游戏 │ └── 算法.c ├── Chapter 2: Algorithm Analysis ├── README.md ├── .DS_Store ├── 2 幂运算 │ ├── 算法1.c │ └── 算法2.c ├── 2.10 Horner法则 │ └── 算法.c ├── 2 欧几里得 │ └── 算法.c ├── 2 查找 │ ├── 算法1.c │ └── 算法2.c ├── 2.17 快速求幂 │ └── 算法.c ├── 2.12 最小子序列和 │ └── 算法.c ├── 2.11 确定Ai=i │ └── 算法.c ├── 2.13 N是否为素数 │ ├── 算法1.c │ └── 算法2.c ├── 2.12 最大子序列乘积 │ └── 算法.c ├── 2.12 最小正子序列和 │ └── 算法.c ├── 2.19 寻找主要元素 │ ├── 算法2.c │ └── 算法1.c ├── 2.7 随机置换 │ ├── 算法3.c │ ├── 算法1.c │ └── 算法2.c └── 2.14 Erastothenes筛法 │ └── 算法.c ├── Chapter 3: Lists, Stacks, and Queues ├── README.md ├── 3.7 多项式相乘 │ ├── 算法2.c │ ├── 算法3.c │ └── 算法1.c ├── 3 多项式处理 │ ├── 链表 │ │ └── 算法.c │ └── 数组 │ │ └── 算法.c ├── 3.1 打印单链表 │ └── 算法.c ├── 3.3 交换链表相邻元素 │ ├── 单链表 │ │ └── 算法.c │ └── 双链表 │ │ └── 算法.c ├── 3.2 打印链表指定元素 │ └── 算法.c ├── 3.4 两链表的交集 │ └── 算法.c ├── 3.5 两链表的并集 │ └── 算法.c ├── 3.6 多项式相加 │ └── 算法.c ├── 3 栈 │ ├── 链表 │ │ └── 算法.c │ └── 数组 │ │ └── 算法.c ├── 3 队列 │ ├── 数组 │ │ └── 算法.c │ └── 链表 │ │ └── 算法.c ├── 3 基数排序 │ └── 算法.c ├── 3 应用 │ ├── 平衡符号 │ │ └── 算法.c │ └── 中缀转换后缀且运算 │ │ └── 算法.c ├── 3 链表基本操作 │ └── 算法.c └── 3 链表游标实现 │ └── 算法.c └── README.md /Chapter 1: Introduction/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.7 多项式相乘/算法2.c: -------------------------------------------------------------------------------- 1 | // O( M^2 * N ) -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.7 多项式相乘/算法3.c: -------------------------------------------------------------------------------- 1 | // O( MN * log( MN ) ) -------------------------------------------------------------------------------- /Chapter 1: Introduction/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liyuanhao6/Data-structures-and-algorithm-analysis-in-c/HEAD/Chapter 1: Introduction/.DS_Store -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liyuanhao6/Data-structures-and-algorithm-analysis-in-c/HEAD/Chapter 2: Algorithm Analysis/.DS_Store -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 多项式处理/链表/算法.c: -------------------------------------------------------------------------------- 1 | // O( 1 ) 2 | 3 | struct Node 4 | { 5 | int Coefficient; 6 | int Exponent; 7 | struct Node* Next; 8 | } *Polynomial; 9 | -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2 幂运算/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | long int Pow( long int X, unsigned int N ) // The Product in proper order 4 | { 5 | if ( N == 1 ) 6 | return 1; 7 | else 8 | return Pow( X, N-1 ); 9 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.8 快速幂取模/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int orginal_algorithm(a,b,c) 4 | { 5 | int i; 6 | int ans = 1; 7 | 8 | a = a % c; 9 | 10 | for ( i = 0; i < b; i++ ) 11 | ans = ( ans * a ) % c; 12 | 13 | return ans; 14 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.10 Horner法则/算法.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int Horner( const int A[] , int N , int x ) 4 | { 5 | int Poly = 0; 6 | int i; 7 | 8 | for ( i = N - 1; i >= 0; i-- ) 9 | Poly = Poly * x + A[i]; // Polynomial 10 | 11 | return Poly; 12 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2 欧几里得/算法.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | unsigned int Gcd ( unsigned int M, unsigned int N ) 4 | { 5 | unsigned int Rem; 6 | 7 | while ( N > 0 ) 8 | { 9 | Rem = M % N; 10 | M = N; 11 | N = Rem; 12 | } 13 | 14 | return M; 15 | } 16 | -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2 查找/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | # define NotFound -1 4 | 5 | int LineSearch( const ElementType A[], ElementType X, int N) // The Line Search 6 | { 7 | int i; 8 | 9 | for ( i = 0; i < N; i++ ) 10 | if ( A[i] == X ) 11 | return i; 12 | 13 | return NotFound; 14 | 15 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.8 快速幂取模/算法2.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | 4 | int quick_algorithm(a,b,c) 5 | { 6 | int i; 7 | int ans = 1; 8 | 9 | a = a % c; 10 | while ( b != 0 ) 11 | { 12 | if ( b & 1 ) 13 | ans = ( ans * a ) % c; 14 | b >>= 1; 15 | a = ( a * a ) % c; 16 | } 17 | 18 | return ans; 19 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2 幂运算/算法2.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | unsigned int Pow ( long int X, unsigned int N ) // The Binary Product 4 | { 5 | if ( N == 0 ) 6 | return 1; 7 | else if ( N == 1 ) 8 | return X; 9 | else if ( N % 2 == 0) 10 | return Pow ( X * X, N / 2 ); 11 | else 12 | return Pow ( X * X, N / 2 ) * X; 13 | } 14 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.1 打印单链表/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Next; 4 | int Element; 5 | } *List; 6 | 7 | void PrintList( List L ) 8 | { 9 | struct Node* P = NULL; 10 | 11 | P = L->Next; 12 | printf( "%d ", P->Element ); 13 | 14 | while ( P->Next != NULL ) 15 | { 16 | P = P->Next; 17 | printf( "%d ", P->Element ); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.3 交换链表相邻元素/单链表/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Next; 4 | int Element; 5 | } *List; 6 | 7 | void Swap( List L, struct Node* PNode, struct Node* QNode ) 8 | { 9 | struct Node *temp = L; 10 | 11 | while ( temp->Next != PNode ) 12 | temp = temp->Next; 13 | 14 | PNode->Next = QNode->Next; 15 | QNode->Next = PNode; 16 | temp->Next = QNode; 17 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.17 快速求幂/算法.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | 4 | # define True 1 5 | # define False 0 6 | 7 | long int Pow( int X, int N ) // The Binary Product 8 | { 9 | long int temp = 1; 10 | 11 | while ( N > 1 ) 12 | { 13 | if ( N % 2 ) 14 | temp *= X; 15 | else 16 | ; 17 | temp *= temp; 18 | N /= 2; 19 | } 20 | if ( N ) 21 | return temp * X; 22 | else 23 | return 0; 24 | 25 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1 最大子序列和/算法4.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int MaxSubsequenceSum( const int A[], int N ) 4 | { 5 | int ThisSum, MaxSum, i; 6 | 7 | ThisSum = MaxSum = 0; 8 | for ( i = 0; i < N; i++ ) 9 | { 10 | /* ThisSum is negative so ThisSum is equal to 0 */ 11 | ThisSum += A[i]; 12 | 13 | if ( ThisSum > MaxSum ) 14 | MaxSum = ThisSum; 15 | else if ( ThisSum < 0 ) 16 | ThisSum = 0; 17 | } 18 | 19 | return MaxSum; 20 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.3 交换链表相邻元素/双链表/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Previous; 4 | struct Node* Next; 5 | int Element; 6 | } *List; 7 | 8 | void Swap( struct Node* PNode, struct Node* QNode ) 9 | { 10 | struct Node* temp = NULL; 11 | 12 | temp->Previous = PNode->Previous; 13 | temp->Next = QNode->Next; 14 | PNode->Previous = QNode; 15 | PNode->Next = temp->Next; 16 | QNode->Previous = temp->Previous; 17 | QNode->Next = PNode; 18 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1 最大子序列和/算法2.c: -------------------------------------------------------------------------------- 1 | // O( N^2 ) 2 | 3 | int MaxSubsequenceSum( const int A[], int N ) 4 | { 5 | int ThisSum, MaxSum, i, j; 6 | 7 | MaxSum = 0; 8 | for ( i = 0; i < N; i++ ) // Base 9 | { 10 | ThisSum = 0; 11 | for ( j = i; j< N; j++ ) // From A[i] to A[j] 12 | { 13 | ThisSum += A[j]; 14 | 15 | if ( ThisSum > MaxSum ) 16 | MaxSum = ThisSum; 17 | } 18 | } 19 | 20 | return MaxSum; 21 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.12 最小子序列和/算法.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int MinSubsequenceSum( const int A[], int N ) 4 | { 5 | int ThisSum, MinSum, i; 6 | 7 | ThisSum = MinSum = 0; 8 | for ( i = 0; i < N; i++ ) 9 | { 10 | /* ThisSum is positive so ThisSum is equal to 0 */ 11 | ThisSum += A[i]; 12 | 13 | if ( ThisSum < MinSum ) 14 | MinSum = ThisSum; 15 | else if ( ThisSum > 0 ) 16 | ThisSum = 0; 17 | } 18 | 19 | return MinSum; 20 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.11 确定Ai=i/算法.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | # define isNotFound -1 4 | 5 | int Judge( const int A[], int N ) 6 | { 7 | int min, max, mid; 8 | 9 | min = 0; 10 | max = N - 1; 11 | 12 | while ( min <= max ) 13 | { 14 | mid = ( min + max ) / 2; 15 | 16 | if ( mid < A[mid] ) 17 | max = mid-1; 18 | else if ( mid > A[mid] ) 19 | min = mid+1; 20 | else 21 | return mid; 22 | } 23 | 24 | return isNotFound; 25 | 26 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.2 打印链表指定元素/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Next; 4 | int Element; 5 | } *List; 6 | 7 | void PrintLots( List L, List P ) 8 | { 9 | struct Node* Ltemp = L, *Ptemp = P->Next; 10 | int index = 0; 11 | 12 | while ( Ptemp->Next != NULL) 13 | { 14 | Ltemp = Ltemp->Next; 15 | index++; 16 | 17 | if ( index == Ptemp->Element ) 18 | { 19 | print( "%d ", Ltemp->Element ); 20 | Ptemp = Ptemp->Next; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1 最大子序列和/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N^3 ) 2 | 3 | int MaxSubsequenceSum( const int A[], int N ) 4 | { 5 | int ThisSum, MaxSum, i, j, k; 6 | 7 | MaxSum = 0; 8 | for ( i = 0; i < N; i++ ) // Base 9 | for ( j = i; j< N; j++ ) // Tail 10 | { 11 | ThisSum = 0; 12 | for ( k = i; k <= j; k++ ) // From A[i] to A[j] 13 | ThisSum += A[k]; 14 | 15 | if ( ThisSum > MaxSum ) 16 | MaxSum = ThisSum; 17 | } 18 | 19 | return MaxSum; 20 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2 查找/算法2.c: -------------------------------------------------------------------------------- 1 | // O( logN ) 2 | 3 | # define NotFound -1 4 | int BinarySearch ( const ElementType A[], ElementType X, int N ) // The Binary Search 5 | { 6 | int Low, Mid, High; 7 | 8 | Low = 0; 9 | High = N - 1; 10 | while ( Low <= High ) 11 | { 12 | Mid = ( Low + High ) / 2; 13 | 14 | if ( A[Mid] > X ) 15 | High = Mid - 1; 16 | else if ( A[Mid] < X ) 17 | Low = Mid + 1; 18 | else 19 | return Mid; 20 | } 21 | 22 | return NotFound; 23 | } 24 | -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.13 N是否为素数/算法1.c: -------------------------------------------------------------------------------- 1 | // O( sqrt( N ) ) 2 | 3 | # define True 1 4 | # define False 0 5 | 6 | int isPrime( int N ) 7 | { 8 | int i; 9 | int flag = 1; 10 | 11 | if ( N == 1 ) 12 | flag = 0; 13 | else if ( N <= 3) 14 | flag = 1; 15 | else 16 | { 17 | for ( i = 2; i*i <= N; i++ ) 18 | if ( N % i == 0 ) 19 | { 20 | flag = 0; 21 | break; 22 | } 23 | } 24 | 25 | if ( flag ) 26 | return True; 27 | else 28 | return False; 29 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.13 N是否为素数/算法2.c: -------------------------------------------------------------------------------- 1 | // O( sqrt( N ) / 3 ) 2 | 3 | # define True 1 4 | # define False 0 5 | 6 | int isPrime( int N ) 7 | { 8 | int i; 9 | int flag = 1; 10 | 11 | if ( N > 5 ) 12 | for ( i = 5; i * i < N; i += 6 ) // When N > 5, Prime number is 6 * i + 1 or 6 * i - 1 13 | if ( N % i != 0 || N % i != 2 ) 14 | { 15 | flag = 0; 16 | break; 17 | } 18 | else if ( N == 2 && N == 3 ) 19 | flag = 1; 20 | else 21 | flag = 0; 22 | 23 | if ( flag ) 24 | return True; 25 | else 26 | return False; 27 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.12 最大子序列乘积/算法.c: -------------------------------------------------------------------------------- 1 | // O( N^2 ) 2 | 3 | int MaxSubsequenceProduct( const double A[], int N ) 4 | { 5 | int ThisProduct, MaxProduct, i, j; 6 | 7 | for ( i = 0; i < N; i++ ) // Initialize the MaxProduct by a positive number 8 | if ( A[i] > 0 ) 9 | MaxProduct = A[i]; 10 | 11 | for ( i = 0; i < N; i++ ) // Base 12 | { 13 | ThisProduct = 1; 14 | for ( j = i; j < N; j++ ) // From A[i] to A[j] 15 | { 16 | ThisProduct *= A[j]; 17 | if ( ThisProduct > MaxProduct ) 18 | MaxProduct = ThisProduct; 19 | } 20 | } 21 | 22 | return MaxProduct; 23 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.12 最小正子序列和/算法.c: -------------------------------------------------------------------------------- 1 | // O( N^2 ) 2 | 3 | int MinSubsequencePositiveSum( const int A[], int N ) 4 | { 5 | int ThisSum, MinPositiveSum, i, j; 6 | 7 | for ( i = 0; i < N; i++ ) // Initialize the MibPositiveSum by a positive number 8 | if ( A[i] > 0 ) 9 | MinPositiveSum = A[i]; 10 | 11 | for ( i = 0; i < N; i++ ) // Base 12 | { 13 | ThisSum = 0; 14 | for ( j = i; j < N; j++ ) // From A[i] to A[j] 15 | { 16 | ThisSum += A[j]; 17 | if ( ThisSum < MinPositiveSum && ThisSum > 0 ) 18 | MinPositiveSum = ThisSum; 19 | } 20 | } 21 | 22 | return MinPositiveSum; 23 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.19 寻找主要元素/算法2.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | # define isNotFound 0 4 | 5 | int FindPivot( const int A[] , int N ) 6 | { 7 | int i; 8 | int Base = A[0], BaseCount = 0, Count = 0; 9 | 10 | for ( i = 0; i < N; i++ ) 11 | { 12 | if ( Base == A[i] ) 13 | BaseCount++; 14 | else 15 | BaseCount--; 16 | 17 | if ( BaseCount == 0 ) 18 | { 19 | Base = A[i--]; 20 | BaseCount = 0; 21 | } 22 | } 23 | 24 | 25 | for ( i = 0; i < N; i++ ) 26 | if ( Base == A[i] ) 27 | Count++; 28 | 29 | if ( Count > N / 2 ) 30 | return Base; 31 | else 32 | return isNotFound; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.7 随机置换/算法3.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int *RandArray ( int i, int j ) 4 | { 5 | int *array = NULL; 6 | int p; 7 | 8 | array = ( int* ) malloc( sizeof( int ) * ( j - i + 1 ) ); // The flag array to store used number 9 | 10 | for ( p = i; p < j + 1; p++ ) // Initialize the array 11 | array[p-i] = p; 12 | 13 | for ( p = 0; p < j - i + 1; p++ ) 14 | Swap( &array[p], &array[RandInt(0, j - i + 1)] ); // random exchange 15 | 16 | return array; 17 | } 18 | 19 | int RandInt( int i, int j ) 20 | { 21 | return rand() % (j - i) + i; 22 | } 23 | 24 | void Swap( int* num1, int* num2) // The function of swap 25 | { 26 | int temp; 27 | 28 | temp = *num1; 29 | *num1 = *num2; 30 | *num2 = temp; 31 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.1 选择问题/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N^2 ) 2 | 3 | int SelectMax( const int A[] , int N, int k ) 4 | { 5 | int *B = ( int* )malloc( N * sizeof( int ) ); // Allocate memory for N * int 6 | int i; 7 | 8 | for ( i = 0; i < N; i++ ) // B is equal to A 9 | B[i] = A[i]; 10 | 11 | Bubble( B, N ); // The Bubble Sort 12 | 13 | return B[k-1]; // The Kth number 14 | } 15 | 16 | void Bubble( int A[], int N ) 17 | { 18 | int i, j; 19 | 20 | for ( i = 0; i < N - 1; i++ ) 21 | for ( j = N - 1; j > i; j-- ) 22 | if ( A[j-1] < A[j] ) 23 | Swap( &A[j-1], &A[j] ); // The function of swap 24 | } 25 | 26 | void Swap( int *a, int *b ) 27 | { 28 | int temp; 29 | 30 | temp = *a; 31 | *a = *b; 32 | *b = temp; 33 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.14 Erastothenes筛法/算法.c: -------------------------------------------------------------------------------- 1 | // O( N * log ( log ( N ) ) ) 2 | 3 | # define True 1 4 | # define False 0 5 | 6 | void ArrayPrime ( int N ) 7 | { 8 | int i, j; 9 | int *array = ( int* )malloc( ( N + 1 ) * sizeof( int ) ); 10 | 11 | /* True isn't Prime, False is Prime */ 12 | 13 | for ( i = 0; i <= 1; i++ ) // Initialize the array from 0 to 1 by True 14 | array[i] = True; 15 | for ( i = 2; i <= N; i++ ) // Initialize the array from 2 to N by False 16 | array[i] = False; 17 | for ( i = 2; i <= N; i++ ) 18 | if ( array[i] == False ) 19 | { 20 | printf("%d ", i); 21 | for ( j = 2 * i; j <= N; j += i) // assign n * Prime by True 22 | array[j] = True; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.4 两链表的交集/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Next; 4 | int Element; 5 | } *List; 6 | 7 | List ListIntersection( List L1, List L2 ) 8 | { 9 | List L = NULL; 10 | struct Node *L1temp = L1, *L2temp = L2, *Ltemp = L; 11 | 12 | L1temp = L1temp->Next; 13 | L2temp = L2temp->Next; 14 | 15 | while ( L1temp != NULL && L2temp != NULL ) 16 | if ( L1temp->Element > L2temp->Element ) 17 | L2temp = L2temp->Next; 18 | else if ( L1temp->Element < L2temp->Element ) 19 | L1temp = L1temp->Next; 20 | else 21 | { 22 | Ltemp->Next = L1temp; 23 | Ltemp = Ltemp->Next; 24 | L1temp = L1temp->Next; 25 | L2temp = L2temp->Next; 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.7 随机置换/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N^2 ) 2 | 3 | int *RandArray ( int i, int j ) 4 | { 5 | int *array = NULL; 6 | int p, q, num, flag; 7 | 8 | array = ( int* ) malloc( sizeof( int ) * ( j - i + 1 ) ); // Allocate memory for ( j - i + 1 ) * int 9 | for ( p = 0; p < j - i + 1; p++ ) 10 | { 11 | flag = 1; // The flag value 12 | num = RandInt( i, j ); // The random number for i to j 13 | for ( q = 0; q < p; q++ ) 14 | if ( array[q] == num ) 15 | { 16 | flag = 0; 17 | break; 18 | } 19 | 20 | if ( flag ) 21 | array[p] = num; // num in the array 22 | else 23 | p--; // go back 24 | } 25 | 26 | return array; 27 | } 28 | 29 | int RandInt( int i, int j ) 30 | { 31 | return rand() % (j - i + 1) + i; 32 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.7 随机置换/算法2.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int *RandArray ( int i, int j ) 4 | { 5 | int *array = NULL, *UsedArray = NULL; 6 | int p, num; 7 | 8 | array = ( int* ) malloc( sizeof( int ) * ( j - i + 1 ) ); 9 | UsedArray = ( int* ) malloc( sizeof( int ) * ( j - i + 1 ) ); // The flag array to store used number 10 | for ( p = 0; p < j - i + 1; p++ ) // Initialize the used array 11 | UsedArray[p] = 0; 12 | 13 | for ( p = 0; p < j - i + 1; p++ ) 14 | { 15 | num = RandInt( i, j ); // The random number for i to j 16 | 17 | if ( UsedArray[num - i] ) // The number is used 18 | p--; 19 | else // The number isn't used 20 | { 21 | array[p] = num; 22 | UsedArray[num - i] = 1; 23 | } 24 | } 25 | 26 | free(UsedArray); // free the memory 27 | 28 | return array; 29 | } 30 | 31 | int RandInt( int i, int j ) 32 | { 33 | return rand() % (j - i + 1) + i; 34 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.5 两链表的并集/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | struct Node* Next; 4 | int Element; 5 | } *List; 6 | 7 | List ListUnion( List L1, List L2 ) 8 | { 9 | List L; 10 | 11 | struct Node *L1temp = L1, *L2temp = L2, *Ltemp = L; 12 | 13 | L1temp = L1temp->Next; 14 | L2temp = L2temp->Next; 15 | 16 | while ( L1temp != NULL || L2temp != NULL ) 17 | if ( L1temp->Element > L2temp->Element ) 18 | { 19 | Ltemp->Next = L2temp; 20 | Ltemp = Ltemp->Next; 21 | L2temp = L2temp->Next; 22 | } 23 | else if ( L1temp->Element < L2temp->Element ) 24 | { 25 | Ltemp->Next = L1temp; 26 | Ltemp = Ltemp->Next; 27 | L1temp = L1temp->Next; 28 | } 29 | else 30 | { 31 | Ltemp->Next = L1temp; 32 | Ltemp = Ltemp->Next; 33 | L1temp = L1temp->Next; 34 | L2temp = L2temp->Next; 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter 2: Algorithm Analysis/2.19 寻找主要元素/算法1.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | # define isNotFound 0 4 | 5 | int FindPivot( const int A[] , int N ) 6 | { 7 | int i; 8 | int index = 0, count = 0; 9 | int *B = ( int* )malloc( ( N + 1 ) / 2 ); // Could overlook the array B 10 | 11 | if ( N % 2 ) 12 | { 13 | for ( i = 0; i < N - 1; i += 2 ) 14 | if ( A[i] == A[i+1] ) 15 | B[index++] = A[i]; 16 | B[index++] = A[i]; 17 | 18 | for ( i = 0; i < N; i++ ) 19 | if ( A[N-1] == A[i] ) 20 | count++; 21 | 22 | if ( count < ( N + 1 ) / 2 ) 23 | index--; 24 | } 25 | else 26 | { 27 | for ( i = 0; i < N; i += 2 ) 28 | if ( A[i] == A[i+1] ) 29 | B[index++] = A[i]; 30 | } 31 | 32 | 33 | if ( index == 1 ) 34 | return B[--index]; 35 | else if ( index == 0 ) 36 | return isNotFound; 37 | else 38 | return FindPivot( B, index); 39 | } 40 | 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Solutions Of Exercises 2 | 3 | ## Notes 4 | 5 | * Use IDE : Xcode / text editor : Visual Studio Code 2019. 6 | * E-mail : Li15932804366@outlook.com 7 | 8 | ## Contents 9 | 10 | * [Chapter 1: Introduction](https://github.com/liyuanhao6/Data-structures-and-algorithm-analysis-in-c/tree/master/Chapter%201:%20Introduction) 11 | * [Chapter 2: Algorithm Analysis](https://github.com/liyuanhao6/Data-structures-and-algorithm-analysis-in-c/tree/master/Chapter%202:%20Algorithm%20Analysis) 12 | * [Chapter 3: Lists, Stacks, and Queues](https://github.com/liyuanhao6/Data-structures-and-algorithm-analysis-in-c/tree/master/Chapter%203:%20Lists%2C%20Stacks%2C%20and%20Queues) 13 | * Chapter 4: Trees 14 | * Chapter 5: Hashing 15 | * Chapter 6: Priority Queues(Heaps) 16 | * Chapter 7: Sorting 17 | * Chapter 8: The Disjoint Set ADT 18 | * Chapter 9: Graph Algorithms 19 | * Chapter 10: Algorithm Design Techniques 20 | * Chapter 11: Amortized Analysis 21 | 22 | ## License 23 | 24 | Copyright © 2019 Antoine Li. All rights reserved. 25 | -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.1 选择问题/算法2.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | int SelectMax( const int A[], int N, int k ) 4 | { 5 | int i, j, p; 6 | int *B = ( int* )malloc( k * sizeof( int ) ); // Allocate memory for K * int 7 | 8 | for ( i = 0; i < k; i++ ) // B is equal to A which from 0 to K-1 9 | B[i] = A[i]; 10 | 11 | Bubble( B, k ); // The Bubble Sort 12 | 13 | for ( i = k; i < N; i++ ) 14 | { 15 | if ( A[i] > B[k-1] ) // A[i] and the Kth of B 16 | for ( j = 0; j <= k; j++ ) 17 | if ( A[i] > B[j] ) 18 | { 19 | for ( p = k; p > j; p-- ) // Delete the min of B 20 | B[p] = B[p-1]; 21 | B[j] = A[i]; 22 | break; 23 | } 24 | } 25 | 26 | return B[k-1]; 27 | } 28 | 29 | void Bubble( int A[], int N ) 30 | { 31 | int i, j; 32 | 33 | for ( i = 0; i < N - 1; i++ ) 34 | for ( j = N - 1; j > i; j-- ) 35 | if ( A[j-1] < A[j] ) 36 | Swap( &A[j-1], &A[j] ); // The function of swap 37 | } 38 | 39 | void Swap( int *a, int *b ) 40 | { 41 | int temp; 42 | 43 | temp = *a; 44 | *a = *b; 45 | *b = temp; 46 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.6 求各和/算法.c: -------------------------------------------------------------------------------- 1 | // O( 1 ) 2 | 3 | long double A_Sum( void ) // 1/4^i i from 0 to inf 4 | { 5 | long double sum = 0, num = 1; 6 | 7 | do 8 | { 9 | num /= 4; 10 | sum += num; 11 | } while( num > 1e-16 ); 12 | 13 | return sum; 14 | } 15 | 16 | long double B_Sum( void ) // i/4^i i from 0 to inf 17 | { 18 | long double sum = 0, num = 1, i = 0; 19 | long double temp; 20 | 21 | do 22 | { 23 | i++; 24 | num /= 4; 25 | temp = i * num; 26 | sum += temp; 27 | } while ( num > 1e-16 ); 28 | 29 | return sum; 30 | } 31 | 32 | long double C_Sum( void ) // i^2/4^i i from 0 to inf 33 | { 34 | long double sum = 0, num = 1, i = 0; 35 | long double temp; 36 | 37 | do 38 | { 39 | i++; 40 | num /= 4; 41 | temp = i * i * num; 42 | sum += temp; 43 | } while( num > 1e-16 ); 44 | 45 | return sum; 46 | } 47 | 48 | // O( N ) 49 | 50 | long double D_Sum( int N ) // i^N/4^i i from 0 to inf 51 | { 52 | long double sum = 0, num = 1, i = 0; 53 | long double temp, iTemp; 54 | int index; 55 | 56 | do 57 | { 58 | i++; 59 | for ( index = 0, iTemp = i; index < N; index++ ) 60 | iTemp *= i; 61 | num /= 4; 62 | temp = iTemp * num; 63 | sum += temp; 64 | } while( num > 1e-16 ); 65 | 66 | return sum; 67 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.6 多项式相加/算法.c: -------------------------------------------------------------------------------- 1 | typedef struct Node 2 | { 3 | int Coefficient; 4 | int Exponent; 5 | struct Node* Next; 6 | } *Polynomial; 7 | 8 | Polynomial Add_Polynomial( Polynomial poly1, Polynomial poly2 ) 9 | { 10 | Polynomial poly = NULL; 11 | struct Node *poly1temp = poly1, *poly2temp = poly2, *polytemp = poly; 12 | 13 | poly1temp = poly1temp->Next; 14 | poly2temp = poly2temp->Next; 15 | 16 | while ( poly1temp != NULL || poly2temp != NULL ) 17 | { 18 | if ( poly1temp->Exponent > poly2temp->Exponent) 19 | { 20 | polytemp->Next = poly2temp; 21 | polytemp = polytemp->Next; 22 | poly2temp = poly2temp->Next; 23 | } 24 | else if ( poly1temp->Exponent < poly2temp->Exponent ) 25 | { 26 | polytemp->Next = poly1temp; 27 | polytemp = polytemp->Next; 28 | poly1temp = poly1temp->Next; 29 | } 30 | else 31 | { 32 | struct Node* temp = NULL; 33 | 34 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 35 | temp->Coefficient = poly1temp->Coefficient + poly2temp->Coefficient; 36 | temp->Exponent = poly1temp->Exponent; 37 | polytemp->Next = temp; 38 | polytemp = polytemp->Next; 39 | poly1temp = poly1temp->Next; 40 | poly2temp = poly2temp->Next; 41 | } 42 | } 43 | 44 | return poly; 45 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1 最大子序列和/算法3.c: -------------------------------------------------------------------------------- 1 | // O( N * logN ) 2 | 3 | static int MaxSubSum( const int A[], int Left, int Right ) 4 | { 5 | int MaxLeftSum, MaxRightSum; 6 | int MaxLeftBorderSum, MaxRightBorderSum; 7 | int LeftBorderSum, RightBorderSum; 8 | int Center, i; 9 | 10 | if ( Left == Right) // Base Case 11 | if ( A[ Left ] > 0 ) 12 | return A[ Left ]; 13 | else 14 | return 0; 15 | 16 | /* Divide-And-Conquer method */ 17 | Center = ( Left + Right ) / 2; 18 | MaxLeftSum = MaxSubSum( A, Left, Center ); 19 | MaxRightSum = MaxSubSum( A, Center + 1, Right ); 20 | 21 | MaxLeftBorderSum = 0; 22 | LeftBorderSum = 0; 23 | for ( i = Center; i >= Left; i--) // From A[Left] to A[Center] 24 | { 25 | LeftBorderSum += A[i]; 26 | if ( LeftBorderSum > MaxLeftBorderSum ) 27 | MaxLeftBorderSum = LeftBorderSum; 28 | } 29 | 30 | MaxRightBorderSum = 0; 31 | RightBorderSum = 0; 32 | for ( i = Center + 1; i <= Right; i++ ) // From A[Center+1] to A[Right] 33 | { 34 | RightBorderSum += A[i]; 35 | if ( RightBorderSum > MaxRightBorderSum ) 36 | MaxRightBorderSum = RightBorderSum; 37 | } 38 | 39 | return Max3( MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum ); 40 | 41 | } 42 | 43 | int MaxSubsequenceSum( const int A[], int N ) 44 | { 45 | return MaxSubSum( A, 0, N-1 ); 46 | } 47 | 48 | int Max3( int num1, int num2, int num3 ) 49 | { 50 | int Max; 51 | 52 | Max = ( num1 >= num2 ) ? num1 : num2; 53 | Max = ( num3 >= Max ) ? num3 : Max; 54 | 55 | return Max; 56 | 57 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3.7 多项式相乘/算法1.c: -------------------------------------------------------------------------------- 1 | // O( M^2 * N^2) 2 | 3 | typedef struct Node 4 | { 5 | int Coefficient; 6 | int Exponent; 7 | struct Node* Next; 8 | } *Polynomial; 9 | 10 | Polynomial Multiply_Polynomial( Polynomial poly1, Polynomial poly2 ) 11 | { 12 | Polynomial poly = NULL; 13 | struct Node *P1temp = poly1, *P2temp = poly2, *Ptemp = poly; 14 | 15 | while ( P1temp != NULL ) 16 | { 17 | Ptemp = poly; 18 | P2temp = poly2; 19 | 20 | int Exponent = P1temp->Exponent * P2temp->Exponent; 21 | int Coefficient = P1temp->Coefficient * P2temp->Coefficient; 22 | 23 | while ( Ptemp->Next != NULL ) 24 | { 25 | Ptemp = Ptemp->Next; 26 | 27 | if ( Ptemp->Next->Exponent <= Exponent ) 28 | continue; 29 | else if ( Ptemp->Exponent < Exponent ) 30 | { 31 | struct Node* Temp = NULL; 32 | Temp->Coefficient = Coefficient; 33 | Temp->Exponent = Exponent; 34 | Add_Node( Temp, Ptemp ); 35 | break; 36 | } 37 | else if ( Ptemp->Exponent == Exponent ) 38 | { 39 | Ptemp->Coefficient += Coefficient; 40 | break; 41 | } 42 | } 43 | 44 | if ( Ptemp->Next == NULL ) 45 | { 46 | struct Node* Temp = NULL; 47 | Temp->Coefficient = Coefficient; 48 | Temp->Exponent = Exponent; 49 | Add_Node( Temp, Ptemp ); 50 | } 51 | } 52 | 53 | } 54 | 55 | void Add_Node( struct Node* N, struct Node* PNode ) 56 | { 57 | N->Next = PNode->Next; 58 | PNode->Next = N; 59 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 栈/链表/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | # define MaxSize 10 4 | 5 | typedef struct Node 6 | { 7 | int Element; 8 | struct Node *Next; 9 | } *Stack; 10 | 11 | int IsEmpty( Stack S ); 12 | void Pop( Stack S ); 13 | void Push( int data, Stack S ); 14 | int Top( Stack S ); 15 | void MakeEmpty( Stack S ); 16 | void DisposeStack( Stack S ); 17 | Stack CreateStack( void ); 18 | 19 | int IsEmpty( Stack S ) 20 | { 21 | if ( S->Next == NULL ) 22 | return True; 23 | else 24 | return False; 25 | } 26 | 27 | void Pop( Stack S ) 28 | { 29 | struct Node *Temp; 30 | 31 | if ( IsEmpty( S ) ) 32 | printf("Empty stack!\n"); 33 | else 34 | { 35 | Temp = S->Next; 36 | S->Next = Temp->Next; 37 | free( Temp ); 38 | } 39 | } 40 | 41 | void Push( int data, Stack S ) 42 | { 43 | struct Node *Temp; 44 | 45 | Temp = ( Stack )malloc( sizeof( struct Node ) ); 46 | 47 | if ( Temp == NULL ) 48 | printf("Out of space!\n"); 49 | else 50 | { 51 | Temp->Next = S->Next; 52 | Temp->Element = data; 53 | S->Next = Temp; 54 | } 55 | } 56 | 57 | int Top( Stack S ) 58 | { 59 | if ( !IsEmpty( S ) ) 60 | return S->Next->Element; 61 | 62 | printf("Empty Stack!\n"); 63 | return False; 64 | } 65 | 66 | void MakeEmpty( Stack S ) 67 | { 68 | if ( S == NULL ) 69 | printf("Must use CreateStack first !\n"); 70 | else 71 | while( !IsEmpty( S ) ) 72 | Pop( S ); 73 | } 74 | 75 | void DisposeStack( Stack S ) 76 | { 77 | while ( !IsEmpty( S ) ) 78 | MakeEmpty( S ); 79 | 80 | free( S ); 81 | } 82 | 83 | Stack CreateStack( void ) 84 | { 85 | Stack P; 86 | 87 | P = ( Stack )malloc( sizeof( struct Node ) ); 88 | 89 | P->Next = NULL; 90 | 91 | return P; 92 | } 93 | 94 | 95 | 96 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 多项式处理/数组/算法.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | struct Polynomial 4 | { 5 | int CoeffArray[ MaxDegree + 1 ]; 6 | int HighPower; 7 | }; 8 | 9 | void Ini_Polynomial( struct Polynomial* poly ); 10 | void Add_Polynomial( const struct Polynomial* poly1, const struct Polynomial* poly2, struct Polynomial* polySum ); 11 | void Mult_Polynomial( const struct Polynomial* poly1, const struct Polynomial* poly2, struct Polynomial* polyProd ); 12 | long double Sum( const struct Polynomial* poly, int x ); 13 | long int Pow( int X, int N ); 14 | 15 | void Ini_Polynomial( struct Polynomial* poly ) 16 | { 17 | int i; 18 | 19 | for ( i = 0; i <= MaxDegree; i++ ) 20 | poly->CoeffArray[i] = 0; 21 | poly->HighPower = 0; 22 | } 23 | 24 | void Add_Polynomial( const struct Polynomial* poly1, const struct Polynomial* poly2, struct Polynomial* polySum ) 25 | { 26 | int i; 27 | 28 | Ini_Polynomial( polySum ); 29 | for ( i = 0; i <= MaxDegree; i++ ) 30 | polySum->CoeffArray[i] = poly1->CoeffArray[i] + poly2->CoeffArray[i]; 31 | polySum->HighPower = ( poly1->HighPower > poly2->HighPower ) ? poly1->HighPower : poly2->HighPower; 32 | } 33 | 34 | void Mult_Polynomial( const struct Polynomial* poly1, const struct Polynomial* poly2, struct Polynomial* polyProd ) 35 | { 36 | int i, j; 37 | 38 | Ini_Polynomial( polyProd ); 39 | for ( i = 0; i <= MaxDegree; i++ ) 40 | for ( j = 0 ; j <= MaxDegree; j++ ) 41 | polyProd->CoeffArray[i+j] += poly1->CoeffArray[i] * poly2->CoeffArray[j]; 42 | polyProd->HighPower = poly1->HighPower * poly2->HighPower; 43 | } 44 | 45 | long double Sum( const struct Polynomial* poly, int x ) 46 | { 47 | int i; 48 | long double sum = 0; 49 | 50 | for ( i = 0; i <= MaxDegree; i++ ) 51 | sum += Pow( x, i ) * poly->CoeffArray[i]; 52 | 53 | return sum; 54 | } 55 | 56 | long int Pow( int X, int N ) 57 | { 58 | long int temp = 1; 59 | 60 | while ( N > 1 ) 61 | { 62 | if ( N % 2 ) 63 | temp *= X; 64 | else 65 | ; 66 | temp *= temp; 67 | N /= 2; 68 | } 69 | if ( N ) 70 | return temp * X; 71 | else 72 | return 0; 73 | 74 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 队列/数组/算法.c: -------------------------------------------------------------------------------- 1 | # define MAXSIZE 100 2 | # define TRUE 1 3 | # define FALSE 0 4 | 5 | struct QueueRecord 6 | { 7 | int Capacity; 8 | int Front; 9 | int Rear; 10 | int Size; 11 | int *Array; 12 | }; 13 | 14 | int IsEmpty( struct QueueRecord* Q ); 15 | int IsFull( struct QueueRecord* Q ); 16 | void MakeEmpty( struct QueueRecord* Q ); 17 | struct QueueRecord* CreatQueue( int Size ); 18 | void DisposeQueue( struct QueueRecord* Q ); 19 | int Succ( struct QueueRecord* Q, int Position ); 20 | void Enqueue( struct QueueRecord* Q, int data ); 21 | void Dequeue( struct QueueRecord* Q ); 22 | int Front( struct QueueRecord* Q ); 23 | 24 | 25 | int IsEmpty( struct QueueRecord* Q ) 26 | { 27 | if ( Q->Size == 0 ) 28 | return TRUE; 29 | else 30 | return FALSE; 31 | } 32 | 33 | int IsFull( struct QueueRecord* Q ) 34 | { 35 | if ( Q->Capacity == Q->Size ) 36 | return TRUE; 37 | else 38 | return FALSE; 39 | } 40 | 41 | void MakeEmpty( struct QueueRecord* Q ) 42 | { 43 | Q->Size = 0; 44 | Q->Front = 1; 45 | Q->Rear = 0; 46 | } 47 | 48 | struct QueueRecord* CreatQueue( int ArraySize ) 49 | { 50 | struct QueueRecord* Q = NULL; 51 | 52 | Q = ( struct QueueRecord* )malloc( sizeof( struct QueueRecord ) ); 53 | Q->Capacity = ArraySize; 54 | MakeEmpty( Q ); 55 | Q->Array = ( int* )malloc( sizeof( int ) * ArraySize ); 56 | 57 | return Q; 58 | } 59 | 60 | void DisposeQueue( struct QueueRecord* Q ) 61 | { 62 | free( Q->Array ); 63 | free( Q ); 64 | } 65 | 66 | int Succ( struct QueueRecord* Q, int Position ) 67 | { 68 | if ( ++Position == Q->Capacity ) 69 | Position = 0; 70 | 71 | return Position; 72 | } 73 | 74 | void Enqueue( struct QueueRecord* Q, int data ) 75 | { 76 | Q->Rear = Succ( Q, Q->Rear ); 77 | 78 | Q->Size++; 79 | Q->Array[Q->Rear] = data; 80 | } 81 | 82 | void Dequeue( struct QueueRecord* Q ) 83 | { 84 | Q->Front = Succ( Q, Q->Front ); 85 | 86 | Q->Size--; 87 | } 88 | 89 | int Front( struct QueueRecord* Q ) 90 | { 91 | if ( !IsEmpty( Q ) ) 92 | return Q->Array[Q->Front]; 93 | else 94 | return FALSE; 95 | } 96 | 97 | int FrontAndDequeue( struct QueueRecord* Q ) 98 | { 99 | int data; 100 | 101 | if ( !IsEmpty( Q ) ) 102 | { 103 | data = Q->Array[Q->Front]; 104 | Q->Front = Succ( Q, Q->Front ); 105 | Q->Size--; 106 | return data; 107 | } 108 | else 109 | return FALSE; 110 | } -------------------------------------------------------------------------------- /Chapter 1: Introduction/1.2 字谜游戏/算法.c: -------------------------------------------------------------------------------- 1 | // 2 | 3 | # define isFound 1 4 | # define isNotFound 0 5 | # define LETTERSIZE 21 6 | # define DICTSIZE 5 7 | 8 | char letter[4][4] = { // Letter 9 | { 't', 'h', 'i', 's' }, 10 | { 'w', 'a', 't', 's' }, 11 | { 'o', 'a', 'h', 'g' }, 12 | { 'f', 'g', 'd', 't' } 13 | }; 14 | 15 | char* dict[DICTSIZE] = { "this", "two", "fat", "that", "of" }; // Dictionary 16 | 17 | void Crossword( char letter[][4], char **dict ) 18 | { 19 | int i, j, k, max; 20 | char word[LETTERSIZE]; 21 | 22 | for ( i = 0; i < 4; i++ ) // The row 23 | for ( j = 0; j < 4; j++ ) // The column 24 | for ( k = 0; k < 8; k++ ) // 8 directions 25 | for ( max = 1; max < 5; max++ ) // The max of number of letter of word 26 | if ( wordExist( i, j, k, max, word, letter, dict ) ) // Do or not word exist 27 | { 28 | printf("%s\n", word); 29 | break; 30 | } 31 | } 32 | 33 | int wordExist( int x, int y, int dir, int maxchars, char* retword, char letter[][4], char** dict ) 34 | { 35 | char str[LETTERSIZE]; // Get the word 36 | int ct = 0; 37 | int i, j; 38 | 39 | for ( i = 0; i < maxchars; i++ ) 40 | { 41 | str[ct] = letter[x][y]; 42 | str[ct + 1] = '\0'; 43 | for ( j = 0; j < DICTSIZE; j++ ) 44 | if ( strcmp( str, dict[j] ) == 0 ) // str is equal to dict[j] 45 | { 46 | strcpy( retword, dict[j] ); // return retword 47 | return isFound; 48 | } 49 | ct++; 50 | 51 | switch ( dir ) 52 | { 53 | case 0: // East 54 | y++; 55 | break; 56 | case 1: // Northeast 57 | x++; 58 | y--; 59 | break; 60 | case 2: // North 61 | x--; 62 | break; 63 | case 3: // Northwest 64 | x--; 65 | y--; 66 | break; 67 | case 4: // West 68 | y--; 69 | break; 70 | case 5: // Southwest 71 | x--; 72 | y++; 73 | break; 74 | case 6: // South 75 | x++; 76 | break; 77 | case 7: // Southeast 78 | x++; 79 | y++; 80 | break; 81 | default: 82 | printf("Direction error!\n"); 83 | return isNotFound; 84 | } 85 | } 86 | return isNotFound; 87 | } 88 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 栈/数组/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | # define EmptyTOS -1 4 | # define MaxSize 100 5 | 6 | struct StackRecord 7 | { 8 | int Capocity; 9 | int TopOfStack; 10 | int *Array; 11 | }; 12 | 13 | int IsEmpty( struct StackRecord* R ); 14 | int IsFull( struct StackRecord* R ); 15 | void MakeEmpty( struct StackRecord* R ); 16 | struct StackRecord* CreatStack( int Size ); 17 | void DisposeStack( struct StackRecord* R ); 18 | void Push( struct StackRecord* R, int Element ); 19 | int Top( struct StackRecord* R ); 20 | void Pop( struct StackRecord* R ); 21 | int TopAndPop( struct StackRecord* R ); 22 | 23 | int IsEmpty( struct StackRecord* R ) 24 | { 25 | if ( R->TopOfStack == -1 ) 26 | return True; 27 | else 28 | return False; 29 | } 30 | 31 | int IsFull( struct StackRecord* R ) 32 | { 33 | if ( R->TopOfStack == MaxSize - 1 ) 34 | return True; 35 | else 36 | return False; 37 | } 38 | 39 | void MakeEmpty( struct StackRecord* R ) 40 | { 41 | R->TopOfStack = EmptyTOS; 42 | } 43 | 44 | struct StackRecord* CreatStack( int Size ) 45 | { 46 | struct StackRecord *P; 47 | 48 | if ( Size > MaxSize ) 49 | { 50 | printf("Stack size is too small!\n"); 51 | return NULL; 52 | } 53 | 54 | P = ( struct StackRecord* )malloc( sizeof( struct StackRecord ) ); 55 | if ( P == NULL ) 56 | printf("Out of space!\n"); 57 | 58 | P->Array = ( int* )malloc( sizeof( int ) * Size ); 59 | if ( P->Array == NULL ) 60 | printf("Out of space!\n"); 61 | 62 | P->Capocity = Size; 63 | MakeEmpty( P ); 64 | 65 | return P; 66 | } 67 | 68 | void DisposeStack( struct StackRecord* R ) 69 | { 70 | if ( R != NULL ) 71 | { 72 | free( R->Array ); 73 | free( R ); 74 | } 75 | } 76 | 77 | void Push( struct StackRecord* R, int Element ) 78 | { 79 | if ( IsFull( R ) ) 80 | printf("Full Stack"); 81 | else 82 | R->Array[++R->TopOfStack] = Element; 83 | } 84 | 85 | int Top( struct StackRecord* R ) 86 | { 87 | if ( IsEmpty( R ) ) 88 | { 89 | printf("Empty Stack\n"); 90 | return 0; 91 | } 92 | else 93 | return R->Array[R->TopOfStack]; 94 | } 95 | 96 | void Pop( struct StackRecord* R ) 97 | { 98 | if ( IsEmpty( R ) ) 99 | printf("Empty Stack\n"); 100 | else 101 | R->TopOfStack--; 102 | } 103 | 104 | int TopAndPop( struct StackRecord* R ) 105 | { 106 | if ( IsEmpty( R ) ) 107 | { 108 | printf("Empty Stack\n"); 109 | return 0; 110 | } 111 | else 112 | return R->Array[R->TopOfStack--]; 113 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 队列/链表/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | 4 | struct Node 5 | { 6 | int Element; 7 | struct Node *Next; 8 | }; 9 | 10 | struct Queue 11 | { 12 | struct Node* Front; 13 | struct Node* Rear; 14 | }; 15 | 16 | int IsEmpty( struct Queue* Q ); 17 | struct Queue* CreateQueue( void ); 18 | void Dequeue( struct Queue* Q ); 19 | void Enqueue( struct Queue* Q, int data ); 20 | int Front( struct Queue* Q ); 21 | int FrontAndDequeue( struct Queue* Q ); 22 | void MakeEmpty( struct Queue* Q ); 23 | void DisposeQueue( struct Queue* Q ); 24 | 25 | int IsEmpty( struct Queue* Q ) 26 | { 27 | if ( Q->Front == Q->Rear ) 28 | return True; 29 | else 30 | return False; 31 | } 32 | 33 | struct Queue* CreateQueue( void ) 34 | { 35 | struct Queue* Q = NULL; 36 | 37 | Q = ( struct Queue* )malloc( sizeof( struct Queue ) ); 38 | 39 | Q->Front = Q->Rear = ( struct Node* )malloc( sizeof( struct Node ) ); 40 | 41 | Q->Front->Next = NULL; 42 | 43 | return Q; 44 | } 45 | 46 | void Dequeue( struct Queue* Q ) 47 | { 48 | if ( !IsEmpty( Q ) ) 49 | { 50 | struct Node* temp = NULL; 51 | 52 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 53 | 54 | temp = Q->Front->Next; 55 | Q->Front->Next = temp->Next; 56 | 57 | free( temp ); 58 | 59 | if ( Q->Rear == temp ) 60 | Q->Rear = Q->Front; 61 | } 62 | } 63 | 64 | void Enqueue( struct Queue* Q, int data ) 65 | { 66 | struct Node* temp = NULL; 67 | 68 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 69 | 70 | temp->Element = data; 71 | temp->Next = NULL; 72 | 73 | Q->Rear->Next = temp; 74 | Q->Rear = temp; 75 | } 76 | void MakeEmpty( struct Queue* Q ) 77 | { 78 | 79 | while ( !IsEmpty( Q ) ) 80 | Dequeue( Q ); 81 | } 82 | 83 | void DisposeQueue( struct Queue* Q ) 84 | { 85 | while ( !IsEmpty( Q ) ) 86 | Dequeue( Q ); 87 | free( Q ); 88 | } 89 | 90 | int Front( struct Queue* Q ) 91 | { 92 | if ( !IsEmpty( Q ) ) 93 | return Q->Front->Next->Element; 94 | 95 | return False; 96 | } 97 | 98 | int FrontAndDequeue( struct Queue* Q ) 99 | { 100 | if ( !IsEmpty( Q ) ) 101 | { 102 | struct Node* temp = NULL; 103 | int data; 104 | 105 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 106 | 107 | temp = Q->Front->Next; 108 | data = temp->Element; 109 | Q->Front->Next = temp->Next; 110 | free( temp ); 111 | 112 | if ( Q->Rear == temp ) 113 | Q->Rear = Q->Front; 114 | 115 | return data; 116 | } 117 | 118 | return False; 119 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 基数排序/算法.c: -------------------------------------------------------------------------------- 1 | // O( N ) 2 | 3 | typedef struct Node 4 | { 5 | int Element; 6 | struct Node* Next; 7 | } *List; 8 | 9 | List ListCreat( void ); 10 | void ListDelete( List L ); 11 | void NodeInsert( struct Node* P, int data ); 12 | void ListPrint( List L ); 13 | int GetPosition( int Radix, int data, int NowLength ); 14 | void RadixSort( List L, int Length, int Radix ); 15 | 16 | List NodeCreat( void ) 17 | { 18 | struct Node* temp = NULL; 19 | 20 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 21 | temp->Next = NULL; 22 | 23 | return temp; 24 | } 25 | 26 | void ListDelete( List L ) 27 | { 28 | struct Node *P = NULL, *Q = NULL; 29 | 30 | P = L->Next; 31 | L->Next = NULL; 32 | 33 | while ( P != NULL ) 34 | { 35 | Q = P; 36 | P = P->Next; 37 | free( Q ); 38 | } 39 | 40 | } 41 | 42 | void NodeInsert( struct Node* P, int data ) 43 | { 44 | struct Node *temp = NULL; 45 | 46 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 47 | 48 | temp->Next = NULL; 49 | temp->Element = data; 50 | P->Next = temp; 51 | } 52 | 53 | void ListPrint( List L ) 54 | { 55 | struct Node* P = NULL; 56 | 57 | P = L->Next; 58 | 59 | while ( P != NULL ) 60 | { 61 | printf("%d ", P->Element); 62 | P = P->Next; 63 | } 64 | 65 | putchar('\n'); 66 | } 67 | 68 | int GetPosition( int Radix, int data, int NowLength ) 69 | { 70 | int temp = 1; 71 | int i; 72 | 73 | for ( i = 0; i < NowLength - 1; i++ ) 74 | temp *= Radix; 75 | 76 | return ( data / temp ) % Radix; 77 | } 78 | 79 | void RadixSort( List L, int Length, int Radix ) 80 | { 81 | struct Node *Array[Radix], *temp = NULL, *P = NULL, *Q = NULL; 82 | int i, j; 83 | 84 | for ( i = 0; i < Radix; i++ ) 85 | Array[i] = NodeCreat(); 86 | 87 | for ( i = 0; i < Length; i++ ) 88 | { 89 | P = L; 90 | while( P->Next != NULL ) 91 | { 92 | Q = P->Next; 93 | P->Next = Q->Next; 94 | Q->Next = NULL; 95 | 96 | int Position = GetPosition( Radix, Q->Element, i + 1 ); 97 | temp = Array[Position]; 98 | while ( temp->Next != NULL ) 99 | temp = temp->Next; 100 | temp->Next = Q; 101 | } 102 | 103 | printf("%dth Allocation\n", i+1); 104 | for ( j = 0; j < Radix; j++ ) 105 | { 106 | printf("N-%d ", j); 107 | ListPrint(Array[j]); 108 | } 109 | 110 | P = L; 111 | for ( j = 0; j < Radix; j++ ) 112 | { 113 | Q = Array[j]; 114 | while ( Q->Next != NULL ) 115 | { 116 | temp = Q->Next; 117 | Q->Next = temp->Next; 118 | temp->Next = NULL; 119 | P->Next = temp; 120 | P = P->Next; 121 | } 122 | } 123 | 124 | printf("%dth Collection\n", i+1); 125 | ListPrint(L); 126 | } 127 | 128 | for ( i = 0; i < Radix; i++ ) 129 | ListDelete(Array[i]); 130 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 应用/平衡符号/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | 4 | typedef struct Node 5 | { 6 | char Element; 7 | struct Node *Next; 8 | } *Stack; 9 | 10 | const char CloseSymbol[] = ")]}"; 11 | const char OpenSymbol[] = "([{"; 12 | 13 | Stack CreateStack( void ); 14 | void MakeEmpty( Stack P ); 15 | int IsEmpty( Stack P ); 16 | void Push( Stack P, char data ); 17 | void Pop( Stack P ); 18 | char Top( Stack P ); 19 | void DisposeStack( Stack P ); 20 | int IsCloseSymbol( char ch ); 21 | int IsOpenSymbol( char ch ); 22 | int IsMatch( char Clop, char Clch ); 23 | 24 | Stack CreateStack( void ) 25 | { 26 | Stack P; 27 | 28 | P = ( Stack )malloc( sizeof( Stack ) ); 29 | P->Next = NULL; 30 | 31 | return P; 32 | } 33 | 34 | void MakeEmpty( Stack P ) 35 | { 36 | while( P->Next != NULL ) 37 | Pop( P ); 38 | } 39 | 40 | int IsEmpty( Stack P ) 41 | { 42 | if ( P->Next == NULL ) 43 | return True; 44 | else 45 | return False; 46 | } 47 | 48 | void Push( Stack P, char data ) 49 | { 50 | struct Node* temp; 51 | 52 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 53 | temp->Element = data; 54 | temp->Next = P->Next; 55 | P->Next = temp; 56 | } 57 | 58 | void Pop( Stack P ) 59 | { 60 | struct Node* temp; 61 | 62 | temp = P->Next; 63 | P->Next = temp->Next; 64 | free( temp ); 65 | } 66 | 67 | char Top( Stack P ) 68 | { 69 | char data; 70 | 71 | return data = P->Next->Element; 72 | } 73 | 74 | void DisposeStack( Stack P ) 75 | { 76 | MakeEmpty( P ); 77 | free( P ); 78 | } 79 | 80 | int IsCloseSymbol( char ch ) 81 | { 82 | int i = 0; 83 | 84 | while ( CloseSymbol[i] != '\0' ) 85 | if ( ch == CloseSymbol[i++] ) 86 | return i; 87 | 88 | return False; 89 | } 90 | 91 | int IsOpenSymbol( char ch ) 92 | { 93 | int i = 0; 94 | 95 | while ( OpenSymbol[i] != '\0' ) 96 | if ( ch == OpenSymbol[i++] ) 97 | return i; 98 | 99 | return False; 100 | } 101 | 102 | int IsMatch( char Clop, char Clch ) 103 | { 104 | return IsOpenSymbol( Clop ) == IsCloseSymbol( Clch ); 105 | } 106 | 107 | int CheckSymbol( char* FilePath ) 108 | { 109 | FILE *fp = NULL; 110 | 111 | char ch; 112 | Stack stack; 113 | stack = CreateStack(); 114 | 115 | fp = fopen( FilePath, "r+" ); 116 | 117 | while ( ( ch = fgetc( fp ) ) != EOF ) 118 | { 119 | printf( "%c", ch ); 120 | 121 | if ( IsOpenSymbol( ch ) != False ) 122 | Push( stack, ch ); 123 | else if ( IsCloseSymbol( ch ) != False ) 124 | { 125 | if ( IsEmpty( stack ) ) 126 | { 127 | printf("Empty Stack!\n"); 128 | return False; 129 | } 130 | 131 | if ( IsMatch( Top( stack ), ch ) == False ) 132 | { 133 | printf("Not Match!\n"); 134 | return False; 135 | } 136 | 137 | Pop( stack ); 138 | } 139 | 140 | } 141 | 142 | if ( IsEmpty( stack ) != True ) 143 | { 144 | printf("Not Match!\n"); 145 | return False; 146 | } 147 | 148 | fclose(fp); 149 | DisposeStack( stack ); 150 | return True; 151 | } 152 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 链表基本操作/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | 4 | typedef struct Node 5 | { 6 | struct Node* Next; 7 | int Element; 8 | } *List; 9 | 10 | List CreatList( int ct ); 11 | int IsEmpty( List head ); 12 | int IsLast( List p ); 13 | List Find( List head, int num ); 14 | List FindPrevious( List head, int num ); 15 | List Delete( List head, int num ); 16 | List Insert( List head, int num, int pos); 17 | 18 | List CreatList( int ct ) 19 | { 20 | struct Node *head = NULL, *p = NULL, *q = NULL; 21 | int temp, i; 22 | 23 | for ( i = 0; i < ct; i++ ) 24 | { 25 | p = ( struct List* )malloc( sizeof( struct Node ) ); 26 | 27 | scanf("%d", &temp); 28 | p->Element = temp; 29 | p->Next = NULL; 30 | 31 | if ( head == NULL ) 32 | head->Next = p; 33 | else 34 | q->Next = p; 35 | 36 | q = p; 37 | } 38 | 39 | return head; 40 | } 41 | 42 | void DelectList( List head ) 43 | { 44 | struct Node *temp = NULL, *p = NULL; 45 | 46 | p = head->Next; 47 | free ( head ); 48 | 49 | while ( p != NULL ) 50 | { 51 | temp = p; 52 | p = temp->Next; 53 | free( temp ); 54 | } 55 | } 56 | 57 | void TraverseList( List head ) 58 | { 59 | int index = 0; 60 | struct Node *p = NULL; 61 | 62 | p = head->node; 63 | 64 | while ( p != NULL ) 65 | { 66 | printf("%d ", p->Element); 67 | index++; 68 | if ( index / 5 == 0 ) 69 | putchar('\n'); 70 | } 71 | } 72 | 73 | int IsEmpty( List head ) 74 | { 75 | if ( head->Next == NULL ) 76 | return True; 77 | else 78 | return False; 79 | } 80 | 81 | int IsLast( List p ) 82 | { 83 | if ( p->Next == NULL ) 84 | return True; 85 | else 86 | return False; 87 | } 88 | 89 | List Find( List head, int num ) 90 | { 91 | struct Node *p = NULL; 92 | 93 | p = head->Next; 94 | 95 | while ( p != NULL && p->Element != num ) 96 | p = p->Next; 97 | 98 | return p; 99 | } 100 | 101 | // O( N ) 102 | 103 | List FindPrevious( List head, int num ) 104 | { 105 | struct Node *p = NULL; 106 | 107 | p = head; 108 | 109 | while ( p->Next != NULL && p->Next->Element != num ) 110 | p = p->Next; 111 | 112 | return p; 113 | } 114 | 115 | List Delete( List head, int num ) 116 | { 117 | struct Node *p = NULL, *q = NULL; 118 | 119 | p = FindPrevious( head, num ); 120 | 121 | if ( IsLast(p) ) 122 | return head; 123 | else 124 | { 125 | q = p->Next; 126 | p->Next = p->Next->Next; 127 | free( q ); 128 | return head; 129 | } 130 | } 131 | 132 | List Insert( List head, int num, int pos) 133 | { 134 | struct Node *p = head, *temp = NULL; 135 | int i; 136 | 137 | temp = ( struct List* )malloc( sizeof( struct Node ) ); 138 | if ( pos == 1) 139 | { 140 | temp->Next = p->Next; 141 | temp->Element = num; 142 | head->Next = temp; 143 | } 144 | else 145 | { 146 | for ( i = 1; i < pos; i++ ) 147 | p = p->Next; 148 | 149 | temp->Next = p->Next; 150 | temp->Element = num; 151 | p->Next = temp; 152 | } 153 | 154 | return head; 155 | } 156 | -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 链表游标实现/算法.c: -------------------------------------------------------------------------------- 1 | # define SpaceSize 10 2 | # define True 1 3 | # define False 0 4 | 5 | struct Node 6 | { 7 | int Element; 8 | int Next; 9 | }; 10 | 11 | struct Node CursorSpace[ SpaceSize ]; 12 | 13 | void InitCursorSpace( struct Node CursorSpace[] ); 14 | int LocateCursor( int data, int L ); 15 | int CursorMalloc( void ); 16 | void CursorFree( int Position ); 17 | int IsEmpty( int L ); 18 | int IsLast( int Position, int L ); 19 | int Find( int data, int L ); 20 | int FindPrevious( int data, int L ); 21 | void CursorDelete( int data, int L ); 22 | void CursorInsert( int Position, int num, int L ); 23 | int CursorLength( int L ); 24 | void print_Cursor( int L ); 25 | 26 | void InitCursorSpace( struct Node CursorSpace[] ) 27 | { 28 | int i; 29 | 30 | for ( i = 0; i < SpaceSize; i++ ) 31 | CursorSpace[i].Next = i == SpaceSize - 1 ? 0 : i + 1; 32 | } 33 | 34 | int LocateCursor( int data, int L ) 35 | { 36 | int i = CursorSpace[L].Next; 37 | 38 | while ( i != 0 && CursorSpace[i].Element != data ) 39 | i = CursorSpace[i].Next; 40 | 41 | return i; 42 | } 43 | 44 | int CursorMalloc( void ) 45 | { 46 | int Position; 47 | 48 | Position = CursorSpace[0].Next; 49 | CursorSpace[0].Next = CursorSpace[Position].Next; 50 | 51 | return Position; 52 | } 53 | 54 | void CursorFree( int Position ) 55 | { 56 | CursorSpace[Position].Next = CursorSpace[0].Next; 57 | CursorSpace[0].Next = Position; 58 | } 59 | 60 | int IsEmpty( int L ) 61 | { 62 | if ( CursorSpace[L].Next == 0 ) 63 | return True; 64 | else 65 | return False; 66 | } 67 | 68 | int IsLast( int Position, int L ) 69 | { 70 | if ( CursorSpace[Position].Next == 0 ) 71 | return True; 72 | else 73 | return False; 74 | } 75 | 76 | int Find( int data, int L ) 77 | { 78 | int P; 79 | 80 | P = CursorSpace[L].Next; 81 | 82 | while ( P != 0 && CursorSpace[P].Element != data ) 83 | P = CursorSpace[P].Next; 84 | 85 | return P; 86 | } 87 | 88 | int FindPrevious( int data, int L ) 89 | { 90 | int P, Q; 91 | 92 | Q = L; 93 | P = CursorSpace[L].Next; 94 | 95 | while ( P != 0 && CursorSpace[P].Element != data ) 96 | { 97 | Q = P; 98 | P = CursorSpace[P].Next; 99 | } 100 | 101 | return Q; 102 | } 103 | 104 | void CursorDelete( int data, int L ) 105 | { 106 | int P, Q; 107 | 108 | P = FindPrevious( data, L ); 109 | if ( !IsLast( P, L ) ) 110 | Q = CursorSpace[P].Next; 111 | 112 | CursorSpace[P].Next = CursorSpace[Q].Next; 113 | CursorFree( Q ); 114 | } 115 | 116 | void CursorInsert( int Position, int num, int L ) 117 | { 118 | int P; 119 | 120 | P = CursorMalloc(); 121 | 122 | if ( P == 0 ) 123 | printf("Out of space!\n"); 124 | 125 | CursorSpace[P].Element = num; 126 | CursorSpace[P].Next = CursorSpace[Position].Next; 127 | CursorSpace[Position].Next = P; 128 | } 129 | 130 | int CursorLength( int L ) 131 | { 132 | int index = 0; 133 | int j = CursorSpace[L].Next; 134 | 135 | while ( j ) 136 | { 137 | j = CursorSpace[j].Next; 138 | index++; 139 | } 140 | 141 | return index; 142 | } 143 | 144 | void print_Cursor( int L ) 145 | { 146 | int P; 147 | 148 | P = CursorSpace[L].Next; 149 | 150 | while ( P != 0 ) 151 | { 152 | printf("%d ", CursorSpace[P].Element); 153 | P = CursorSpace[P].Next; 154 | } 155 | 156 | putchar('\n'); 157 | } -------------------------------------------------------------------------------- /Chapter 3: Lists, Stacks, and Queues/3 应用/中缀转换后缀且运算/算法.c: -------------------------------------------------------------------------------- 1 | # define True 1 2 | # define False 0 3 | 4 | typedef struct Node 5 | { 6 | char Element; 7 | struct Node *Next; 8 | } *Stack; 9 | 10 | const char operator[] = "+-*/"; 11 | 12 | Stack CreateStack( void ); 13 | void MakeEmpty( Stack P ); 14 | int IsEmpty( Stack P ); 15 | void Push( Stack P, char data ); 16 | void Pop( Stack P ); 17 | char Top( Stack P ); 18 | void DisposeStack( Stack P ); 19 | int IsOperator( char ch ); 20 | int IsDigit( char ch ); 21 | int Precedence( char operator ); 22 | double Calcute( double num1, double num2, char operator ); 23 | void InfixToSuffix( const char* infix, char* suffix ); 24 | 25 | Stack CreateStack( void ) 26 | { 27 | Stack P; 28 | 29 | P = ( Stack )malloc( sizeof( struct Node ) ); 30 | P->Next = NULL; 31 | 32 | return P; 33 | } 34 | 35 | void MakeEmpty( Stack P ) 36 | { 37 | while( P->Next != NULL ) 38 | Pop( P ); 39 | } 40 | 41 | int IsEmpty( Stack P ) 42 | { 43 | if ( P->Next == NULL ) 44 | return True; 45 | else 46 | return False; 47 | } 48 | 49 | void Push( Stack P, char data ) 50 | { 51 | struct Node* temp; 52 | 53 | temp = ( struct Node* )malloc( sizeof( struct Node ) ); 54 | temp->Element = data; 55 | temp->Next = P->Next; 56 | P->Next = temp; 57 | } 58 | 59 | void Pop( Stack P ) 60 | { 61 | struct Node* temp; 62 | 63 | temp = P->Next; 64 | P->Next = temp->Next; 65 | free( temp ); 66 | } 67 | 68 | char Top( Stack P ) 69 | { 70 | char data; 71 | 72 | return data = P->Next->Element; 73 | } 74 | 75 | void DisposeStack( Stack P ) 76 | { 77 | MakeEmpty( P ); 78 | free( P ); 79 | } 80 | 81 | int IsOperator( char ch ) 82 | { 83 | int i = 0; 84 | 85 | while ( operator[i] != '\0' ) 86 | if ( ch == operator[i++] ) 87 | return i; 88 | 89 | return False; 90 | } 91 | 92 | int IsDigit( char ch ) 93 | { 94 | if ( ( ch >= '0' && ch <= '9' ) || ch == '.' ) 95 | return True; 96 | else 97 | return False; 98 | } 99 | 100 | int Precedence( char operator ) 101 | { 102 | if ( operator == '*' || operator == '/' ) 103 | return 1; 104 | else if ( operator == '+' || operator == '-' ) 105 | return 0; 106 | else 107 | return -1; 108 | } 109 | 110 | double Calcute( double num1, double num2, char operator ) 111 | { 112 | switch ( operator ) 113 | { 114 | case '+': return num1 + num2; 115 | case '-': return num1 - num2; 116 | case '*': return num1 * num2; 117 | case '/': 118 | if ( num2 != 0 ) 119 | return num1 / num2; 120 | else 121 | return False; 122 | default: return False; 123 | } 124 | } 125 | 126 | void InfixToSuffix( const char* infix, char* suffix ) 127 | { 128 | Stack stack = CreateStack(); 129 | int InfixLen = strlen( infix ); 130 | double num; 131 | int i = 0, j = 0; 132 | 133 | for ( i = 0; i < InfixLen; i++ ) 134 | { 135 | if ( IsEmpty( stack ) && IsOperator( infix[i] ) ) 136 | Push( stack, infix[i] ); 137 | else 138 | { 139 | if ( infix[i] == ' ' ) 140 | continue; 141 | else if ( IsDigit( infix[i] ) ) 142 | suffix[j++] = infix[i]; 143 | else if ( infix[i] == '(' ) 144 | Push( stack, infix[i] ); 145 | else if ( infix[i] == ')') 146 | { 147 | while ( Top( stack ) != '(' ) 148 | { 149 | suffix[j++] = Top( stack ); 150 | Pop( stack ); 151 | } 152 | Pop( stack ); 153 | } 154 | else if ( Precedence( infix[i] ) == 1 ) 155 | { 156 | 157 | } 158 | else if ( Precedence( infix[i] ) == 0 ) 159 | { 160 | 161 | } 162 | } 163 | 164 | while( !IsEmpty( stack ) ) 165 | { 166 | suffix[j++] = Top( stack ); 167 | Pop( stack ); 168 | } 169 | 170 | free( stack ); 171 | } --------------------------------------------------------------------------------