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++數據結構鏈表基本操作示例過程的詳細內容,更多關於C++數據結構鏈表基本操作的資料請關註WalkonNet其它相關文章!

推薦閱讀: