考研数据结构

数据结构

一、线性表

顺序表

静态数组

#include<iostream>
using namespace std;
//线性表 顺序表实现
#define MaxSize 10
typedef struct{
	int data[MaxSize];//存放数据元素
	int lengh;//记录顺序表当前的长度
}SqList;
void InitList(SqList &L){//初始化操作
	for(int i=0;i<MaxSize;i++)
		L.data[i]=0;
	L.lengh=0;
}
bool ListInsert(SqList &L,int i,int e){//插入操作
	if(i<1||i>L.lengh+1)			   //1--length+1
	{
		return false;
	}	
	if(L.lengh>=MaxSize)
	{
		return false;
	}
	for(int j=L.lengh;j>=i;j--)
	{
		L.data[j]=L.data[j-1];
	}
	L.data[i-1]=e;

	L.lengh++;
	return true;
}
bool ListDelete(SqList &L,int i,int &e){
	//删除的小标1--L.Lengh
	if(i<1||i>L.lengh){
		return false;
	}
	e=L.data[i-1];
	for(int j=i;j<L.lengh;j++){
		L.data[j-1]=L.data[j];
	}
	L.lengh--;
	return true;
}
void PrintList(SqList &L){//打印操作
	for(int i=0;i<L.lengh;i++){
		cout<<L.data[i]<<"--";
	}
	cout<<endl;
}
//查找操作
//操作1按位查找
int GetElem(SqList L,int i){
	return L.data[i-1];
}
//查找操作2按值查找
int LocateElem(SqList L,int  e){
	for(int i=0;i<L.lengh;i++){
		if(L.data[i]==e){
			return i+1;
		}
	}
}
int main(){
	//1.初始化操作
	SqList L;
	InitList(L);//初始化链表顺序
	cout<<"--------------"<<endl;
	//插入操作
	ListInsert(L,1,1);
	ListInsert(L,1,2);
	ListInsert(L,1,3);
	ListInsert(L,1,4);
	PrintList(L);//打印
	int e=0;
	ListDelete(L,4,e);//删除
	PrintList(L);//打印
	cout<<"e="<<e<<endl;
	cout<<GetElem(L,3)<<endl;;//按位获取
	cout<<LocateElem(L,2)<<endl;//按值获取
	return 0;
}

动态数组

#include<iostream>
#include<cstdlib>
using namespace std;
#define InitSize 10
typedef struct{
	int *data;//指向动态分配数组的指针 
	int MaxSize;//顺序表的最大容量 
	int length;//顺序表的当前长度 
}SeqList;
//动态分配 
void InitList(SeqList &L){
	//申请一片连续存储空间 
	L.data=(int *)malloc(InitSize*sizeof(int));
	L.length=0;
	L.MaxSize=InitSize;
}
bool ListInsert(SeqList &L,int i,int e){
	if(i<1||i>L.length+1){
		return false;
	}
	if(i>L.MaxSize){
		return false;
	}
	for(int j=L.length;j>=i;j--){
		L.data[j]=L.data[j-1];
	}
	L.data[i-1]=e;
	L.length++;
} 
bool ListDelete(SeqList &L,int i,int &e){
	//删除的小标1--L.Lengh
	if(i<1||i>L.length){
		return false;
	}
	e=L.data[i-1];
	for(int j=i;j<L.length;j++){
		L.data[j-1]=L.data[j];
	}
	L.length--;
	return true;
}//查找操作
//操作1按位查找
int GetElem(SeqList L,int i){
	return L.data[i-1];
}
//查找操作2按值查找
int LocateElem(SeqList L,int  e){
	for(int i=0;i<L.length;i++){
		if(L.data[i]==e){
			return i+1;
		}
	}
}
void PrintList(SeqList L){
	for(int i=0;i<L.length;i++){
		cout<<L.data[i]<<"--";
	}
	cout<<endl;
}
int main(){
	SeqList L;
	InitList(L);//初始化操作
	ListInsert(L,1,1);	//插入操作
	ListInsert(L,1,2);	
	ListInsert(L,1,3);
	ListInsert(L,1,4);
	ListInsert(L,1,5);
	PrintList(L);
	int e=0;
	ListDelete(L,5,e);
	cout<<e<<endl;
	PrintList(L);
	cout<<GetElem(L,1)<<endl;
	//查找操作 
	cout<<LocateElem(L,2)<<endl;
} 

链表

