数额结构(6.1~6.8)

6-1链表的插入算法

题目:

代码

c 复制代码
int InsertPost_link(LinkList llist,DataType x,DataType y)
{
    LinkList m=llist->next;
    LinkList n;
    while(m->data!=x)
    {
        m=m->next;4
        if(m==NULL)
        {
            printf("not exist data %d\n",x);
            return 0;
        }
    }
    n=(LinkList)malloc(sizeof(struct Node));
    if(n!=NULL)
    {
        n->data=y;
        n->next=m->next;
        m->next=n;
        return 0;
    }
}

6-2链表的删除算法



代码

c 复制代码
void DelNode_Link(LinkList head, DataType deldata)
{
    PNode p=head->next;
    PNode beforeP=head;
    while(p!=NULL)
    {
        if(p->data==deldata)
        {
            beforeP->next=p->next;
            free(p);
            return 0;
        }
        else
        {
            beforeP=p;
            p=p->next;
        }
    }
    printf("not exist %d\n",deldata);
}

6-3移动链表中的最大值到尾部

c 复制代码
void  MoveMaxToTail(LinkList head)
{
    PNode pmax=NULL,p=NULL,pre=NULL,end=NULL;
    pmax=head->next;
    p=head->next->next;
    while(p)
    {
        if(p->data>pmax->data)
        {
            pmax=p;
        }
        p=p->next;
    }
    if(pmax->next==NULL)
    {
        return 1;
    }
    else
    {
        p=head;
        while(p)
        {
            if(p->next==pmax)
            {
                pre=p;
            }
            if(p->next==NULL)
            {
                end=p;
            }
            p=p->next;
        }
        pre->next=pmax->next;
        pmax->next=end->next;
        end->next=pmax;
    } 
    return 0;
}

6-4合并两个递增有序的单循环链表

c 复制代码
PNode mergeNDeduplicateList(PNode tail1, PNode tail2)
{
    int temp;
    PNode pre,q;
    LinkList head=tail1->next->next;
    tail1->next->next=tail2->next->next;
    tail2->next->next=NULL;
    for(pre=head;pre->next!=NULL;pre=pre->next)
    {
        for(q=pre->next;q!=NULL;q=q->next)
        {
            if(pre->data>q->data)
            {
                temp=pre->data;
                pre->data=q->data;
                q->data=temp;
            }
        }
    }
    pre=head;
    q=pre->next;
    while(q)
    {
        if(pre->data==q->data)
        {
            if(q==tail2->next)
            {
                tail2->next=pre;
                pre->next=NULL;
            }
            else
            {
                pre->next=q->next;
                free(q);
                q=pre->next;
            }
        }
        else
        {
            pre=q;
            q=q->next;
        }
    }
    tail2->next->next=head;
    return tail2;
}

6-5链表中奇偶结点的移动


c 复制代码
PNode Move_Odd_Even(LinkList tail)
{
    PNode head=tail->next,pre=head->next,q=pre->next;
    PNode pre1,head1=(PNode)malloc(sizeof(struct Node));
    PNode pre2,head2=(PNode)malloc(sizeof(struct Node));
    pre1=head1;
    pre2=head2;
    while(q!=head->next)
    {
        if(pre->data%2==0)
        {
            pre->next=pre1->next;
            pre1->next=pre;
            pre1=pre;
        }
        else
        {
            pre->next=pre2;
            pre2->next=pre;
            pre2=pre;
        }
    pre=q;
    q=q->next;
    }
head1=head1->next;
pre2->next=head1;
pre1->next=head2;
return pre1;
}

6-6循环队列入队出队


c 复制代码
void EnQueue_seq(SeqQueue squeue, DataType x)
{
    if((squeue->r+1)%squeue->Max==squeue->f)
    {
        printf("It is FULL Queue!");
    }
    else
    {
        squeue->elem[squeue->r]=x;
        squeue->r=(squeue->r+1)%(squeue->Max);
    }
}

