鱼C论坛

 找回密码
 立即注册
查看: 2797|回复: 0

[学习笔记] 初始化链表、头插链表、尾插链表、两个链表相连

[复制链接]
发表于 2021-9-28 18:23:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
相连后我就不释放N链表的头节点了我这从新建立了一个K链表返回之后付给新新链表
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAXSIZE 10

typedef struct Like
{
    int T;
    struct Like *next;
}Like, *Likes;


void Explain();     //程序说明
void InitList(Likes *N);    //初始化及清除数据
void TailList(Likes *M);     //创建尾插法链表
void HeadList(Likes *N);     //创建尾插法链表
void AlPuList(Like *N);     //打印链表
struct Like *LinkList(Like *N, Like *M); //两个链表合并

struct Like *LinkList(Like *N, Like *M)
{
    Like *temp = N->next->next;     //N的一号节点传给临时变量
    N->next->next = M->next->next;  //M的1号节点传给N尾节点之后
    M->next->next = temp;           //N1号节点传给M节点之后也就是当M的一号数据

    return M->next;
}

void AlPuList(Like *N)
{
    Like *temp = N->next;

    do
    {
        printf("<%d>", temp->T);
        temp = temp->next;

    }while(temp != N->next);
    putchar('\n');

}

void TailList(Likes *M)
{
    Like *temp, *tail;
    srand((unsigned)time(NULL));        //随意创建N个链表

    for(int i = 0; i < MAXSIZE; i++)
    {
        temp = (Likes )malloc(sizeof(Like ));
        temp->T = (rand() % 100);
        temp->next = (*M)->next;

        if((*M)->next == *M)
        {
            (*M)->next = temp;
        }
        else
        {
            tail->next = temp;
        }
        tail = temp;
    }
    (*M)->next = tail;

}
void HeadList(Likes *N)
{
    Like *temp, *tail;
    srand((unsigned)time(NULL));

    for(int i = 0; i < MAXSIZE; i++)
    {
        temp = (Likes )malloc(sizeof(Like ));
        temp->T = (rand() % 100);

        if((*N)->next == *N )
        {
            (*N)->next = temp;
            temp->next = temp;  //自己指向自己
            tail = temp;        //把尾链表传给tail,(tail不做更改)
        }
        else
        {
            temp->next = (*N)->next;    //把链表1号节点传给临时链表temp->next
            (*N)->next = temp;          //把temp替换1号节点的数据因为前面已经做了向下一个的next所以链表是相连的
            tail->next = temp;          //把尾节点的指向next替换成新的1号节点点形成循环
        }
    }
    (*N)->next = tail;                  //把链表的头节点指向链表的尾节点

}

void InitList(Likes *N)
{
    Like *temp,*temps;

  if(*N != NULL)
    {
        temp = (*N)->next;
        do
        {
            temps = temp;
            temp = temp->next;
            free(temps);
        }while((*N)->next != temp);
    }
    else
    {
        *N = (Likes )malloc(sizeof(Like ));
    }
    (*N)->next = *N;




}
void Explain()
{
    printf("1、初始化程序及清除数据\n");
    printf("2、创建头插法链表\n");
    printf("3、创建尾插法链表\n");
    printf("4、两个链表合并\n");
    printf("9、打印全部链表\n");
    printf("0、结束本程序\n");
}

int main()
{
    int i;
    Like *N = NULL, *M = NULL, *K;

    Explain();

    while(1)
    {
        printf("输入选项:");
        scanf("%d",&i);

        switch(i)
        {
            case 1:{
                InitList(&N);
                InitList(&M);
            }
            break;
            case 2:{
                HeadList(&N);
            }
            break;
            case 3:{
                TailList(&M);
            }
            break;
            case 4:{
                K = LinkList(N, M);
                AlPuList(K);
            }
            break;
             case 9:{
               AlPuList(N);
               AlPuList(M);
            }
            break;
            case 0:{
                return 0;
            }
            break;
        }

    }

    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-2 19:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表