山东大学软件工程849数据结构 重点代码

一、基础排序算法

1.折半搜索

template <class T>
int BinarySearch(T a[],int n,const T &x)
{
	int left=0,right=n-1,mid;
	
	while(left<=right)
	{
		mid=(left+right)/2;
		if(a[mid]==x)
			return mid;
		if(a[mid]>x)
			right=mid-1;
		else
			left=mid+1;
	}
	
	return -1;
 } 

O(logn)

2.名次排序

template <class T>
void Rank(T a[],int n,int r[])
{
	for(int i=0;i<n;i++)
		r[i]=0;
	for(int i=1;i<n;i++)
		for(int j=0;j<i;j++)
			if(a[j]>a[i])
				r[j]++;
			else
				r[i]++;
}
template <class T>
void Rearrange(T a[],int n,int r[])
{
	T *u=new T[n];
	for(int i=0;i<n;i++)
		u[r[i]]=a[i];
	//delete []a;
	//a=u;
	for(int i=0;i<n;i++)
		a[i]=u[i];
	delete []u;
}

template <class T>
void Swap(T &a,T &b)
{
	T temp=a;
	a=b;
	b=temp;
}
//原地重排
template <class T>
void RankSort(T a[],int n,int r[])
{
	for(int i=0;i<n;i++)
		while(r[i]!=i)
		{
			int t=r[i];
			Swap(a[i],a[t]);
			Swap(r[i],r[t]);
		}
}

O(n²)

3.选择排序

//拆开
template <class T>
int Max(T a[],int n)
{
	int pos=0;
	for(int i=1;i<n;i++)
		if(a[pos]<=a[i])
			pos=i;
	return pos;
}
template <class T>
void SelectionSort1(T a[],int n)
{
	for(int size=n;size>1;size--)
	{
		int t=Max(a,size);
		Swap(a[t],a[size-1]);
	}
}

//合并
template <class T>
void SelectionSort2(T a[],int n)
{
	for(int size=n;size>1;size--)
	{
		int pos=0;
		for(int i=1;i<size;i++)
			if(a[pos]<=a[i])
				pos=i;
		Swap(a[pos],a[size-1]);
	}
}

//及时刹车
template <class T>
void SelectionSort3(T a[],int n)
{
	bool sorted=false;
	for(int size=n;size>1;size--)
	{
		sorted=true;
		int pos=0;
		for(int i=1;i<size;i++)
			if(a[pos]<=a[i])
				pos=i;
			else
				sorted=false;
		Swap(a[pos],a[size-1]);
	}
}

O(n²)

4.冒泡排序

//拆开
template <class T>
void Bubble1(T a[],int n)
{
	for(int i=0;i<n-1;i++)
		if(a[i]>a[i+1])
			Swap(a[i],a[i+1]);
} 
template <class T>
void BubbleSort1(T a[],int n)
{
	for(int size=n;size>1;size--)
		Bubble1(a,size);
}

//合并
template <class T>
void BubbleSort2(T a[],int n)
{
	for(int size=n;size>1;size--)
		for(int i=0;i<size-1;i++)
			if(a[i]>a[i+1])
				Swap(a[i],a[i+1]);
}

//及时刹车
template <class T>
bool Bubble2(T a[],int n)
{
	bool swapped=false;
	for(int i=0;i<n-1;i++)
		if(a[i]>a[i+1])
		{
			Swap(a[i],a[i+1]);
			swapped=true;
		}
	return swapped;
}
template <class T>
void BubbleSort3(T a[],int n)
{
	for(int size=n;size>1&&Bubble2(a,size)==true;size--);
}

O(n²)

5.插入排序

//拆开
template <class T>
void Insert(T a[],int n,const T &x)
{
	int i;
	for(i=n-1;i>=0&&a[i]>x;i--)
		a[i+1]=a[i];
	a[i+1]=x;
}
template <class T>
void InsertSort1(T a[],int n)
{
	for(int i=1;i<n;i++)
	{
		T t=a[i];
		Insert(a,i,t);
	}
}

//合并
template <class T>
void InsertSort2(T a[],int n)
{
	for(int i=1;i<n;i++)
	{
		T t=a[i];
		int j;
		for(j=i-1;j>=0&&a[j]>t;j--)
			a[j+1]=a[j];
		a[j+1]=t;
	}
}

O(n²)

二、链表操作

1.链表常用操作

//单链表插入
r->next=p->next;
p->next=r;

//单链表删除
q=p->next;
p->next=q->next;
delete q;

//双链表插入
r->next=p->next;
r->next->prior=r;
r->prior=q;
q->next=r;

//双链表删除
q=p->next;
p->next=q->next;
p->next->prior=p;
delete q; 

2.建立单链表

//头插法建立单链表(带头结点)
template <class T>
void CreateListF(LNode<T> * &C,T a[],int n)
{
	LNode<T> *s;
	C=new LNode<T>;
	C->next=NULL;
	
	for(int i=0;i<n;i++)
	{
		s=new LNode<T>;
		s->data=a[i];
		s->next=C->next;
		C->next=s;
	}
}

