c++里的链表

#include "stdafx.h"

#include<Windows.h>

#include<iostream>

using namespace std;

struct  sStudent

{

        sStudent()

        {

               iId = 0;

               //memset(szName, 0, sizeof(szName));

               iGrade = 0;

               pNext = nullptr;

        }

        int iId;

        //char szName[32];

        int iGrade;

        sStudent* pNext;

};

//创建链表

void CreateList(sStudent* pHeadNode,int iSize);

//遍历链表

void ViewList(sStudent* pHead);

//查找链表

sStudent* FindList(sStudent* pHead, int iIndex);

//删除链表

void DeleteList(sStudent*& pHead, int iIndex);

//插入链表

void InsertList(sStudent*& pHead, int iIndex);

//链表翻转

void ReversalList(sStudent*& pHead);

void ReversalList2(sStudent*& pHead);

sStudent* pHeadNode = new sStudent;//头结点

int main()

{

        pHeadNode->iId = 1;

        pHeadNode->iGrade = 1;

        int iLen = 0;

        int iFindLen = 0;

        int iDeleteLen = 0;

        int iInsertLen = 0;

        cout << "输入创建链表的长度:" << endl;

        cin >> iLen;

        CreateList(pHeadNode,iLen);

        /*cout << "输入查找链表的id:";

        cin >> iFindLen;

        sStudent* pFindNode= FindList(pHeadNode, iFindLen);

        if (nullptr != pFindNode)

        {

               cout << pFindNode->iId << "\t" << pFindNode->iGrade << endl;

        }

        else

        {

               cout << "找不到信息" << endl;

        }*/

        //cout << "所有链表的信息:" << endl;

        //ViewList(pHeadNode);

        //cout << "输入要删除的节点序号";

        //cin >> iDeleteLen;

        //DeleteList(pHeadNode, iDeleteLen);

        //cout << "所有链表的信息:" << endl;

        //ViewList(pHeadNode);

        /*cout << "输入要插入节点的位置";

        cin >> iInsertLen;

        InsertList(pHeadNode, iInsertLen);

        ViewList(pHeadNode);*/

        ReversalList2(pHeadNode);

        cout << "翻转后的所有链表的信息:" << endl;

        ViewList(pHeadNode);

        system("pause");

    return 0;

}

void CreateList(sStudent* pHeadNode, int iSize)

{       

        sStudent* pNextNode = pHeadNode;//下一节点

        int i = 1;

        int iCount = 1;

        while (i<iSize)

        {

               sStudent* pNewNode = new sStudent;

               cout << "输入第" << iCount << "节点的id" << endl;

               cin >> pNewNode->iId;

               cout << "输入第" << iCount << "节点的成绩" << endl;

               cin >> pNewNode->iGrade;

               pNextNode->pNext = pNewNode;

               pNextNode = pNewNode;

               i++;

               iCount++;

        }

}

void ViewList(sStudent* pHead)

{

        //当前节点,默认为头结点

        sStudent* pCurNode = pHead;

        int iCount=0;

        while (nullptr != pCurNode)

        {

               cout << "第" << iCount << "个节点"<<endl;

               cout << pCurNode->iId << "\t" << pCurNode->iGrade << endl;

               pCurNode = pCurNode->pNext;

               iCount++;

        }

}

sStudent* FindList(sStudent * pHead, int iIndex)

{

        sStudent* pCurNode =pHead;

        int iCount = 0;

        while (nullptr!=pCurNode)

        {

               if (iIndex == iCount)

               {

                       return pCurNode;

               }

               pCurNode = pCurNode->pNext;

               iCount++;

        }

        return nullptr;

}

void DeleteList(sStudent *& pHead, int iIndex)

{

        if (0 == iIndex)

        {

               pHead = pHead->pNext;

               delete pHead;

               pHead = nullptr;

               return;

        }

        else

        {

               //要删除的节点

               sStudent* pCurNode = FindList(pHead, iIndex);

               //要删除的上一个节点

               sStudent* pCurLastNode = FindList(pHead, iIndex - 1);

               //将要删除的下一个节点赋值给要删除的上一个节点的pNext;

               if (nullptr != pCurNode)

               {

                       pCurLastNode->pNext = pCurNode->pNext;

                       //删除节点

                       delete pCurNode;

                       pCurNode = nullptr;

               }

               else

               {

                       cout << "未找到该序号";

               }

        }

}

void InsertList(sStudent *& pHead, int iIndex)

{

        sStudent* pNewNode = new sStudent;

        cout << "要插入节点的信息";

        cin >> pNewNode->iId;

        cin >> pNewNode->iGrade;

        if (0 == iIndex)

        {

               pNewNode->pNext = pHead;

               pHead = pNewNode;

               return;

        }

        else

        {

               sStudent* pCurNode = FindList(pHead, iIndex - 1);

               if (nullptr != pCurNode)

               {

                       pNewNode->pNext = pCurNode->pNext;

                       pCurNode->pNext = pNewNode;

               }

               else

               {

                       cout << "插入失败";

               }

        }

}

