单向链表实现源代码

来源:互联网 发布:安卓微信聊天恢复软件 编辑:程序博客网 时间:2024/06/11 12:51

下面是我实现的一个简单的链表,回顾了以往的知识点,不足之处,敬请提出宝贵意见,共同进步!!!

 

#include <iostream>
using namespace std;

//0、链表结构

struct MyLinkData
{
 int RoleID;
 MyLinkData *next;
};

typedef struct MyLinkData LinkStruct;

typedef LinkStruct* pLink;

//1、生成链表

pLink CreateLink()
{
 pLink pHead, pMiddle, pTail;//头结点、中间结点、尾结点
 int Volue;
 cout << "请您随便输入一个数值,输入0意味终止..." << endl;
 cin >> Volue;

 pHead = new(LinkStruct);

 if (pHead == NULL)
 {
  cout << "分配头结点内存失败" << endl;
 }

 if (Volue == 0)
 {
  pHead->RoleID = Volue;
  pTail = pHead;
  pTail->next = NULL;

  return pHead;
 }
 else
 {
  
  pHead->RoleID = Volue;
  pTail = pHead;

  cout << "请您继续输入自然值,输入0意味结束..." << endl;
  cin >> Volue;

  while (Volue)
  {
   pMiddle = new(LinkStruct);

   if (pMiddle == NULL)
   {
    cout << "分配中间结点失败..." << endl;
    pTail->next = NULL;
    return pHead;
   }
   else
   {
    pMiddle->RoleID = Volue;
    pMiddle->next = NULL;
    pTail->next = pMiddle;
    pTail = pMiddle;
    cout << "请您继续输入...,以0结束输入..." << endl;
    cin >> Volue;
   }
  }

  //pTail->RoleID = 0;
  pTail->next = NULL;
  return pHead;
 }
}

//2、输出链表

void printCout(pLink pHead)
{
 pLink pointer;

 cout << endl;
 cout << endl;
 cout << endl;
 cout << "输出整个链表的数值:" << endl;
 cout << endl;
 cout << endl;
 cout << endl;

 while (pHead != NULL)
 {
  pointer = pHead;
  pHead = pHead->next;

  cout << pointer->RoleID << endl;
 }

 cout << "链表输出完毕,退出该函数..." << endl;
}

//3、链表的释放

void FreeLink(pLink pHead)
{
 pLink pointer;

 while (pHead)
 {
  pointer = pHead;
  pHead = pHead->next;

  free(pointer);
 }

 cout << "释放链表成功!!!" << endl;
}

//4、查找链表
void CheckLink(pLink pHead,int Volude)
{
 pLink pointer;

 while(pHead != NULL)
 {
  pointer = pHead;
  pHead = pHead->next;

  if (pointer->RoleID == Volude)
  {
   cout << "***在链表中存有该数据***" << endl;
   break;
  }
 }

  getchar();
}

//5、修改链表数据值
void ReplaceLinkKey(pLink pHead, int Volude, int TrueVolue)
{
 pLink pointer;

 while(pHead != NULL)
 {
  pointer = pHead;
  pHead = pHead->next;

  if (pointer->RoleID == Volude)
  {
   pointer->RoleID = TrueVolue;
   cout << "替换数据成功!!!" << endl;
  }
 }
}

//6、链表的插入
pLink InsertLink(pLink pHead ,int Volue)
{
 cout << "演示链表的插入操作..." << endl;
 pLink pTail,pMiddle,pTemp;
 int _Volue;
 //pTail = pHead;
 pTemp = pHead;
 while (pHead != NULL)
 { 
  pTail = pHead;
  
  //(1)头结点的插入
  if (NULL == pHead->next)
  {
   pMiddle = new(LinkStruct);
   if (pMiddle == NULL)
   {
    cout << "申请存储单元失败..." << endl;
    break;
   }
   else
   {
    cout << "请您输入要插入的数据:" << endl;
    cin>>_Volue;
    pMiddle->RoleID = _Volue;
    pMiddle->next = pHead;
    pHead = pMiddle;
    return pHead;
   }
  }

  //(2)中间结点的插入
  if (pTail->RoleID == Volue)
  {
   pMiddle = new (LinkStruct);

   if (pMiddle == NULL)
   {
    cout << "申请存储单元失败..." << endl;
    return pHead;
   }
   else
   {
    cout << "请您插入要插入的数值:" << endl;
    cin >> _Volue;
    pMiddle->RoleID = _Volue;

    pMiddle->next = pTail->next;
    pTail->next = pMiddle;
    return pTemp;
   }
  }

  pHead = pHead->next;
 }
}

//7、链表的删除
pLink DelLink(pLink pHead, int Volue)
{
 pLink pointer,pTempfirst,pTemp;
 
 pTempfirst = pHead;

 while (pHead != NULL)
 {
  pointer = pHead;
  
  //(1)删除头结点

  if (pTempfirst->RoleID == Volue)
  {
   // a、只有前驱,没有后继
   if (pTempfirst->next == NULL)
   {
    free(pTempfirst);
    pTempfirst = NULL;
    return pTempfirst;
   }
   // b、有后继
   else
   {
    pHead = pTempfirst->next;
    free(pTempfirst);
    return pHead;
   }
  }

  //(2)删除其他结点
  if (pointer->RoleID == Volue)
  {
   pTemp->next = pointer->next;
   free(pointer);
   cout << "删除指定结点成功~~~" << endl;
   return pTempfirst;
  }
  pTemp = pHead;
  pHead = pHead->next;
 }

 return pTempfirst;
}


int main()
{
 pLink pHead,pMiddle;
 pHead = CreateLink();
 printCout(pHead);
 //CheckLink(pHead,200);

 //ReplaceLinkKey(pHead,2,200);
 //printCout(pHead);

 /*pHead = InsertLink(pHead,3);
 printCout(pHead);*/
 cout << "---------------------------------------------------------" << endl;
 pHead = DelLink(pHead,2);
 printCout(pHead);
 getchar();
 FreeLink(pHead);
 getchar();
 return 0;
}

原创粉丝点击