双链表 数据结构

#ifndef LinkedList_h_
#define LinkedList_h_
#include<vector>;
#include "AStarCtril.h"
using namespace std;

struct ListNode
{
	MyPoint data;
	ListNode *next;
	ListNode *prev;
};

class LinkedList
{
public:
	ListNode*mHead ;
	ListNode*mTail;
	int mSize;
	int position;
public:
	LinkedList():mHead(NULL), mTail(NULL), mSize(0), position(0){};
	~LinkedList() { delete mHead; delete mTail;};
	ListNode *create(vector<MyPoint>vec);                            //创建单链表
	void append(MyPoint data);                                       //添加一个节点
	bool insert(int pos, MyPoint data);                              //插入一个节点
	ListNode *getElementAt(int pos);                                 //获取指定位置的节点元素
	bool removeAt(int pos);                                          //移除指定位置的节点元素
	int indexOf(ListNode *node);                                     //获取链表中元素所在位置,没有的话,返回-1
	int removeNode(ListNode *node);                                  //移除指定节点
	bool isEmpty();                                                  //判断是否为空链表
	int getSize();                                                   //获取链表长度
	ListNode *getHead();                                             //获取链表表头
	ListNode *getTail();                                             //获取链表表尾
	ListNode* clear();                                               //清空链表
	void print();                                                    //打印链表信息
};

#endif // !LinkedList_h_


#include "LinkedList.h"

ListNode  *LinkedList::create(vector<MyPoint> vec)
{
	ListNode *s = new ListNode;
	mTail = new ListNode;
	while (mSize < vec.size())
	{
		s->data = vec.at(mSize);
		s->next = NULL;
		s->prev = NULL;
		if (mHead == NULL) {
			mHead = s;
			mHead->next = mTail;
		}
		else
		{
			mTail->next = s;
			s->prev = mTail;
		}
		mTail = s;
		s = new ListNode;
		mSize += 1;
	}
	delete s;
	return mHead;
}

void LinkedList::append(MyPoint data)
{
	ListNode *node = new ListNode;
	node->data = data;
	node->next = NULL;
	node->prev = NULL;
	if (mHead == NULL) {
		mHead = node;
		mTail = node;
	}
	else
	{
		mTail->next = node;
		node->prev = mTail;
		mTail = node;
	}
	mSize++;
}

bool LinkedList::insert(int pos, MyPoint data)
{
	if (pos<0 || pos>mSize)return false;
	ListNode *node = new ListNode;
	node->data = data;
	if (pos == 0) {
		node->next = mHead;
		mHead->prev = node;
		mHead = node;
	}
	else if(pos== mSize)
	{
		node->prev = mTail;
		mTail->next = node;
		mTail = node;
	}
	else
	{
		ListNode *previous = getElementAt(pos-1);
		if (previous != nullptr) {
			ListNode *current = previous->next;
			current->prev = node;
			node->next = current;
			node->prev = previous;
			previous->next = node;
		}
	}
	mSize++;
	return true;
}

ListNode * LinkedList::getElementAt(int pos)
{
	if (pos<0 || pos>mSize)return nullptr;
	ListNode *current = nullptr;
	if (pos > floor(mSize / 2)) {
		current = mTail;
		for (int i = mSize -1; i > pos; i--)
		{
			current = current->prev;
		}
		return current;
	}
	else
	{
		current = mHead;
		for (int i = 0; i < pos; i++)
		{
			current = current->next;
		}
		return current;
	}

	return nullptr;
}

bool LinkedList::removeAt(int pos)
{
	if (pos<0 || pos>mSize)return false;
	ListNode * current = mHead;
	ListNode *tile = mTail;
	if (pos == 0) {
		mHead = current->next;
		mHead->prev = nullptr;
	}
	else if (pos == mSize) {
		mTail = tile->prev;
		mTail->next = nullptr;
	}
	else
	{
		ListNode * previous = getElementAt(pos-1);
		current = previous->next;
		previous->next = current->next;
		current->next->prev = previous;

	}
	mSize--;
	return true;
}

int LinkedList::indexOf(ListNode *node)
{
	ListNode * current = mHead;
	for (int i = 0; i < mSize; i++)
	{
		if (current == node)return i;
		current = current->next;
	}
	return -1;
}

int LinkedList::removeNode(ListNode * node)
{
	int index = indexOf(node);
	return removeAt(index);
}

bool LinkedList::isEmpty()
{
	return mSize==0;
}

int LinkedList::getSize()
{
	return mSize;
}

ListNode * LinkedList::getHead()
{
	return mHead;
}

ListNode * LinkedList::getTail()
{
	return mTail;
}

ListNode* LinkedList::clear()
{
	ListNode *head, *tail;
	head = mHead;
	if (head == NULL) {
		log("链表为空");
	}
	else
	{
		while (head->next!=nullptr)
		{
			tail = head;
			head = head->next;
			delete tail;
			mSize--;
		}
		delete head;
		mSize--;
		mHead = NULL;
	}
	log("链表删除成功");
	return mHead;
}

void LinkedList::print()
{
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值