算法练习题

2014

1、在带头结点的循环链表L中,删除第i个结点,并由e返回其值
//双向循环链表
typedef struct DuLNode{
	ElemType data;
	struct DuLNode *prior;
	struct DuLNode *next;
}DuLNode,*DuLinkList;
status ListDelete_Dul(DuLinkList &L,int i,ElemType &e){
	if(!(GetElemP_DuL(L,i))){//找到第i个结点
		return error;
	}
	e=p->data;
	//删除第i个结点
	p->prior->next=p->next;
	p->next->prior=p->prior;
	free(p);
	return ok;
}
2、从栈中弹出一个元素入队
void PopStack_EnQue(LinkStack &LS,LinkQue &LQ){
	if(LS!=NULL){
		LinkNode p=LS;
		LS=LS->next;
		p->next=NULL;
		LQ.rear->next=p;
		LQ.rear=p;
	}
}
3、求在先序遍历中处于第k个位置的结点
int count=0;
BiTree search(BiTree t,int k){
	if(t!=null){
		count++;
		if(count==k){
			return t;
		}else{
			search(t->lchild,k);
			search(t->rchild,k);
		}
	}
}
4、判断链表元素是否递增
bool isIncreasing(LinkList L){
	LinkNode *p=L;
	while(p->next==NULL){
		if(p->data<p->next->data){
			p=p->next;
		}else{
			return false;
		}
	}
	return true;
}
5、查找结点值为x的结点的双亲
typedef struct bitree{
	ElemType data;
	struct btree *parent;
	struct btree *lchild,*rchild;
}BiTree;

void findparent(BTNode *b,ElemType x,BTNode *&p){
	if(p!=null){
		if(p->data==x){
			p=null;
		}else if(b->lchild!=null && b->lchild->data==x){
			p=b;
		}else if(b->rchild!=null && b->rchild->data==x){
			p=b;
		}else{
			findparent(b->lchild,x,p);
			if(p==null){
				findparent(b->rchild,x,p);
			}
		}
	}else{
		p=null;
	}
}
6、给出求最小生成树的算法
void minspantree(mgraph g,int u){
	int k=locatevex(g,u);
	for(int j=0;j<n;j++){
		if(j!=k){
			closeedge[j]={u,g[k][j].sdj};
		}
	}
	closeedge[k].lowcost=0;
	for(int i=1;i<n;i++){
		
	}
}
7、拓扑排序

2015

1、合并两个有序链表并且要求其中没有重复值
void mergeList(LinkList &La,LinkList &Lb,LinkList &Lc){
	LinkNode *pa,*pb;
	pa=La->next;
	pb=Lb->next;
	Lc=pc=La;//La的头节点作为Lc的头节点
	while(pa && pb){
		if(pa->data<pb->data){
			pc->next=pa;
			pc=pa;
			pa=pa->next;
		}else if(pa->data>pb->data){
			pc->next=pb;
			pc=pb;
			pb=pb->next;
		}else{
			pc->next=pa;
			pc=pa;
			pa=pa->next;
			//删除Lb中重复的部分
			q=pb->next;
			free(pb);
			pb=q;
		}
	}
	pc->next=pa?pa:pb;
	free(Lb);//因为用La作了Lc的头节点,这里需要将Lb的头节点
}
2、先序遍历二叉树的非递归算法(借助栈)
void PreOrder(Bitree T){
	InitStack(S);
	BNode p=T;
	while(p || !StackEmpty(S)){
		if(p){
			pritntf(p->data);
			push(S,p);
			p=p->lchild;
		}else{
			pop(S,p);
			p=p>rchild;
		}
	}
}
3、移动最小值结点到最前面
void DelInsert(LinkList &L){
	LinkNode *p=L->next;
	LinlNode *pre=L;//pre指向最小值结点的前缀
	LinkNode *q=p;//q指向最小值结点
	while(p->next!=null){
		if(p->next->data<q->data){
			pre=p;
			q=p->next;
		}
		p=p->next;
	}
	if(q!=L->next){
		pre->next=q->next;//将最小值结点从链表上取下来
		q->next=L->next;
		L->next=q;
	}
}
4、判断两颗二叉树是否相等
struct BiNode{
	char data;
	struct BiNode *lchild,*rchild;
}BiNode;

int BTreeEqual(BiTNode *T1,BiTNode *T2){
	if(T1==null && T2==null){
		return 1;
	}else if(T1==NULL || T2==null){
		return 0;
	}else if((T1->data==T2->data) && BTreeEqual(T1->lchild,T2->lchild) &&
		BTreeEqual(T1->rchild,T2->rchild)){
		return 1;
	}else{
		return 0;
	}
}
5、在顺序有序表中实现二分查找算法
int biSearch(struct record r[],int k){
	int low=0,mid,high=r.length-1;
	while(low<=high){
		mid=(low+high)/2;
		if(r[mid==k]){
			return mid+1;
		}else if(r[mid].key>k){
			high=mid-1;
		}else{
			low=mid+1;
		}
	}
	return 0;
}
6、将无向图的邻接矩阵转换为邻接表的算法
typedef struct arr{
	int *base;
	int n;
}
typedef struct Node{
	int data;
	int *next;
}Node,*Printer,List[];
Create_Link(arr A,List &A){
	int i;
	for(i=0;i<n;i++){
		L[i].next=null;
	}
	for(i=0;i<n;i++){
		for(int j=0;j<=n;j++){
			if(A[i][j]){
				p=(Node *)malloc(sizeof(Node));
				p->data=i+1;
				p->next=L[i].next;
				L[i].next=p;
			}
		}
	}
}

2016

