PTA题解-数据结构与算法题目集(中文)-12道函数题

地址:数据结构与算法题目集(中文)

0x01.单链表逆转

地址:6-1 单链表逆转

List Reverse( List L )
{
    List tmp,pnew;
    pnew=NULL;
    while(L)
    {
        tmp=L->Next;
        L->Next=pnew;
        pnew=L;
        L=tmp;
    }
    return pnew;
}

0x02.顺序表操作集

地址:6-2 顺序表操作集

List MakeEmpty()
{
    List L;
    L = (List)malloc(sizeof(struct LNode));//注意分配空间,不然是NULL
    L->Last=-1;
    return L;
}

Position Find(List L, ElementType X)
{
    int i;
    for(i=0;i<=L->Last;i++)
    {
        if(L->Data[i]==X)
        {
            return i;
        }
    }
    return ERROR;
}

bool Insert(List L,ElementType X,Position P)
{
    if(L->Last>=MAXSIZE-1)
    {
        printf("FULL");
        return false;
    }
    if(P<0||P>L->Last+1)
    {
        printf("ILLEGAL POSITION");
        return false;
    }
    ElementType q=L->Last;
    for( ;q>=P;q--)
    {
        L->Data[q+1]=L->Data[q];
    }
    L->Data[P]=X;
    L->Last++;
    return true;
}

bool Delete( List L, Position P )
{
    if(P<0||P>L->Last)
    {
        printf("POSITION %d EMPTY",P);
        return false;
    }
    ElementType p=P;
    for( ;p<=L->Last;p++)
    {
        L->Data[p]=L->Data[p+1];
    }
    L->Last--;
    return true;
}

0x03.求链式表的表长

地址:6-3 求链式表的表长

int Length( List L )
{
    int i=0;
    while(L)
    {
        i++;
        L=L->Next;
    }
    return i;
}

 0x04.链式表的按序号查找

地址:链式表的按序号查找

ElementType FindKth( List L, int K )
{
    int i=0;
    while(L)
    {
        i++;
        
        if(i==K)
        {
            return L->Data;
        }
        
        L=L->Next;
    }
    return ERROR;
}

0x05.链式表操作集

地址:6-5 链式表操作集

Position Find( List L, ElementType X )
{
    while(L)
    {
        if(L->Data==X)
        {
            return L;
        }
        L=L->Next;
    }
    return ERROR;
}
List Insert( List L, ElementType X, Position P )
{
    List p=L;//保留头结点
    if(P==L)//在头部添加
    {
        List pnew=(List)malloc(sizeof(struct LNode));
        pnew->Data=X;
        pnew->Next=L;
        return pnew;       
    }
    if(L==NULL)//链表为空无法添加
    {
		printf("Wrong Position for Insertion\n");
		return ERROR;
    }
    while(p->Next!=NULL&&p->Next!=P)//遍历到相等或到尾部
    {
        p=p->Next;
    }
    if(p->Next==NULL&&P!=NULL)
    {
        printf("Wrong Position for Insertion\n");
        return ERROR;        
    }
    if(p->Next==P)
    {
            List pnew=(List)malloc(sizeof(struct LNode));
            pnew->Data=X;
            pnew->Next=p->Next;
            p->Next=pnew;
            return L;
    }
}
List Delete( List L, Position P )
{
    List p=L;
    if(L==NULL||P==NULL)
    {
        printf("Wrong Position for Deletion\n");
        return ERROR;      
    }
    if(P==L)
    {
        p=p->Next;
        return p;
    }
    while(p->Next!=NULL&&p->Next!=P)
    {
        p=p->Next;
    }
    if(p->Next==NULL&&P!=NULL)
    {
        printf("Wrong Position for Deletion\n");
        return ERROR;
    }
    p->Next=p->Next->Next;
    return L;
}

0x06.带头结点的链式表操作集

地址:6-6 带头结点的链式表操作集

