数据结构期末总复习-线性表。单链表。树及其相应的函数

 类型定义

#define INIT_LIST 100 
#define LIST_INCREASE 10
#define INIT_LIST 100
#define OK 0
#define ERROR -1
#define OVERFLOW 1


//线性表
struct Sqlist
{
	char* elem;
	int length;
	int listsize;
};

//单链表
typedef struct LNode
{
	LNode* next;
	char data;
} LNode,*Linklist;

//树
typedef struct TreeNode
{
	char data;
	TreeNode* lchild;
	TreeNode* rchild;
}*BiTree;

 函数实现

// stdafx.cpp : 只包括标准包含文件的源文件
// 期末复习草稿纸.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息

#include "stdafx.h"
#include "stdlib.h"

/*树*/
//插入元素构造树(有空元素符号的先序序列)
int CreateTree(BiTree &B)
{
	char a;
	scanf("%c", &a);
	if (a ==' ')//注意单引号才是字符,双引号是字符串
		B = NULL;
	else
	{
		TreeNode* T = (TreeNode*)malloc(sizeof(TreeNode));
		if (!T)
			return OVERFLOW;
		else
		{
			T->data = a;
			CreateTree(T->lchild);
			CreateTree(T->rchild);
		}
	}
	return OK;
}
//先序遍历
void PreOrder(BiTree B)
{
	if (B != NULL)
	{
		printf("%c  ", B->data);
		PreOrder(B->lchild);
		PreOrder(B->rchild);
	}
}
//中序遍历
void InOrder(BiTree B)
{
	if (B != NULL)
	{
		PreOrder(B->lchild);
		printf("%c  ", B->data);
		PreOrder(B->rchild);
	}
}
//后序遍历
void PostOrder(BiTree B)
{
	if (B != NULL)
	{
		PreOrder(B->lchild);
		PreOrder(B->rchild);
		printf("%c  ", B->data);
	}
}
//求高度
int HighTree(BiTree B)
{
	if (B = NULL)
		return 0;
	else
	{
		return (HighTree(B->lchild) + HighTree(B->rchild) + 1);
	}
}
//找到指定根节点的子树
BiTree FindCh(BiTree B,char e)
{
	if (B->data == e)
		return B;
	else 
	{
		BiTree T1 = FindCh(B->lchild,e);
		if (!T1)
		{
			return T1;
		}
		BiTree T2 = FindCh(B->rchild, e);
		if (!T2)
		{
			return T2;
		}
	}
}
//求树中叶子节点总个数
int CountLeaves(BiTree B)
{
	if (B->lchild == NULL&&B->rchild == NULL)
		return 1;
	else
	{
		return (CountLeaves(B->lchild) + CountLeaves(B->rchild));
	}


}
//求节点总个数
int CountAllNode(BiTree B)
{
	if (B == NULL)
	{
		return 0;
	}
	else
	{
		return (1 + CountAllNode(B->lchild) + CountAllNode(B->rchild));
	}
}


/*单链表*/
//初始化
int Initlist(Linklist &L)
{
	//前方结构体定义的时候使用别名typdef别名才可生效
	L = (LNode*)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	return OK;
}

int GETLEN(Linklist L)
{
	int i = 1;
	while (L->next != NULL)
		i++;
	return i;
}
//插入
int Insertlist(Linklist &L, int i, char e)
{
	if (i<0 || i>GETLEN(L))  return OVERFLOW;
	else
	{
		Linklist p = L;   int j = 1;
		while (p&&j <i-1)
		{p = p->next;j++;}
		Linklist s = (Linklist)malloc(sizeof(LNode));
		s->data = e;   s->next = p->next;
		p->next = s;   return OK;
	}
}
//删除
int Deletelist(Linklist &L, int i, char e)
{
	if (i<0 || i>GETLEN(L))  return OVERFLOW;
	else
	{
		Linklist p = L;   int j = 1;
		while (p&&j <i - 1)
		{p = p->next; j++;}
		//因为后面要free掉一个节点,所以前面要NEW一个来代替
		Linklist q = p->next;   p->next = q->next;
		q->data = e;   free(q);   return OK;
	}
}
//输入建立
int CreateList(Linklist &L, int n)
{
	L = (Linklist)malloc(sizeof(LNode));
	L->next = NULL;
	for (int i = 0; i < n; i++)
	{
		int elem;   scanf("%d\n", &elem);
		Linklist s = (Linklist)malloc(sizeof(LNode));
		if (!s || !elem)    return ERROR;
		s->next = L->next; L->next = s; s->data = elem;
	}
}
//清空
int Clearlist(Linklist &L)
{
	Linklist p = L;L->next = NULL;
	while (p)
	{
		Linklist q = p;p =p->next;free(q);
	//删除整个链表free(L);
	}
}


/*线性表*/
//初始化
int InitList(Sqlist &L)
{
    L.elem = (char*)malloc(INIT_LIST * sizeof(char));
    if (!L.elem)  
		return ERROR;
    else
    {
		L.length = 0;  L.listsize = INIT_LIST;
	}
     return OK;
}
//插入
int Insertlist(Sqlist &L,int i,char e)
{
	if (i < 0 || i >= L.length)
		return ERROR;
	else
	{if (L.length >= L.listsize)
		{
			char * newbase = (char*)realloc((L.elem), (LIST_INCREASE + L.length) * sizeof(char));
			if (!newbase)  return ERROR;
			else
			{
				L.elem = newbase;
				L.listsize = LIST_INCREASE + L.length;
			}
		}
		char* p = L.elem+i - 1;
		char* q = L.elem + L.length - 1;
		while (p < q)
		{*(q + 1) = *q;  q--;}
		*p = e;  L.length++;  return OK;}
}
//删除
int Deletelist(Sqlist &L, int i, char e)
{
	if (i<0 || i>L.length)  return ERROR;
	else
   {
		char* p = L.elem + i - 1;   e = *p;
		char* q = L.elem + L.length - 1;
		while (p<q)
		{*p = *(p + 1);p++;}
		L.length--;return OK;
	}
}
//查找
int Searchlist(Sqlist L, char e)
{
	int i = 1; char* p = L.elem;
	while (*p != e&&i<=L.length)
	{p++;i++;}
	if (i <= L.length) return i;
	else return 0;
}
//归并
int Mergelist(Sqlist La, Sqlist Lb, Sqlist &Lc)
{
	Lc.listsize = La.length + Lb.length;
	char* pa = La.elem;
	char* pb = Lb.elem;
	char* pc = Lc.elem;
	Lc.elem = (char*)malloc(Lc.length * sizeof(char));
	if (!Lc.elem)  return ERROR;
	else
	{
		while (pa < La.elem + La.length - 1 && pb < Lb.elem + Lb.length - 1)
		{
			if (*pa > *pb)  *pc = *pb;
			else   *pc = *pa;
			pa++; pb++; pc++;
		}
		while (pa < La.elem + La.length - 1)   *pc++ = *pa++;
		while (pb < Lb.elem + Lb.length - 1)   *pc++ = *pb++;
		return OK;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值