单向链表的实现
链表核心要素:每个节点由数据域和指针域组成,指针域指向下一个节点的内存地址。
代码实现:
定义结构体
typedef struct Linknode { int data;//节点的数据域 Linknode* next;//节点的指针域 }_Linknode,Linklist;//数据域,指针域
初始化一个空链表
bool InitList(Linklist* &list) { list = new _Linknode; if (!list) return false; list->next = NULL; return true; }
前插法
bool Listinsert_front(Linklist* &list, _Linknode* node) { if (!list || !node) return false; node->next = list->next; list->next = node; return true; }
尾插法
bool Listinsert_back(Linklist* &list, _Linknode* node) { _Linknode* last = NULL; if (!list || !node) return false; last = list; while (last->next)last = last->next; node->next = NULL; last->next = node; return true; }
指定位置插入
bool LinkInsert(Linklist* & list, int i, int& e) { if (!list) return false; int j = 0; Linklist* p, * s; p = list; while (p && j < i - 1)//查找位置为i-1的节点,p指向该节点 { p = p->next;//每循环一次p向后进一步,指向下一个节点 j++; } if (!p || j > i - 1) return false; s = new _Linknode;//生成新的节点 s->data = e; s->next = p->next; p->next = s; return true; }
元素获取
bool Link_GetElem(Linklist*& list, int i, int& data) { int index; Linklist* p; if (!list || !list->next) return false; p = list->next; index = 1; while (p && index < i)//顺链表向后扫描,直到p指向第i个元素或p为空 { p = p->next; index++; } if (!p || index > i) return false; data = p->data; return true; }
查找元素按值查找
bool Lonk_FindElim(Linklist*& list, int data) { int index; Linklist* p; p = list->next; index = 1; if (!list || !list->next) return false; while (p && p->data != data) { p = p->next; index++; } cout << index << endl; if (!p) { index = 0; return false; } return true; }
单链表的遍历
void Linkprint(Linklist* list) { _Linknode* p = NULL; if (!list) { cout << "链表为空。" << endl; return; } p = list->next; while (p) { cout << p->data << "\t"; p = p->next; } cout << endl; }
删除元素
bool LinkDelete(Linklist*& list, int i) { Linklist* p, * q; int index = 0; p = list; while ((p->next) && (index < i - 1))//查找第 i-1 个结点,p 指向该结点 { p = p->next; index++; } if (!p->next || (index > i - 1))//当 i>n 或 i<1 时,删除位置不合理 { return false; } q = p->next; p->next = q->next; delete q; return true; }
销毁单链表
void LinkDestory(Linklist*& list) { //定义临时节点p指向头节点 Linklist* p = list; cout << "销毁链表!" << endl; while (p) { list = list->next;//list 指向下一个节点 delete p; p = list;//p 移向下一个节点 } return; }
main函数
int main(void) { Linklist* list = NULL; _Linknode* s = NULL; //初始化一个空链表 InitList(list); //前插法 for (int i = 0; i < n; i++) { s = new _Linknode; cout << "头部第" << i << "个:"; cin >> s->data; Lestinsert_front(list, s); } Linkprint(list); //尾插法 std::cout << "请输入元素个数 n:"; cin >> n; for (int i = 0; i < n; i++) { s = new _Linknode; cout << "尾部第" << i << "个:"; cin >> s->data; Listinsert_back(list, s); } Linkprint(list); //指定位置插入 for (int i = 0; i < 3; i++) { cout << "插入地址:"; cin >> n; int data = 0; cout << "参数:"; cin >> data; LinkInsert(list, n, data); Linkprint(list); } int element; //获取第二个节点的值 if (Link_GetElem(list, 2, element)) { cout << "获取成功!" << element << endl; } else { cout << "获取失败!" << endl; } //查找值为2的节点 if (Lonk_FindElim(list, 2)) { cout << "查找成功" << endl; } else { cout << "查找失败" << endl; } cout << "删除之后" << endl; LinkDelete(list, 2);//删除 Linkprint(list);//遍历 LinkDestory(list);//销毁 Linkprint(list);//遍历 return 0; }
编译环境:visual studio 2019
注:如有错误请与我联系,万分感谢!!!