单链表C++实现

仅供参考  

#include <iostream>
using namespace std;

// 函数结果状态代码
#define TRUE    1
#define FALSE   0
#define OK      1
#define ERROR   0
#define INFEASIBLE -1
#define OVERFLOW   -2
#define MAXSIZE    100

// Status 是函数的类型 其值是函数结果状态代码
typedef int Status;

typedef struct {
    char num[8];    // 数据域
    char name[8];   // 数据域
    int score;      // 数据域
}ElemType2;

typedef int ElemType;

typedef struct ListNode{
    ElemType data;          // 数据域
    struct ListNode *next;  // 指针域
}ListNode, *LinkList;

// 单链表的初始化
Status InitList_L(LinkList &L);

// 判断链表是否为空
int ListEmpty(LinkList L);

// 单链表的销毁,销毁后不存在
Status DestroyList_L(LinkList &L);

// 清空链表:链表仍存在,但链表中无元素,成为空链表(头指针和头节点仍然存在)
// 算法思路:依次释放所有节点,并将头结点的指针域设置为空
Status ClearList(LinkList &L);

// 单链表的表长
int ListLength(LinkList L);     // 返回L中数据元素个数

// 获取线性表L中的某个数据元素的内容,通过变量e返回
Status GetElem_L(LinkList L, int i, ElemType &e);

// 在线性表L中查找值为e的数据元素
// 找到,则返回L中值为e的数据元素的地址,查找失败返回NULL;
ListNode* LocateElem_address(LinkList L, ElemType e);

// 在线性表L中按照值为e的数据元素的位置序号
// 返回L中值为e的数据元素的序号,查找失败返回0;
int LocateElem_value(LinkList L, ElemType e);

// 在L中第i个元素之前插入数据元素e
Status LinkInsert_L(LinkList &L, int i, ElemType e);

// 将线性表L中第i个数据元素删除
Status ListDelete_L(LinkList &L, int i, ElemType &e);

// 头插法  算法复杂度O(n)
void CreateList_H(LinkList &L, int n);

// 尾插法:正序输入N个元素值,建立带头结点的单链表  算法复杂度O(n)
void CreatList_R(LinkList &L, int n);

// 遍历链表
Status PrintList(LinkList L);

// 头插法测试代码
int main() {

    // 创建一个空链表
    LinkList L;
    // 初始化链表
    InitList_L(L);
    // 头插法
    CreateList_H(L, 5);
    // 遍历链表
    PrintList(L);
    // 求线性表的长度
    int length = ListLength(L);
    cout << "链表长度:" << length << endl;

    // 获取线性表第i个元素的值
    ElemType e;
    int i;
    cout << "查找第i个元素:";
    cin >> i;
    GetElem_L(L, i, e);
    cout << "第 " << i << " 个位置的值: " << e << endl;

    // 获取某个值的地址
    ListNode *temp;
    cout << "想要获取值为e的地址:";
    cin >> e;
    temp = LocateElem_address(L, e);
    cout << "temp->data: "  << temp->data << endl;

    // 查找某个值的序号
    int index = 0;
    cout << "想要获取值为e的序号:";
    cin >> e;
    index =  LocateElem_value(L, e);
    cout << "值为 " << e << " 的序号: " << index << endl;

    // 插入元素
    cout << "在第几个位置插入元素:";
    cin >> i;
    cout << "插入的值为:";
    cin >> e;
    LinkInsert_L(L, i, e);
    // 遍历链表
    PrintList(L);

    // 删除元素
    cout << "在第几个位置删除元素:";
    cin >> i;
    ListDelete_L(L, i, e);
    cout << "被删除的值为: " << e << endl;

    // 清空链表
    ClearList(L);
    // 是否为空
    ListEmpty(L);
    // 销毁链表
    DestroyList_L(L);
    
    return 0;
}

// 单链表的初始化
Status InitList_L(LinkList &L){
    L = new ListNode;   // 生成新节点作头结点,用头指针L指向头结点
    L->next = nullptr;  // 将头结点的指针域置空
    return OK;
}

// 判断链表是否为空
int ListEmpty(LinkList L){
    if (L->next) {
        cout<<"链表非空";
        return FALSE;
    }
    else {
        cout << "链表为空";
        return TRUE;
    }

}

// 单链表的销毁,销毁后不存在
Status DestroyList_L(LinkList &L){
    ListNode *p;
    while (L){          // L头结点非空
        p = L;          // 存储头结点的地址
        L = L->next;    // 头结点下移
        delete p;
    }
    return OK;
}

// 清空链表:链表仍存在,但链表中无元素,成为空链表(头指针和头节点仍然存在)
// 算法思路:依次释放所有节点,并将头结点的指针域设置为空
Status ClearList(LinkList &L){
    ListNode *p, *q;    // 2个辅助指针
    p = L->next;        // p指向第一个结点
    while (p){
        q = p->next;
        delete p;
        p = q;
    }
    L->next = nullptr;
    return OK;
}