单链表
#include<iostream>
#include<cstdlib>
using namespace std;
//单链表结构定义
typedef struct LNode{
	int data;//结点数据域 
	struct LNode *next;//结点指针域 
}LNode,*LinkList; 
//单链表有头指针 指向 单链表的第一个结点 
//初始化操作
bool InitList(LinkList &L){
	L = (LNode *)malloc(sizeof(LNode));
	if(L==NULL){
		return false;
	}
	L->next=NULL;
	return true;
} 
bool InitLis2t(LinkList &L){
	L= NULL;
	return true;
}
bool ListInsert(LinkList &L,int i,int e){
	if(i<1){
		return false;
	}
	LNode *p;
	int j=0;
	p=L;
	while(p!=NULL&&j<i-1){//找到第i-1位置的节点 
		p=p->next;
		j++; 
	}
	if(p==NULL) 
		return true;
	LNode *s=(LNode *)malloc(sizeof(LNode));
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
bool ListInsert2(LinkList &L,int i,int e){
	if(i<1){
		return false;
	}
	if(i==1){
		LNode *s=(LNode *)malloc(sizeof(LNode));
		s->data=e;
		s->next=L;
		L=s;
		return true;
	} 
	LNode *p;
	int j=0;
	p=L;
	while(p!=NULL&&j<i-1){//找到第i-1位置的节点 
		p=p->next;
		j++; 
	}
	if(p==NULL) 
		return true;
	LNode *s=(LNode *)malloc(sizeof(LNode));
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
//指定节点后插操作
bool InsertNextNode(LNode *p,int e){
	if(p==NULL){
		return false;
	}
	LNode *s= (LNode *)malloc(sizeof(LNode));
	if(s==NULL){
		return false;
	}
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true; 
} 
//指定节点前插操作
bool InsertPriorNode(LNode *p,int e){
	if(p==NULL){
		return false;
	}
	LNode *s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL){
		return false;
	}
	s->next=p->next;
	p->next=s;
	s->data=p->data;
	p->data=e;
	return true;
} 
//按位序删除(带头结点)
bool ListDelete(LinkList &L,int i,int &e){
	if(i<1){//i<1不合法 
		return false;
	}
	LNode *p;
	int j=0;
	p=L;//指向当前的节点 
	while(p!=NULL && j<i-1){
		p=p->next;
		j++;
	}
	if(p==NULL){
		return false;
	}
	if(p->next==NULL){
		return false;
	}
	LNode *q=p->next;
	e=q->data;
	p->next=q->next;
	free(q);
	return true;
} 
//指定节点删除
 bool ListDelete2(LinkList &L,int i,int &e); 
 //按位查找
 LNode *GetElem(LinkList L,int i) {
 	if(i<0){
 		return NULL;
	 }
	 LNode *p;
	 int j=0;
	 p=L;
	 while(p!=NULL&&j<i){
	 	p=p->next;
	 	j++;
	 } 
	 return p;
 }
 //按值查找
 LNode * LocateElem(LinkList L,int e){
 	LNode *p = L->next;
 	while(p!=NULL && p->data !=e){
 		p=p->next;
	 }
	 return p;
 }   
 //求表长度
 int Length(LinkList L){
 	int len=0;
 	LNode *p =L;
 	while(p->next !=NULL){
 		p=p->next;
 		len++;
	 }
	 return len;
 } 
//打印
PrintList(LinkList &L){
	LNode *p=L->next;
	while(p!=NULL){
		cout<<p->data<<endl;
		p=p->next;
	}
} 

int main(){
	LinkList L;
	InitList(L);//初始化 
	ListInsert(L,1,1);//插入操作 
	ListInsert(L,1,2);
	ListInsert(L,1,3);
	ListInsert(L,1,4);
	PrintList(L); //打印 
	int e=0;
	cout<<"删除的元素"<<ListDelete(L,4,e)<<endl;
	int index=1;
	cout<<"获取下标为"<<index<<"的元素"<<GetElem(L,index)->data<<endl;
	cout<<"按值查找"<<LocateElem(L,2)->data<<endl;
	cout<<"表长"<<Length(L)<<endl;
}
双链表
#include<iostream>
#include<cstdlib>
using namespace std;
//双链表
typedef struct DNode{
	int data;
	struct DNode *prior,*next;
}DNode,*DLinklist; 
bool InitDLinkList(DLinklist &L){
	L = (DNode *)malloc(sizeof(DNode));
	if(L==NULL){
		return false;//内存不足 
	}
	L->prior=NULL;
	L->next=NULL;
	return true;
}
//后插操作 
bool InsertNextDNode(DNode *p, DNode *s){
	if(p==NULL|| s==NULL){
		return false;
	}
	s->next=p->next;
	if(p->next!=NULL){
		p->next->prior=s;
	}
	s->prior=p;
	p->next=s;
} 
//删除操作
bool DeleteNextDNode(DNode *p){
	if(p==NULL)
		return false;
	DNode *q = p->next;
	if(q==NULL) 
		return false;
	p->next=q->next;
	if(q->next!=NULL){
		q->next->prior=p;
	}
	free(q);
	return true;
} 
//销毁操作 
void DestoryList(DLinklist &L){
	while(L->next !=NULL){
		DeleteNextDNode(L);
		free(L);
		L=NULL;
	}
} 
//遍历操作
void PrintList(DLinklist L){
	DNode *p=L->next;
	while(p!=NULL){	
		cout<<p->data<<"--";
		p=p->next;
	}
	cout<<endl;
}
int main(){
	DLinklist L;
	InitDLinkList(L);//初始化 
	for(int i=0;i<5;i++){
		DNode *p=(DNode *)malloc(sizeof(DNode));
		p->data=1+i;
		p->next=NULL;
		p->prior=NULL;
		InsertNextDNode(L,p);	
	}//插入节点 

	PrintList(L);	
	DNode *p=L->next->next;//2
	DeleteNextDNode(p);//删除节点 
	PrintList(L);
	return 0;
} 
循环链表
循环单链表
#include<iostream>
using namespace std;
/*循环单链表,表尾节点的next指针指向头结点
从一个节点出发可以找到其他人任何节点 
*/
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList; 
bool InitList(LinkList &L){
	L=(LNode *)malloc(sizeof(LNode));
	if(L==NULL){
		return false;
	}
	L->next=L;
	return true;
}
//判断循环单链表是否为空
bool Empty(LinkList L){
	if(L->next=L)
		return true;
	else
		false;	
}
//判断节点是否为循环单链表的表位节点 
bool isTail(LinkList L,LNode *p){
	if(p->next=L)
		return true;
	else
		false;
} 

int main(){
		
	return 0;
} 
循环双链表
#include<iostream>
using namespace std;
/*
	循环双链表:
	表头结点的prior指向表尾节点;
	表尾节点的next指向头结点 
*/
typedef struct DNode{
	int data;
	struct DNode *prior,*next;
}DNode,*DLinklist; 
//初始化操作
bool InitDLinkList(DLinklist &L){
	L = (DNode *)malloc(sizeof(DNode));
	if(L==NULL){
		return false;
	}
	L->prior = L;
	L->next = L;
	return true;
} 
//判断循环双链表是否为空
bool Empty(LinkList L){
	if(L->next=L)
		return true;
	else
		false;	
}
//判断节点是否为循环双链表的表尾节点 
bool isTail(LinkList L,LNode *p){
	if(p->next=L)
		return true;
	else
		return false;
} 
//插入操作 
bool InsertNextDNode(DNode *p, DNode *s){
	s->next=p->next;
	p->next->prior=s;
	s->prior=p;
	p->next=s;
} 
//删除操作
bool DeleteNextDNode(DNode *p){
	p->next=q->next;
	q->next->or=p;
	free(q);
	return true;
} 
int main(){
	InitDLinkList L;
	InitDDlinkList(L);
	return 0;
} 
静态链表
#include<iostream> 
using namespace std;
/*
	静态链表:分配一整片连续的内存空间,各个节点集中安置。
	 
*/
#define MaxSize 10//静态链表的最大长度 
struct Node{//静态链表结构类型的定义 
	int data;//存储数据元素 
	int next;//下一个元素的数组下标 
}SLinkList[MaxSize];
int main(){
	
	
	return 0;
}

二、栈与队列

顺序栈
#include<iostream>
using namespace std;
/*
	顺序栈 
*/
#define MaxSize 10//定义栈中元素最大个数 
typedef struct{
	int data[MaxSize];//静态数组存放栈中元素 
	int top;//栈顶指针 
}SqStack;
void InitStack(SqStack &S){
	S.top=-1;
} 
//判断是否为空 
bool StackEmpty(SqStack S){
	if(S.top==-1){
		return true;
	}
	else{
		return false;
	}
}
//进栈操作
bool Push(SqStack &S,int x){
	if(S.top==MaxSize-1){
		return false;
	}
	S.top =S.top+1;//指针先加1 
	S.data[S.top]=x;//新元素入栈 
	return true;

} 
//出栈操作
bool Pop(SqStack &S,int &x){
	if(S.top==-1){
		return false;
	}
	x=S.data[S.top];
	S.top = S.top - 1;
	return true; 
} 
//读取栈顶元素操作
bool GetTop(SqStack S,int &x){
	if(S.top==-1){
		return false;
	}
	x=S.data[S.top];
	return true;
} 
int main(){
	SqStack S;//定义顺序栈 
	InitStack(S);//初始化操作 
	Push(S,1); //进栈凑在哦 
	Push(S,2); 
	Push(S,3); 
	Push(S,4); 
	//查看栈顶元素 
	{
	int x=0;
	GetTop(S,x);
	cout<<"查看栈顶元素"<<x<<endl;
	}	
	//出栈操作
	int x=0;
	Pop(S,x);
	cout<<"出栈操作"<<x<<endl; 
	Pop(S,x);
	cout<<"出栈操作"<<x<<endl; 	
	Pop(S,x);
	cout<<"出栈操作"<<x<<endl; 	
	Pop(S,x);
	cout<<"出栈操作"<<x<<endl; 
	bool flag = StackEmpty(S);
	cout<<"判断是否为空:"<<flag; 
	return 0;
}
链式栈
#include<iostream>
using namespace std;
typedef struct Linknode{
	int data;
	struct Linknode *next;
}*LiStack;
//后插操作:在结点之后插入元素e 进栈 
bool InsertNextNode(LNode *p,int e){
	if(p=NULL){
		return false;
	}
	LNode *s=(LNode *)malloc(sizeof(LNode));
	if(s=NULL){
		return false
	}
	s->data =e;
	s->next=p->next;
	p->next=s;
	return true;
}
int main(){
	
	return 0;
}

队列

顺序存储
#include<iostream>
using namespace std;
//顺序存储 
#define MaxSize 10
typedef struct{
	int data[MaxSize];
	int front,rear;
}SqQueue; 
void InitQueue(SqQueue &Q){
	//初始时 队头、 队尾指针指向0
	Q.rear=Q.front=0; 
} 
//判空操作 
bool QueueEmpty(SqQueue Q){
	if(Q.rear==Q.front){
		return true;
	}
	else{
		return false;
	}
}
//入队
bool EnQueue(SqQueue &Q,int x){
	if((Q.rear+1%MaxSize)==Q.front){
		return false;//队满则报错 
	}
	Q.data[Q.rear]=x;//将x插入队尾 
	Q.rear=(Q.rear+1)%MaxSize;//队尾指针后移 
	return true;
} 
//出队
bool DeQueue(SqQueue &Q,int &x){
	if(Q.rear==Q.front){
		return false;
	}
	x=Q.data[Q.front];
	Q.front=(Q.front+1)%MaxSize;
	return true;
} 
//获队头元素的值,用x返回
bool GetHead(SqQueue Q,int &x){
	if(Q.rear==Q.front){
		return false;
	}
	x=Q.data[Q.front];
	return true;
} 
//判断队列是否为空
bool QueueEmpty2(SqQueue Q){
	if(Q.rear==Q.front)
		return true;
	else
		return false;
}
int main(){
	SqQueue Q;//声明队列 
	InitQueue(Q); 
	EnQueue(Q,1); 
	EnQueue(Q,2); 
	EnQueue(Q,3); 
	EnQueue(Q,4); 	
	int x=0;
	GetHead(Q,x);
	cout<<"查看队头元素"<<x<<endl;
	while(Q.rear!=Q.front){
		int x=0;
		DeQueue	(Q,x);
		cout<<x<<endl;
	}
	
	return 0; 
} 
链式存储
#include<iostream>
#include<cstdlib>
using namespace std;
//队列链式存储
typedef struct LinkNode{
	int data;
	struct LinkNode *next;
}LinkNode;
typedef struct{//链式队列 
	LinkNode *front,*rear;//队列的对头和队尾指针 
}LinkQueue;
//初始化操作 (带头结点) 
void InitQueue(LinkQueue &Q){
	Q.front=Q.rear=(LinkNode *)malloc(sizeof(LinkNode));
	Q.front->next=NULL;
}
//初始化 (不带头结点)
void InitQueue2(LinkQueue &Q){
	Q.front=NULL;
	Q.rear=NULL; 
} 
bool IsEmpty(LinkQueue Q){
	if(Q.front==NULL){
		return true;
	}
	else{
		return false;
	}
}
//入队(带头结点) 
void EnQueue(LinkQueue &Q,int x){
	LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
	s->data=x;
	s->next=NULL;
	Q.rear->next=s;
	Q.rear=s; 
} 
//入队(不带头结点) 
void EnQueue2(LinkQueue &Q,int x){
	LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
	s->data=x;
	s->next=NULL;
	if(Q.front == NULL){
		Q.front =s;
		Q.rear =s;
	}
	else{
		Q.rear->next=s;
		Q.rear=s;
	}
} 
//出队(带头结点)
bool DeQueue (LinkQueue &Q,int &x){
	if(Q.front==Q.rear)
		return false;
	LinkNode *p=Q.front->next;
	x=p->data;
	Q.front->next=p->next;
	if(Q.rear==p){
		Q.rear=Q.front;
	}
	free(p);
	return true;
} 
//出队(不带头结点)
bool DeQueue2 (LinkQueue &Q,int &x){
	if(Q.front==NULL)
		return false;
	LinkNode *p=Q.front;
	x=p->data;
	Q.front=p->next;
	if(Q.rear==p){
		Q.rear=NULL;
		Q.front=NULL;
	}
	free(p);
	return true;
}
//判断队列是否为空 
int main(){
	LinkQueue Q;//声明一个队列
	InitQueue(Q);//	初始化 
	EnQueue(Q,1);//入队 
	EnQueue(Q,2);
	EnQueue(Q,3);
	EnQueue(Q,4);
	EnQueue(Q,5);
	while(Q.front!=Q.rear){
		int x=0;
		DeQueue(Q,x);//出队 
		cout<<x<<endl;
	}
//	不带头结点
 LinkQueue Q2;
 	InitQueue(Q2);//	初始化 
	EnQueue2(Q2,1);//入队 
	EnQueue2(Q2,2);
	EnQueue2(Q2,3);
	EnQueue2(Q2,4);
	EnQueue2(Q2,5);
	while(Q2.front!=NULL){
		int x=0;
		DeQueue2(Q2,x);//出队 
		cout<<x<<endl;
	}
	return 0;
} 

三、树

 /*
 二叉树的设计
 1.节点元素
 2.左孩子节点
 3.右孩子节点 
 */
 //树的节点
 #include<stdio.h>
 #include<stdlib.h>
 #include<iostream>
 using namespace std;
 typedef struct node{
 	int data;
 	struct node* left;
 	struct node* right;
 } Node;
 //树根
 typedef struct{
 	Node* root;
 } Tree;
 //创建树 
 void insert(Tree* tree,int value){
 	Node* node = (Node*)malloc(sizeof(Node));
 	node->data = value;
 	node->left=NULL;
 	node->right=NULL;
 	
 	if(tree->root == NULL ){
 		tree->root=node;
	 }else{
	 	Node* temp = tree->root;
	 	while(temp != NULL){
	 		//进入左子树 
	 		if(value<temp->data){
	 			if(temp->left ==NULL){
	 				temp->left=node;
	 				return;
				 }else{
			 	temp=temp->left;
			 	}
			 }
		 else{
		 	//进入右子树
			 if(temp->right ==NULL){
			 	temp->right=node;
			 	return;
			 } else{
			 	temp=temp->right;
			 }
		 }
	}
	}
 }
 //树的后序遍历 Post-order traversal
void postorder(Node* node){
    if (node != NULL)
    {
        inorder(node->left);
        inorder(node->right);
        printf("%d ",node->data);
    }
}
 //树的先序遍历 
void preorder(Node* node){
    if (node != NULL)
    {
        printf("%d ",node->data);
        inorder(node->left);
        inorder(node->right);
    }
}
 //中序遍历
 void inorder(Node* node){
 	if(node !=NULL){
 		inorder(node->left);
 		printf("%d",node->data);
 		inorder(node->right); 
	 }
 } 
 int main(){
 	Tree tree;
 	tree.root=NULL;
 	int n;
 	scanf("%d",&n);
 	for(int i=0;i<n;i++){
 		int temp;
 		scanf("%d",&temp);
 		insert(&tree,temp);
	 }
	 inorder(tree.root);//中序遍历 
 	return 0;
 }
 
#include <iostream>

using namespace std;

struct Tree
{
    int data;//数值域
    struct Tree *lchild,*rchild;//左孩子和右孩子
};

Tree* creatTree(int a[],int i)
{
    //创建树
    if(a[i])
    {
        Tree *p=new Tree;
        p->data=a[i];
        p->lchild=creatTree(a,2*i);
        p->rchild=creatTree(a,2*i+1);
        return p;
    }
    else
    {
        return NULL;
    }

}

void Pro(Tree *t)
{
    //先序遍历-递归
    if(t)
    {
        cout<<t->data<<" ";
        Pro(t->lchild);
        Pro(t->rchild);
    }
}

void Mid(Tree *t)
{
    //中序遍历-递归
    if(t)
    {
        Mid(t->lchild);
        cout<<t->data<<" ";
        Mid(t->rchild);
    }
}

void Post(Tree *t)
{
    //后序遍历-递归
    if(t)
    {
        Post(t->lchild);
        Post(t->rchild);
        cout<<t->data<<" ";
    }
}


//栈函数的开始-----------------------
struct LqStake
{
    //链栈的结构体
    Tree *data;//存放树指针
    struct LqStake *next;//栈顶所在结点
};

void push(LqStake *L,Tree *e)
{
    //把元素e放入链栈L,使用头插法
    LqStake *q,*p=L;
    q=new LqStake;//需要开辟空间

    q->data=e;

    q->next=p->next;
    p->next=q;

}

bool pop(LqStake *L,Tree* &e)
{
    //把栈顶元素出链栈L
    LqStake *p=L,*q=L->next;
    if(q)
    {
        p->next=q->next;
        e=q->data;
        return 1;
    }
    else
    {
        return 0;
    }

}

void initStack(LqStake *s)
{
    //初始化栈
    s->data=NULL;
    s->next=NULL;
}
//栈函数的结束-------------------

void pro_(Tree *t)
{
    //树的先序遍历非递归
    LqStake *s=new LqStake;
    initStack(s);

    Tree *p=t;
    Tree *q=new Tree;

    while( p || s->next!=NULL )
    {
        if(p)
        {
            cout<<p->data<<" ";
            push(s,p);
            p=p->lchild;
        }
        else
        {
            pop(s,q);
            p=q->rchild;
        }
    }
}

void mid_(Tree *t)
{
    //树的中序遍历非递归
    LqStake *s=new LqStake;
    initStack(s);

    Tree *p=t;
    Tree *q=new Tree;

    while( p || s->next!=NULL )
    {
        if(p)
        {
            push(s,p);
            p=p->lchild;
        }
        else
        {
            pop(s,q);
            cout<<q->data<<" ";
            p=q->rchild;
        }
    }
}

void post_(Tree *t)
{
    //树的后序遍历非递归
    LqStake *s=new LqStake;
    initStack(s);

    Tree *p=t;
    Tree *q=new Tree;

    Tree *r=NULL;//检查是否被访问的

    while( p || s->next!=NULL )
    {
        if(p)
        {
            push(s,p);
            p=p->lchild;
        }
        else
        {
            pop(s,q);
            if(q->rchild  && q->rchild!=r )
            {
                push(s,q);
                push(s,q->rchild);

                p=q->rchild->lchild;
            }
            else
            {
                cout<<q->data<<" ";
                r = q;
            }
        }
    }
}

//链队的开始--------------------------------------
struct LQueue
{
    //定义链队结构体(队头为首元结点,队尾只用指针)
    Tree *data;//存放树指针
    struct LQueue *next;//下一指针
};

void enQueue(LQueue *Q,Tree *e)
{

    //在队尾指针r后面入队元素e
    while(Q->next)
    {
        Q=Q->next;
    }

    LQueue *q=new LQueue;

    q->data=e;

    q->next=NULL;

    Q->next=q;

}

bool deQueue(LQueue *Q,Tree* &e)
{
    //将队列首元结点出队

    LQueue *p=Q,*q=Q->next;
    //①判断
    if(q==NULL)
    {
        cout<<"队空"<<endl;
        return 0;//队空
    }

    //②执行
    // cout<<"出队的元素:"<<q->data->data<<endl;
    e=q->data;
    p->next=q->next;

    //③后处理
    return 1;

}

void InitQueue(LQueue *Q)
{
    //初始化队列
    Q->data=NULL;
    Q->next=NULL;
}
//链队的结束-------------------------------

void level(Tree *t)
{
    LQueue *Q=new LQueue;//新建链队
    InitQueue(Q);

    Tree *p;

    enQueue(Q,t);

    while(Q->next)
    {
        deQueue(Q,p);
        cout<<p->data<<" ";
        if(p->lchild) enQueue(Q,p->lchild);
        if(p->rchild) enQueue(Q,p->rchild);
    }
}

int main()
{
    Tree *root;//根结点
    int a[20]= {0,100,97,102,88,99,101,103,80,90};

    //建树
    root=creatTree(a,1);

    //先序遍历-递归
    cout<<"先序遍历序号:";
    Pro(root);
    cout<<endl;

    //中序遍历-递归
    cout<<"中序遍历序号:";
    Mid(root);
    cout<<endl;

    //后序遍历-递归
    cout<<"后序遍历序号:";
    Post(root);
    cout<<endl;


    //先序遍历-非递归
    cout<<"先序遍历序号-非递归:";
    pro_(root);
    cout<<endl;

    //中序遍历-非递归
    cout<<"中序遍历序号-非递归:";
    mid_(root);
    cout<<endl;

    //后序遍历-非递归
    cout<<"后序遍历序号-非递归:";
    post_(root);
    cout<<endl;

    //层次遍历
    cout<<"层次遍历:";
    level(root);
    cout<<endl;

    return 0;
}

//练习的代码(无法运行的)
/*---1------
void Copy ( BiTree T, BiTree &NewT )
{
    //复制一棵和T完全相同的二叉树

    if(T==NULL)//如果是空树,递归结束
    {
        NewT=NULL;
        return ;
    }
    else
    {
        NewT=new BiTNode;
        NewT->data = T->data;
        Copy ( T->lchild, BNewT->lchild );
        Copy ( T->rchild, BNewT->rchild );
    }
}

int Depth(BiTree T)
{//计算二叉树T的深度

    if(T==NULL)
    {
        return 0;
    }
    else
    {
        m=Depth(T->lchild);
        n=Depth(T->rchild);

        if(m>n)
        {
            return m+1;
        }
        else
        {
            return n+1;
        }
    }
}

int NodeCount(BiTree T)
{
    //统计二叉树中结点的个数
    
    if(T==NULL)
    {
        return 0;
    }
    else
    {
        return NodeCount(T->lchild)+NodeCount(T->rchild);
    }
}
*/

四、图

#include <iostream>

using namespace std;

//----------图的邻接矩阵存储表示----------
struct AMGraph
{
    //邻接矩阵
    int vexs[4];     //顶点信息
    int arcs[4][4]; //二维数组存储边信息
    int vexmun,arcnum;  //图的当前顶点数和边数
};


void CreateAMG(AMGraph &p)
{
    //创建邻接矩阵
    int arcs[4][4]= {{0,1,0,1},{0,0,0,1},{0,1,0,0},{0,0,1,0}}; //邻接矩阵中的二维数组

    p.vexmun=4;//顶点数
    p.arcnum=5;//边数

    p.vexs[0]=1;//顶点信息
    p.vexs[1]=2;
    p.vexs[2]=3;
    p.vexs[3]=4;

    int i,j;
    for(i=0; i<4; i++)//边信息处理
    {
        for(j=0; j<4; j++)
        {
            p.arcs[i][j]=arcs[i][j];
        }
    }
}

//----------图的邻接表存储表示----------
struct ArcNode
{
    //边表结点
    int data; //结点的信息
    ArcNode *next;    //指向下一个结点
};
struct VNode
{
    //邻接表的顶点表的结点
    int data;          //顶点信息
    ArcNode *firstarc;  //指向第一个出度的结点
};
struct ALGraph
{
    VNode vertices[4];
    int vexmun,arcnum;  //图的当前顶点数和边数
};

//邻接矩阵转邻接表
void turn(AMGraph &g,ALGraph &t)
{
    t.arcnum=g.arcnum;//边数赋值
    t.vexmun=g.vexmun;//顶点数赋值

    int i,j;

    for(i=0; i<g.vexmun; i++)//顶点信息赋值
    {
        t.vertices[i].data=g.vexs[i];
        t.vertices[i].firstarc=NULL;
    }

    for(i=0; i<g.vexmun; i++)//边信息处理
    {
        for(j=0; j<g.vexmun; j++)
        {

            if(g.arcs[i][j]==1)
            {
                //头插法
                ArcNode *q=new ArcNode; //新建-边结点
                q->data=t.vertices[j].data;
                q->next=t.vertices[i].firstarc;
                t.vertices[i].firstarc=q;
            }
        }
    }
}

//----------有向图的十字链表存储表示----------
struct ArcBox
{
    //边表结点
    int tailvex,headvex;//弧尾和弧头结点
    ArcBox *hlink, *tlink;//分别为弧头相同和弧尾相同的弧的链域
    int info;//该弧点信息
};
struct VexNode
{
    //十字链表表的顶点表的结点
    int data;
    ArcBox *firstin,*firstout;
};

struct OLGraph
{
    VexNode xlist[10];
    int vexnum,arcnum; //有向图的当前点数和边数
};


//----------无向图的邻接多重表存储表示----------
struct EBox
{
    int ivex,jvex;//该边依附的两个顶点的位置
    EBox *ilink,*jlink;//分别指向依附这两个结点的下一条边
    int info;//该边信息
};

struct VexBox
{
    int data;
    EBox *firstedge;
};

struct AMLGraph
{
    VexBox adjmulist[10];
    int vexnum,arcnum; //无向图的当前点数和边数
};


//----------深度优先搜索遍历----------
int visit[4]={0};  //标记
void DFS(ALGraph t,int v)
{
    //从顶点v开始深度遍历邻接表t
    cout<<t.vertices[v-1].data<<" ";
    visit[v-1]=1;//标记已访问

    ArcNode *p=t.vertices[v-1].firstarc;//第一个结点
    while(p)
    {
        if(visit[p->data-1]==0)//是否被访问过
        {
            DFS(t,p->data);
        }
        else
        {
            p=p->next;
        }
    }
}

//链队的开始--------------------------------------
struct LQueue
{
    //定义链队结构体(队头为首元结点,队尾只用指针)
    int data;//存放树指针
    struct LQueue *next;//下一指针
};

void enQueue(LQueue *Q,int e)
{
    //在队尾指针r后面入队元素e
    while(Q->next)
    {
        Q=Q->next;
    }

    LQueue *q=new LQueue;
    q->data=e;
    q->next=NULL;
    Q->next=q;
}

bool deQueue(LQueue *Q,int &e)
{
    //将队列首元结点出队

    LQueue *p=Q,*q=Q->next;
    //①判断
    if(q==NULL)
    {
        cout<<"队空"<<endl;
        return 0;//队空
    }

    //②执行
    // cout<<"出队的元素:"<<q->data->data<<endl;
    e=q->data;
    p->next=q->next;

    //③后处理
    return 1;

}

void InitQueue(LQueue *Q)
{
    //初始化队列
    Q->data=-1;
    Q->next=NULL;
}
//链队的结束-------------------------------


//---------广度优先搜索遍历----------
int visited[4]={0};//标记访问
void BFS(ALGraph t,int v)
{
    cout<<t.vertices[v-1].data<<" ";
    visited[v-1]=1;//标记已访问

    int u;//中间变量

    LQueue *Q=new LQueue;//新建链队
    InitQueue(Q);
    enQueue(Q,v);//把v入队

    ArcNode *p;

    while(Q->next)//队列不为空
    {
        deQueue(Q,u);//出队放到u

        p=t.vertices[u-1].firstarc;//第一个结点

        while(p)
        {
            if(visited[p->data-1]==0)//若未被访问
            {
                visited[p->data-1]=1;//标记
                cout<<p->data<<" ";
                enQueue(Q,p->data);//入队
            }
            p=p->next;
        }

    }
}

int main()
{
    AMGraph g;//邻接矩阵
    CreateAMG(g);//创建邻接矩阵

    ALGraph t;//邻接表
    turn(g,t);//把邻接矩阵转化成邻接表

    //打印
    ArcNode *p;
    int i;
    for(i=0; i<4; i++)
    {
        p=t.vertices[i].firstarc;
        cout<<t.vertices[i].data<<":";
        while(p->next)
        {
            cout<<p->data<<"->";
            p=p->next;
        }
        cout<<p->data<<endl;
    }

    DFS(t,1);//深度优先遍历
    cout<<endl;
    BFS(t,1);//广度优先遍历
    return 0;
}

五、排序

插入排序

1.直接插入
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[n];

    int i,j;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }

    for(i=0;i<n;i++)
    {
        for(j=i;j>0;j--)
        {
            if(a[j]>a[j-1])
            {
                swap(a[j],a[j-1]);
            }
        }
    }

    for(i=0; i<n; i++)
    {
        cout<<a[i];
    }

    return 0;
}

