加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

C++中链表操作实例分析

发布时间:2021-01-11 19:35:45 所属栏目:创业 来源:网络整理
导读:链表概述 链表是一种常见的重要的数据结构。它是动态地进行存储分配的一种结构。它可以根据需要开辟内存单元。链表有一个“头指针”变量,以head表示,它存放一个地址。该地址指向一个元素。链表中每一个元素称为“结点”,每个结点都应包括两个部分:一为用

    p = head;              //有序链表不为空
    while(p->num < node->num && p != NULL)       //p指向的节点的学号比插入节点的学号小,并且它不等于NULL
    {
        t = p;              //保存当前节点的前驱,以便后面判断后处理
        p = p->next;        //后移一个节点
    }

    if (p == head)        //刚好插入第一个节点之前
    {
        node->next = p;
        head = node;
    }
    else                 //插入其它节点之后
    {
        t->next = node;        //把node节点加进去
        node->next = p;
    }
    n += 1;            //插入完毕,节点总数加1

    return head;
}

/*
以上函数的测试程序:
提示:根据测试函数的不同注释相应的程序段,这也是一种测试方法。
*/
int main(void)
{
    struct student *head;
    struct student *stu;
    int thenumber;

    // 测试Create()、Print()
    head = Create();
    Print(head);

    //测试Del()
    printf("nWhich one delete: ");
    scanf("%d",&thenumber);
    head = Del(head,thenumber);
    Print(head);

    //测试Insert()
    stu = (struct student *)malloc(LEN);
    printf("nPlease input insert node -- num,score: ");
    scanf("%d %f",&stu->num,&stu->score);
    printf("nInsert behind num: ");
    scanf("%d",&thenumber);
    head = Insert(head,thenumber,stu);
    Print(head);

    //测试Reverse()
    printf("nReverse the LinkList: n");
    head = Reverse(head);
    Print(head);

    //测试SelectSort()
    printf("nSelectSort the LinkList: n");
    head = SelectSort(head);
    Print(head);

    //测试InsertSort()
    printf("nInsertSort the LinkList: n");
    head = InsertSort(head);
    Print(head);

    //测试BubbleSort()
    printf("nBubbleSort the LinkList: n");
    head = BubbleSort(head);
    Print(head);

    printf("nSortInsert the LinkList: n");
    //测试SortInsert():上面创建链表,输入节点时请注意学号num从小到大的顺序
    stu = (struct student *)malloc(LEN);
    printf("nPlease input insert node -- num,&stu->score);
    head = SortInsert(head,stu);
    Print(head);

    //销毁链表
    DestroyList(head);

    printf ("n");
    system ("pause");
}

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读