#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()
{
}