C++ 实现单链表

使用C++实现简单链表(含头结点),并对其进行基本操作;

将以下三个文件放入同一文件夹中,对 main.cpp 执行编译即可;

本人萌新,评论区欢迎各位大佬提问、指正

 LinkList.h

#ifndef LINKLIST_H_
#define LINKLIST_H_
#include <iostream>
typedef double DataType;                                              // DataType can't be int

struct Node
{
	DataType data;
	Node *next;
};
class LinkList
{
    private:
	    Node *head;                                                   // head->next == first Node, head->data == size
        inline Node *search(int location) const;                      // search by location
        inline Node *search(DataType value) const;                    // search by value
    public:
        LinkList();                                                   // create a empty LinkList
        LinkList(DataType ar[],int n);                                // using ar[] to create a LinkList
        bool isempty() const;                                         // judge it's empty or not
        bool insert(DataType d1, DataType d2);                        // insert d1 after d2
        bool insert(DataType d1, int location);                       // insert d1 to a location
        bool remove(DataType d1);                                     // remove d1 from LinkList
        bool remove(int location, DataType &d1);                      // remove location's Node and return to d1
        bool display() const;                                         // show all the Node
};

#endif

LinkList.cpp 

#include <iostream>
#include "LinkList.h"
using namespace std;

LinkList::LinkList()
{
    head = new Node;
    head->data = 0;
    head->next = NULL;
}
LinkList::LinkList(DataType ar[], int n)
{
    Node *ptemp = new Node; 
    ptemp->data = ar[0];
    ptemp->next = NULL;

    head = new Node;
    head->data = n;
    head->next = ptemp;

    for (int i = 1; i < n; i++)
    {
		Node *pnew = new Node;
        pnew->data = ar[i];
        pnew->next = NULL;
        ptemp->next = pnew;
        ptemp = pnew;
    }
    ptemp->next = NULL;
}
bool LinkList::isempty() const
{
    if(head->data == 0)
        return true;
    else
        return false;
}
bool LinkList::display() const
{
    if(head->data == 0)
    {
        cout << "NULL";
        return false;
    }
    Node *ptemp = head->next;
    while(ptemp)
    {
        cout << ptemp->data << " -> ";
        ptemp = ptemp->next;
    }
    cout << "NULL " << "(the length is "<<head->data<<")";
    return true;
}
inline Node *LinkList::search(int location) const
{
    if (location == 0)
        return head;
    Node *ptemp = head->next;
    int n = 1;
    while(n != location)
    {
        ptemp = ptemp->next;
        n++;
    }
    return ptemp;
}
inline Node *LinkList::search(DataType value) const
{
    Node *ptemp = head->next;
    while (ptemp && ptemp->data != value)
        ptemp = ptemp->next;
    return ptemp;             // will return a *Node or NULL
}
bool LinkList::insert(DataType d1, DataType d2)
{
    if(search(d2) == NULL)
        return false;
    Node *pd2 = search(d2);
    Node *pnew = new Node;
    pnew->data = d1;
    pnew->next = pd2->next;
    pd2->next = pnew;
    head->data++;
    return true;
}
bool LinkList::insert(DataType d1, int location)
{
    if(location < 0)
        return false;
    if(location == 0)
    {
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = head->next;
        head->next = pnew;
    }
    else if (location <= head->data)
    {
        Node *ploc = search(location);
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = ploc->next;
        ploc->next = pnew;
    }
    else
    {
        Node *plast = search(int(head->data));
        Node *pnew = new Node;
        pnew->data = d1;
        pnew->next = NULL;
        plast->next = pnew;
    }
    head->data++;
    return true;
}
bool LinkList::remove(DataType d1)
{
    if(search(d1) == NULL)
    {
        cout << d1 << " is not in the list...";
        return false;
    }
    else
    {
        Node *pd1 = search(d1);
        int i;
        for (i = 1; i <= head->data; i++)
            if (search(i) == pd1)
                break;
        Node *pr = search(--i);
        pr->next = pd1->next;
        head->data--;
        return true;
    }
}
bool LinkList::remove(int location, DataType &d1)
{
    if (location < 0 || location > head->data)
    {
        cout << "The list isn't so long...";
        return false;
    }
    else
    {
        Node *ploc = search(location);
        d1 = ploc->data;
        Node *pr = search(--location);
        pr->next = ploc->next;
        head->data--;
        return true;
    }
}

main.cpp 

#include <iostream>
#include "LinkList.h"
#include "LinkList.cpp"
using namespace std;

int main()
{
    double ar[5] = {1.5, 2.5, 3.5, 4.5, 5.5};
    LinkList L1(ar, 5);
    
    L1.display();

    while(true)
        cin.get();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是用C++实现单链表操作的示例代码: ```cpp #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; // 存储节点的值 ListNode *next; // 指向下一个节点的指针 ListNode(int x) : val(x), next(NULL) {} // 构造函数 }; // 定义链表类 class LinkedList { public: LinkedList() { // 构造函数 head = NULL; } ~LinkedList() { // 析构函数 ListNode *cur = head; while (cur != NULL) { ListNode *temp = cur; cur = cur->next; delete temp; } } void add(int val) { // 添加节点 if (head == NULL) { head = new ListNode(val); } else { ListNode *cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = new ListNode(val); } } void remove(int val) { // 移除节点 if (head == NULL) { return; } if (head->val == val) { ListNode *temp = head; head = head->next; delete temp; } else { ListNode *cur = head; while (cur->next != NULL && cur->next->val != val) { cur = cur->next; } if (cur->next != NULL) { ListNode *temp = cur->next; cur->next = cur->next->next; delete temp; } } } void print() { // 打印链表 ListNode *cur = head; while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } private: ListNode *head; // 链表头指针 }; int main() { LinkedList list; list.add(1); list.add(2); list.add(3); list.print(); // 输出:1 2 3 list.remove(2); list.print(); // 输出:1 3 return 0; } ``` 该代码实现单链表的添加、移除和打印操作。需要注意的是,析构函数需要手动释放链表中所有节点的内存,否则可能会出现内存泄漏的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gaoqizhong7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值