void ReversalList(sStudent *& pHead)

{

        //头结点

        sStudent* pHeadNode=pHead;

        //头结点

        sStudent* pHeadNode2 = pHead;

        //头结点

        sStudent* pHeadNode3 = pHead;

        //当前节点

        sStudent* pCurNode = pHead;

        //最后一个节点

        sStudent* pLastNode =new sStudent;

        //倒数第二个节点

        sStudent* pLastNode2 = new sStudent;

        int i = 0;

        //从头结点开始一直往后找,直到找到最后一个节点.当最初的头结点的pNext指向翻转后的节点时,循环结束

        while(nullptr != pHeadNode->pNext)

        {

               while (nullptr != pCurNode->pNext)

               {

                       pLastNode2 = pCurNode;

                       pLastNode  = pCurNode->pNext;

                       pCurNode = pCurNode->pNext;

               }

               pCurNode = pHeadNode;

               if (0 == i)

               {

                       pLastNode->pNext = pHeadNode;

                       pHeadNode2 = pLastNode;

                       pHeadNode3 = pLastNode;

               }

               else

               {

                       //此时将最后一个节点插入到头结点之前  

                       pLastNode->pNext = pHeadNode;

                       //重定义的头结点pNext指向插入头结点的最后一个节点

                       pHeadNode2->pNext = pLastNode;

                       //重定义的头结点变成已插进头结点之的节点

                       pHeadNode2 = pLastNode;

               }

               //倒数第二个节点置空

               pLastNode2->pNext = nullptr;

               i++;

        }

        cout << i;#include "stdafx.h"

#include<Windows.h>

#include<iostream>

using namespace std;

struct  sStudent

{

        sStudent()

        {

               iId = 0;

               //memset(szName, 0, sizeof(szName));

               iGrade = 0;

               pNext = nullptr;

        }

        int iId;

        //char szName[32];

        int iGrade;

        sStudent* pNext;

};

//创建链表

void CreateList(sStudent* pHeadNode,int iSize);

//遍历链表

void ViewList(sStudent* pHead);

//查找链表

sStudent* FindList(sStudent* pHead, int iIndex);

//删除链表

void DeleteList(sStudent*& pHead, int iIndex);

//插入链表

void InsertList(sStudent*& pHead, int iIndex);

//链表翻转

void ReversalList(sStudent*& pHead);

void ReversalList2(sStudent*& pHead);

sStudent* pHeadNode = new sStudent;//头结点

int main()

{

        pHeadNode->iId = 1;

        pHeadNode->iGrade = 1;

        int iLen = 0;

        int iFindLen = 0;

        int iDeleteLen = 0;

        int iInsertLen = 0;

        cout << "输入创建链表的长度:" << endl;

        cin >> iLen;

        CreateList(pHeadNode,iLen);

        /*cout << "输入查找链表的id:";

        cin >> iFindLen;

        sStudent* pFindNode= FindList(pHeadNode, iFindLen);

        if (nullptr != pFindNode)

        {

               cout << pFindNode->iId << "\t" << pFindNode->iGrade << endl;

        }

        else

        {

               cout << "找不到信息" << endl;

        }*/

        //cout << "所有链表的信息:" << endl;

        //ViewList(pHeadNode);

        //cout << "输入要删除的节点序号";

        //cin >> iDeleteLen;

        //DeleteList(pHeadNode, iDeleteLen);

        //cout << "所有链表的信息:" << endl;

        //ViewList(pHeadNode);

        /*cout << "输入要插入节点的位置";

        cin >> iInsertLen;

        InsertList(pHeadNode, iInsertLen);

        ViewList(pHeadNode);*/

        ReversalList2(pHeadNode);

        cout << "翻转后的所有链表的信息:" << endl;

        ViewList(pHeadNode);

        system("pause");

    return 0;

}

void CreateList(sStudent* pHeadNode, int iSize)

{       

        sStudent* pNextNode = pHeadNode;//下一节点

        int i = 1;

        int iCount = 1;

        while (i<iSize)

        {

               sStudent* pNewNode = new sStudent;

               cout << "输入第" << iCount << "节点的id" << endl;

               cin >> pNewNode->iId;

               cout << "输入第" << iCount << "节点的成绩" << endl;

               cin >> pNewNode->iGrade;

               pNextNode->pNext = pNewNode;

               pNextNode = pNewNode;

               i++;

               iCount++;

        }

}

void ViewList(sStudent* pHead)

{

        //当前节点,默认为头结点

        sStudent* pCurNode = pHead;

        int iCount=0;

        while (nullptr != pCurNode)

        {

               cout << "第" << iCount << "个节点"<<endl;

               cout << pCurNode->iId << "\t" << pCurNode->iGrade << endl;

               pCurNode = pCurNode->pNext;

               iCount++;

        }

}

sStudent* FindList(sStudent * pHead, int iIndex)

{

        sStudent* pCurNode =pHead;

        int iCount = 0;

        while (nullptr!=pCurNode)

        {

               if (iIndex == iCount)

               {

                       return pCurNode;

               }

               pCurNode = pCurNode->pNext;

               iCount++;

        }

        return nullptr;

}

void DeleteList(sStudent *& pHead, int iIndex)

{

        if (0 == iIndex)

        {

               pHead = pHead->pNext;

               delete pHead;

               pHead = nullptr;

               return;

        }

        else

        {

               //要删除的节点

               sStudent* pCurNode = FindList(pHead, iIndex);

               //要删除的上一个节点

               sStudent* pCurLastNode = FindList(pHead, iIndex - 1);

               //将要删除的下一个节点赋值给要删除的上一个节点的pNext;

               if (nullptr != pCurNode)

               {

                       pCurLastNode->pNext = pCurNode->pNext;

                       //删除节点

                       delete pCurNode;

                       pCurNode = nullptr;

               }

               else

               {

                       cout << "未找到该序号";

               }

        }

}

void InsertList(sStudent *& pHead, int iIndex)

{

        sStudent* pNewNode = new sStudent;

        cout << "要插入节点的信息";

        cin >> pNewNode->iId;

        cin >> pNewNode->iGrade;

        if (0 == iIndex)

        {

               pNewNode->pNext = pHead;

               pHead = pNewNode;

               return;

        }

        else

        {

               sStudent* pCurNode = FindList(pHead, iIndex - 1);

               if (nullptr != pCurNode)

               {

                       pNewNode->pNext = pCurNode->pNext;

                       pCurNode->pNext = pNewNode;

               }

               else

               {

                       cout << "插入失败";

               }

        }

}

void ReversalList(sStudent *& pHead)

{

        //头结点

        sStudent* pHeadNode=pHead;

        //头结点

        sStudent* pHeadNode2 = pHead;

        //头结点

        sStudent* pHeadNode3 = pHead;

        //当前节点

        sStudent* pCurNode = pHead;

        //最后一个节点

        sStudent* pLastNode =new sStudent;

        //倒数第二个节点

        sStudent* pLastNode2 = new sStudent;

        int i = 0;

        //从头结点开始一直往后找,直到找到最后一个节点.当最初的头结点的pNext指向翻转后的节点时,循环结束

        while(nullptr != pHeadNode->pNext)

        {

               while (nullptr != pCurNode->pNext)

               {

                       pLastNode2 = pCurNode;

                       pLastNode  = pCurNode->pNext;

                       pCurNode = pCurNode->pNext;

               }

               pCurNode = pHeadNode;

               if (0 == i)

               {

                       pLastNode->pNext = pHeadNode;

                       pHeadNode2 = pLastNode;

                       pHeadNode3 = pLastNode;

               }

               else

               {

                       //此时将最后一个节点插入到头结点之前  

                       pLastNode->pNext = pHeadNode;

                       //重定义的头结点pNext指向插入头结点的最后一个节点

                       pHeadNode2->pNext = pLastNode;

                       //重定义的头结点变成已插进头结点之的节点

                       pHeadNode2 = pLastNode;

               }

               //倒数第二个节点置空

               pLastNode2->pNext = nullptr;

               i++;

        }

        cout << i;

        pHead = pHeadNode3;

}

void ReversalList2(sStudent *& pHead)

{

        sStudent* pNextNode = pHead->pNext;

        sStudent* pNextNode2 = pHead->pNext->pNext;

        pHead->pNext = nullptr;

        pNextNode->pNext = pHead;

        pHead = pNextNode;

        while (nullptr != pNextNode2)

        {

        pNextNode = pNextNode2;

        pNextNode2 = pNextNode2->pNext;

        pNextNode->pNext = pHead;

        pHead = pNextNode;

        }

}

        pHead = pHeadNode3;

}

void ReversalList2(sStudent *& pHead)

{

        sStudent* pNextNode = pHead->pNext;

        sStudent* pNextNode2 = pHead->pNext->pNext;

        pHead->pNext = nullptr;

        pNextNode->pNext = pHead;

        pHead = pNextNode;

        while (nullptr != pNextNode2)

        {

        pNextNode = pNextNode2;

        pNextNode2 = pNextNode2->pNext;

        pNextNode->pNext = pHead;

        pHead = pNextNode;

        }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值