#define ERROR 0 #define OK 1 typedef int Status; typedef int ElemType; typedef struct Node{ ElemType data; struct Node *next; }Node; typedef struct Node * LinkList; Status ListInit(LinkList *L, ElemType array[], int count) { LinkList tail = (LinkList)malloc(sizeof(Node)); if (!tail) return ERROR; *L = tail; for (int i = 0; i < count; i++) { tail->next = (LinkList)malloc(sizeof(Node)); if (!tail->next) return ERROR; tail->next->data = array[i]; tail = tail->next; } tail->next = NULL; return OK; } Status ListTraverse(LinkList L) { LinkList p=L->next; while(p){ printf("%d ",p->data); p=p->next; } printf("\n"); return OK; } 複製代碼
要求:算法
算法分析:數組
void MergeList(LinkList *La, LinkList *Lb, LinkList *Lc){ LinkList pa,pb,pc,temp; pa = (*La)->next; pb = (*Lb)->next; *Lc = pc = *La; while (pa && pb) { if (pa->data < pb->data) { //取較小者La中的元素,將pa連接在pc的後面,pa指針後移 pc->next = pa; pc = pa; pa = pa->next; }else if(pa->data > pb->data){ //取較小者Lb的元素,將pb連接在pc後面, pb指針後移 pc->next = pb; pc = pb; pb = pb->next; }else{ //相等時取La中的元素,刪除Lb的元素; pc->next = pa; pc = pa; pa = pa ->next; temp = pb->next; free(pb); pb = temp; } } //將非空表的剩餘元素之間連接在Lc表的最後 pc->next = pa?pa:pb; //釋放Lb的頭結點 free(*Lb); } int main(int argc, const char * argv[]) { LinkList L1, L2, L3; ElemType a[6] = {1, 3, 5, 6, 7, 9}; ElemType b[6] = {2, 4, 5, 6, 8, 10}; ListInit(&L1, a, 6); ListInit(&L2, b, 6); MergeList(&L1, &L2, &L3); ListTraverse(L3); return 0; } 複製代碼
如: La = {2,4,6,8}; Lb = {4,6,8,10}; 輸出La = {4,6,8}。bash
算法分析:markdown
void Intersection(LinkList *La, LinkList *Lb, LinkList *Lc){ LinkList pa,pb,pc,temp; pa = (*La)->next; pb = (*Lb)->next; *Lc = pc = *La; while (pa && pb) { if (pa->data == pb->data) { //相等,交集併入結果鏈表中; //(1).取La中的元素,將pa連接到pc的後面,pa指針後移; pc->next = pa; pc = pa; pa = pa->next; //(2)刪除Lb中對應相等的元素 temp = pb; pb = pb->next; free(temp); }else if(pa->data < pb->data){ //刪除較小值La的元素; temp = pa; pa = pa->next; free(temp); }else{ //刪除較小值Lb中的元素 temp = pb; pb = pb->next; free(temp); } } //Lb爲空,刪除非空表La中的全部元素 while (pa) { temp = pa; pa = pa->next; free(temp); } //La爲空,刪除非空表Lb中的全部元素 while (pb) { temp = pb; pb = pb->next; free(temp); } pc->next = NULL; free(*Lb); } int main(int argc, const char * argv[]) { LinkList L1, L2, L3; ElemType a[4] = {2,4,6,8}; ElemType b[4] = {4,6,8,10}; ListInit(&L1, a, 6); ListInit(&L2, b, 6); Intersection(&L1, &L2, &L3); ListTraverse(L3); return 0; } 複製代碼
要求:spa
void Inverse(LinkList *L){ LinkList p,q; p = (*L)->next; (*L)->next = NULL; while (p!=NULL) { q = p->next; p->next = (*L)->next; //*p 插入到頭結點以後; (*L)->next = p; p = q; } } int main(int argc, const char * argv[]) { LinkList L1; ElemType a[6] = {1, 2, 4, 6, 8, 10}; ListInit(&L1, a, 6); Inverse(&L1); ListTraverse(L1); return 0; } 複製代碼
算法分析:設計
void DeleteMinMax(LinkList *L, int mink, int maxk){ LinkList p,q,pre; pre = *L; LinkList temp; p = (*L)->next; while (p && p->data < mink) { pre = p; p = p->next; } while (p && p->data<=maxk) { p = p->next; } q = pre->next; pre->next = p; while (q != p) { temp = q->next; free(q); q = temp; } } int main(int argc, const char * argv[]) { LinkList L1; ElemType a[6] = {0, 2, 4, 6, 8, 10}; ListInit(&L1, a, 6); DeleteMinMax(&L1, 1, 7); PrintList(L1); return 0; } 複製代碼
算法分析:指針
void Reverse(int *pre,int left ,int right){ //i等於左邊界left,j等於右邊界right; int i = left,j = right; int temp; //交換pre[i] 和 pre[j] 的值 while (i < j) { //交換 temp = pre[i]; pre[i] = pre[j]; pre[j] = temp; //i右移,j左移 i++; j--; } } void LeftShift(int *pre,int n,int p){ //將長度爲n的數組pre 中的數據循環左移p個位置 if (p>0 && p<n) { //1. 將數組中全部元素所有逆置 Reverse(pre, 0, n-1); //2. 將前n-p個數據逆置 Reverse(pre, 0, n-p-1); //3. 將後p個數據逆置 Reverse(pre, n-p, n-1); } } int main(int argc, const char * argv[]) { int n = 10, p = 3; ElemType *array = (ElemType *)malloc(sizeof(ElemType) * n); for (int i = 0; i < n; i++) { array[i] = i; } LeftShift(array, n, p); for (int i = 0; i < n; i++) { printf("%d ", array[i]); } printf("\n"); free(array); return 0; } 複製代碼
算法分析:code
int MainElement(int *A, int n){ int count = 1; int key = A[0]; //選取候選主元素 for (int i = 1; i < n; i++) { if (A[i] == key) { count++; }else{ if(count >0){ count--; }else{ key = A[i]; count = 1; } } } //若是count >0 if (count >0){ //統計候選主元素的實際出現次數 for (int i = count = 0; i < n; i++) if (A[i] == key) count++; } //判斷count>n/2, 確認key是否是主元素 if (count > n/2) return key; else return -1; //不存在主元素 } int main(int argc, const char * argv[]) { int A[] = {0,5,5,3,5,7,5,5}; int B[] = {0,5,5,3,5,1,5,7}; int C[] = {0,1,2,3,4,5,6,7}; int value = MainElement(A, 8); printf("數組A 主元素爲: %d\n",value); value = MainElement(B, 8); printf("數組B 主元素爲(-1表示數組沒有主元素): %d\n",value); value = MainElement(C, 8); printf("數組C 主元素爲(-1表示數組沒有主元素): %d\n",value); return 0; } 複製代碼
算法分析:orm
void DeleteEqualNode(LinkList *L,int n){ int *p = alloca(sizeof(int)*n); LinkList r = *L; for (int i = 0; i < n; i++) { *(p+i) = 0; } LinkList temp = (*L)->next; while (temp!= NULL) { if (p[abs(temp->data)] == 1) { r->next = temp->next; free(temp); temp = r->next; }else { p[abs(temp->data)] = 1; r = temp; temp = temp->next; } } } int main(int argc, const char * argv[]) { LinkList L1; ElemType a[6] = {0, 15, 3, -3, -15, 2}; ListInit(&L1, a, 6); DeleteEqualNode(&L1, 16); PrintList(L1); return 0; } 複製代碼