标题: 链表的数据域和指针域的问题 [打印本页]

作者: 1314love    时间: 2022-8-9 17:39
标题: 链表的数据域和指针域的问题
请问各位,就是一个链表,我交换了两个节点的数据域和指针域,它输出的时候会按交换后的来输出吗
就比方 head->2->4->3
交换后 head->4->2->3
他最终会按交换后的输出吗?
如果可以的话,为什么会按交换后的输出,他不是在交换前就指向了下一个的地址吗?如果交换了指针域,他这个地址不就乱了吗,也就是链表不就乱了。求大神解答。

作者: Hephaestus    时间: 2022-8-9 23:15
既然是交换,那么输出为什么不会交换?说了这么多,我觉得你还是对交换操作没有自信,把有疑虑的代码贴出来才是正道。
作者: 1314love    时间: 2022-8-10 08:19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//定义一个学生结构体
typedef struct Stu
{
    int id;
    int score;
    struct Stu *pnext;

}Stu;

//创建一个链表
Stu *create_Stu(int n)
{
    //先定义一个头结点,不存放有效数据
    Stu *head = (Stu*)malloc(sizeof(Stu));
    if (head == NULL)
        return NULL;
    head->id = -1;
    head->score = -1;
    head->pnext = NULL;

    //输入要加入的n个结点的信息,新的结点加到链表最后
    Stu *tmp = head;
    int i;
    for (i = 0; i < n; i++)
    {
        Stu *new_node = (Stu*)malloc(sizeof(Stu));
        if (new_node == NULL)
            return NULL;
        scanf("%d", &new_node->id);
        scanf("%d", &new_node->score);
        new_node->pnext = NULL;
        //新节点加到链表最后
        tmp->pnext = new_node;

        tmp = new_node;
    }

    return head;
}

//将一个链表与另一个链表合并,返回第一个链表头部
Stu *merge_Stu(Stu *students1, Stu *students2)
{
    if (students1 == NULL || students2 == NULL)
        return NULL;

    //先找到第一个链表的尾结点
    Stu *tmp1 = students1;
    while (tmp1->pnext != NULL)
    {
        tmp1 = tmp1->pnext;
    }
    //出来之后tmp1就为第一个链表的尾结点

    Stu *tmp2 = students2;
    //第一个链表的尾结点连接第二个链表的首结点
    tmp1->pnext = tmp2->pnext;

    //不要忘记释放第二个链表的头结点
    free(tmp2);

    //返回合并之后的头结点
    return students1;
}

//链表结点排序重组
void sort_Stu(Stu *students)
{
    if (students == NULL)
        return;

    Stu *pre = NULL;
    Stu *cur = NULL;
    Stu tmp;

    //选择法对结点进行排序
    for (pre = students->pnext; pre->pnext != NULL; pre = pre->pnext)
    {
        for (cur = pre->pnext; cur != NULL; cur = cur->pnext)
        {
            if (pre->id > cur->id)
            {
                //数据域和指针域都要进行交换
                //数据域交换
                tmp = *pre;
                *pre = *cur;
                *cur = tmp;
                //指针域交换
                tmp.pnext = pre->pnext;
                pre->pnext = cur->pnext;
                cur->pnext = tmp.pnext;
            }
        }
    }
}

//打印链表信息
void print_Stu(Stu *students)
{
    if (students == NULL || students->pnext == NULL)
    {
        printf("invalid list!\n");
        return;
    }

    Stu *cur = students->pnext; //指向首结点
    while (cur != NULL)
    {
        printf("%d %d\n", cur->id, cur->score);
        cur = cur->pnext;
    }
}

//释放整个链表
void destory_Stu(Stu *students)
{
    if (students == NULL)
        return;

    Stu *s = students;
    Stu *tmp = NULL; //用来保存当前所释放的结点的下一个结点

    while (s != NULL)
    {
        tmp = s->pnext;
        free(s);
        s = tmp;
    }
}

int main()
{
    int N, M;
    scanf("%d %d", &N, &M);

    //创建两个链表
    Stu *students1 = create_Stu(N);
    Stu *students2 = create_Stu(M);

    //合并两个链表
    Stu *students = merge_Stu(students1, students2);

    //对新链表中的内容按学号升序排列并打印
    sort_Stu(students);
    print_Stu(students);

    destory_Stu(students);
    return 0;
}



//数据域和指针域都要进行交换
                //数据域交换
                tmp = *pre;
                *pre = *cur;
                *cur = tmp;
                //指针域交换
                tmp.pnext = pre->pnext;
                pre->pnext = cur->pnext;
                cur->pnext = tmp.pnext;
就是上面这个指针域和数据域交换了
作者: 天ノ忆    时间: 2022-8-10 10:05
你这种写法交换的是节点指向的值,而地址是不变的,最后这6行代码这么写是没问题的,其他的太长不看




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1