2.折半插入
//折半插入排序
#include<iostream>
using namespace std;
void InsertSort(int arr[],int n){
	int i ,j,low,high,mid;
	for(i=1;i<n;i++){
		arr[0]= arr[i];
		low = 1;high=i-1;
		while(low<=high){
			mid = (low+high)/2;
			if(arr[mid]>arr[0]){
				high = mid -1;
			}else{
				low = mid + 1 ;
			}
		}
		for(j = i-1;j>=high+1;--j){
			arr[j+1] = arr[j]; 
		}
		arr[high+1] = arr[0];
	}
}
int main(){
	
	int arr[]={0,2,3,4,5,11,10,8,9};//存储元素从下标1开始 
	InsertSort(arr,9);
	for(int i =1;i<9;i++){
		cout<<arr[i]<<endl;
	}
	return 0;
} 
3.希尔排序
#include <iostream>

using namespace std;

void Shell_Insert(int L[],int n,int dt)
{
    //按照增量dt对长度为n的顺序表L作希尔排序
    int i,j,t;//t为中间量
    for(i=dt; i<n; i++)
    {
        if(L[i]<L[i-dt])
        {
            t=L[i];//要前移的值
            for(j=i-dt; j>=0&&t<L[j] ; j=j-dt)//后移
            {
                L[j+dt]=L[j];
            }
             L[j+dt]=t;//放入正确位置
        }
    }
}

