链表的实现(C++)

复习数据结构时把6年前自己读代码写的分析再读一遍,收获颇多

源码内容:建立一个链表并且对其进行“插入值”和“删除值”

· 链表的基本分析 ·

//链表的默认大小
const int Capacity = 1024;
template <typename T>
class arrayList
{
public:
    //建立链表,定义初始化
    arrayList();
    //判空
    bool empty() const;
    //链表大小,结点个数
    int size() const;
    //在pos处添加值item
    void insert(T item, int pos);
    //删除pos处的值
    void erase(int pos);
    //重载“<<”输出符
    friend ostream &operator<<(ostream &out, const arrayList &aList);

private:
    //输入实际大小
    int mySize;
    //私有变量建立默认大小的链表
    T myArray[Capacity];
};

//初始化定义实际大小
arrayList::arrayList() : mySize(0)
{
}

//判空,大小为0为false
bool arrayList::empty() const
{
    return mySize == 0;
}

//赋值实际大小
int arrayList::size() const
{
    return mySize;
}

//输出操作符重载
ostream &operator<<(ostream &out, const List &aList)
{
    for (int i = 0; i < mySize; i++)
        out << alist.myArray[i] << "  ";
    return out;
}

== == == == == == == == == == == == == == == == == == == =
//输出的举例
    void arrayList::display(ostream & out) const
{
    for (int i = 0; i < mySize; i++)
        out << myArray[i] << "  ";
}
ostream &operator<<(ostream &out, const List &aList)
{
    aList.display(out);
    return out;
}
== == == == == == == == == == == == == == == == == == == =

//插入值
void List::insert(T item, int pos)
{
    //如果实际大小等于默认大小就会由于插不进去抛错
    if (mySize == Capacity)
    {
        //引用D_select.h的头文件
        cerr << "*** No space for list element -- terminating "
                "execution ***\n";
        exit(1);
    }
    //再判断插入位置是否有问题/抛错
    if (pos < 0 || pos > mySize)
    {
        cerr << "*** Illegal location to insert -- " << pos
             << ".  List unchanged. ***\n";
        return;
    }
    //从后开始检测
    for (int i = mySize; i > pos; i--)
    {
        //每个元素往后移动一位,将pos位处腾空
        myArray[i] = myArray[i - 1];
    }
    //把值放进pos处
    myArray[pos] = item;
    //更改实际大小的数值
    mySize++;
}

//删除pos位置的值
void arrayList::erase(int pos) 
{
    //判空提示
    if (mySize == 0) 
    {
        cerr << "*** List is empty ***\n";
        return;
    }

    //检测位置抛错
    if (pos < 0 || pos >= mySize) 
    {
        cerr << "Illegal location to delete -- " << pos
             << ".  List unchanged. ***\n";
        return;
    }

    //直接找到这个位置,然后循环处理,每后边的一个数值往前提一位,直接覆盖了原数值以便达到要求
    for (int i = pos; i < mySize; i++) 
        myArray[i] = myArray[i + 1];
    //改变实际大小的数值
    mySize--; 
}

 

约瑟夫环(Josephus Problem)是一个经典的算法问题,通常用于演示递归和循环的概念。在面向对象编程中,我们可以用C++实现一种类结构,比如一个`JosephusLinkedList`,它代表环中的节点,包含了下一个节点的引用以及一个计数器,模拟环中的淘汰过程。 以下是一个简单的C++实现约瑟夫环面向对象链表的步骤: ```cpp #include <iostream> class Node { public: int value; // 节点值 Node* next; // 指向下一个节点的指针 Node(int val) : value(val), next(nullptr) {} // 构造函数 }; class JosephusLinkedList { private: Node* head; // 链表头节点 int step; // 每次淘汰步长 public: JosephusLinkedList(int n, int k) : head(new Node(0)), step(k) { for (int i = 1; i < n; ++i) { Node* newNode = new Node(i); newNode->next = head; head = newNode; } head->next = head; // 形成环 } void eliminate() { if (!head || !head->next) return; // 链表为空或只有一个元素 Node* temp = head; for (int i = 0; i < step - 1; ++i) { temp = temp->next; } Node* victim = temp->next; // 计算淘汰节点 delete victim; eliminate(); // 递归调用,直到链表为空 } // 其他可能的方法,如遍历、添/删除节点等 ~JosephusLinkedList() { Node* current = head; while (current) { Node* nextTemp = current->next; delete current; current = nextTemp; } } // 虚拟析构函数,确保正确释放内存 }; int main() { JosephusLinkedList list(10, 3); // 创建10个节点,步长为3 list.eliminate(); // 执行约瑟夫环算法 return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值