前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构:带头节点双向循环链表的实现(考研)

数据结构:带头节点双向循环链表的实现(考研)

作者头像
lexingsen
发布2022-02-24 19:10:55
2850
发布2022-02-24 19:10:55
举报
文章被收录于专栏:乐行僧的博客
代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;

class DoubleLinkedCircleList{
private:
  struct node {
    int val;
    node *next, *prev;

    node (int x) {this->val = x;}
    node () {}
  };

  node *head;
  int size;
public:
  DoubleLinkedCircleList() {
    head = new node();
    head->next = head->prev = head;
    size = 0;
  }
  ~DoubleLinkedCircleList() {
    while (head->next!=head && head->prev!=head) delTail();
    delete head;
  }

  void insert(int index, int x) {
    if (index<0 || index>size) {
			cout << "the index is invalid!" << endl;
			return;
		} 


    // p是待删除节点的前去节点
    node *p = head;
    for (int i=0; i<index; ++i) p = p->next;
    node *q = new node(x);
    q->next = p->next;
    if (p->next) p->next->prev = q;
    q->prev = p;
    p->next = q;
    size ++;
  }
  void insertHead(int x) {insert(0, x);}
  void insertTail(int x) {insert(size, x);}

  int del(int index) {
    if (index<0 || index>=size) {
			cout << "the index is invalid!" << endl;
			return -1;
		}
	
		if (isEmpty()) {
			cout << "the DoubleLinkedCircleList is null!" << endl;
			return -1;
		} 

    node *p = head;
    for (int i=0; i<index; ++i) p = p->next;
    node *q = p->next;
    int res = q->val;
    q->next->prev = p;
    p->next = q->next;
    delete q;
    q = NULL;
    size --;
  }

  int delHead() {return del(0);}
  int delTail() {return del(size-1);}

  int search(int index) {
    if (index<0 || index>=size) {
			cout << "the index is invalid!" << endl;
			return -1;
		}
		
		if (head->next == head && head->prev == head) {
			cout << "the DoubleLinkedCircleList is null!" << endl;
			return -1;
		}

    node *p = head->next;
    for (int i=0; i<index; ++i) p = p->next;
    return p->val;
  }
  int searchHead() {return search(0);}
  int searchTail() {return search(size-1);}

  void show() {
    node *p = head->next;
    while (p && p!=head) {
      cout << p->val << " ";
      p = p->next;
    }
    cout << endl;
  }
  bool isEmpty() {return head->next==head && head->prev==head;}
  int getSize() {return size;}
};

int main() {
  int a[] = {1,2,3,4,5};
  int n = sizeof(a)/sizeof(int);
  DoubleLinkedCircleList list = DoubleLinkedCircleList();
  for (int i=0; i<n; ++i) list.insertTail(a[i]);
  list.show();
  list.delHead();
  list.show();
  cout << "size:" << list.getSize() << endl;
  list.delTail();
  list.show();
  list.del(2);
  list.show();
  cout << list.searchHead() << endl;
  cout << list.searchTail() << endl;
  cout << "======================" << endl;
  cout << list.search(2) << endl;
  cout << list.search(0) << endl;
  cout << "======================" << endl;
  cout << list.search(1) << endl;
  list.show();
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档