您的位置:首页 > 理论基础 > 数据结构算法

【C++数据结构】几种单链表的模类板实现及基本操作

2015-08-23 18:15 836 查看
提供代码如下:

循环单链表:

#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

typedef enum { FALSE, TRUE }Status;

template <class _Ty>
class SCList;

template <class _Ty>
class Node
{
friend class SCList<_Ty>;
public:
Node() :data(_Ty()), next(NULL)
{}
Node(_Ty d, Node<_Ty>* n = NULL) :data(d), next(n)
{}
private:
_Ty data;
Node<_Ty>* next;
};
template <class _Ty>
class SCList
{
public:
typedef Node<_Ty>* _NodePtr;
public:
SCList():Size(0)
{
First = Last = _BuyNode(0);
Last->next = First;
}
~SCList()
{
FreeNode(First);
//clear();
}

public:
Status IsEmpty() const
{
if (Size==0)
return TRUE;
return FALSE;
}

_NodePtr Insert(_NodePtr _P, const _Ty& x = _Ty())
{
_NodePtr s = _BuyNode(x, _P->next);
if (s == NULL)
return false;
_P->next = s;

if (0 == Size)
Last = s;

if (_P == First->next)
Last = _P;

Size++;
return s;
}

Status push_back(const _Ty& v)
{
Last = Insert(Last, v);
return TRUE;
}

Status push_front(const _Ty& v)
{
Insert(First, v);
return TRUE;
}

void ShowList()
{
if (IsEmpty())
return;
_NodePtr p = First->next;
while (p != First)
{
cout << p->data << "->";
p = p->next;
}
cout << "null" << endl;
}

void erase(_NodePtr _P)
{
if (IsEmpty())
{
cout << "空,不能删除" << endl;
return;
}

_NodePtr _Cur = _P->next;
_P->next = _Cur->next;
delete _Cur;
_Cur = NULL;

Size--;
}

Status pop_front()
{
if (Size == 1)
Last = First;
erase(First);
return TRUE;
}

Status pop_back()
{
if (IsEmpty())
return FALSE;
_NodePtr _Cur = First;
_NodePtr _P = _Cur->next;
while (_P->next != First)
{
_Cur = _P;
_P = _P->next;
}
Last = _Cur;
erase(_Cur);
return TRUE;
}

_NodePtr find(const _Ty& v) //返回前一个指针
{
_NodePtr pre = First;
_NodePtr cur = pre->next;
while (cur->next != First)
{
if (cur->data == v)
return pre;
pre = cur;
cur = cur->next;
}
return NULL;
}

void delete_value(_Ty& _V)
{
_NodePtr p;
while (p = find(_V),p)
{
if (p->next == Last)
{
Last = p;
Last = First;
}
erase(p);
}
}

void resver()
{
if (IsEmpty())
return;
_NodePtr p = First->next;
_NodePtr q;
Last = p;
First->next = NULL;
while (p != First)
{
q = p;
p = p->next;
q->next = First->next;
First->next = q;
}
Last->next = First;
}

void Insert(_NodePtr _P, _NodePtr _S)
{
_S->next = _P->next;
_P->next = _S;
if (_P == Last)
Last = _S;
Last->next = First;
Size++;
}

Status Insert_val(const _Ty& x)
{
_NodePtr _P = _BuyNode(x);
Insert_val(_P);
return TRUE;
}

void Insert_val(_NodePtr _Cur)
{
_NodePtr _P = First;
while (_P->next != First  && _Cur->data > _P->next->data)
_P = _P->next;
Insert(_P, _Cur);
}

void sort()
{
if (Size == 0 || Size == 1)
return;
_NodePtr s = First->next;
_NodePtr q = s->next;
_NodePtr p = q->next;
Last = s;
Size = 1;
s->next = NULL;
while (q != First)
{
Insert_val(q);
q = p;
if (q != First)//error!
p = p->next;
}
Last->next = First;
}
_Ty& findBypos(size_t pos)
{
if (pos > Size)
{
cout << "Array index error" << endl;
return First->data;
}
_NodePtr p = First;
while (p->next != First && pos)
{
p = p->next;
pos--;
}
return p->data;
}
void clear()
{
while (!IsEmpty())
pop_front();
}
_NodePtr prio(_NodePtr _P)const
{
if (_P->next != First)
return find(_P->data);
return NULL:
}
_NodePtr Next(_NodePtr _P)const
{
if (_P->next->next != First)
return _P->next;
}

protected:
_NodePtr _BuyNode(const _Ty& x, _NodePtr _Narg = NULL)
{
_NodePtr s = new Node<_Ty>(x, _Narg);
assert(s != NULL);
return s;
}

void FreeNode(_NodePtr _P)
{
delete _P;
_P = NULL;
}
private:
_NodePtr First;
_NodePtr Last;
size_t Size;
};


