服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++数据结构链表基本操作示例过程

C++数据结构链表基本操作示例过程

2022-02-25 14:57xr415 C/C++

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

首先创建好一个节点

?
1
2
3
4
5
6
7
8
9
10
11
12
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;
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
 
}

增加节点

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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++;
}
 

删除节点

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//表头删除
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++数据结构链表基本操作示例过程的详细内容,更多关于C++数据结构链表基本操作的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/weixin_56366633/article/details/120181283

延伸 · 阅读

精彩推荐