随笔分类 -  数据结构笔试题集锦_栈与队列篇(不断更新)

摘要:1.递归算法: 程序代码: void move(char x,char y) { static int count = 0; printf("%c -> %c",x,y); count++; if(count == 4) { printf("\n"); count = 0; } else printf("\t"); } void hanio(int n,char x,char y,char z) //把n个圆盘从塔座X移动到塔座Z的过程,Y为辅助塔座 ... 阅读全文
posted @ 2012-09-09 23:05 毛毛hhmm 阅读(223) 评论(0) 推荐(0)
摘要:1.递归算法: 程序代码: int fib(int n) { if(n < 0) return -1; if(n == 0 || n==-1) return n; return fib(n-2)+fib(n-1); } 时间复杂度:fib(n)所耗的时间为T(n),则T(0) = T(1) = O(1),当n>=2时,T(n) = T(n-2)+T(n-1)。由于T(n-1) = T(n-3)+T(n-2)>=T(n-2),因此,T(n) = T(n-2)+T(n-1) <= 2T(n-1),所以,T(n) <= 21T(n-1) <= 22T(... 阅读全文
posted @ 2012-09-09 23:03 毛毛hhmm 阅读(458) 评论(0) 推荐(0)
摘要:数据结构: struct Queue { DataType sequ[m]; int rear,quelen; }; typedef struct Queue * PQueue;思路:入队列和出队列的算法和普通队列的思路近似,只是原来用两指针指向只差来表示队列中元素的个数,现在直接用quelen来表达。程序代码: PQueue createEmptyQueue() //创建空队列 { PQueue pqu = (PQueue)malloc(sizeof(struct Queue)); pqu->rear = 0; pqu->quelen ... 阅读全文
posted @ 2012-09-09 22:43 毛毛hhmm 阅读(890) 评论(0) 推荐(0)