LinkList 实现( c++)

 概述

链表是物理存储单元上非连续的、非顺序的存储结构,数据元素的逻辑顺序是通过链表的指针地址实现,有一系列结点(地址)组成,结点可动态的生成。

结点包括两个部分:

  1. 存储数据元素的数据域(内存空间),
  2. 存储指向下一个结点地址的指针域。

相对于线性表顺序结构,操作复杂。


代码实现

#ifndef LINKLIST_H_
#define LINKLIST_H_
#include "List.h"
#include "Object.h"
#include "Exception.h"

namespace MYLIB{

template<typename T>
class LinkList:public List<T>
{
protected:
    class Node:public Object
    {
        // 单链表的节点
    public:
        T data;
        Node* next;
    };

    mutable struct : public OBject
    {
        // 采用匿名的方式构造头节点
        // 设计使得结构上和 node 完全相同,但是不调用 node 的构造函数
        char reserved[sizeof(T)];
        Node* next;
    }m_head;
    
    int m_length;
    Node* m_current;
    int m_step;

    Node* position(i) const
    {
     
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++链表实现进程管理的示例代码: ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; struct Process { string proname; int time, pri; bool status; }; struct Node { Process val; Node* next; Node(Process x) : val(x), next(NULL) {} }; class Linklist { public: Node* head; Linklist(); void Insert(Process val, int pos); void Remove(Process val); void Print(); int Find(Process val); int Length(); ~Linklist(); private: int length; }; Linklist::Linklist() { head = NULL; length = 0; } void Linklist::Insert(Process val, int pos) { if (pos < 0 || pos > length) { cout << "Invalid position!" << endl; return; } Node* newNode = new Node(val); if (pos == 0) { newNode->next = head; head = newNode; } else { Node* curr = head; for (int i = 0; i < pos - 1; i++) { curr = curr->next; } newNode->next = curr->next; curr->next = newNode; } length++; } void Linklist::Remove(Process val) { Node* curr = head; Node* prev = NULL; while (curr != NULL) { if (curr->val.proname == val.proname) { if (prev == NULL) { head = curr->next; } else { prev->next = curr->next; } delete curr; length--; return; } prev = curr; curr = curr->next; } cout << "Process not found!" << endl; } void Linklist::Print() { Node* curr = head; while (curr != NULL) { cout << "Process Name: " << curr->val.proname << endl; cout << "Priority: " << curr->val.pri << endl; cout << "Running Time: " << curr->val.time << endl; cout << "Status: " << (curr->val.status ? "Running" : "Not Running") << endl; cout << endl; curr = curr->next; } } int Linklist::Find(Process val) { Node* curr = head; int pos = 0; while (curr != NULL) { if (curr->val.proname == val.proname) { return pos; } curr = curr->next; pos++; } return -1; } int Linklist::Length() { return length; } Linklist::~Linklist() { Node* curr = head; Node* next = NULL; while (curr != NULL) { next = curr->next; delete curr; curr = next; } } int main() { Linklist processList; Process p1 = { "Process1", 10, 2, true }; Process p2 = { "Process2", 5, 1, false }; Process p3 = { "Process3", 8, 3, true }; processList.Insert(p1, 0); processList.Insert(p2, 1); processList.Insert(p3, 2); processList.Print(); processList.Remove(p2); processList.Print(); int pos = processList.Find(p3); if (pos != -1) { cout << "Process found at position: " << pos << endl; } else { cout << "Process not found!" << endl; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值