天勤数据结构编程题
文章目錄
- 第二章 線性表
- 第三章 棧和隊列
- 第四章 串
第二章 線性表
設計算法,逆置順序表中所有元素
void Reverse(Sqlist &L) {int i, j;int temp;for (int i = 0, j = L.Length; i < j; i++, j++){temp = L.data[i];L.data[i] = L.data[j];L.data[j] = temp;} }設計算法,順序表L中刪除下標i~j的所有元素
void Del_span(Sqlist &L, int i, int j) {int dalta = j - i + 1; //元素移動的距離for (int k = j + 1; k < L.length; k++){L.data[k - dalta] = L.data[k];}L.length = dalta; // 修改表的長度 }將順序表L中所有小于表頭元素的整數放在前半部分,大于表頭元素的整數放在后半部分
注意:這個過程里面的i和j是輪流移動的,即j掃描結束換到i
有一個遞增非空單鏈表,設計一個算法刪除值域重復的結點
void Del_re(LNode *L) {LNode *p = L->next, *q;while (p->next != NULL){if (p->data == p->next->data){q = p->next;p->next = q->next;free(q);}elsep = p->next;} }刪除單鏈表L(有頭結點)中的一個最小值結點 (不太懂)
void Del_min(LNode *L) {LNode *pre = L, *p = pre->next, *minp = p, *minpre = pre;while (p != NULL) // 查找最小值結點minp以及期前區結點minpre{if (p->dara < minp->data){minp = p;minpre = pre;}pre = p;p = p->next;}minpre->next = minp->next; //刪除*minp結點free(minp); }線性表,帶頭結點的單鏈表L。設計算法將其逆置,要求不能建立新節點,只能通過表中已有節點的重新組合來完成。
void Reverse_1(LNode *L) {LNode *p = L->next, *q;L->next = NULL;while (p != NULL){q = p->next; // q結點作為輔助結點來記錄p的直接后繼結點的位置p->next = L->next; //將p所指的結點插入新的鏈表中,我覺得這里p->next = NULL應該也是對的L->next = p; // 因為后繼結點已經存入q中,所以p仍然可以找到后繼p = q;} }將一個頭結點為A的單鏈表分解成兩個單鏈表A和B,使得A鏈表只含有原來鏈表中data域為奇數的結點,B鏈表為偶結點,且保持原來的相對順序
void Depart_AB(LNode *A, LNode *&B) {LNode *p, *q, *r;B = (LNode *)malloc(sizeof(LNode)); //申請鏈表B的頭結點B->next = NULL; //申請結點后的常規操作r = B;p = A;while (p->next != NULL){if (p->next->data % 2 == 0) // 挑出是偶數的,從鏈表中取下{q = p->next;p->next = q->next;q->next = NULL;r->next = q; // 將取下的結點插入到r中r = q;}elsep = p->next;} }在N個個位正整數存放在int型數組A[0, … , N-1]中,N為已定義的常量且N≤9,數組N[]的長度為N,另給一個int型變量i,要求只用上述變量(A[0] ~A[N-1]與i,這N+1個整形變量)寫一個算法,找出這N個整數中的最小者,并且要求不能破壞數組A[]中的數據。
void findMin(int a[], int &i) // i用來保存最小值 {i = A[0]; // i先保存存入A[0]的值while (i / 10 <= N - 1) // 取i的十位上的數字作為循環變量,與N-1作比較{if (i % 10 > A[i / 10]) // 取i的個位上的數字與A[i/10]中的各數值作比較{i = i - i % 10; // 如果i的個位上的數字大于A[i/10]中的數字,則將i的個位上的數字換成A[i/10]i = i + a[i / 10];}i = i + 10; // i的十位上的數字加一,就是對A[]中的下一個數字進行檢測}i = i % 10; // 循環結束后,i的個位上的數字保存了A[]中的最小值,將i更新為i的個位上的數字 }逆序打印單鏈表中的數據局,假設指針指向了單鏈表的開始結點
void reprint(LNode *L) {if(L != NULL){reprint(L->next);cout << L->data << " "; } }設有兩個有序鏈表表示的集合A和B,判斷他們是否相等
void isEqual(LNode *A, LNode *B) {LNode *p = A->next;LNode *q = B->next;while (p != NULL && q != NULL){if(p->data == q->data){p = p->next;q = q->next;}else return 0;}if(p != NULL || q != NULL) // 判斷A、B兩個序列長度是否相等return 0;else return 1; }鍵盤輸入n個英文字幕,請編程用輸入數據建立一個單鏈表,并要求將字母不重復存入鏈表
void createLinkSameElem(LNode *head) {head = (LNode*)malloc(sizeof(LNode));head->next = NULL;LNode *p;int n;char ch;scanf("%d", n); for (int i = 0; i < n; i++){scanf("%s", ch);p = head->next;while (p != NULL){if(p->data == ch)break;p = p->next;}if(p == NULL){p = (LNode*)malloc(sizeof(LNode));p->data = ch;p->next = head->next;head->next = p;}} }第三章 棧和隊列
假設以帶頭結點的循環鏈表表示隊列,并且只設一個指針指向對為節點,但不設頭指針,請寫出相應的入隊和出隊算法。
// 入隊 void enQueue(LNode *&rear, int x) {LNode *s = (LNode*)malloc(sizeof(LNode)); // 申請結點空間s->data = x;s->next = rear->next; //將s結點插入隊尾rear->next = s;rear = s; //rear指向新隊尾 } // 出隊 int deQueue(LNode *&rear, int &x) {LNode *s;if(rear->next == rear)return 0;else{s = rear->next->next; // s指向開始結點rear->next->next = s->next; // 隊頭元素出隊x = s->data;if(s == rear) // 如果元素出隊后隊列為空rear = rear->next; // 將rear指向頭結點free(s); // 釋放隊結點空間return 1;} }如果允許在循環隊列地兩端都可以進行插入和刪除操作,要求:寫出循環隊列的類型定義;分別寫出從隊尾刪除和從隊頭插入的算法。
// 結構體定義 typedef struct {int data[maxSize];int front, rear; }cysqueue; // 出隊算法(隊尾刪除) int deQueue(cycqueue &Q, int &x) {if(Q.front == Q.rear) // 判斷隊是否為空return 0;else{x = Q.data[Q.rear];Q.rear = (Q.rear - 1 + maxSize) % maxSize; // 修改隊尾指針return 1;} }// 入隊算法(隊頭插入) int enQueue(cycqueue &Q, int x) {if(Q.rear == (Q.front - 1 + maxSize) % maxSize) // 判斷隊是否為滿return 0;else{Q.data[Q.front] = x; Q.front = (Q.front - 1 + maxSize) % maxSize; // 修改隊頭指針return 1;} }將一個非負的十進制整數N轉換為一個二進制數
int BaseTrans(int N) {int i, result = 0;int stack[maxSize], top = -1;while (N != 0){i = N % 2;N = N / 2;stack[++top] = i;}while (top != -1){i = stack[top];--top;result = result*10 + i;}return result; } int bracketCheck(char f[]) {stack S; char ch; // 定義一個棧char* p = f;while (*p != '\0') // 順序掃描串中的每一個字符{if(*p == 39){++p; // 跳過第一個單引號while (*p != 39)++p;++p; // 跳過最后一個單引號}else if(*p == 34) // 雙引號內的字符不參與配對比較{++p;while (*p != 34) // 跳過第一個雙引號++p;++p; // 跳過最后一個雙引號}else{switch (*p){case '{':case '[':case '(': push(A, *p);// 出現左括號,進棧break;case '}': getTop(S, ch);if(ch == '{')pop(S, ch); // 棧頂的左花括號出棧elsereturn 0;break;case ']': getTop(S, ch);if(ch == '[') // 棧頂的左方括號出棧pop(S, ch);elsereturn 0;break;case ')': getTop(S, ch);if(ch == '(') // 棧頂的左圓括號出棧pop(S, ch);elsereturn 0;}++p;}}if(isEmpty(S))return 1;elsereturn 0; }設計一個遞歸算法,求n個不同字符的所有全排序列
void perm(char str[], int k, int n) {int i, j;char temp;if(k==0){for (j = 0; j <= n-1; ++j)printf("%c", str[j]); }else{for (i = 0; i <= k; ++i){temp = str[k];str[k] = str[i];str[i] = temp;perm(str, k-1, n);temp = str[i];str[i] = str[k];str[k] = temp;} } }第四章 串
將串str中所有值為ch1的字符轉換成ch2的字符,如果str為空串,或者串中不含值為ch1的字符,則什么都不做。
void replace(Str &str, char ch1, char ch2) {for (int i = 0; i < str.Length; i++){if (str.ch[i] == ch1)str.ch[i] = ch2;} }實現串str的逆轉函數,如果str為空串,則什么都不做
void swap(char& ch1, char& ch2) {char temp = ch1;ch1 = ch2;ch2 = temp; } void reverse(Str& str) {int i = 0;int j = str.Length - 1;while (i < j){swap(str.ch[i], str.ch[j]);++i;--j;} }刪除str中值為ch的所有字符,如果str為空串或者串中不含值為ch的字符,則什么都不做
typedef stuct {char ch[maxSize];int length; }Str; void delCh(Str &str, char ch) {if (str.length != 0){for (int i = 0; i < str.length;){if (str.ch[i] == ch){for (int j = i; j < str.length - 1; ++j)str.ch[j] = str.ch[j + 1];--str.length;}else++i;}str.ch[str.length] = '\0';} }從串str中的pos位置起,求出與substr串匹配的子串的位置,如果str為空串,或者串中不含與substr匹配的子串,則返回-1做標記
int KMP(Str str, Str substr, int pos) {int i = pos, j = 1;while (i <= str.length && j <= substr.length){if(j == 0 || str.ch[i] == substr.ch[j]){++i;++j;}elsej = next;}if(j > substr.length)return i - substr.length;else return -; }采用定長順序存儲表示串,編寫一個函數,刪除從下表為i的字符開始的j個字符,如果i后面的字符串不足j個,有幾個刪幾個
算法思想:從下標為i+j個字符開始,將所有的字符向前移動j個單位,然后將字符串進行調整。
void delIJ(Str &str, int i, int j) {if (i < str.length && i >= 0 && j >= 0){for (int k = i + j; k < str.length; ++k){str.ch[k - j] = str.ch[k];str.length -= (str.length - i < j ? str.length - i : j);// 調整字符串長度,注意要求str.ch[str.length] = '\0';}} }采用順序存儲方式存儲串,編寫一個函數,將串str1中的下標i到下標j之間的字符用str2替換
算法思想:取str1中0到i-1位置上的子串str11,取str1中j到串尾的子串str12,最后連接str11、str2、str12
這道題我需要冷靜一下,真的需要這么復雜嗎?計算一個子串在一個主串中出現的次數,如果該子串不出現,則返回0。不考慮子串重疊
int index(Str str, Str substr) {int i = 1, j = 1, k = 1, sum = 0;while (i <= str.length){if (str.ch[i] == substr.ch[j]){++i;++j;}else{j = 1;i = ++k; // 匹配失敗,i從主串下一位置開始,k中記錄了上一次的起始位置}if (j > substr.length){j = 1;++sum;}}return sum; }構造串的鏈表結點數據結構(每個節點內存儲一個字符),編寫函數,找出串str1中第一個不在str2中出現的字符
typedef struct SNode {char data;struct SNode *next; }SNode;char findfirst(SNode* str1, SNode*str2) {for (SNode* p = str1; p != NULL; p = p->next){bool flag = false;for (SNode* q = str1; q != NULL; q = q->next){if(p->data == q->data){flag = true;break;}}if(flag == false)return p->data; }return '\0'; }總結
- 上一篇: Flash cs4 for mac 序列
- 下一篇: 完全免费的在线遥感影像下载器-转载