void DeQueue_seq(SeqQueue squeue)
{
    if(squeue->f==squeue->r)
    {
        printf("It is empty queue!");
    }
    else
    {
        squeue->f=(squeue->f+1)%(squeue->Max);
    }
}

6-7 进制转换(10->16)


c 复制代码
void Push_seq(SeqStack sstack ,DataType x)
{
    if(sstack->top>=(sstack->MAX-1))
    {
        printf("overflow!\n");
    }
    else
    {
        sstack->top++;
        sstack->elem[sstack->top]=x;
    }
}
void Hexconversion(SeqStack sstack,int n)
{
    while(n)
    {
        int tmp=n%16;
        switch(tmp)
        {
                case 10:tmp='A';break;
                case 11:tmp='B';break;
                case 12:tmp='C';break;
                case 13:tmp='D';break;
                case 14:tmp='E';break;
                case 15:tmp='F';break;
        }
        Push_seq(sstack,tmp);
        n=n/16;
    }
    while(!IsNullStack_seq(sstack))
    {
        n=Top_seq(sstack);
        if(n<10)
        {
            printf("%d",n);
        }
        else
        {
            printf("%c",n);
        }
        Pop_seq(sstack);
    }
    
}

6-8 递归建立和层次遍历二叉树

我先说一下,这道题我没做出来,但是代码我敲了


c 复制代码
BinTree CreateBinTree_NRecursion()
{
    LinkQueue queue=SetNullQueue_Link();
    BinTreeNode *s, *p,*Bt;
    char ch;
    int count=-1;
    ch=getchar();
    Bt=NULL;
    while(ch!='#')
    {
        s=NULL;
        if(ch!='@')
        {
            s=(BinTreeNode*)malloc(sizeof(BinTreeNode));
            s->data=ch;
            s->leftchild=s->rightchild=NULL;
        }
        EnQueue_link(queue,s);
            count++;
        if(count==0)
        {
            Bt=s;
        }
        else
        {
            p=FrontQueue_link(queue);
            if(s!=NULL&&p!=NULL)
            { if(count%2==1)
            {
             p->leftchild=s;
            }
            else
            {
                p->rightchild=s;
            }
             
            }
            if(count%2==0)
                DeQueue_link(queue);
         }
        ch=getchar();
        }
    }
     return Bt;
}

void LevelOrder(BinTree bt)
{
    BinTree p;
    LinkQueue queue=SetNullQueue_Link();
    if(bt==NULL)return;
    p=bt;
    EnQueue_link(queue,bt);
    while(!IsNullQueue_Link(queue))
    {
        p=FrontQueue_link(queue);
        DeQueue_link(queue);
        printf("%c",p->data);
        if(p->leftchild!=NULL)
            EnQueue_link(queue ,p->leftchild);
        if(p->rightchild!=NULL)
            EnQueue_link(queue,p->rightchild);
    }
}
相关推荐
viperrrrrrrrrr73 小时前
大数据学习(105)-Hbase
大数据·学习·hbase
行思理5 小时前
go语言应该如何学习
开发语言·学习·golang
oceanweave6 小时前
【k8s学习之CSI】理解 LVM 存储概念和相关操作
学习·容器·kubernetes
吴梓穆8 小时前
UE5学习笔记 FPS游戏制作43 UI材质
笔记·学习·ue5
学会870上岸华师8 小时前
c语言学习16——内存函数
c语言·开发语言·学习
XYN618 小时前
【嵌入式面试】
笔记·python·单片机·嵌入式硬件·学习
啊哈哈哈哈哈啊哈哈8 小时前
R3打卡——tensorflow实现RNN心脏病预测
人工智能·深度学习·学习
KangkangLoveNLP9 小时前
深度探索:策略学习与神经网络在强化学习中的应用
人工智能·深度学习·神经网络·学习·机器学习·自然语言处理
穷儒公羊10 小时前
第一部分——Docker篇 第六章 容器监控
运维·后端·学习·docker·云原生·容器
CAE虚拟与现实10 小时前
记录一下学习docker的命令(不断补充中)
学习·docker·容器·容器化·docker部署·docker命令