//尾插法建立单链表(带头结点)
template <class T>
void CreateListL(LNode<T> * &C,T a[],int n)
{
	LNode<T> *s,*r;
	C=new LNode<T>;
	C->next=NULL;
	r=C;
	
	for(int i=0;i<n;i++)
	{
		s=new LNode<T>;
		s->data=a[i];
		r->next=s;
		r=r->next;
	}
	r->next=NULL;
}

3.例题:链表合并

//例题:链表合并(A、B为头指针)
template <class T>
void Merge(LNode<T> * &A,LNode<T> * &B,LNode<T> * &C)
{
	LNode<T> *p=A->next;
	LNode<T> *q=B->next;
	delete B;
	C=A;
	LNode<T> *s=C;
	s->next=NULL;
	
	while(p!=NULL&&q!=NULL)
	{
		if(p->data<=q->data)
		{
			s->next=p;
			p=p->next;
		}
		else
		{
			s->next=q;
			q=q->next;
		}
		s=s->next;
	}
	
	s->next=NULL;
	if(p!=NULL)
		s->next=p;
	if(q!=NULL)
		s->next=q;
		
}

三、堆栈

1.顺序栈

//顺序栈定义
typedef struct
{
	int top;
	int MaxTop;
	int *stack;
}SqStack;

//顺序栈进栈
template <class T>
bool Push(SqStack<T> &sq,const T &x)
{
	if(sq.top==sq.MaxTop)
		return false;
	sq.stack[++sq.top]=x;
	return true;
}
//顺序栈出栈
template <class T>
bool Pop(SqStack<T> &sq,T &x)
{
	if(sq.top==-1)
		return false;
	x=sq.stack[sq.top--];
	return true;
} 

2.链栈

//链栈结点定义
typedef struct LNode
{
	int data;
	struct LNode *next;
};
//链栈定义
typedef struct
{
	LNode *top;
}LinkedStack;

//链栈进栈
template <class T>
void Push(LinkedStack<T> &ls,const T &x)
{
	LNode<T> *p=new LNode<T>;
	p->data=x;
	p->next=ls.top;
	ls.top=p;
}
//链栈出栈
bool Pop(LinkedStack<T> &ls,T &x)
{
	if(ls.top==NULL)
		return false;
	x=ls.top->data;
	LNode<T> *p=ls.top;
	ls.top=ls.top->next;
	delete p;
	return true;
}

3.例题

//十进制数n转d进制数 
void conversion(int n,int d)
{
	int k;//保存余数
	int stack[100];
	int top=-1;
	
	while(n>0)
	{
		k=n%d;
		stack[++top]=k;
		n=n/d;
	}
	
	while(top!=-1)
		cout<<stack[top--];
}

//判断链表前n个字符是否中心对称
template <class T>
bool dc(LNode<T> *L,int n)
{
	int k=n/2;
	T stack[k];
	LNode<T> *p=L->next;
	int top=-1;
	for(int i=0;i<k;i++)
	{
		stack[++top]=p->data;
		p=p->next;
	}
	if(n%2==1)
		p=p->next;
	while(p!=NULL&&stack[top]==p->data)
	{
		top--;
		p=p->next;
	}
	if(top==-1)
		return true;
	else
		return false;
}

//括号匹配
void PrintMatchedPairs(char *expr)
{
	Stack<int> s(100);
	int length=strlen(expr);
	int j;
	
	for(int i=1;i<=length;i++)
	{
		if(expr[i-1]=='(')
			s.push(i);
		else if(expr[i-1]==')')
		{
			if(s.IsEmpty())
				cout<<"No Match For "<<i<<endl;
			else
			{
				s.pop(j);
				cout<<j<<" Matches "<<i<<endl;
			}
		}
		while(!s.IsEmpty())
		{
			s.pop(j);
			cout<<"No Match For "<<j<<endl;
		}
	}
}

四、队列

1.顺序队列

//顺序队列定义
typedef struct
{
	int front;
	int rear;
	int MaxSize;
	int *queue;
}SqQueue;

//循环队列入队
template <class T>
bool EnQueue(SqQueue<T> &sq,const T &x)
{
	if((sq.rear+1)%sq.MaxSize==sq.front)//队满 
		return false;
	sq.rear=(sq.rear+1)%sq.MaxSize;
	sq.queue[sq.rear]=x;
	return true; 
}
//循环队列出队
template <class T>
bool DeQueue(SqQueue<T> &sq,T &x)
{
	if(sq.front==sq.rear)
		return false;
	sq.front=(sq.front+1)%sq.MaxSize;
	x=sq.queue[sq.front];
	return true;
}

2.链队列

//链队列结点定义
typedef struct QNode
{
	int data;
	QNode *next;
}QNode;
//链队列定义
typedef struct
{
	QNode *front;
	QNode *rear;
}LiQueue;