// 单链表的表长
int ListLength(LinkList L){     // 返回L中数据元素个数
    ListNode *p;
    int i = 0;
    p = L->next;           // p指向第一个结点
    while (p){              // 遍历单链表,统计结点数
        p = p->next;
        i++;
    }
    return i;
}

// 获取线性表L中的某个数据元素的内容,通过变量e返回
Status GetElem_L(LinkList L, int i, ElemType &e){
    ListNode *p;
    p = L->next;
    int j = 1;
    while (p && j < i){

        p = p->next;
        j++;
    }
    if ( !p || j > i) return ERROR;
    e = p->data;
    return OK;
}

// 在线性表L中查找值为e的数据元素
// 找到,则返回L中值为e的数据元素的地址,查找失败返回NULL;
ListNode* LocateElem_address(LinkList L, ElemType e){
    ListNode *p;
    p = L->next;
    while ( p && p->data != e){
        p = p->next;
    }
    return p;
}

// 在线性表L中超照值为e的数据元素的位置序号
// 返回L中值为e的数据元素的序号,查找失败返回0;
int LocateElem_value(LinkList L, ElemType e){

    ListNode *p;
    p = L->next;
    int j = 1;
    while (p && p->data != e){
        p = p->next;
        j++;
    }
    if (p) return j;
    else return 0;
}

// 在L中第i个元素之前插入数据元素e
Status LinkInsert_L(LinkList &L, int i, ElemType e){
    ListNode *p, *s;
    p = L;
    int j = 0;
    while (p && j < i -1){      // 寻找
        p = p->next;
        j++;
    }
    if (!p || j>i-1)
        return ERROR;
    s = new ListNode;
    s->data = e;

    s->next = p->next;
    p->next = s;
    return OK;
}

// 将线性表L中第i个数据元素删除
Status ListDelete_L(LinkList &L, int i, ElemType &e){

    ListNode *p = L;
    int j = 0;
    while (p->next && j < i-1){         // 寻找第i个节点,并令p指向其前驱
        p = p->next;
        j++;
    }
    if (!(p->next) || j > i-1) return ERROR;    // 删除位置不合理
    ListNode *q = p->next;     // 临时保存被删节点的地址以备释放
    p->next = q->next;         //  改变删除结点前驱节点的指针域
    e = q->data;                // 保存删除节点的数据域
    delete q;                   // 释放删除节点的空间
    return OK;
}

// 头插法  算法复杂度O(n)
void CreateList_H(LinkList &L, int n){

    L = new ListNode;
    L->next = nullptr;  // 先建立一个带头节点的单链表
    L->data = 0;

    for (int i = n; i > 0; --i) {
        auto *p = new ListNode;     // 生成新结点
        cout << "头插法:";
        cin >> p->data;             // 输入元素值
        p->next = L->next;          // 插入到表头
        L->next = p;
    }
}

// 尾插法:正序输入N个元素值,建立带头结点的单链表  算法复杂度O(n)
void CreatList_R(LinkList &L, int n){

    L = new ListNode;
    L->next = nullptr;
    ListNode *r = L;   // 尾指针r指向头节点
    for (int i = 0; i < n; ++i) {
        auto p = new ListNode;      // 生成新节点,输入元素值
        cout << "尾插法:";
        cin >> p->data;
        p->next = nullptr;
        r->next = p;            // 插入到表尾
        r = p;          // r 指向新的为结点
    }
}

// 遍历链表
Status PrintList(LinkList L){

    ListNode *p = L->next;
    while (p){
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
    return OK;
}

感谢青岛大学-王卓老师细心的讲解~!

  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个简的C++程序,用于实现链表的基本操作(包括插入、删除和打印): ```cpp #include <iostream> // 定义链表节点 struct Node { int data; // 存储节点的数据 Node* next; // 指向下一个节点的指针 }; // 在链表末尾插入节点 void insert(Node*& head, int data) { Node* newNode = new Node(); // 创建新节点 newNode->data = data; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } } // 在链表中删除指定的节点 void remove(Node*& head, int data) { if (head == nullptr) { return; } Node* temp = head; Node* prev = nullptr; // 当找到要删除的节点时,将其从链表中移除 while (temp != nullptr && temp->data != data) { prev = temp; temp = temp->next; } if (temp == nullptr) { return; } if (prev == nullptr) { head = head->next; } else { prev->next = temp->next; } delete temp; // 释放内存 } // 打印链表中的所有节点 void printList(Node* head) { Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " "; temp = temp->next; } std::cout << std::endl; } int main() { Node* head = nullptr; // 链表的头节点 // 插入节点 insert(head, 1); insert(head, 2); insert(head, 3); insert(head, 4); // 打印链表 std::cout << "链表中的节点:"; printList(head); // 删除节点 remove(head, 3); // 打印链表 std::cout << "删除后的链表:"; printList(head); return 0; } ``` 这个程序定义了一个`Node`结构体来表示链表的节点,其中`data`存储节点的数据,`next`指针指向下一个节点。通过`insert`函数可以在链表末尾插入新节点,通过`remove`函数可以删除链表中指定的节点,通过`printList`函数可以打印链表中的所有节点。在主函数中,我们可以使用这些函数来操作链表

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值