普通结构:

#pragma once

#include <iostream>
using namespace std;
//无管理
template <class T>
class LinkList
{
protected:
struct LinkNode;
friend struct LinkNode;
typedef T* _ptr;
typedef LinkNode* _NodePtr;
struct LinkNode
{
_NodePtr _Next;
T value;
};

private:
size_t Size;
_NodePtr Handle;

protected:
_NodePtr _BuyNode(_NodePtr _Narg = NULL)
{
_NodePtr _S = new LinkNode;
_S->_Next = _Narg != 0 ? _Narg :NULL;
return (_S);
}
_NodePtr _FreeNode(_NodePtr _X)
{
delete _X;
_X = NULL;
}
public:
LinkList():Size(0),Handle(_BuyNode())
{}
~LinkList()
{
delete Handle;
Size = 0;
}

void Insert(_NodePtr _P, const T& x)
{
_NodePtr s = _BuyNode(_P->_Next);
_P->_Next = s;
s->value = x;
Size++;
}

bool IsEmpty()
{
return Size == 0;
}

void push_back(const T& x)
{
_NodePtr p = Handle;
while (p->_Next)
{
p = p->_Next;
}
Insert(p, x);
}

void push_front(const T& x)
{
Insert(Handle, x);
}

void ShowList()
{
if (IsEmpty())
return;
_NodePtr p = Handle->_Next;
while (p)
{
cout << p->value << "->";
p = p->_Next;
}
cout << "null" << endl;
}

_NodePtr find(const T& x)
{

_NodePtr pre = Handle;
_NodePtr cur = pre->_Next;
while (cur)
{
if (cur->value == x)
return pre;
pre = cur;
cur = cur->_Next;
}
return NULL;
}

T& findBypos(size_t pos)
{
if (pos >= Size)
{
cout << "Array index error" << endl;
return Handle->value;
}
_NodePtr p = Handle->_Next;
while (p && pos)
{
p = p->_Next;
pos--;
}
return p->value;
}

size_t Length()
{
return Size;
}

void deleteByPrev(_NodePtr _P)
{
if (IsEmpty())
{
cout << "空,不能删除" << endl;
return;
}

_NodePtr _Cur = _P->_Next;
_P->_Next = _Cur->_Next;
delete _Cur;
_Cur = NULL;
Size--;
}

void delete_value(T& _V)
{
_NodePtr p;
while (p = find(_V), p != NULL)
deleteByPrev(p);
}

void pop_front()
{
deleteByPrev(Handle);
}

void pop_back()
{
if (IsEmpty())
return;
_NodePtr _Cur= Handle;
_NodePtr _P = _Cur->_Next;
while (_P->_Next)
{
_Cur = _P;
_P = _P->_Next;
}
deleteByPrev(_Cur);
}

T getvalue(const _NodePtr _P)const
{
return _P->value;
}

void clear()
{
while (!IsEmpty())
{
deleteByPrev(Handle);
}
}

void DestroyList()
{
clear();
delete Handle;
Handle = NULL;
}

void resver()
{
_NodePtr p, q;
p = Handle->_Next;
Handle->_Next = NULL;
while (p != NULL)
{
q = p;
p = p->_Next;
q->_Next = Handle->_Next;
Handle->_Next = q;
}
}

void Sort()
{
if (!IsEmpty())
{
for (int i = Size - 1; i; i--)
{
for (int l = 0; l < i; l++)
{
if ((*this)[l]>(*this)[l + 1])
{
T tmp = (*this)[l];
(*this)[l] = (*this)[l + 1];
(*this)[l + 1] = tmp;
}
}
}
}
}

public:
T& operator[](size_t index)
{
return findBypos(index);
}

};


