qq_45481204
种胡萝卜少男
采纳率66.7%
2020-06-15 14:24

int vis[1000]是什么意思

已采纳

int vis[1000]是什么意思?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#pragma warning(disable:4996)

int vis[1000];


typedef struct ltbl
{
  struct ltbl* pPre;/* 指向链表中前一个元素的指针 */
  struct ltbl* pNxt;/* 指向链表中后一个元素的指针 */
  int nKey;/* KEY VALUE*/
  unsigned char ucDummy[32];/*备用*/
}LTBL;

typedef struct mng
{
  int nNodeCnt;/*链表的长度*/
  LTBL* pHead;/*指向链表头的指针*/
}MNG;


int TBLCreate(MNG* pMng, int nCnt) 
{
  memset(vis, 0, sizeof(vis)); //防止随机值重复
  if (!pMng)return -1;
  pMng->nNodeCnt = nCnt;
  LTBL* p = (LTBL*)malloc(sizeof(LTBL)); //创建头节点
  if (!p)return -1;
  int x = 0;
  x = rand() % 1000;
  vis[x] = 1;
  p->nKey = x;
  p->pPre = NULL;
  p->pNxt = NULL;
  pMng->pHead = p;

  for (int i = 1; i < nCnt; ++i) {
    LTBL* temp = (LTBL*)malloc(sizeof(LTBL));
    if (!temp)return -1;
    while (1) { //随机值防止重复
      x = rand() % 1000;
      if (!vis[x]) {
        vis[x] = 1;
        break;
      }
    }
    temp->nKey = x;
    temp->pNxt = NULL;
    temp->pPre = p;//新节点前驱指向当前节点
    p->pNxt = temp;//当前节点的后继为新节点
    p = p->pNxt;//当前节点后移
  }
  return 0;
}

void TBLFree(MNG* pMng) {
  pMng->nNodeCnt = 0;
  LTBL* pre = pMng->pHead;
  while (pre) {
    LTBL* p = pre->pNxt;
    free(pre);
    pre = p;
  }
  pMng->pHead = NULL;
}

LTBL* TBLSearch(const MNG* pMng, int nKey) {
  LTBL* p = pMng->pHead;
  while (p) {
    if (p->nKey == nKey)return p;
    p = p->pNxt;
  }
  return p;
}

int TBLInsert(MNG* pMng, LTBL* pNode) {
  LTBL* p = pMng->pHead;
  LTBL* pre = NULL;
  while (p) {
    if (p->nKey == pNode->nKey)return -1;
    else if (p->nKey < pNode->nKey) {
      pre = p;
      p = p->pNxt;
    }
    else { //按升序排
      pNode->pPre = pre;
      if (pre)pre->pNxt = pNode;//插入节点不会变为头节点
      else pMng->pHead = pNode; //pre为NULL则插入节点变为头节点
      pNode->pNxt = p;
      if(p)p->pPre = pNode;//插入节点不是尾结点
      pMng->nNodeCnt++;
      return 0;
    }
  }
}

int TBLDelete(MNG* pMng, int nKey) {
  LTBL* p = TBLSearch(pMng, nKey); //找到删除节点
  if (!p)return -1;
  LTBL* pre = p->pPre;
  LTBL* next = p->pNxt;
  if (pre)pre->pNxt = next; //删除节点不是头节点
  else pMng->pHead = p->pNxt; //删除节点为头节点
  if (next)next->pPre = pre; //删除节点不是尾节点
  free(p);
  pMng->nNodeCnt--;
  return 0;

}

void TBLSort(MNG* pMng, int nFlg) {
  LTBL* p = NULL;
  LTBL* q = NULL;
  for (p = pMng->pHead; p!=NULL && p->pNxt != NULL; p = p->pNxt) { //冒泡排序
    for (q = p->pNxt; q != NULL; q = q->pNxt) {
      if ((p->nKey < q->nKey) == nFlg) {
        int temp = p->nKey;
        p->nKey = q->nKey;
        q->nKey = temp;
      }
    }
  }
}

void show(MNG* pMng) {
  LTBL* p = pMng->pHead;
  while (p) {
    printf("%d ", p->nKey);
    p = p->pNxt;
  }
  printf("\n");
}

int main() {
  MNG mng;
  mng.pHead = NULL;
  while (1) {
    srand(time(NULL));
    printf("1.新建链表\n");
    printf("2.插入节点\n");
    printf("3.删除节点\n");
    printf("4.查找节点\n");
    printf("5.排序链表\n");
    int choice;
    scanf("%d", &choice);
    if (choice == 6)break;
    switch (choice)
    {
    case 1: {
      printf("请输入想要创建多少节点的链表:\n");
      int cnt;
      scanf("%d", &cnt);
      TBLFree(&mng);
      if (TBLCreate(&mng, cnt) == -1) {
        printf("创建失败,请重试!\n");
      }
      else {
        printf("创建后的链表为:\n");
        show(&mng);
      }
      break;
    }
    case 2: {
      printf("请输入想要插入节点的值:\n");
      LTBL* p = (LTBL*)malloc(sizeof(LTBL));
      p->pNxt = NULL;
      p->pPre = NULL;
      scanf("%d", &p->nKey);
      if (TBLInsert(&mng, p) == -1) {
        printf("插入失败,请重试!\n");
      }
      else {
        printf("插入后的链表为:\n");
        show(&mng);
      }
      break;
    }
    case 3: {
      printf("请输入想要删除节点的值:\n");
      int key;
      scanf("%d", &key);
      if (TBLDelete(&mng, key) == -1) {
        printf("删除失败,请重试!\n");
      }
      else {
        printf("删除后的链表为:\n");
        show(&mng);
      }
      break;
    }
    case 4: {
      printf("请输入想要查找节点的值:\n");
      int key;
      scanf("%d", &key);
      if (!TBLSearch(&mng, key)) {
        printf("没有该节点\n");
      }
      else {
        printf("该节点存在\n");
      }
      break;
    }
    case 5: {
      printf("请输入想要排序的方式(0:升序,1:降序):\n");
      int flag;
      scanf("%d", &flag);
      TBLSort(&mng, flag);
      printf("排序后的链表为:\n");
      show(&mng);
      break;
    }
    default:
      break;
    }
  }
  return 0;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 1年前

    vis是一个全局的数组,目的是为了判断是否生成过某个随机数
    因为要生成不重复的随机数。

    问题解决的话,请点下采纳

    点赞 评论 复制链接分享
  • zz547885520 zz547885520 1年前

    int vis[1000]的意思是记录标记

    点赞 1 评论 复制链接分享

相关推荐