动态顺序表
#pragma once
#define _SEQ_LIST_
#ifdef _SEQ_LIST_
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
#define DEFAULT_CAPACITY 3
typedef int DataType;
typedef struct FindRet
{
bool isFind; // 是否找到的标示
size_t index; // 找到数据的下标
}Findret;
class SeqList
{
public:
SeqList(DataType *arr = NULL, size_t size = 0, size_t capacity = DEFAULT_CAPACITY)
{
_arr = new DataType[DEFAULT_CAPACITY];
_size = size;
_capacity = capacity;
}
void Swap(SeqList &s)
{
/*DataType *tmp = s._arr;
s._arr = _arr;
_arr = tmp;*/
swap(_arr, s._arr); //swap是c++库函数
swap(_size, s._size);
swap(_capacity, s._capacity);
}
SeqList(const SeqList &s) :_arr(NULL)
{
SeqList tmp(s._arr);
Swap(tmp);
}
SeqList& operator= (SeqList sList)
{
Swap(sList);
return *this;
}
~SeqList()
{
if (_arr)
{
delete[] _arr;
}
}
private:
void _Cheekcapacity()
{
if (_size == _capacity) //判断链表是否已满
{
_capacity = _capacity * 2;
DataType* tmp = new DataType[_capacity];
memcpy(tmp, _arr, sizeof(DataType)* _size);
delete[] _arr;
_arr = tmp;
}
}
public:
void PushBack(const DataType& x) //尾插
{
assert(this);
_Cheekcapacity();
_arr[_size] = x;
_size++;
}
void PopBack() //尾删
{
assert(this);
if (_size == 0)
{
cout << "SeqList is empty" << endl;
return;
}
_arr[ --_size] = 0;
}
void PushFront(const DataType& x) //头插
{
assert(this);
_Cheekcapacity();
for (int i = _size; i > 0; i--)
{
_arr[i] = _arr[i - 1];
}
_arr[0] = x;
_size++;
}
void PopFront() //头删
{
assert(this);
if (_size == 0)
{
cout << "SeqList is empty" << endl;
return;
}
for (size_t i = 0; i < _size - 1; i++)
{
_arr[i] = _arr[i + 1];
}
_size--;
}
void Print()
{
assert(this);
if (_size == 0)
{
cout << "SeqList is empty" << endl;
return;
}
for (size_t i = 0; i < _size; i++)
{
cout << _arr[i] << "->";
}
cout <<"NULL"<<endl;
}
void Insert(size_t index, const DataType& x)
{
assert(this);
_Cheekcapacity();
if (index < _size)
{
for (size_t i = _size; i > index; i--)
{
_arr[i] = _arr[i - 1];
}
_arr[index] = x;
_size++;
}
}
void Modified(size_t index, const DataType& x) //将index位置的元素修改为x
{
assert(this);
if (index < _size)
{
_arr[index] = x;
}
}
void Remove(size_t index)
{
assert(this);
if (index < _size)
{
for (size_t i = index; i < _size - 1; i++)
{
_arr[i] = _arr[i + 1];
}
_size--;
}
}
FindRet Find(const DataType& x)
{
Findret ret;
assert(this);
for (size_t i = 0; i < _size; i++)
{
if (_arr[i] == x)
{
ret.isFind = true;
ret.index = i;
cout << "find " << x << endl;
return ret;
}
}
ret.isFind = false;
cout << "not find "<< x << endl;
return ret;
}
private:
DataType *_arr;
size_t _size;
size_t _capacity;
};
#endif //_SEQ_LIST
单链表
#pragma once
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
typedef int DataType ;
typedef struct Node
{
DataType data;
struct Node* next;
}Node, *PLinkList;
void InitList(PLinkList* pplist); //初始化
void PrintList(PLinkList* pplist); //打印
int GetLength(PLinkList plist);//求长度
void PushBack(PLinkList* pplist, DataType x);//尾插
void PushFront(PLinkList* pplist, DataType x);//头插
void PopFront(PLinkList* pplist);//头删
void PopBack(PLinkList* pplist);//尾删
Node* Find(PLinkList plist, DataType x);
void Insert(PLinkList* pplist, Node * n, DataType x);
void InsertFrontNode(Node *n, DataType x);
void Reverse(PLinkList *pplist); // 翻转
void sort(PLinkList *plist);//排序
void PrintTailToHead(PLinkList plist);
PLinkList _CreateNode(DataType x)
{
PLinkList tmp = (PLinkList)malloc(sizeof(Node));
tmp->data = x;
tmp->next = NULL;
return tmp;
}
void InitList(PLinkList* pplist)
{
assert(pplist);
*pplist = 0;
}
void PrintList(PLinkList* pplist)
{
PLinkList begin = *pplist;
assert(pplist);
while (begin != NULL)
{
printf("%d->", begin->data);
begin = begin->next;
}
printf("NULL\n");
}
void PushBack(PLinkList* pplist, DataType x)
{
PLinkList tmp;
PLinkList begin = *pplist;
assert(pplist);
if (*pplist == NULL)
{
*pplist = _CreateNode(x);
}
else
{
tmp = _CreateNode(x);
while (begin->next != NULL)
{
begin = begin->next;
}
begin->next = tmp;
tmp->next = NULL;
}
}
void PushFront(PLinkList* pplist, DataType x)
{
PLinkList tmp;
assert(pplist);
if (*pplist == NULL)
{
*pplist = _CreateNode(x);
}
else
{
tmp = _CreateNode(x);
tmp->next = *pplist;
*pplist = tmp;
}
}
int GetLength(PLinkList plist)
{
int count = 0;
PLinkList begin = plist;
assert(plist);
while (begin != NULL)
{
begin = begin->next;
count++;
}
return count;
}
void PopFront(PLinkList* pplist)
{
PLinkList tmp;
assert(pplist);
if (*pplist == NULL)
{
printf("SList is empty\n");
return;
}
else
{
tmp = *pplist;
*pplist = (*pplist)->next;
free(tmp);
}
}
void PopBack(PLinkList* pplist)
{
PLinkList prev = *pplist,end = *pplist;
assert(pplist);
if (*pplist == NULL)
{
printf("SList is empty\n");
return;
}
else if ((*pplist)->next == NULL)//只有一个节点
{
free(*pplist);
*pplist = NULL;
printf("delet success\n");
return;
}
else //有多个节点
{
while (end->next != NULL)
{
prev = end;
end = end->next;
}
prev->next = NULL;
free(end);
}
}
Node* Find(PLinkList plist, DataType x)
{
PLinkList begin = plist;
assert(plist);
if (plist == NULL)
{
printf("SList is empty\n");
return begin;
}
else
{
while (begin != NULL)
{
if (begin->data == x)
{
printf("find %d\n", x);
return begin;
}
begin = begin->next;
}
}
printf("not find %d\n", x);
return NULL;
}
void Insert(PLinkList* pplist, Node * n, DataType x)
{
PLinkList tmp;
assert(pplist);
if (*pplist == NULL)
{
*pplist = _CreateNode(x);
return;
}
else
{
tmp = _CreateNode(x);
tmp->next = n->next;
n->next = tmp;
}
}
void DelMidNode(Node* n)
{
assert(n);
if ( n == NULL)
{
printf("node is NULL\n");
return;
}
else if (n->next == NULL)
{
printf("n is trail node\n");
}
else
{
Node* tmp = n->next;
n->data = tmp->data;
n->next = tmp->next;
free(tmp);
}
}
int Remove(PLinkList* pplist, Node * n)
{
assert(pplist);
if ((n->next) == NULL)
{
PopBack(pplist);
return 0;
}
else
{
DelMidNode(n);
return 0;
}
}
void Erase(PLinkList* pplist, DataType x, int all)
{
Node* ret = *pplist;
assert(pplist);
do
{
ret = Find(ret,x);
if (ret != NULL)
{
Remove(pplist, ret);
}
} while (ret != 0 && all != 0);
}
void InsertFrontNode(Node *n, DataType x)
{
Node *tmp;
assert(n);
if (n == NULL)
{
printf("n is NULL\n");
return;
}
else
{
tmp = _CreateNode(n->data);
tmp->next = n->next;
n->next = tmp;
n->data = x;
}
}
//逆置/反转单链表
void Reverse(PLinkList *pplist)
{
Node* NewHead = NULL;
Node* begin = (*pplist)->next;
assert(pplist);
if (*pplist != NULL)
{
// 取第一个节点做新的头结点
NewHead = *pplist;
NewHead->next = NULL;
}
while (begin != NULL)
{
// 取节点进行头插
Node* tmp = begin;
begin = begin->next;
tmp->next = NewHead;
NewHead = tmp;
}
*pplist = NewHead;
}
//链表排序
void sort(PLinkList *pplist)
{
int count = 0;
int exchange = 0;
PLinkList tmp = *pplist;
assert(pplist);
if (*pplist == NULL)
{
printf("SList is empty\n");
return;
}
else
{
while (count < GetLength(*pplist))
{
exchange = 0;
while (tmp->next != NULL)
{
if (tmp->data > tmp->next->data)
{
DataType x = tmp->data;
tmp->data = tmp->next->data;
tmp->next->data = x;
exchange = 1;
}
tmp = tmp->next;
}
if (exchange == 0)
{
return;
}
tmp = *pplist; //又从头开始检查
count++;
}
}
}
//从尾到头打印单链表
void PrintTailToHead(PLinkList pList)
{
if(pList)
{
PrintTailToHead(pList->next); //递归调用
printf("%d->", pList->data);
}
}
//单链表实现约瑟夫环
void JosephCycle(PLinkList* ppList, int k)
{
Node* begin = *ppList;
assert(ppList);
if (*ppList == NULL)
{
return;
}
else
{
while (1)
{
if (begin->next == begin) //只剩下一个节点
{
break;
}
Node* del = NULL;
int x = k;
while (--x)
{
begin = begin->next;
}
//应用删除一个无头无尾的中间节点的思想
printf("delete node is %d\n", begin->data);
begin->data = begin->next->data;
del = begin->next;
begin->next = del->next;
free(del);
}
*ppList = begin;
}
}
//合并两个有序链表,合并后依然有序
PLinkList Merge(PLinkList pList1, PLinkList pList2)
{
PLinkList pList = pList1; //新的头节点
PLinkList end;
assert(pList1);
assert(pList2);
if (pList1 == NULL)
{
return pList2;
}
if (pList2 == NULL)
{
return pList1;
}
if (pList1 == pList2)
{
return pList1;
}
//选择一个小的作为头节点
if (pList1->data < pList2->data)
{
pList = pList1;
pList1 = pList1->next;
}
else
{
pList = pList2;
pList2 = pList2->next;
}
end = pList; //链接下一个节点
while (pList1 && pList2)
{
Node* tmp = NULL;
if (pList1->data < pList2->data)
{
tmp = pList1;
pList1 = pList1->next;
}
else
{
tmp = pList2;
pList2 = pList2->next;
}
end->next = tmp;
end = tmp;
}
if (pList1)
{
end->next = pList1;
}
if (pList2)
{
end->next = pList2;
}
return pList;
}
//查找单链表的中间节点,要求只能遍历一次链表
Node* FindMidNode(PLinkList *ppList)
{
PLinkList slow = *ppList;
PLinkList fast = *ppList;
assert(ppList);
if (*ppList == NULL)
{
return NULL;
}
else
{
while (fast && fast->next)
{
fast = fast->next->next; //快指针一次走两步
slow = slow->next; //慢指针一次走一步
}
printf("MidNode is %d\n", slow->data);
return slow;
}
}
//查找单链表的倒数第k个节点,要求只能遍历一次链表
Node* FindKNode(PLinkList *ppList,DataType k)
{
int x = k;
PLinkList slow = *ppList;
PLinkList fast = *ppList;
assert(ppList);
if (*ppList == NULL)
{
return NULL;
}
else
{
while (fast && x--) //快指针先走k步
{
fast = fast->next;
}
if (x > 0)//链表总共都没有k个节点
{
return NULL;
}
while (fast) //当快指针走到结尾慢指针走到倒数第k个节点
{
fast = fast->next;
slow = slow->next;
}
printf("The last %d node is %d\n", k, slow->data);
return slow;
}
}
//判断单链表是否带环
bool IsCycle(PLinkList pList, PLinkList *ppMeetNode)
{
assert(pList);
PLinkList slow = pList;
PLinkList fast = pList;
while (fast && fast->next)
{
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
{
*ppMeetNode = slow;
return true;
}
}
*ppMeetNode = NULL;
return false;
}
队列
#include<iostream>
#include<assert.h>
using namespace std;
template<typename T>
struct Node
{
template<class T>
friend class Queue;
public:
Node(const T& x)
:_data(x)
, _next(NULL)
{}
private:
T _data;
Node* _next;
};
template<class T>
class Queue
{
public:
Queue()
:_QHead(NULL)
, _QTail(NULL)
{}
~Queue()
{
while (_size != 0)
{
Node<T>* del = _QHead;
_QHead = _QHead->_next;
delete del;
_size--;
}
}
void Push(const T& x) //队尾插入元素
{
assert(this);
if (_QHead == NULL)
{
_QHead = new Node<T>(x);
_QTail = _QHead;
}
else
{
Node<T>* tmp = new Node<T>(x);
_QTail->_next = tmp;
_QTail = tmp;
}
_size++;
}
void Pop() //队头删除元素
{
assert(this);
if (_QHead == NULL) //链表为空
{
return;
}
if (_QHead == _QTail) //只有一个节点
{
delete _QHead;
_QHead = NULL;
_QTail = NULL;
}
else //有多个节点
{
Node<T>* del = _QHead;
_QHead = _QHead->_next;
delete del;
}
_size--;
}
bool Empty() //判空
{
return _size == 0;
}
int Size() //返回队列长度
{
return _size;
}
const T& Fornt() //返回队头元素
{
return _QHead->_data;
}
const T& Back() //返回队尾元素
{
return _QTail->_data;
}
private:
Node<T>* _QHead;
Node<T>* _QTail;
size_t _size;
};
栈
#pragma once
#include <assert.h>
template<class T>
class Stack
{
public:
Stack()
:_capacity(3)
,_size(0)
,_data(new T[_capacity])
{}
~Stack()
{
if (_data)
{
delete [] _data;
}
}
public:
void Push(const T& x)
{
_CheckCapacity();
_data[_size++] = x;
}
void Pop()
{
assert(_size > 0);
--_size;
}
const T& Top()
{
assert(_size > 0);
return _data[_size - 1];
}
size_t Size()
{
return _size;
}
bool Empty()
{
return _size == 0;
}
void Print()
{
for (size_t i = 0; i < _size; ++i)
{
cout<<_data[i]<<"->";
}
}
private:
void _CheckCapacity()
{
if (_capacity == _size)
{
T* tmp = new T[_capacity];
//
// 如果T是自定类型的考虑深浅拷贝的问题
// 当T是string等类型时得用operator=
//
//memcpy(tmp, _data, _size*sizeof(T));
for(size_t i = 0; i < _size; ++i)
{
tmp[i] = _data[i];
}
_capacity *= 2;
delete[] _data;
_data = tmp;
}
}
private:
size_t _size; // 数据的个数
size_t _capacity; // 容量
T* _data; // 指向数据块的指针
};