List MakeEmpty()
{
    List L=(List)malloc(sizeof(struct LNode));
    L->Next==NULL;
    return L;
}
Position Find( List L, ElementType X )
{
     while(L)
    {
        if(L->Data==X)
        {
            return L;
        }
        L=L->Next;
    }
    return false;
}
bool Insert( List L, ElementType X, Position P)
{
     List p=L;//保留头结点
    if(P==L)//在头部添加
    {
        List pnew=(List)malloc(sizeof(struct LNode));
        pnew->Data=X;
        pnew->Next=p;
        L=pnew;
        return true;       
    }
    if(L==NULL)//链表为空无法添加
    {
		printf("Wrong Position for Insertion\n");
		return false;
    }
    while(p->Next!=NULL&&p->Next!=P)//遍历到相等或到尾部
    {
        p=p->Next;
    }
    if(p->Next==NULL&&P!=NULL)
    {
        printf("Wrong Position for Insertion\n");
        return false;        
    }
    if(p->Next==P)
    {
            List pnew=(List)malloc(sizeof(struct LNode));
            pnew->Data=X;
            pnew->Next=p->Next;
            p->Next=pnew;
            return true;
    }
}
bool Delete( List L, Position P )
{
     List p=L;
    if(L==NULL||P==NULL)
    {
        printf("Wrong Position for Deletion\n");
        return false;      
    }
    if(P==L)
    {
        p=p->Next;
        L=p;
        return true;
    }
    while(p->Next!=NULL&&p->Next!=P)
    {
        p=p->Next;
    }
    if(p->Next==NULL&&P!=NULL)
    {
        printf("Wrong Position for Deletion\n");
        return false;
    }
    p->Next=p->Next->Next;
    return true;
}

0x07.在一个数组中实现两个堆栈

地址:6-7 在一个数组中实现两个堆栈

Stack CreateStack( int MaxSize )
{
    Stack S=(Stack)malloc(sizeof(struct SNode));
    S->Data=(ElementType*)malloc(MaxSize*sizeof(ElementType));
    S->Top1=-1;
    S->Top2=MaxSize;
    S->MaxSize=MaxSize;
    return S;
}
bool Push( Stack S, ElementType X, int Tag )
{
    if(S==NULL) return false;
    if(Tag==1)
    {
        if(S->Top1+1==S->Top2)
        {
            printf("Stack Full\n");
            return false;
        }
        S->Data[++S->Top1]=X;
        return true;
    }
    if(Tag==2)
    {
        if(S->Top1+1==S->Top2)
        {
            printf("Stack Full\n");
            return false;
        }
        S->Data[--S->Top2]=X;
        return true;
    }
}
ElementType Pop( Stack S, int Tag )
{
    if(S==NULL) return ERROR;
    if(Tag==1)
    {
        if(S->Top1==-1)
        {
            printf("Stack 1 Empty\n");
            return ERROR;
        }
        return S->Data[S->Top1--];
    }
    if(Tag==2)
    {
        if(S->Top2==S->MaxSize)
        {
            printf("Stack 2 Empty\n");
            return ERROR;
        }
        return S->Data[S->Top2++];
    }
}

0x08.求二叉树高度

地址:6-8 求二叉树高度

int GetHeight( BinTree BT )
{
    int LH,RH;
    if(!BT) return 0;
    LH=GetHeight(BT->Left);
    RH=GetHeight(BT->Right);
    return LH>RH?++LH:++RH;
}

0x09.二叉树的遍历

地址:6-9 二叉树的遍历

