数据结构学习笔记(1)---单向链表

来源:互联网 发布:unity3d游戏开发 pdf 编辑:程序博客网 时间:2024/06/03 23:43
数据结构学习笔记—单向链表
  • 链表的初始化
  • 头插法创建链表
  • 尾插发创建链表
  • 链表的显示
  • 按索引值查找节点
  • 按索引值删除节点
  • 按索引值添加节点
(1)链表的初始化
void InitList(pListNode *pHead){    *pHead = new ListNode;    (*pHead)->next  = NULL;}
(2)头插法创建链表
void HeadCreatList(pListNode pHead){    int date;    pListNode pNew = NULL;    cout 
(3)尾插法创建链表
void TailCreatList(pListNode pHead){    int date;    pListNode pTail = NULL;    pTail = pHead;    cout 
#####(4)显示链表节点
void ShowList(pListNode pHead){    pListNode p = pHead->next ;    while (p)    {        cout 
(5)按索引值查找节点
pListNode FindByIndex(pListNode pHead,int index){    pListNode p = pHead->next;    int i = 0;    while (p && i
(6)按索引值删除节点
void DeleteByIndex(pListNode pHead, int index){    if (index == 0)    {        pListNode pNew = pHead->next;        pHead->next = pHead->next->next;        delete pNew;        return;    }    pListNode pNew = FindByIndex(pHead,index-1);    if (!pNew)    {        return;    }    pListNode de = pNew->next;    pNew->next = pNew->next->next;    delete de;}
(7)按索引值添加节点
void AddByIndex(pListNode pHead, int index,int date){    if (0 == index )    {        pListNode p = new ListNode;        p->date = date;        p->next = pHead->next;        pHead->next = p;        return;    }    pListNode pNew = FindByIndex(pHead, index-1);    if (!pNew)    {        return;    }    pListNode p = new ListNode;    p->next = NULL;    p->date = date;    p->next = pNew->next;    pNew->next = p;}
对于上面的按索引值相当于数组 ,即第一个存有有效数据的节点的索引值为0,
原创粉丝点击