1. design an algorithm to reverse the local configuration of a single-chain table with the leading node. The storage structure (data type) of a single-chain table is defined.
#include <iostream>#include <cstdlib>#include <cstdio>#include <ctime>using namespace std;typedef char ElemType;typedef struct Node{ElemType data;struct Node *next;}Node, *LinkList;LinkList CreateList(){LinkList L;ElemType c;L = (LinkList)malloc(sizeof(Node));L->next = NULL;Node *p , *tail;tail = L;c = getchar();while(c != '#'){p = (Node *)malloc(sizeof(Node));p->data = c;tail->next = p;tail = p;c = getchar();}tail->next = NULL;return L;}void ShowList(LinkList L){Node *p;p = L->next;while(p != NULL){cout << p->data << " ";p = p->next;}cout << endl;}void ReverseList(LinkList L){Node *p, *q;p = L->next;L->next = NULL;while(p != NULL){q = p->next;p->next = L->next;L->next = p;p = q;}}int main(){LinkList L;L = CreateList();ShowList(L);ReverseList(L);ShowList(L);return 0;}
2. Write a recursive algorithm to exchange left and right subtree of all nodes in the binary tree. The storage structure (data type) of the binary tree used in the algorithm is defined.
typedef struct BiNode{char data;struct BiNode *left;struct BiNode *right;}BiNode, *BiTree;
BiNode* Exchange(BiNode* T){ BiNode* p; if(NULL==T || (NULL==T->lchild && NULL==T->rchild)) return T; p = T->lchild; T->lchild = T->rchild; T->rchild = p; if(T->lchild) { T->lchild = Exchange(T->lchild); } if(T->rchild) { T->rchild = Exchange(T->rchild); } return T;}
3. Half-fold search algorithm.
#include<iostream>#include<cstdio>using namespace std;int search(int *array, int n, int target){ int low = 0; int high = n - 1; if( low > high ) return -1; while( low <= high ) { int mid = low + ( high - low ) / 2; if( array[mid] < target ) low = mid + 1; else if( array[mid] > target ) high = mid - 1; else return mid; }}int main(){ int a[] = {1,2,3,4,5,6,7,8,9}; cout<<search(a,9,4)<<endl;return 0;}
4. Data Structure textbooks p74, exercises 2, and 13
Void deletenode (listnode * s) {// Delete the listnode * P, * q; P = s of the direct frontend node of the specified node in the single-cycle linked list; while (p-> next! = S) P = p-> next; // Delete the node q = p-> next; P-> next = Q-> next; free (P ); // release space}
5. count the number of leaf nodes. P168
# Include <iostream> # include <queue> # include <cstdlib> # include <cstdio> using namespace STD; typedef struct binode {char data; struct binode * left; struct binode * right;} binode, * bitree; int sum = 0; void createbinarytree (bitree & T) // create a binary tree ABC, de, G, F ,,, {// t = (binode *) malloc (sizeof (binode); t = new binode; CIN> T-> data; If (t-> DATA = ', ') {T = NULL;} If (T! = NULL) {createbinarytree (t-> left); createbinarytree (t-> right) ;}} void preorder (bitree t) // preorder traversal {If (T! = NULL) {cout <t-> data; preorder (t-> left); preorder (t-> right) ;}} void inorder (bitree T) // traverse {If (T! = NULL) {inorder (t-> left); cout <t-> data; inorder (t-> right) ;}} void postorder (bitree T) // post-order traversal {If (T! = NULL) {postorder (t-> left); postorder (t-> right); cout <t-> data ;}} void levorder (bitree T) // level traversal {If (T! = NULL) {bitree P = T; queue <bitree> que; Que. Push (p); While (! Que. Empty () {P = que. Front (); cout <p-> data; Que. Pop (); If (p-> left! = NULL) {que. Push (p-> left);} If (p-> right! = NULL) {que. Push (p-> right) ;}}} int size (bitree t) // calculate the number of Binary Tree nodes {If (T! = NULL) {If (t-> left = NULL & T-> right = NULL) {sum ++;} size (t-> left ); size (t-> right);} return sum;} int deep (bitree t) // calculate the binary tree depth {int M, N; If (t = NULL) return 0; M = Deep (t-> left); n = Deep (t-> right); If (M> N) return m + 1; else return n + 1 ;} int main (void) {bitree t; createbinarytree (t); cout <"preorder traversal result:" <Endl; preorder (t ); cout <Endl; cout <"returns the following result:" <Endl; inorder (t); cout <Endl; the result of cout <"post-order traversal is:" <Endl; postorder (t); cout <Endl; cout <"the result of layered traversal is: "<Endl; levorder (t); cout <Endl; cout <" Number of binary tree leaf nodes: "<size (t) <Endl; cout <"binary tree depth:" <deep (t) <Endl; System ("pause"); Return 0 ;}
6. Merge sequence tables.
#define MAXSIZE 100typedef int ElemType;typedef struct SeqList{ElemType elem[MAXSIZE];int last;}SeqList;void mergeList(SeqList *LA, SeqList * LB, SeqList *LC){int i, j, k;i = j = k = 0;while (i <= LA->last && j <= LB->last){if (LA->elem[i] <= LB->elem[j]){LC->elem[k++] = LA->elem[i++];}else{LC->elem[k++] = LB->elem[i++];}}while (i <= LA->last){LC->elem[k++] = LA->elem[i++];}while (j <= LB->last){LC->elem[k++] = LB->elem[j++];}LC->last = LA->last + LB->last + 1;}