带管理节点:

#pragma once
#include<iostream>
#include <assert.h>
using namespace std;
typedef enum { FALSE, TRUE }Status;

template <class _T>
class LinkList;
template <class _T>
class ListNode
{
friend class LinkList<_T>;
public:
ListNode() :data(_T()), next(NULL)
{}
ListNode(_T d, ListNode<_T>* n = NULL) :data(d), next(n)
{}
private:
_T data;
ListNode<_T>* next;
};
template <class _T>
class LinkList
{
public:
typedef ListNode<_T>* _NodePtr;
public:
LinkList()
{
ListNode<_T>* s = _BuyNode(0);
First = Last = s;
Size = 0;
}
~LinkList()
{}
protected:
_NodePtr _BuyNode(const _T& x, _NodePtr _Narg = NULL)
{
_NodePtr s = new ListNode<_T>(x, _Narg);
assert(s != NULL);
return s;
}
private:
ListNode<_T>* First;
ListNode<_T>* Last;
size_t Size;
public:
Status IsEmpty() const
{
if (Size == 0)
return TRUE;
return FALSE;
}
_NodePtr Insert(_NodePtr _P, const _T& x = _T())
{
_NodePtr s = _BuyNode(x, _P->next);
if (s == NULL)
return false;
_P->next = s;

if (0 == Size)
Last = s;

Size++;
return s;
}
Status push_back(const _T& v)
{
Last = Insert(Last, v);
return TRUE;
}
Status push_front(const _T& v)
{
Insert(First, v);
return TRUE;
}
void ShowList()
{
if (IsEmpty())
return;
_NodePtr p = First->next;
while (p)
{
cout << p->data << "->";
p = p->next;
}
cout << "null" << endl;
}
void erase(_NodePtr _P)
{
if (IsEmpty())
{
cout << "空,不能删除" << endl;
return;
}

_NodePtr _Cur = _P->next;
_P->next = _Cur->next;
delete _Cur;
_Cur = NULL;
Size--;
}
Status pop_front()
{
if (Size == 1)
Last = First;
erase(First);
return TRUE;
}
Status pop_back()
{
if (IsEmpty())
return FALSE;
_NodePtr _Cur = First;
_NodePtr _P = _Cur->next;
while (_P->next)
{
_Cur = _P;
_P = _P->next;
}
Last = _Cur;
erase(_Cur);
return TRUE;
}
_NodePtr find(const _T& v) //返回前一个指针
{
_NodePtr pre = First;
_NodePtr cur = pre->next;
while (cur)
{
if (cur->data == v)
return pre;
pre = cur;
cur = cur->next;
}
return NULL;
}
void delete_value(_T& _V)
{
_NodePtr p;
while (p = find(_V), p != NULL)
{
if (p->next == Last)
Last = p;

erase(p);
}
}
void resver()
{
if (IsEmpty())
return;
_NodePtr p = First->next;
_NodePtr q;
Last=p;
First->next = NULL;
while (p != NULL)
{
q = p;
p = p->next;
q->next = First->next;
First->next = q;
}

}
void Insert(_NodePtr _P, _NodePtr _S)
{
_S->next = _P->next;
_P->next = _S;
if (_P == Last)
Last = _S;
Size++;
}
Status Insert_val(const _T& x)
{
_NodePtr _P = _BuyNode(x);
Insert_val(_P);
return TRUE;
}
void Insert_val(_NodePtr _Cur)
{
_NodePtr _P = First;
while (_P->next  && _Cur->data > _P->next->data)
_P = _P->next;
Insert(_P, _Cur);
}
void sort()
{
if (Size == 0 || Size == 1)
return;
_NodePtr s = First->next;
_NodePtr q = s->next;
_NodePtr p = q->next;
Last = s;
Size = 1;
s->next = NULL;
while (q )
{
Insert_val(q);
q = p;
if(q)
p = p->next;
}
}
_T& findBypos(size_t pos)
{
if (pos > Size)
{
cout << "Array index error" << endl;
return First->data;
}
_NodePtr p = First;
while (p && pos)
{
p = p->next;
pos--;
}
return p->data;
}
void clear()
{
while (!IsEmpty())
pop_front();
}
_NodePtr prio(_NodePtr _P)const
{
if(_P != NULL)
return find(_P->data);
return NULL:
}
_NodePtr Next(_NodePtr _P)const
{
if (_P->next != NULL)
return _P->next;
}
};


