C++链表的实现

#include <iostream>
using namespace std;
#include <stdio.h>
typedef struct node
{
    char name[20];
    int age;
    struct node *next;
}Student;

Student * creatList(int n) 
{
    Student * head = new Student;
    Student * pre = head;
    for (int i = 0; i < n; i++)
    {
        Student* p = new Student;
        printf("请输入第%d个学生的姓名和年龄:",i+1);
        cin >> p->name;
        cin >> p->age;

        pre->next = p;
        pre = p;
        p->next = NULL;
    }
    return head;
}

void display(Student* head) 
{
    Student* p = head->next;
    while (p != NULL) 
    {
        cout << p->name << " " << p->age<<endl;
        p = p->next;
    }
    
}

int main()
{
    int n = 2;
    Student *head = creatList(n);
    display(head);
    system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值