双链表的各种操作 创建 插入 删除 查找

首先定义一下双链表的结点结构

#include<iostream>  
using namespace std;  
class Node  
{  
    public:  
        Node()=default;   // c++ 11标准,如果不支持其实就是各种成员值默认都赋0  
        Node(int val):value(val),pre(nullptr),next(nullptr){}  
        inline Node*& getPre(){ return pre; }  //  得到前一个结点  
        inline Node*& getNext(){ return next; }  //得到后一个结点  
        inline int getValue(){ return value; }   //得到当前结点的值  
    private:  
        Node *pre;  
        Node *next;  
        int value;  
};  
void CreateList(Node *&head);   //创建一个双链表  
void printLList(Node *head);   //打印双链表  
void Insert(Node *&head, size_t pos, int val);  //插入一个结点  
void DeleteByValue(Node *&head,int val);     //按给的值删一个元素  
void DeleteByPos(Node *&head,size_t position);      //按位置删一个元素  
Node* SearchByValue(Node *head, int val, size_t *where=0);    //按值查找  
Node* SearchByPos(Node *head, size_t pos, int *val=0);        //按给的位置查找元素  
size_t Length(Node *head);   //返回链表的长度  
void CreateList(Node *&head)  
{  
    int input;  
    size_t pos=1;  
    cout<<"正在创建链表,输入结点值 (ctrl+z 结束输入)"<<endl;
    while(cin >> input)  
        Insert(head,pos++,input);  
    cout<<"The list was created!"<<endl<<endl;  
    cin.clear();  
    return;  
}  
  
void Insert(Node *&head, size_t pos, int val)  
{  
    size_t length = Length(head);  
    if(pos < 1 || pos > (length+1))    // 插入位置不合法   
    {  
        cout<<"The given position is illegal!"<<endl<<endl;  
        return;  
    }     
    Node *newNode = new Node(val);  
    if(pos == 1)   //  表头插入     
    {  
        if(head == nullptr)   //空表   
            head = newNode;  
        else          //非空表   
        {  
            head->getPre() = newNode;  
            newNode->getNext() = head;  
            head = newNode;  
        }  
    }  
    else  
    {  
        Node *position = SearchByPos(head,pos-1);;   
        if( pos == (length+1))  //  表尾插入   
        {  
            position->getNext() = newNode;  
            newNode->getPre() = position;  
        }   
        else                  //  其他位置插入  
        {  
            newNode->getNext() = position->getNext();  
            newNode->getPre() = position;  
            position->getNext()->getPre() = newNode;  
            position->getNext() = newNode;  
        }  
    }  
    return;  
}  
  
size_t Length(Node *head)  
{  
    size_t len = 0;  
    Node *pos = head;  
    while( pos != nullptr )  
    {  
        ++len;  
        pos = pos->getNext();  
    }  
    return len;  
}  
  
void DeleteByValue(Node *&head,int val)  
{  
    Node* pos = SearchByValue(head,val);  
    if(pos == nullptr)  
    {  
        cout<<val<<" is not found, cannot delete it!"<<endl<<endl;  
        return;  
    }  
    else if(pos == head)  //  删表头   
        head = head->getNext();  
    else if(pos->getNext() != nullptr)  //删其他位置   
    {  
        pos->getPre()->getNext() = pos->getNext();  
        pos->getNext()->getPre() = pos->getPre();  
    }  
    else               //删表尾   
        pos->getPre()->getNext() = nullptr;  
    delete pos;  
    pos = nullptr;  
    cout<<val<<" is deleted!"<<endl<<endl;  
    return;  
}  
  
void DeleteByPos(Node *&head,size_t position)  
{  
    size_t length = Length(head);  
    if(position < 1 || position > length)  
    {  
        cout<<"The position is illegal, cannot delete it!"<<endl<<endl;  
        return;  
    }  
    Node* pos = SearchByPos(head,position);  
    if(position == 1)  //  删除 表头  
        head = head->getNext();  
    else  
    {  
        if(pos->getNext() == nullptr)  //删除 表尾   
            pos->getPre()->getNext() = nullptr;  
        else                      //删其他位置   
        {  
            pos->getPre()->getNext() = pos->getNext();  
            pos->getNext()->getPre() = pos->getPre();  
        }  
    }  
    delete pos;  
    pos = nullptr;  
    cout<<"The value at position "<<position<<" is deleted!"<<endl<<endl;  
    return;  
}  
  
Node* SearchByValue(Node *head, int val, size_t *where)  
{  
    if(where != 0)  
        ++(*where);  
    Node *pos = head;  
    while(pos != nullptr && val != pos->getValue())  
    {  
        pos = pos->getNext();  
        if(where != 0)  
            ++(*where);  
    }  
    if(pos == nullptr)  
        return nullptr;  
    else  
        return pos;  
}  
  
