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