1、n - 1; q=p; q-) elementq+1 = palist-elementq;elementp = x;n = palist-n + 1; return 1;int deleteP_seq(PSeqList palist, int p)n-1) for(q=p; qelementq+1;LinkList createNullList_link(void)/创建一个带头结点的空链表 /申请表头结点空间 LinkList llist = (LinkList)malloc(sizeof( struct Node); if (llist ! llist-link = NULL; else
2、printf( /创建失败 return (llist);int insertPost_link(LinkList llist, Node *p, DataType x) /在llist带头结点的单链表中,p所指结点后面插入元素x Node *q = (Node*)malloc(sizeof( struct Node); /申请新结点 if ( q = NULL ) return(0); else q-info = x;link = p-link; p-link = q; return(1); int deleteV_link( LinkList llist, DataType x ) /在l
3、list带有头结点的单链表中删除第一个值为x的结点 Node *p, *q; p = llist; if (p=NULL) return(0); while ( p-link != NULL &link-info != x ) p = p- /找值为x的结点的前驱结点的存储位置 if ( p-link = NULL ) /没找到值为x的结点 else q = p- /找到值为x的结点link = q- /删除该结点 free(q);#include stdlib.h#include SeqList.h“void josephus_seq(PSeqList palist, int s, int
4、m) int s1, i, w; for(i = palist-n; i0; i-) /找出列的元素 s1 = (s1 - 1 + m - 1) % i ; w = palist-elements1; /求下标为s1的元素的值Out element %d n, w); /元素出列 deleteP_seq(palist, s1); /删除出列的元素void main() int i, n, s, m;n please input the values(100) of n = scanf(%d, &n); please input the values of s = s); please inpu
5、t the values of m = m); PSeqList jos_alist = createNullList_seq( n ); /创建空顺序表 if (jos_alist!=NULL) for( i = 0; i element); free(jos_alist);int index(PSeqString t, PSeqString p) int i, j; /i为p串中当前字符的下标,j为t串中当前字符的下标 i=0; j=0; /初始化 while (i n & j n) /反复比较 if (p-ci = t-cj) /继续匹配下一个字符 i+; j+; else /主串、子串
6、的i、j值回溯,重新开始下一次匹配 j = j - i + 1; i = 0; if (i = p-n) return( j - p-n + 1); /匹配成功,返回p中第一个字符在t中的序号 else return( 0 ); /匹配失败void push_seq( PSeqStack pastack, DataType x ) if( pastack-t = pastack-MAXNUM - 1) printf( ); else pastack-t = pastack-t + 1;spastack-t = x;void pop_seq( PSeqStack pastack ) if (pa
7、stack-t = -1 )Underflow!t - 1;void push_link(PLinkStack plstack, DataType x) PNode p; p = (PNode)malloc(sizeof(struct Node); if (p = NULL ) printf(link = plstack-top; plstack-top = p;void pop_link(PLinkStack plstack) if (isEmptyStack_link(plstack) printf(Empty stack pop.n p = plstack-top = plstack-t
8、op- free(p);mazeFrame(void) 创建一个(保存探索过程的)空栈; 把入口位置压入栈中; while 栈不空 取栈顶位置并设置为的当前位置; while 当前位置存在试探可能 取下一个试探位置; if (下一个位置是出口) 打印栈中保存的探索过程然后返回; if(下一个位置是通道) 把当前位置进栈并且设置新的当前位置;void mazePath(int *maze, int x1, int y1, int x2, int y2, int M, int N) 初始设置:当前位置为x1, y1 while !( (当前位置为终点) | (回到原点,并无路可走) ) if (下
9、一个位置可以走) 当前位置 = 下一位置; 当前位置设为已走标志; 当前位置 = 上一位置; if (当前位置 = 终点) then 输出当前走的路径 输出:不存在路径void mazePath(int *maze,int x1,int y1,int x2,int y2,int M,int N) while (存在下一个可走的位置) 遍历当前位置的四个方向,找到下一位置(回到原点,并无路可走) if (当前位置 = 终点) thenvoid enQueue_seq(PSeqQueue paqu, DataType x) if (paqu-r + 1) % paqu-MAXNUM = paqu-
10、f )Full queue.n paqu-qpaqu-r = x;r = (paqu-MAXNUM;void deQueue_seq(PSeqQueue paqu) if( paqu-f = paqu-r )Empty Queue.nf = (paqu-f + 1) % paqu-void enQueue_link(PLinkQueue plqu, Datatype x) p = (PNode )malloc( sizeof( struct Node); /申请新结点空间 if (p = NULL) printf( /填写新结点信息 if (plqu-f = NULL) plqu-f = p;
11、 /插入前是空队列 else plqu-r-link = p; /将新结点插入 plqu-r = p; /修改对尾指针void deQueue_link(PLinkQueue plqu) if( plqu-f = NULL )Empty queue.n /队列已空 p = plqu-f;f = p - /修改队头指针 /释放已经删除结点空间void preOrder(BinTree t) if (t=NULL) return; visit(root(t); preOrder(leftChild(t); preOrder(rightChild(t);void inOrder(BinTree t)
12、 inOrder(leftChild(t); inOrder(rightChild(t);void postOrder(BinTree t) postOrder(leftChild(t); postOrder(rightChild(t);void nPreOrder(BinTree t) Stack s; /栈元素的类型是BinTree BinTreeNode* c; if (t = NULL) return; s = createEmptyStack(); push(s, t); while (!isEmptyStack(s) /每当栈不空 c = top(s); pop(s); /取栈顶,
13、出栈 if (c ! visit(root(c); /访问 push(s, rightChild(c); /右子树进栈 push(s, leftChild(c); /左子树进栈void insertSort(SortObject * pvector) /按递增序进行直接插入排序 RecordNode temp; for (i = 1; pvector- i+) /依次插入记录R1, R2Rn-1 temp = pvector-recordi; /本次待插入的记录 j = i-1; /由后向前找插入位置 while (temp.key =0)recordj+1 = pvector-recordj
14、; j-; /将排序码大于ki的记录后移 if (j != (i-1) pvector-recordj+1 = temp;void selectSort(SortObject * pvector) int i, j, k; i+ ) /做n-1趟选择排序 k = i; for (j = i+1; j+) /在无序区内找出排序码最小的记录Rk if (pvector-recordj.key record k;record k = temp; void bubbleSort(SortObject * pvector) int i, j, noswap; for (i=0; i i+) /做n-1次
15、起泡 noswap = TRUE; /置交换标志初态 for (j=0; jn-i-1; j+) recordj+1.key recordj+1; noswap = FALSE; /修改交换标志 if (noswap) break; /本趟起泡未发生记录交换,算法结束void quickSort(SortObject * pvector, int L, int R) if(L = R) return; /只有一个记录或无记录,则无须排序 i = L; j = R; temp=pvector- while(i != j) /寻找Rl的最终位置 while( (pvector-recordj.ke
16、y = temp.key) & (ji) ) j-; /从右向左扫描,查找第1个排序码小于temp.key的记录 if (i recordi+ = pvector-record j;recordi.key recordi=temp; /找到Rl的最终位置 quickSort(pvector, L, i-1); /递归处理左区间 quickSort(pvector, i+1, R); /递归处理右区间void mergeSort(SortObject * pvector) RecordNode recordpvector-n; int length = 1; while (length record, record, pvector-n, length); /一趟归并,结果放在r1中 length*=2; mergePass(record, pvector-record, pvector- /一趟归并,结果放在r中
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1