Node* SearchByPos(Node *head, size_t pos, int *val)  
{  
    size_t length = Length(head);  
    if(pos < 1 || pos > length)  
        return nullptr;  
    Node *position = head;   
    size_t temp = 0;  
    while(position != nullptr && temp < pos-1)  
    {  
        ++temp;  
        position = position->getNext();  
    }  
    if(position == nullptr)  
        return nullptr;  
    else  
    {  
        if(val != 0)  
            *val = position->getValue();  
        return position;  
    }  
}  
  
  
void printLList(Node *head)  
{  
    if(head == nullptr)  
        cout<<"The list is empty!"<<endl<<endl;  
    else  
    {  
        do  
        {  
            cout<<head->getValue()<<" ";  
            head = head->getNext();  
        }while(head != nullptr);  
        cout<<endl<<endl;  
    }  
    return;  
}  
int main(int argc, char** argv)   
{  
    Node *temp;  
    size_t pos;  
    int val;  
    Node *head = nullptr;  
      
    CreateList(head);  //创建链表   
    printLList(head);  
      
    cout<<"插入,  输入要插入的位置和要插入的值(位置从1开始)"<<endl;  
    cin>>pos>>val;  
    Insert(head,pos,val);  
    printLList(head);  
      
    pos=0;  
    cout<<"按值查找,  输入要查找的值"<<endl;  
    cin>>val;  
    temp = SearchByValue(head,val,&pos);  
    if(temp == nullptr)  
        cout<<"The value of "<<val<<" was no found!"<<endl<<endl;  
    else  
        cout<<"The value of "<<val<<"  was found! Its position is "<<pos<<endl<<endl;  
      
    val=0;  
    cout<<"按地址查找, 输入要查找的位置"<<endl;  
    cin>>pos;  
    temp = SearchByPos(head,pos,&val);  
    if(temp == nullptr)  
        cout<<"The value at position "<<pos<<" was no found!"<<endl<<endl;  
    else  
        cout<<"The value at position "<<pos<<"  was found! Its value is "<<val<<endl<<endl;  
      
    cout<<"按值删除,输入要删除的值"<<endl;  
    cin>>val;  
    DeleteByValue(head,val);    
    printLList(head);  
      
   
     cout<<"按地址删除,输入要删除的位置"<<endl;  
     cin>>pos;  
     DeleteByPos(head,pos);  
     printLList(head);  
     
     return 0;  
}   

运行样例




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
双向链表是一种常见的数据结构,它可以在 O(1) 时间内实现插入删除操作,并且可以双向遍历链表。 以下是一个简单的双向链表实现,包括插入删除查找操作: ```python class Node: def __init__(self, data): self.data = data self.prev = None self.next = None class DoublyLinkedList: def __init__(self): self.head = None def insert(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: cur_node = self.head while cur_node.next is not None: cur_node = cur_node.next cur_node.next = new_node new_node.prev = cur_node def delete(self, data): cur_node = self.head while cur_node is not None: if cur_node.data == data: if cur_node.prev is not None: cur_node.prev.next = cur_node.next else: self.head = cur_node.next if cur_node.next is not None: cur_node.next.prev = cur_node.prev return else: cur_node = cur_node.next def find(self, data): cur_node = self.head while cur_node is not None: if cur_node.data == data: return True else: cur_node = cur_node.next return False ``` 在上面的代码中,我们定义了一个 `Node` 类来表示双向链表中的节点,包括节点存储的数据、前驱节点和后继节点。然后,我们定义了 `DoublyLinkedList` 类来表示双向链表。在 `DoublyLinkedList` 类中,我们实现插入删除查找操作。 在 `insert` 方法中,我们首先创建一个新节点,并将其插入到链表的末尾。如果链表为空,则将新节点作为头节点。否则,我们遍历整个链表,直到找到链表的最后一个节点,然后将新节点插入到链表的末尾。 在 `delete` 方法中,我们遍历整个链表,查找删除的节点。如果找到了该节点,则将其从链表中删除。如果要删除的节点是头节点,则将头指针指向下一个节点。否则,我们将前驱节点的 `next` 指针指向要删除节点的后继节点,将后继节点的 `prev` 指针指向要删除节点的前驱节点。 在 `find` 方法中,我们遍历整个链表,查找存储指定数据的节点。如果找到了这个节点,则返回 True,否则返回 False。 在使用双向链表时,我们需要注意以下几点: - 双向链表有两个指针,因此需要更多的内存来存储它们。 - 在插入删除节点时,需要更新前驱节点和后继节点的指针。 - 双向链表可以双向遍历,因此可以从前往后或从后往前遍历链表。 - 如果双向链表只有一个指针指向头节点,则可以将其视为单向链表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值