当前位置 : 主页 > 编程语言 > 其它开发 >

伪链表的相关操作

来源:互联网 收集:自由互联 发布时间:2022-05-30
#include stdio.h #include stdlib.h struct Node { int ShuJu; //数据 struct Node* pNext; //连接下一个节点的地址 }; void Look_ShuJu(struct Node* pHead); //查看数据 struct Node* FindDate(struct Node* pHead, int iDate); //查找数

#include <stdio.h>
#include <stdlib.h>

struct Node
{
  int ShuJu; //数据
  struct Node* pNext; //连接下一个节点的地址
};

void Look_ShuJu(struct Node* pHead);   //查看数据
struct Node* FindDate(struct Node* pHead, int iDate);    //查找数据
struct Node* FindIndex(struct Node* pHead, int iIndex);    //根据下标查找数据
void ShowDate(struct Node* pNode);   //输出查找结果
int Count(struct Node* pHead, int iDate);    //统计指定数据的数量
void GetNode(struct Node* pHead, struct Node* arr[4], int iDate);  //根据数据得到对应的节点
void Change_ShuJu(struct Node* pHead, int i, int changeI);   //修改数据 //地址;要修改的数据;修改为

int main(void)
{
  struct Node a = { 123,NULL },
         b = { 2,NULL },
                           c = { 888,NULL },
                           d = { 398,NULL };
  a.pNext = &b;
  b.pNext = &c;
  c.pNext = &d;

  //printf("%d\n", a.ShuJu);
  //printf("%d\n", a.pNext ->ShuJu );
  //printf("%d\n", a.pNext ->pNext ->ShuJu );
  //printf("%d\n", a.pNext->pNext ->pNext ->ShuJu);

  //struct Node* pHead = &a;
  //printf("%d\n", pHead->ShuJu);

  //pHead = pHead->pNext;
  //printf("%d\n", pHead->ShuJu);

  //pHead = pHead->pNext;
  //printf("%d\n", pHead->ShuJu);

  //pHead = pHead->pNext;
  //printf("%d\n", pHead->ShuJu);

  //Look_ShuJu(&a); //查看数据

  //struct Node* pFind=FindDate(&a, 3); //查找数据
  //if (pFind == NULL)
  // printf("未找到节点\n");
  //else
  // printf("已找到节点:%d\n", pFind->ShuJu );

  //struct Node* pFind=FindIndex(&a, 2); //按下标查找数据
  //if (pFind == NULL)
  // printf("未找到该下标对应的节点\n");
  //else
       // printf("已找到该下标对应的节点:%d\n", pFind->ShuJu );

  //printf("出现的次数为:%d\n",Count(&a, 2) ); //统计指定数据的数量

  //ShowDate(FindIndex(&a, 2)); //显示输出函数

  //struct Node* arr[4] = { NULL };
  //GetNode(&a, arr, 2);
  //for (int i = 0; i < 4 && arr[i] != NULL; i++)
  //{
    // printf("数据:%d\n", arr[i]->ShuJu);
  //}

  ////头添加
  //struct Node A = { 111,&a };
  //Look_ShuJu(&A);

  ////尾添加
  //struct Node B = { 999,NULL };
  //d.pNext = &B;
  //Look_ShuJu(&A);

  ////中间添加
  //struct Node C = { 555,&c };
  //b.pNext = &C;
  //Look_ShuJu(&A);

  ////删除头
  //a.pNext = NULL;
  //Look_ShuJu(&b);

  ////删除尾
  //c.pNext = NULL;
  //Look_ShuJu(&a);

  ////删除中间
  //c.pNext = NULL;
  //b.pNext = &d;
  //Look_ShuJu(&a);

  ////修改数据
  //Look_ShuJu(&a);
  //printf("修改后:\n");
  //Change_ShuJu(&a, 2, 222);
  //Look_ShuJu(&a);

  system("pause>0");
  return 0;
}

void Look_ShuJu(struct Node* pHead)
{
  while (pHead != NULL)
 {
   printf("%d\n", pHead->ShuJu);
   pHead = pHead->pNext; //达到连接的作用
 }
}

struct Node* FindDate(struct Node* pHead, int iDate)
{
  while (pHead != NULL)
   {
    if (pHead->ShuJu == iDate)
    return pHead; //找到数据
    pHead = pHead->pNext;
   }
  return NULL; //未找到数据

}

struct Node* FindIndex(struct Node* pHead, int iIndex)
{
  int iNum=0; //从0开始是以下标查找数据,1开始是第几个数据
  while (pHead != NULL)
   {
    if (iNum == iIndex)
     {
    return pHead;
     }
  iNum++;
  pHead = pHead->pNext;
   }
  return NULL;
}

void ShowDate(struct Node* pNode)
{
  if (pNode == NULL)
  printf("未找到此节点\n");
  else
  printf("已找到此节点为:%d\n", pNode->ShuJu);
}

int Count(struct Node* pHead, int iDate)
{
  int iCount = 0;
  while (pHead != NULL)
   {
  if (pHead->ShuJu == iDate)
  iCount++;
  pHead = pHead->pNext;
   }
  return iCount;
}

void GetNode(struct Node* pHead, struct Node* arr[4], int iDate)
{
  int i = 0;
  while (pHead != NULL)
   {
     if (iDate == pHead->ShuJu)
  {
      arr[i++] = pHead;
      //arr[i] = pHead; //等效
           //i++;
  }
  pHead = pHead->pNext;
  }
}

void Change_ShuJu(struct Node* pHead, int i, int changeI)
{
  while (pHead != NULL)
  {
   if (pHead->ShuJu == i)
  {
   pHead->ShuJu = changeI;
        }
   pHead = pHead->pNext;
  }
}

上一篇:Idea常用快捷键 已收录156条常用快捷键
下一篇:没有了
网友评论