//链队列入队
template <class T>
void EnQueue(LiQueue<T> &lq,const T &x)
{
	QNode<T> *q=new QNode<T>;
	q->data=x;
	q->next=NULL;
	
	if(lq.front!=NULL)
		lq.rear->next=q;
	else
		lq.front=q;
	lq.rear=q;
}
//链队列出队
template <class T>
bool DeQueue(LiQueue<T> &lq,T &x)
{
	if(lq.front==NULL)
		return false;
	x=lq.front->data;
	QNode<T> *q=lq.front;
	lq.front=lq.front->next;
	delete q;
	return true;
}

3.例题

//将队列Q中的元素逆置 
template <class T>
void Inverser(Stack<T> S,Queue<T> Q)
{
	T x;
	while(!Q.IsEmpty())
	{
		DeQueue(Q,x);
		Push(S,x);
	}
	
	while(!S.IsEmpty())
	{
		Pop(S,x);
		EnQueue(Q,x);
	}
}

五、二叉树

1.二叉树定义及常用操作

//二叉树链式存储结构
template <class T>
typedef struct BTNode
{
	int data;
	BTNode *lchild;
	BTNode *rchild;
}BTNode;

//求二叉树高度
template <class T>
int Depth(BTNode<T> *t)
{
	if(!t)
		return 0;
	else
	{
		int m=Depth(t->lchild);
		int n=Depth(t->rchild);
		return 1+(m>n?m:n);
	}
}

//统计叶子结点个数
template <class T>
int CountLeaf(BTNode<T> *t)
{
	if(!t)
		return 0;
	else if(!t->lchild&&!t->rchild)
		return 1;
	else
	{
		int m=CountLeaf(t->lchild);
		int n=CountLeaf(t->rchild);
		return m+n;
	}
}

//统计所有结点
template <class T>
int Count(BTNode<T> *t)
{
	if(!t)
		return 0;
	else
	{
		int m=Count(t->lchild);
		int n=Count(t->rchild);
		return m+n+1;
	}
}

//前序复制二叉树
template <class T>
BTNode<T> * PreCopy(BTNode<T> *t)
{
	if(!t)
		return NULL;
	BTNode<T> *root=new BTNode<T>;
	root->data=t->data;
	root->lchild=PreCopy(t->lchild);
	root->rchild=PreCopy(t->rchild);
	return root;
}

//删除二叉树
template <class T>
void Erase(BTNode<T> * &t)
{
	if(t)
	{
		Erase(t->lchild);
		Erase(t->rchild);
		delete t;
	}
}

2.二叉树遍历

//二叉树前序遍历
//递归: 
template <class T>
void PreOrder(BTNode<T> *t)
{
	if(t)
	{
		Visit(t);
		PreOrder(t->lchild);
		PreOrder(t->rchild);
	}
}
//非递归:
template <class T>
void PreOrder(BTNode<T> *t)
{
	Stack<BTNode<T> *> S;
	BTNode<T> *p=t;
	
	do
	{
		while(p)
		{
			Visit(p);
			Push(S,p);
			p=p->lchild;
		}
		
		if(!S.IsEmpty())
		{
			Pop(S,p);
			p=p->rchild
		}
	}while(p||!S.IsEmpty())
}

//二叉树中序遍历
//递归: 
template <class T>
void InOrder(BTNode<T> *t)
{
	if(t)
	{
		InOrder(t->lchild);
		Visit(t);
		InOrder(t->rchild);
	}
}
//非递归:
template <class T>
void InOrder(BTNode<T> *t)
{
	Stack<BTNode<T> *> S;
	BTNode<T> *p=t;
	
	do
	{
		while(p)
		{
			Push(S,p);
			p=p->lchild;
		}
		if(!S.IsEmpty())
		{
			Pop(S,p);
			Visit(p);
			p=p->rchild;
		}
	}while(p||!S.IsEmpty())
}

//二叉树后序遍历
//递归:
template <class T>
void PostOrder(BTNode<T> *t)
{
	if(t)
	{
		PostOrder(t->lchild);
		PostOrder(t->rchild);
		Visit(t);
	}
}
//非递归:
template <class T>
void PostOrder(BTNode<T> *t)
{
	Stack<BTNode<T> *> S;
	int flag;
	Stack<int> flagStack;
	BTNode<T> *p=t;
	
	do
	{
		
		while(p)
		{
			Push(S,p);
			Push(flagStack,1);
			p=p->lchild;
		}
		
		if(!S.IsEmpty())
		{
			p=Top(S);
			flag=Top(flagStack);
			if(p->rchild&&flag==1)
			{
				Pop(flagStack,flag);
				Push(flag,2);
				p=p->rchild;
			}
			else
			{
				Pop(S,p);
				Pop(flagStack,flag);
				Visit(p);
				p=NULL;
			}
		}
		
	}while(p||!S.IsEmpty())
}

//层次遍历
template <class T>
void LevelOrder(BTNode<T> *t)
{
	LinkedQueue<BTNode<T> *> Q;
	while(t)
	{
		Visit(t);
		if(t->lchild)
			EnQueue(Q,t->lchild);
		if(t->rchild)
			EnQueue(Q,t->rchild);
		try{
			DeQueue(Q,t);
		}catch(OutofBounds){
			return;
		}
	}
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值