int main()
{
    int L[10]= {49,38,65,97,76,13,27,49,55,04};
    int dt[3]= {5,3,1};

    //按照增量dt[0,1,2]对顺序表L作希尔排序
    int i,j;
    for(i=0; i<3; i++)
    {
        Shell_Insert(L,10,dt[i]);//希尔排序


        //下面是打印每一趟结果
        cout<<"第"<<i<<"趟结果:";
        for(j=0;j<10;j++)
        {
            cout<<L[j]<<" ";
        }
        cout<<endl;
        
    }

    return 0;
}

交换排序

1.冒泡排序
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[n];

    int i,j;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }

    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]<a[j+1])
            {
                swap(a[j],a[j+1]);
            }
        }
    }

     for(i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}


2.快速排序

选择排序

1.简单选择排序
2.堆排序
#include <iostream>

using namespace std;

void adjust(int l[],int i,int m)
{
    //把l[i...m]调整为大根堆
    int left=2*i;//左孩子
    int right=2*i+1;//右孩子

    if(l[left]>l[right])//左孩子大
    {
        if(l[left]>l[i] && left<=m)
        {
            swap(l[left],l[i]);//交换
            adjust(l,left,m);//向下调整
        }
    }
    else//右孩子大
    {
        if(l[right]>l[i] && right<=m)
        {
            swap(l[right],l[i]);//交换
            adjust(l,right,m);//向下调整
        }
    }
}

