int Palindrome()
{
MyStack *stack;
LinkQueue *lq;
int sch,qch;
stack = InitStack();
lq = InitQueue();
printf("请输入字符串:");
while((sch=getchar())!='@')
{
PushStack(stack, sch);
EnQueue(lq, sch);
}
while(stack->top!=stack->base)
if(PopStack(stack,&sch)==-1||
DeQueue(lq, &qch)==-1||(sch!=qch))
{
DestroyStack(stack);
DestroyQueue(lq);
return -1;
}
DestroyStack(stack);
DestroyQueue(lq);
return 0;
}
1.统计并返回线性表list中值为X的元素个数
public static int count(LList list, Object X) {
int t=0;
for (int i=0;i<list.size():i++)
if(list.value(i+1).equals(X))t++;
return t;
}
2. //求出并返回参数线性表list中所有元素的最小值
public static Object minmum(LList list){
if (list.size()==0)return 0;
Object min=list.value(1);
for (int i=2;i<list.size();i++){
Object x=list.value(i);
if (x<min) min=t;
}
return min;
}
3.//设计判断单链表中元素是否是递增的算法
public int isriselk(linklist head){
if (head==0||head.next==0)
return(1);
else
for (q=head,p=head.next;p!=NULL;q=p,p=p.next)
if (q.dath.compare to (p.ata)>0)
return(0);
return(1);
}
- 假设循环队列只设rear和Queuelength来分别指示队尾元素的位置和队中元素的个数,试给出判别循环队列的队满条件,并写出相应的入队和出队算法,要出出队时需返回队头元素。
//队满的条件
this.Queuelength==MAXSIZE;//MAXSIZE是指循环队列的最大空间
int enqueue(Qeletype e){//入队列
if (this.Queuelength==MAXSIZE)
return;0;
this.rear=(this.rear+1)%MAXSIZE;
this.element[this.rear]=e;
this.Queuelength++;
}
Qeletype Delqueue(){//出队列
if (this.Queuelength==0)
Exit(0);
e=this.element[(this.rear-this.Queuelength+MAXSIZE)]
this.Queuelength--;
return e;
}
- 根据下面成员方法声明,编写出求一颗二叉树中叶子结点总数的递归方法,参数p初始指向根结点。
public int countleaf(BinaryNode<T>p){
int i=-1;
if (p==null)
return 0;
else if ((p.left==null)&&(p.right==null))
return 1;
else return countleaf(p.left)+countleaf(p.right);
}
- 编写递归算法:求二叉树p中关键字以x的结点为根的子树的深度。
public int height(BinaryNode<T>p){
//返回以p结点为根的子树高度
if (p==null)
return 0;
int lh=height(p.left);
int rh=height(p.right);
return(lh>=rh)?lh+1:rh+1;
}
public BinaryNode<T>searchNode(BinaryNode<T>p,Tkey){
//在以p为根的子树中查找并返回首次出现的关键字为key元素结点
if (p==null||key==null)
return null;
if (p.data.equals(key))
return p;
BinaryNode<T>find=searchNode(p.left,key);
if (find==null)
find=searchNode(p.right,key);
return find;
}
public int getchilddepth(BinaryNode<T>p,T x){
//二叉树p中关键字为x的结点为根的子树的深度
BinaryNode<T>q;
q.searchNode(p,x);
if (q==null)
return 0;
else
return height(q);
}
- 编写一个算法,求出邻接矩阵表示的有向图中序号为num的顶点入度和出度数。
public int indegree(int num){//返回顶点vi的入度
int n=this.vertexcount();//顶点数
int degree=0;
for (int j=0;j<n;j++)//第i列上各元素和是顶点vi的入度
if (num!=j&&this.adjmatrix[j][num]!=MAX_WEIGHT)
degree++;
return degree;
}
public int outdegree(int num){//返回顶点vi的出度
int n=this.vertexcount();//顶点数
int degree=0;
for (int j=0;j<n;j++)//第i列上各元素和是顶点vi的出度
if (num!=j&&this.adjmatrix[num][j]!=MAX_WEIGHT)
degree++;
return degree;
}
- 设计判断二叉树是否为二叉树排序树的算法
public class BinarySortTree<Textends Comparable<T>>extend BinaryTree<T>//二叉排序树类
int minnum=-32768,flag=1;
typedef struct BinaryTreeNode{int key;BinaryTreeNode lchild,rchild;}
BinaryTree;
public void inorder(BinaryTree bt){
if (bt!=0)
{
inorder(bt.lchild);
if (minnum>bt.key)
flag=0;
minnum=bt.key;
inorder(bt.rchild);
}
}
- 设计在二叉排序树上查找结点X的算法
BinaryTree bstsearchX(BinaryTree,int key){
BinaryTree p=t;
while (p!=0)
if (p.key==key)
return(p);
else if (p.key>key)
p=p.lchild;
else p=p.rchilid;
return(0);
}
- 设计在链式结构上实现简单选择排序算法
public void paixu(Linklist head){
LinkList p,q,s;
int min,t;
if (head==0||head.next==0) return 0;
for (q=head;q!=0;q=q.next){
min=q.data;
s=q;
for (p=q.next;p!=0;p=p.next)
if (min>p.data){
min=p.data;
s=p;
}
if (s!=q){
t=s.data;
s.data=q.data;
q.data=t;
}
}
}