数据结构算法题整理3

1.将两个顺序表合并,同时要求新的顺序表没有重复数据

void MList(List L1,List L2,List &L)
{
	InitList(L);
	i=j=1;k=0;
	L1.len=ListLength(L1);
	L2.len=ListLength(L2);
	while((i<=L1.len) && (j<=L2.len))
	{
		GetElem(L1,i,a[i]);
		GetElem(L2,j,b[j]);
		if(a[i]<b[j])
		{
			ListInsert(L,++k,a[i]);
			++i;
		}
		else if(a[i]==b[j])//插入L,并删除重复元素
		{
			ListInsert(L,++k,a[i]);
			++j;   
		}
		else
		{
			ListInsert(L,++k,b[j]);
			++j;
		}
	}

	while(i<=L1.len)
	{
		GetElem(L1,i++,a[i]);
		ListInsert(L,++k,a[i]);
	}
	while(j<=L2.len)
	{
		GetElem(L2,j++,b[j]);
		ListInsert(L,++k,b[j]);
	}
}

2.用非递归算法交换二叉树的左右子树
算法的基本思想为,先把树根入栈,判断左右子树,
如果不为空则交换;再将交换的左右孩子分别入栈,
重复整个过程,直到栈空位置
,再将交换的左右,算法的时间复杂度为O(n)

void Exchange(BiTree *tr)
{
	BiTree *tmp,*t;
	Stack s;
	InitStack(S);
	if(tr!=NULL)
		Push(s,tr);
	else
		return 1;
	do{
		Pop(s,t);
		if(t->lchild!=NULL)||(t->rchild!=NULL))
		{
			tmp=t->lchild;
			t->lchild=t->rchild;
			t->rchild=tmp;
		}
		if(t->lchild)
			Push(s,t->lchild);
		if(t->rchild)
			Push(s,t->rchild);
	}while(!StackEmpty(s));
}

3.逆序单链表中的数据

void reverse(node head)
{
	node *p,*q;
	p=head->next;
	head->next=NULL;
	while(p)
	{
		q=p->next;
		p->next=head->next;
		head->next=p;
		p=q;
	}
}

4.统计二叉树的叶子结点个数和树的高度

int countleaf(BiTree bt)
{
	if(bt==NULL)
		return 0;
	else if(bt->lchild==NULL && bt->rchild==NULL)
		return 1;
	else 
		return countleaf(bt->lchild)+countleaf(bt->rchild);

}
int countheight(BiTree bt)
{
	int height,heightl,heightr;
	if(bt==NULL)
		height=0;
	else
	{
		heightl=countheight(bt->lchild);
		heightr=countheight(bt->rhcild);
		height=(height>height?heightl:heightr)+1;
	}
	return height;
}
#define status int 
#define true 1
#define flase 0

Status SEI(ElemType a[],int *n,ElemType x)
{
	int l=0,h=*n-1,mid,i;
	Status s=false;
	if(l>h)return s;
	if(*n<=0)return s;
	while(l<=h)
	{
		mid=(l+h)/2;
		if(a[mid]==x)
			break;
		else if(a[mid]<x)
			l=mid+1;
		else h=mid-1;
	}
	if(l<=h)
	{
		s=true;
		if(mid==*n-1)return s;
		a[mid]=a[mid+1];
		a[mid+1]=x;
	}
	else
	{
		for(i=*n-1;i>=i;i++)
			a[i+1]=a[i];
		a[low]=x;
		(*n)++;
	}
	return s;
}

6.统计二叉树非叶子结点个数的层次遍历算法

typedef struct BiTreeNode
{
	DataType data;
	struct BiTreeNode *lchild,*rchild;
}BiTreeNode,*BiTree;

int lorder(BiTree bt)
{
	BiTreeNode Que[max];
	int front=-1,count=0;
	if(bt==NULL)return 0;
	Que[rear]=bt; //根结点入队列
	while(rear!=front)
	{
		front++; 
		if(Que[front]->lchild!=NULL || Que[front]->rchild!=NULL)
			count++;
		if(Que[front]->lchild!=NULL) //存在左孩子,入队
		{
			rear++;
			Que[rear]=Que[front]->lchild;
		}
		if(Que[front]->rchild!=NULL)
		{
			rear++;
			Que[rear]=Que[front]->rchild;
		}
	}
	return count;
}

7.将顺序表转存为带头结点的单循环链表

#define Max 100
typedef struct SList
{
	ElemType elem[Max];
	int length;
}SList;

typedef struct node
{
	ELemType data;
	struct node *next;
}ListNode,*LinkList;

void rever(SList L,LinkList *L2)
{
	*L2=(LinkList)malloc(sizeof(ListNode));
	L2->next=L2;
	for(i=L.length;i>0;i--)
	{
		p=(ListNode *)malloc(sizeof(ListNode));
		p->data=L.elem[i-1];
		p->next=L2->next;
		L2->next=p;
	}
}

8.删除不带头结点的双循环链表中p指向的某个结点

void Del(LinkList L)
{
	q=L;
	while(q->next->next!=p) //定位到p的前驱
		q=q->next;
	q=p->next;  //连接q和p的后驱
		p->next->prior=q; //连接p的后驱和q
		free(p);   //删除p
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值