c++急速进阶之链表

c++急速进阶之链表

  • 代码
//-----------------------------------【程序说明】----------------------------------------------
//  程序名称::list链表
//  2016年2月 Create by arrstd
//  描述:实现链表的基本操作,c++入门式
//----------------------------------------------------------------------------------------------

#pragma once
#ifndef LIST_H
#define LIST_H

#include<iostream>
using namespace std;

#define NULL 0

struct listnode {
    int data = NULL;
    listnode *next = NULL;
};

class list {
private:
    listnode* head;
public:
    list() { this->head = new listnode; };
    ~list() {};

    int backlength();
    void show();
    void create();
    void create(int);
    void insert(int n,int number);
    void list_delete(int n);
};

/*返回链表的长度,这里还有一点bug*/
int list::backlength()  
{
    int length = 1;
    listnode* q = this->head;
    while (1) {
        if (q->next != NULL) {
            length++;
            q = q->next;
        }
        else break;
    }
    return length;
}

/*删除*/
void list::list_delete(int n)
{
    listnode *front = NULL , *behind = NULL;
    if (n == 1) 
    {
        this->head->data = this->head->next->data;
        this->head->next = this->head->next->next;
    }
    else 
    {
        front = this->head;
        behind = this->head;
        for (int i = 0; i != n; i++)
        {
            behind = behind->next;
        }
        for (int i = 2; i != n; i++)
        {
            front = front->next;
        }
        front->next = behind;
    }
}

/*插入*/
void list::insert(int n,int number)
{
    listnode *p = new listnode;
    listnode *front = NULL , *behind = NULL;
    if (n == 1) 
    {
        p->data = this->head->data;
        this->head->data = number;
        p->next = this->head->next;
        this->head->next = p;
    }
    else
    {
        p->data = number;
        behind = this->head;
        front = this->head;
        for (int i = 1; i != n; i++)
        {
            behind = behind->next;
        }
        for (int i = 2; i != n; i++)
        {
            front = front->next;
        }
        front->next = p;
        p->next = behind;
    }
}

/*输出链表*/
void list::show() {
    listnode* q = this->head;
    for (int i = 0; i != backlength(); i++)
    {
        cout << q->data << " ";
        q = q->next;
    }
    cout << endl;
}

/*创建链表*/
void list::create() {
    if (this->head->data == NULL) { cin >> this->head->data; }
    else {
        listnode* p = new listnode;
        listnode *q = this->head;
        for (int i = 1; i != backlength(); i++)
        {
            q = q->next;
        }
        cin >> p->data;
        q->next = p;
    }
}

void list::create(int number) {
    if (this->head->data == NULL) { this->head->data = number; }
    else {
        listnode* p = new listnode;
        listnode *q = this->head;
        for (int i = 1; i != backlength(); i++)
        {
            q = q->next;
        }
        p->data = number;
        q->next = p;
    }
}

#endif // !LIST_H

c语言的时候也有做过链表,但是很久没碰,所以并不怎么熟悉了,这里着了个最简单的链表,还有太多的缺陷,有机会再修改吧。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值