2、循环队列,插入和删除一个元素
struct CyclicQueue{
	ElemType elem[M];
	int rear;
	int length;
}
bool EnQueue(CyclicQueue &Q,ElemType x){
	//Q不为空时,x插入队尾
	if(Q.length==M){
		return false;
	}
	Q.elem[Q.rear]=x;
	Q.rear=(Q.rear+1)%M;
	Q.length++;
	return true;
}
bool DelCQueue(CyclicQueue &Q,ElemType &x){
	if(Q.length==0){
		return flase;
	}
	x=Q.elem[(Q.rear-Q.length+M)%M];
	Q.length--;
	return true;
}
3、链表中增增加一个元素后依然保持有序
void Insert(LNode *H,ElemTp x){
	LNode s=(LNode *)malloc(sizeof(LNode));
	s->data=x;
	LNode q=H;
	LNode p=H->next;
	while(p && p->data<=x){
		q=p;
		p=p->next;
	}
	s->next=p;
	q-next=s;
}
4、先序遍历二叉树
5、bfs算法
6、快速排序算法
void QuickSort(int R[],int low,int high){
	int temp;
	int i=low,j=high;
	if(low<high){
		temp=R[low];
		while(i<j){
			while(j>i&&R[j]>=temp){//从右往左扫描
				j--;
			}
			if(i<j){
				R[i]=R[j];
				i++;
			}
			while(i<j&&R[i]<temp){
				++i;
			}
			if(i<j){
				R[j]=R[i];
				--j;
			}
		}
		R[i]=temp;
		QuickSort(R,low,i-1);
		QuickSort(R,i+1,high);
	}
}

2017

1、求带头结点链表长度
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
int ListLen(LinkList L){
	LinkNode p=L->next;
	int len=0;
	while(p!=null){
		len++;
		p->next;
	}
	return len;
}
2、二分查找关键字
typedef struct LNode{
	ElemType *elem;
	int length;
}SSTable;
int Search_Bin(SSTable ST,keyType key){
	int low=1,high=ST.length;
	while(low<=high){
		mid=(low+high)/2;
		if(ST.elem[mid].key==key){
			return mid;
		}else if(ST.elem[mid].key>key){
			high=mid-1;
		}else{
			low=mid+1;
		}
	}
	retuen 0;
}
3、中序遍历的非递归算法
typedef struct BiTNode{
	ElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void InOrder(BiTree T){
	InitStack(S);
	BiTNode p=T;
	while(p || !StackEmpty(S)){
		if(p){
			push(S,p);
			p=p->lchild;
		}else{
			pop(S,p);
			printf(p->data);
			p=p->rchild;
		}
	}
}
4、求无向图的基于DFS的深度优先遍历算法
//孩子兄弟链表
typedef struct CSNode{
	ElemType data;
	struct CSNode *firstchild,*nextsibling;
}CSNode,*CSTree;

2018

1、单链表中取出第i个元素的算法
typedef struct LNode{
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;
bool getElem_L(LinkList L,int i,ElemType &e){
	LinkNode *p=L->next;
	int j=0;
	while(p && j<i){
		p=p->next;
		j++;
	}
	if(!p || j>i){
		return true;
	}
	e=p->data;
	return true;
}
2、删除双向链表中的第i个元素
typedef struct DuLNode{
	ElemType data;
	struct DUlNode *prior;
	struct DulNode *next;
}DulNode,*DulLinkList;
bool deleteDulElem(DulLinkList &L,int i,ElemType &e){
	struct LNode* p=L->next;
	int k=0;
	while(p && k<i){
		++k;
		p=p->next;
	}
	if(k>i){ //?存疑
		return ture;
	}
	e=p->data;
	p->prior->next=p->next;
	p->next->prior=p->priir;
	free(p);
	return true;
}
3、元素e压入顺序栈
typedef struct{
	int data[maxSize];
	int top;
}SqStsck;
bool push(SqStack &st,ElemType e){
	if(st.top==maxSize-1){
		return false;
	}
	++(st.top);
	st.data[st.stop]=e;
	return true;
}
4、创建一颗二叉树
typedef struct BiTNode{
	TElemType data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
bool createBiTree(BiTree &T){
	scanf(&ch);
	if(ch==' '){
		T=null;
	}else{
		if(!(T=(BiTNode*)malloc(sizeof(BiTNode)))){
			exit(OVERFLOW);
		}
		T->data=ch;
		CreateBiTree(T->lchild);
		CreateBitree(T->rchild);
	}
	return ok;
}
5、赫夫曼树
6、图的BFS算法
7、最小生成树的普里姆算法
8、归并算法

2019

1、删除单链表中最小值结点
typedef struct{
	int data;
	struct LNode *next;
}LNode;
void Del_minnode(LNode *L){
	LNode *pre=L,*p=pre->next,*minp=p,*minpre=pre;
	while(p!=null){
		if(p->data<minp->data){
			minp=p;minpre=pre;
		}
		pre=p;
		p=p->next;
	}
	minpre->next=minp->next;
	free(minp);
}
2、双序遍历
//改造二叉树的前序或中序即可
void Double_order(BTNode *t){
	if(t!=null){
		Visit(t);
		Double_order(t->lchild);
		Visit(t);
		Double_order(t->rchild);
	}
}
3、循环队列的入队和出队算法
int EnQueue(SqQueue &qu,int x){
	if((qu.rear+1)%maxsize==qu.front){//队满不能入队
		return 0;
	}
	qu.rear=(qu.rear+1)%maxSize;
	qu.data[qu.rear]=x;
	return 1;
}
int Dn_queue(SqQueue &qu,int &x){
	if(qu.front==qu.rear){//队空无法出队
		return 0;
	}
	qu.front=(qu.front+1)%maxSize;
	x=qu.data[qu.front];
	return 1;
}
4、图的算法,求编号为k的顶点的入度
5、重排数组
6、判断有向图是否有根
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值