【C++】链表的实现

1.定义

//单向
struct Node{
   
     int value;
     Node * next;
 };
 //双向
struct DNode{
   
     int value;
     DNode * left;
     DNode * right;
 };

2.基本操作

(1)插入节点

//p节点后插入值为i的节点
void insertNode(Node *p, int i){
   
    Node* node = new Node;
    node->value = i;
    node->next = p->next;
    p->next = node;
}

(2)删除节点

void deleteNode(Node *p){
   
    p->value = p->next->value;
    p->next = p->next->next;
}

(3)反向遍历链表

//1.stack
void printLinkedListReversinglyByStack(Node *head){
   
    stack<Node* > nodesStack;
    Node* pNode = head;
    //遍历链表
    while (pNode != NULL) {
   
        nodesStack.push(pNode);
        pNode = pNode->next;
    }
    while (!nodesStack.empty()) {
   
        pNode=nodesStack.top();
        printf("%d\t", pNode->value);
        nodesStack.pop();
    }
}
//2.递归
void printLinkedListReversinglyRecursively(Node *head){
   
    if (head!=NULL) {
   
        if (head->next!=NULL) {
   
            printLinkedListReversinglyRecursively
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值