C++数据结构链表的基本操作(数据结构链表c语言实现)

C++数据结构链表的基本操作(数据结构链表c语言实现)

编码文章call10242025-02-01 3:30:2621A+A-

这篇文章主要为大家介绍了C++数据结构链表基本操作的示例过程有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪

首先创建好一个节点

typedef struct node {
    int date;
    struct node* next;
}*PNODE;
 PNODE creatnode(int date )
{
    PNODE newnode = (PNODE)malloc(sizeof(struct node));
    assert(newnode);
    newnode->next = NULL;
    newnode->date = date;
    return newnode; 
}

其次创建一个统计节点属性

struct List {
    struct node* pronode;//这只是一个类型
    struct node*tailnode;
    int size;
};
//创建统一链表属性的list  
//用来统计链表的(size)节点数
//head和tail用来统计链表的表头和表尾
struct List* creatlist()
{
    struct List* list = (struct List*)malloc(sizeof(struct List));
    assert(list);
    list->pronode = NULL;
    list->tailnode = NULL;
    list->size = 0;//初始化
    return  list;
  
}

增加节点

用表头插入的方法插入节点

void insertbyhead(struct List* list,int date)
{
    PNODE newnode = creatnode(date);
    if (list->size == 0)
    {
        list->pronode = list->tailnode = newnode;
    }
    else
    {
        newnode->next = list->pronode;
        list->pronode = newnode;
    }
    list->size++;
}

删除节点

//表头删除
void deletehead(struct List* list)
{
    PNODE next = list->pronode->next;
    free(list->pronode);
    list->pronode = next;
}
//表尾删除
void deletetail(struct List* list)
{
    PNODE pmove = list->pronode;//定义一个移动指针
                                //目的找到表尾指针
    if (list->size == 0)
    {
        printf("无法删除");
        return;
    }
    while (pmove->next != list->tailnode)
    {
        pmove = pmove->next;
    }
    pmove->next = NULL;//表尾指针前面一个下一个指向null
    free(list->tailnode);
    list->tailnode = pmove;
  
}

C++数据结构链表的基本操作 | 《Linux就该这么学》 (linuxprobe.com)

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4