//中序遍历,递归实现
void InorderTraversal(BinTree BT)
{
	if (BT == NULL) return;
	InorderTraversal(BT->Left);
	printf(" %c", BT->Data);
	InorderTraversal(BT->Right);
	return;
}
//前序遍历,递归实现
void PreorderTraversal(BinTree BT)
{
	if (BT == NULL) return;
	printf(" %c", BT->Data);
	PreorderTraversal(BT->Left);
	PreorderTraversal(BT->Right);
	return;
}
//后序遍历,递归实现
void PostorderTraversal(BinTree BT)
{
	if (BT == NULL) return;
	PostorderTraversal(BT->Left);
	PostorderTraversal(BT->Right);
	printf(" %c", BT->Data);
	return;
}
//层序遍历,队列实现
//为方便直接调用该函数,所以队列结构放于函数内部
void LevelorderTraversal(BinTree BT)
{
	BinTree Queue[100];//创建一个二叉树指针类型的队列
	int head = -1;
	int tail = -1;
	if (BT)
	{
		Queue[++tail] = BT;//如果头结点非空,头结点入队
	}
	while (head < tail)//队列为空是遍历完成的条件
	{
		BinTree tmp = Queue[++head];//队头出队,打印信息
		printf(" %c", tmp->Data);
		if (tmp->Left)//如果左孩子存在,左孩子先入队
		{
			Queue[++tail] = tmp->Left;
		}
		if (tmp->Right)
		{
			Queue[++tail] = tmp->Right;
		}
	}
}

0x10.二分查找

地址:6-10 二分查找

Position BinarySearch( List L, ElementType X )
{
    int low,mid,high;
    low=0;
    high=L->Last;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(X>L->Data[mid])
        {
            low=mid+1;
        }
        if(X<L->Data[mid])
        {
            high=mid-1;
        }
        if(X==L->Data[mid])
        {
            return mid;
        }
    }
    return NotFound;
}

0x11.先序输出叶结点

地址:6-11 先序输出叶结点

void PreorderPrintLeaves( BinTree BT )
{
    if(!BT) return;
    if(BT->Left==NULL&&BT->Right==NULL)
    {
        printf(" %c",BT->Data);
    }
    PreorderPrintLeaves( BT->Left );
    PreorderPrintLeaves( BT->Right );
}

0x12.二叉搜索树的操作集

地址:6-12 二叉搜索树的操作集

Position Find( BinTree BST, ElementType X )
{
    if(!BST)
    {
        return NULL;
    }
    else if(X==BST->Data)
    {
        return BST;
    }
    else if(X<BST->Data)
    {
        return Find(BST->Left,X);
    }
    else
    {
        return Find(BST->Right,X);
    }
}
BinTree Insert( BinTree BST, ElementType X )
{
    if(!BST)//插入头
    {
        BinTree p=(BinTree)malloc(sizeof(struct TNode));
        p->Data=X;
        p->Left=p->Right=NULL;
        BST=p;
    }
    else
    {
        if(X<BST->Data)
        {
           BST->Left=Insert(BST->Left,X);
        }
        else
        {
           BST->Right=Insert(BST->Right,X);
        }
    }
    return BST;
}

BinTree Delete(BinTree BST,ElementType X)
{
	BinTree tmp;
	if (!BST) puts("Not Found");
	else
	{
		if (X < BST->Data)
			BST->Left = Delete(BST->Left, X); // 左子树递归删除
		else if (X > BST->Data)
			BST->Right = Delete(BST->Right, X); // 右子树递归删除
		else // 找到需要删除的结点
		{
			if (BST->Left && BST->Right) // 被删除的结点有左、右子结点
			{
                BinTree q=BST->Right;
                if(!q)
                {
                    tmp =q;
                }
                else
                {
				    while (q->Left)// 在右子树种找到最小结点填充删除结点
				    {
                        q=q->Left;
			    	}
                    tmp =q;
                }		
				BST->Data = tmp->Data;
				BST->Right = Delete(BST->Right, BST->Data); // 递归删除要删除结点的右子树种最小元素
			}
			else // 被删除结点有一个或没有子结点
			{
				tmp = BST;
				if (!BST->Left) BST = BST->Right; // 有右孩子或者没孩子
				else if (!BST->Right) BST = BST->Left; 
			}
		}
	}
	return BST;
}

Position FindMax(BinTree BST)
{
	if (BST)
	{
		while (BST->Right)
		{
			BST = BST->Right;
		}
	}
	return BST;
}

Position FindMin(BinTree BST)
{
	if (BST)
	{
		while (BST->Left)
		{
			BST = BST->Left;
		}
	}
	return BST;
}
发布了50 篇原创文章 · 获赞 35 · 访问量 1293

猜你喜欢

转载自blog.csdn.net/ATFWUS/article/details/104473720
今日推荐