int main()
{
    int L[9]= {0,49,38,65,97,76,13,27,49};

    int i,m;

    for(m=9; m>1; m--)
    {
        for(i=m/2; i>0; i--)//调整为大根堆
        {
            adjust(L,i,m-1);
        }
        swap(L[1],L[m-1]);


        //每趟调整后输出
        for(i=1; i<9; i++)
        {
            cout<<L[i]<<" ";
        }
        cout<<endl;
    }

    return 0;
}

3.归并排序
#include <iostream>

using namespace std;

void Merge(int r[],int low,int mid,int high)
{
    //将r[low...mid]和r[mid+1...high]归并到t[low...high]
    int t[9];//临时存放的数组
    int i=low,j=mid+1;
    int k=low;

    while(i<=mid && j<=high)//归并时比较过程
    {
        if(r[i]<=r[j])
        {
            t[k++]=r[i++];
        }
        else
        {
            t[k++]=r[j++];
        }
    }

    while(i<=mid)//若r[low...mid]有剩余
    {
        t[k++]=r[i++];
    }
    while(j<=high)//若r[mid+1...high]有剩余
    {
        t[k++]=r[j++];
    }

    for(i=low;i<=high;i++)//把临时数组赋值回到r数组
    {
        r[i]=t[i];
    }
}

void Msort(int r[],int low,int high)
{

    if(low!=high)
    {
        int mid=(low+high)/2;
        Msort(r,low,mid);
        Msort(r,mid+1,high);

        Merge(r,low,mid,high);
    }
}

int main()
{
    int r[9]= {49,38,65,97,76,13,27,49,66};

    Msort(r,0,8);//归并排序

    //打印排序后的数组
    int i;
    for(i=0;i<9;i++)
    {
        cout<<r[i]<<" ";
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值