C语言结构实现静态链表:

#pragma once

#include <iostream>
using namespace std;

#define MAXSIZE 8
#define ElemType int

typedef struct Node
{
ElemType data;
int cur;
}SticNode;

typedef SticNode SticList[MAXSIZE];

void Init(SticList& list)
{
list[0].cur = -1;
list[0].data = 0;
for (int i = 1; i < MAXSIZE-1; ++i)
{
list[i].cur = i + 1;
}
list[MAXSIZE - 1].cur = -1;
}

bool IsEmpty(SticList& list)
{
if (list[0].cur == -1)
return true;
return false;
}

int MyFind(SticList& list, ElemType v)
{
int j = list[0].cur;
while (list[j].cur != -1 && list[j].data != v)
{
j = list[j].cur; //p=p->next
}
if (list[j].data == v)
return j;
cout << "查询无此数据" << endl;
exit(-1);
}

int Malloc_SL(SticList& list)
{
if(list[1].cur == -1)
return -1;
int index = list[1].cur;
list[1].cur = list[index].cur;

list[0].data += 1;
return index;

}

void pop_back(SticList& list)
{
int j = 0;
while (list[(list[j].cur)].cur != -1)
{
j = list[j].cur; //p=p->next
}
list[(list[j].cur)].cur = list[1].cur;
list[(list[j].cur)].data = -1;
list[1].cur = list[j].cur;
list[j].cur = -1;

list[0].data -= 1;
}

void pop_front(SticList& list)
{
int i = list[0].cur;
list[0].cur = list[i].cur;
list[i].cur = list[1].cur;
list[1].cur = i;

list[0].data -= 1;
}

void clear(SticList& list)
{
while (!IsEmpty(list))
pop_back(list);
}

void push_back(SticList& list, ElemType x)
{
int i = Malloc_SL(list);
if (i == -1)
return;

list[i].data = x;
list[i].cur = -1;
int j = 0;
while (list[j].cur != -1)
{
j = list[j].cur; //p=p->next
}
list[j].cur = i;
}

void show(SticList& list)
{
int i = list[0].cur;
if (IsEmpty(list))
{
cout <<"null"<<endl <<"链表已空 >" << endl;
return;
}
for (; list[i].cur != -1; i = list[i].cur)
{
cout << list[i].data<<"->";
}
cout <<list[i].data<<"->"<< "null" << endl;
}

void Delete(SticList& list,ElemType x )
{
int index = MyFind(list, x);
if (index == list[0].cur)
{
pop_front(list);
return;
}
if (list[index].cur == -1)
{
pop_back(list);
return;
}
int j = 0;
while (list[j].cur != index)
{
j = list[j].cur; //p=p->next
}
list[j].cur = list[index].cur;
list[index].cur = list[1].cur;
list[1].cur = index;

list[0].data -= 1;
}

void insert(SticList& list, ElemType x, ElemType v)	//x后插入v
{
int index = MyFind(list, x);

if (list[index].cur == -1)
{
push_back(list, v);
return;
}

int tmp = list[1].cur;

list[tmp].data = v;
list[1].cur = list[tmp].cur;
list[tmp].cur = list[index].cur;
list[index].cur = tmp;

list[0].data += 1;
}

int Length(SticList& list)
{
return list[0].data;
}


静态链表,即通过一位数组描述链表结构,用游标模拟指针。



游标所指的位置即当前节点的后继。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: