C++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作示例過程
更新時間:2021年11月18日 16:25:22 作者:xr415
這篇文章主要為大家介紹了C++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作的示例過程有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
首先創(chuàng)建好一個節(jié)點
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; }
其次創(chuàng)建一個統(tǒng)計節(jié)點屬性
struct List { struct node* pronode;//這只是一個類型 struct node*tailnode; int size; }; //創(chuàng)建統(tǒng)一鏈表屬性的list //用來統(tǒng)計鏈表的(size)節(jié)點數(shù) //head和tail用來統(tǒng)計鏈表的表頭和表尾 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; }
增加節(jié)點
用表頭插入的方法插入節(jié)點
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++; }
刪除節(jié)點
//表頭刪除 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++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作示例過程的詳細內(nèi)容,更多關(guān)于C++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
最新VScode C/C++ 環(huán)境配置的詳細教程
這篇文章主要介紹了最新VScode C/C++ 環(huán)境配置的詳細教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11