C++实现简单的链表

  • 创建链表:
    book *creat()//创建链表
    {
        book *head = NULL,*tail=NULL;
        int _num;
        float _price;
        while (1)
        {
            cout << "输入书编号:(-1表示输入结束)" << endl;
            cin >> _num;
            if (_num >= 0)
            {
                book *p = new book;
                p->num = _num;
                cout << "输入书价格:(-1表示输入结束)" << endl;
                cin >> _price;
                p->price = _price;
                //p->next = NULL;//如果在结构体里没有默认值,就有这句
                if (head==NULL)
                {
                    head = p;
                    tail = p;
                }
                else
                {
                    tail->next = p;
                    tail = p;
                }
            }
            else if (_num == -1)
            {
                return head;
            }
            else
            {
                cout << "输入有误,请重新输入!" << endl;
                //待写  清理缓存区
            }
        }
        return head;
    }
  • 显示链表:
    void Show(book *head)//链表显示
    {
        while (head)
        {
            cout << "图书编号:"<<head->num <<"  图书价格:"<< head->price << endl;
            head = head->next;
        }
    }
  • 删除结点
    book* Delete(book *head,int num)//(每次删除一个结点)返回头结点的指针,因为如果删除了头结点,就得将删除后的头结点返回出来
    {
        book *cur = head;
        if (head->num==num)
        {
            head = head->next;
            delete cur;
            cout << "操作成功!" << endl;
            return head;
        }
        while (cur->next)
        {
            if (cur->next->num==num)
            {
                book *del = cur->next;
                cur->next = del->next;
                delete del;
                cout << "操作成功!" << endl;
                return head;
            }
            else
            {
                cur = cur->next;
            }    
        }
        cout << "没找到!" << endl;
        return head;
    }
  • 插入结点
    • 尾插法:
      book*insert(book *headint num)//返回头结点  因为有可能插在第一个
      {
          book *p = new book,*cur=head;
          cin >> p->num >> p->price;
          while (cur->next)
          {
              cur = cur->next;
          }
          cur->next = p;
          return head;
      }
    • 头插法:
      book*insert(book *headint num)//返回头结点  因为有可能插在第一个
      {
          book *p = new book,*cur=head;
          cin >> p->num >> p->price;
          head = p;
          head->next = cur;
          return head;
      }
    • 中间插法:
      book*insert(book *headint num)//返回头结点  因为有可能插在第一个
      {
          book *p = new book,*cur=head;
          cin >> p->num >> p->price;
          if (p->num <=head->num)//头部插入
          {
              head = p;
              head->next = cur;
              return head;
          }
          while (1)
          {
              if (cur->next == NULL)//尾部插入
              {
                  cur->next = p;
                  return head;
              }
              if (cur->next->num>p->num)//中间插入
              {
                  p->next = cur->next;
                  cur->next = p;
                  return head;
              } 
              cur = cur->next;
          }
      }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值