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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

xr415   2021-11-18 我要评论
想了解C++数据结构链表基本操作示例过程的相关内容吗,xr415在本文为您仔细讲解C++数据结构链表基本操作的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C++数据结构,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;
 
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们