zoukankan      html  css  js  c++  java
  • 数据结构复习 之 一个简单双向链表的实现

    1. 什么时候能默写出来呢?

    #include <iostream>

    using namespace std;
    struct node
    {
    int data;
    node
    *next;
    node
    *prior;
    };



    class Link
    {
    public:
    Link()
    {
    head_ptr
    = NULL;
    tail_ptr
    = NULL;

    }
    void insertNode();
    void insertNode(node *ptr);
    void insertNodeAtHead(node *ptr);
    void insertNodeAtTail(node *ptr);
    void deleteNodeAtHead();
    void deleteNodeAtTail();

    void showList();
    void findData(int data);
    private:
    node
    * head_ptr;
    node
    * tail_ptr;
    };

    void Link::insertNode()
    {
    node
    * p = new node;
    int data;
    if(p != NULL)
    {
    cin
    >> data;
    p
    ->data = data;
    p
    ->next = NULL;
    p
    ->prior = NULL;
    }
    insertNode(p);
    }
    void Link::insertNode(node *ptr)
    {
    node
    * current_ptr;
    if(head_ptr == NULL)
    {
    head_ptr
    = ptr;
    tail_ptr
    = ptr;
    return;
    }
    if(head_ptr->next == NULL)
    {
    if(ptr->data < head_ptr->data)
    insertNodeAtHead(ptr);

    else
    insertNodeAtTail(ptr);
    return;
    }
    if(head_ptr->next != NULL)
    {
    current_ptr
    = head_ptr;
    while(current_ptr != NULL
    && ptr->data > current_ptr->data)
    {
    current_ptr
    = current_ptr->next;
    }
    if(current_ptr == head_ptr)
    insertNodeAtHead(ptr);
    else if(current_ptr == NULL)
    insertNodeAtTail(ptr);
    //如果没有查找完整个表,并且满足排序条件的
    //输出,欲插入的节点应该插入到current_ptr之前
    else
    {
    ptr
    ->next = current_ptr;
    ptr
    ->prior = current_ptr->prior;
    current_ptr
    ->prior->next = ptr;
    current_ptr
    ->prior = ptr;
    }
    return;
    }


    }

    void Link::insertNodeAtHead(node *ptr)
    {
    ptr
    ->next = head_ptr;
    ptr
    ->prior = NULL;
    head_ptr
    ->prior = ptr;
    head_ptr
    = ptr;
    }
    void Link::insertNodeAtTail(node *ptr)
    {
    ptr
    ->prior = tail_ptr;
    ptr
    ->next = NULL;
    tail_ptr
    ->next = ptr;
    tail_ptr
    = ptr;
    }

    void Link::showList()
    {
    node
    *current_ptr = head_ptr;
    if(current_ptr == NULL)
    {
    cout
    << "链表为空" << endl;
    return;
    }
    while(current_ptr != NULL)
    {
    cout
    << current_ptr->data << " ";
    current_ptr
    = current_ptr->next;
    }
    cout
    << endl;
    }
    void Link::deleteNodeAtHead()
    {
    if(head_ptr->next == NULL)
    {
    delete head_ptr;
    head_ptr
    = tail_ptr = NULL;
    }
    node
    *current_ptr = head_ptr;
    head_ptr
    = head_ptr->next;
    head_ptr
    ->prior = NULL;
    delete current_ptr;


    }
    void Link::deleteNodeAtTail()
    {
    node
    * current_ptr =tail_ptr;
    tail_ptr
    = tail_ptr->prior;
    tail_ptr
    ->next = NULL;
    delete current_ptr;
    }

    void Link::findData(int data)
    {
    if(head_ptr == NULL)
    {
    cout
    << "链表为空" << endl;
    }
    int count = 0;
    node
    * current_ptr = head_ptr;
    while(data > current_ptr->data && current_ptr->next != NULL)
    {
    current_ptr
    = current_ptr->next;
    count
    ++;
    }
    if(current_ptr != NULL)
    cout
    << "已经找到元素,在第" << count << "" << endl;
    else
    cout
    << " 没有找到,不存在" << endl;


    }

    int main()
    {
    Link link;
    for(int i = 0;i < 10;i ++)
    link.insertNode();


    link.showList();

    link.deleteNodeAtHead();
    link.showList();
    link.deleteNodeAtTail();
    link.showList();

    link.findData(
    5);
    return 0;
    }

      

  • 相关阅读:
    数组里的数据绑定到dataset中
    有关字符串匹配的方法
    sql语句全集
    Dialog 的6中提示方式
    android开源项目和框架
    MyEclipse DB Browser使用图文全攻略
    省市县联动(转)
    LRU算法
    Java 性能优化小细节
    HashMap
  • 原文地址:https://www.cnblogs.com/hitwtx/p/2162411.html
Copyright © 2011-2022 走看看