第四周项目三 单链表的应用

1.
/*     
文件名称:单链表的应用    
作    者:胡德杰     
完成日期:2017年9月27号     
版 本 号:v1.1.9
*/    
#include "linklist.h"
void Reverse(LinkList *&L)
{
    LinkList *p=L->next,*q;
    L->next=NULL;
    while(p!=NULL)
    {
        q=p->next;
        p->next=L->next;
        L->next=p;
        p=q;
    }
}
int main()
{
    LinkList *L;
    ElemType a[]= {1,3,5,7, 2,4,8,10};
    CreateListR(L,a,8);
    printf("L:");
    DispList(L);
    Reverse(L);
    printf("逆置后L: ");
    DispList(L);
    DestroyList(L);
    return 0;
}

2.
#include "linklist.h"
void Link(LinkList *&L1,LinkList *&L2)
{
    LinkList *p=L1->next,*q=L2->next;
    while (p->next!=NULL)
        p=p->next;
    p->next=q;
    free(L2);
}
int main()
{
    LinkList *A, *B;
    int i;
    ElemType a[]= {1,3,2,9};
    ElemType b[]= {0,4,7,6,5,8};
    InitList(A);
    for(i=3; i>=0; i--)
        ListInsert(A, 1, a[i]);
    InitList(B);
    for(i=5; i>=0; i--)
        ListInsert(B, 1, b[i]);
    Link(A, B);
    printf("A:");
    DispList(A);
    DestroyList(A);
    return 0;
}


3.

#include "linklist.h"
bool increase(LinkList *L)
{
    LinkList *p = L->next, *q;  //p指向第1个数据节点
    if(p != NULL)
    {
        while(p->next != NULL)
        {
            q = p->next;   //q是p的后继
            if (q->data > p->data)   //只要是递增的,就继续考察其后继
                p = q;
            else
                return false;    //只要有一个不是后继大于前驱,便不是递增
        }
    }
    return true;
}
int main()
{
    LinkList *A, *B;
    int i;
    ElemType a[]= {1, 3, 2, 9};
    ElemType b[]= {0, 4, 5 ,6, 7, 8};
    InitList(A);
    for(i=3; i>=0; i--)
        ListInsert(A, 1, a[i]);
    InitList(B);
    for(i=5; i>=0; i--)
        ListInsert(B, 1, b[i]);
    printf("A: %c\n", increase(A)?'Y':'N');
    printf("B: %c\n", increase(B)?'Y':'N');
    DestroyList(A);
    DestroyList(B);
    return 0;
}

知识点总结:对于单链表的应用。

学习心得:更好的掌握了单链表,也体会到了链表的趣味性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值