数据结构
一、线性表
1. 顺序表(静态分配)
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define MaxSize 100
typedef struct {
int data[MaxSize];
int Length;
} SqList;
Status InitList(SqList &L) {
for (int i = 0; i < MaxSize; i++) {
L.data[i] = 0; }
L.Length = 0;
return OK;
}
Status TraverseListSq_Static(SqList L) {
int i;
for (i = 0; i < L.Length; ++i) {
cout << L.data[i] << " ";
}
cout << endl;
return OK;
}
Status ListInsert(SqList &L, int i, int e) {
if (i < 1 || i > L.Length + 1) {return FALSE;}
if (L.Length > MaxSize) {return FALSE;}
for (int j = L.Length; j > i - 1; j--) {
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.Length++;
return OK;
}
Status GetElem(SqList L, int i, int &e) {
if (i<1 || i>L.Length+1){
return ERROR;
}
e = L.data[i - 1];
return OK;
}
int LocateListSq_Static(SqList L, ElemType e) {
int i;
for (i = 0; i < L.Length; ++i) {
if (L.data[i] == e) {
break;
}
}
return i;
}
Status ListDelete(SqList &L, int i, int &e) {
if (i < 1 || i > L.Length + 1) { return FALSE; }
e = L.data[i - 1];
for (int j = i-1; j < L.Length - 1; j++) {
L.data[j] = L.data[j+1];
}
L.Length--;
return OK;
}
int LengthListSq_Static(SqList L) {
return L.Length;
}
Status EmptyListSq_Static(SqList L) {
return L.Length == 0;
}
Status ClearListSq_Static(SqList &L) {
L.Length = 0;
return OK;
}
2. 顺序表(动态分配)
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define ListMaxSize 100
typedef struct {
ElemType *elem;
int length;
int listsize;
} SqList;
Status InitListSq(SqList &L) {
L.elem = new ElemType[ListMaxSize];
if (!L.elem) return OVERFLOW;
L.length = 0;
L.listsize = ListMaxSize;
return OK;
}
Status GetListSq(SqList L, int i, ElemType &e) {
if (i < 1 || i > L.length) return ERROR;
e = *(L.elem + i - 1);
return OK;
}
int LocateListSq(SqList L, ElemType e) {
int i;
ElemType *p;
i = 1;
p = L.elem;
while (i <= L.length && *p++ != e) i++;
if (i <= L.length) return i;
else return 0;
}
Status InsertListSq(SqList &L, int i, ElemType e) {
ElemType *p, *q;
if (i < 1 || i > L.length + 1) return ERROR;
if (L.length >= L.listsize) return OVERFLOW;
q = L.elem + i - 1;
for (p = L.elem + L.length - 1; p >= q; p--) *(p + 1) = *p;
*q = e;
L.length++;
return OK;
}
Status DeleteListSq(SqList &L, int i, ElemType &e) {
ElemType *p, *q;
if (i < 1 || i > L.length) return ERROR;
p = L.elem + i - 1;
e = *p;
q = L.elem + L.length - 1;
for (++p; p <= q; p++) *(p - 1) = *p;
L.length--;
return OK;
}
void TraverseListSq(SqList L) {
ElemType *p;
p = L.elem;
while (p <= L.elem + L.length - 1)
cout << *p++ << " ";
cout << endl;
}
int LengthListSq(SqList L) {
return L.length;
}
Status EmptyListSq(SqList L) {
return L.length == 0;
}
void ClearListSq(SqList &L) {
L.length = 0;
}
void DestroyListSq(SqList &L) {
L.length = L.listsize = 0;
delete[] L.elem;
L.elem == NULL;
}
void InverseListSq(SqList &L) {
ElemType *p, *q, temp;
for (p = L.elem, q = L.elem + L.length - 1; p < q; p++, q--) {
temp = *p;
*p = *q;
*q = temp;
}
}
void Inverse(SqList &L, int start, int end) {
int i, j;
ElemType temp;
for (i = start, j = end; i < j; i++, j--) {
temp = L.elem[i - 1];
L.elem[i - 1] = L.elem[j - 1];
L.elem[j - 1] = temp;
}
}
void CycMoveListSq(SqList &L, int m) {
if (m < 0) {
cout << "输入的左移位个数错误!" << endl;
return;
}
if (L.length <= 1) return;
m %= L.length;
Inverse(L, 1, L.length);
Inverse(L, 1, L.length - m);
Inverse(L, L.length - m + 1, L.length);
}
void SortListSq(SqList &L) {
int i, j, k;
ElemType temp;
for (i = 0; i < L.length - 1; i++) {
k = i;
for (j = i + 1; j < L.length; j++)
if (*(L.elem + k) > *(L.elem + j)) k = j;
if (k != i) {
temp = *(L.elem + i);
*(L.elem + i) = *(L.elem + k);
*(L.elem + k) = temp;
}
}
}
void Josephus(SqList &L, int n, int m, int s) {
L.length = n;
for (int i = 0; i < n; i++)
L.elem[i] = i + 1;
cout << "圆桌初始状态:" << endl;
TraverseListSq(L);
int len = n;
int i;
int p = (s - 1) % len;
while (len > 1) {
p = (p + m - 1) % len;
int t = L.elem[p];
for (i = p + 1; i < len; i++)
L.elem[i - 1] = L.elem[i];
L.elem[len - 1] = t;
len--;
}
InverseListSq(L);
cout << "离开圆桌的顺序为:" << endl;
TraverseListSq(L);
}
3. 单链表
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status InitList_L(LinkList &L) {
L = new LNode;
L->next = NULL;
if(!L){return ERROR;}
return OK;
}
void visit(LinkList L)
{
cout<<L->data<<endl;
}
void TraverseList_L(LinkList L) {
LinkList p=L->next;
while (p) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
Status ListInsert_L(LinkList &L, int i, ElemType e) {
LinkList p = L,s;
int j = 0;
while (p && j < i - 1) {
p = p->next;
++j;
}
if (!p || j>i-1){
return ERROR;
}
s = new LNode;
s->data = e;
s->next=p->next;
p->next=s;
return OK;
}
Status DeleteList_L(LinkList &L, int i, ElemType &e){
LinkList p=L,q;
int j=0;
while (p->next && j<i-1){
p = p->next;
++j;
}
if (!(p->next)||j>i-1){
return ERROR;
}
q=p->next;
p->next=q->next;
e=q->data;
delete q;
return OK;
}
Status LocateElem_L(LinkList L, ElemType &e) {
LinkList p;
p = L->next;
int i=0;
while (p && p->data != e) {
p = p->next;
i++;
}
return i;
}
void CreateList_F(LinkList &L,int n){
L=new LNode;
L->next=NULL;
for(int i=n;i>0;--i){
LinkList p=new LNode;
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
void CreateList_L(LinkList &L,int n){
L=new LNode;
L->next=NULL;
LinkList r=L;
for(int i=0;i<n;++i){
LinkList p=new LNode;
cin>>p->data;
p->next=NULL; r->next=p;
r=p;
}
}
Status GetElem_L(LinkList L, int i, ElemType &e) {
LinkList p;
p = L->next;
int j = 1;
while (p && j < i) {
p = p->next;
++j;
}
if (!p || j > i) {
return ERROR;
}
e = p->data;
return OK;
}
int LengthList_L(LinkList L) {
LinkList p;
p = L->next;
int i = 0;
while (p) {
i++;
p = p->next;
}
return i;
}
Status ClearList(LinkList &L) {
LinkList p, q;
p = L->next;
while (p)
{
q = p->next;
delete p;
p = q;
}
L->next = NULL;
return OK;
}
Status ListEmpty(LinkList L) {
if (L->next) {
return OK;
} else {
return ERROR;
}
}
Status DestroyList_L(LinkList &L) {
while (L) {
LinkList p;
p = L;
L = L->next;
delete p;
}
return OK;
}
int find(LinkList &L,int s)
{
LinkList p=L->next;
int i=0;
int j= LengthList_L(L);
while(p->data!=s && i!=j)
{
p=p->next;
i++;
}
if(i==j) cout<<"未找到";
else cout<<"第"<<i+1<<"个元素"<<endl;
return 0;
}
int reverse(LinkList &L)
{
LinkList p=L->next;
L->next=NULL;
while(p)
{
LinkList q=p->next;
p->next=L->next;
L->next=p;
p=q;
}
return 0;
}
int merge(LinkList &L,LinkList &s)
{
LinkList p=L->next;
L->next=NULL;
int L_len = LengthList_L(L);
int S_len = LengthList_L(s);
L_len+=S_len;
s=s->next;
LinkList o=L;
while(s&&p)
{
if(s->data<p->data)
{
LinkList q=s->next;
s->next=L->next;
L->next=s;
s=q;
}
else
{
LinkList q=p->next;
p->next=L->next;
L->next=p;
p=q;
}
L=L->next;
}
if(p) L->next=p;
else L->next=s;
L=o;
return 0;
}
4. 双链表
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct DulNode {
ElemType data;
struct DulNode *prior, *next;
} DulNode, *DulLinkList;
Status InitDLinkList(DulLinkList &L) {
L = new DulNode;
if (!L) { return ERROR; }
L->prior = L;
L->next = L;
return OK;
}
int ListLength(DulLinkList L) {
int i = 0;
DulLinkList p = L->next;
while (p != L)
{
i++;
p = p->next;
}
return i;
}
void TraverseList_L(DulLinkList L) {
DulLinkList p = L->next;
while (p!=L) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
Status GetElem(DulLinkList L, int i, ElemType &e) {
int j = 1;
DulLinkList p = L->next;
while (p != L && j < i) {
p = p->next;
j++;
}
if (p == L || j > i)
return ERROR;
e = p->data;
return OK;
}
DulLinkList GetElemP(DulLinkList L,int i)
{
int j;
DulLinkList p=L;
if(i<0||i>ListLength(L))
return NULL;
for(j=1;j<=i;j++)
p=p->next;
return p;
}
int LocateElem(DulLinkList L, ElemType e, Status(*compare)(ElemType, ElemType)) {
int i = 0;
DulLinkList p = L->next;
while (p != L) {
i++;
if (compare(p->data, e))
return i;
p = p->next;
}
return 0;
}
Status InsertDulLink(DulLinkList &L, int i, ElemType e) {
DulLinkList p, s;
if (i < 1 || i > ListLength(L) + 1) {
return ERROR;
}
p = GetElemP(L, i - 1);
if (!p)
return ERROR;
s = new DulNode;
s->data = e;
s->prior = p;
s->next = p->next;
p->next->prior = s;
p->next = s;
return OK;
}
Status DeleteDulList(DulLinkList &L, int i, ElemType &e) {
DulLinkList p;
if (i<1){
return ERROR;
}
p = GetElemP(L,i);
if (!p){
return ERROR;
}
e = p->data;
p->prior->next = p->next;
p->next->prior = p->prior;
delete p;
return OK;
}
Status DestroyList_L(DulLinkList &L) {
DulLinkList q, p = L->next;
while (p != L) {
q = p->next;
delete p;
p = q;
}
delete L;
L = NULL;
return OK;
}
5. 静态链表
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define MAXSIZE 1000
typedef struct {
ElemType data;
int cur;
} Component, StaticLinkList[MAXSIZE];
Status InitList(StaticLinkList &space) {
int i;
for (i = 0; i < MAXSIZE - 1; ++i) {
space[i].data=0;
space[i].cur = i + 1;
}
space[MAXSIZE - 1].cur = 0;
return OK;
}
int Malloc_SLL(StaticLinkList &space) {
int i = space[0].cur;
if (space[0].cur) {
space[0].cur = space[i].cur;
}
return i;
}
int ListLength(StaticLinkList L) {
int j = 0;
int i = L[MAXSIZE - 1].cur;
while (i) {
i = L[i].cur;
j++;
}
return j;
}
Status ListInsert(StaticLinkList &L, int i, ElemType e) {
int j, k, l;
k = MAXSIZE - 1;
if (i < 1 || i > ListLength(L) + 1) {
return ERROR;
}
j = Malloc_SLL(L);
if (j) {
L[j].data = e;
for (l = 1; l < i - 1; ++l) {
k = L[k].cur;
}
L[j].cur = L[k].cur;
L[k].cur = j;
return OK;
}
return ERROR;
}
void Free_SSL(StaticLinkList &space, int k) {
space[k].cur = space[0].cur;
space[0].cur = k;
}
Status ListDelete(StaticLinkList &L, int i, ElemType &e) {
int j, k;
if (i < 1 || i > ListLength(L) + 1) {
return ERROR;
}
k = MAXSIZE - 1;
for (j = 1; j <= i - 1; ++j) {
k = L[k].cur;
}
j = L[k].cur;
e = L[j].data;
L[k].cur = L[j].cur;
Free_SSL(L, j);
return OK;
}
Status ListTraverse_LS(StaticLinkList L) {
int k=MAXSIZE-1;
while (L[k].cur){
k = L[k].cur;
cout<<L[k].data<<" ";
}
cout<<endl;
return OK;
}
int GetElem_LS(StaticLinkList L, int i, ElemType &e) {
int j;
for (j = 0; j < ListLength(L); ++j) {
if (i - 1 == j) {
return L[j].data;
}
j = L[j].cur;
}
return -1;
}
int LocateElem_LS(StaticLinkList L, ElemType &e) {
int j;
for (j = 0; j < ListLength(L); ++j) {
if (L[j].data == e) {
return L[j].cur;
}
j = L[j].cur;
}
return -1;
}
6. 循环链表
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
Status InitList_CL(LinkList &L)
{
L=new LNode;
if(!L)
return OVERFLOW;
L->next=L;
return OK;
}
Status DestroyList_CL(LinkList &L)
{
LinkList q,p=L->next;
while(p!=L)
{
q=p->next;
free(p);
p=q;
}
delete L;
L=NULL;
return OK;
}
Status ClearList_CL(LinkList *L)
{
LinkList p,q;
*L=(*L)->next;
p=(*L)->next;
while(p!=*L)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=*L;
return OK;
}
Status ListEmpty_CL(LinkList L)
{
if(L->next==L)
return TRUE;
else
return FALSE;
}
int ListLength_CL(LinkList L)
{
int i=0;
LinkList p=L->next;
while(p!=L)
{
i++;
p=p->next;
}
return i;
}
Status GetElem_CL(LinkList L,int i,ElemType &e)
{
int j=1;
LinkList p=L->next->next;
if(i<=0||i>ListLength_CL(L))
return ERROR;
while(j<i)
{
p=p->next;
j++;
}
e=p->data;
return OK;
}
int LocateElem_CL(LinkList L,ElemType e)
{
int i=0;
LinkList p=L->next->next;
while(p!=L->next)
{
i++;
if(p->data==e)
return i;
p=p->next;
}
return 0;
}
Status PriorElem_CL(LinkList L,ElemType cur_e,ElemType *pre_e)
{
LinkList q,p=L->next->next;
q=p->next;
while(q!=L->next)
{
if(q->data==cur_e)
{
*pre_e=p->data;
return TRUE;
}
p=q;
q=q->next;
}
return FALSE;
}
Status NextElem_CL(LinkList L,ElemType cur_e,ElemType *next_e)
{
LinkList p=L->next->next;
while(p!=L)
{
if(p->data==cur_e)
{
*next_e=p->next->data;
return TRUE;
}
p=p->next;
}
return FALSE;
}
Status ListInsert_CL(LinkList &L,int i,ElemType e)
{
LinkList p=L->next,s;
int j=0;
if(i<=0||i>ListLength_CL(L)+1)
return ERROR;
while(j<i-1)
{
p=p->next;
j++;
}
s=(LinkList)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
if(p==L)
L=s;
return OK;
}
Status ListDelete_CL(LinkList &L,int i,ElemType &e)
{
LinkList p=L->next,q;
int j=0;
if(i<=0||i>ListLength_CL(L))
return ERROR;
while(j<i-1)
{
p=p->next;
j++;
}
q=p->next;
p->next=q->next;
e=q->data;
if(L==q)
L=p;
delete q;
return OK;
}
Status ListTraverse_CL(LinkList L)
{
LinkList p=L->next->next;
while(p!=L->next)
{
cout<<p->data<<" ";
p=p->next;
}
printf("\n");
return OK;
}
二、栈
1. 顺序栈
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType data[MAXSIZE];
int top;
} SqStack;
Status InitStack(SqStack &S){
S.top = -1;
return OK;
}
Status StackEmpty(SqStack S){
if (S.top ==-1){
return TRUE;
} else {
return FALSE;
}
}
Status Push(SqStack &S, SElemType e) {
if (S.top == MAXSIZE - 1) {
return ERROR;
}
S.top++;
S.data[S.top] = e;
return OK;
}
Status Pop(SqStack &S, SElemType &e) {
if (S.top == -1) {
return ERROR;
}
e = S.data[S.top];
S.top--;
cout<<"弹出栈:"<<e<<endl;
return OK;
}
Status GetTop(SqStack S, SElemType &e){
if (S.top == -1){
return FALSE;
}
e = S.data[S.top];
return TRUE;
}
2. 链栈
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct StackNode{
SElemType data;
struct StackNode *next;
}StackNode, *LinkStack;
Status InitStack(LinkStack &S){
S=NULL;
return OK;
}
Status StackEmpty(LinkStack &S){
if (S==NULL){
return TRUE;
} else {
return FALSE;
}
}
Status Push(LinkStack &S, SElemType e){
LinkStack p;
p = new StackNode;
if (!p){
return OVERFLOW;
}
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S, SElemType &e){
LinkStack p;
if (S==NULL){
return ERROR;
}
e = S->data;
p=S;
S=S->next;
delete p;
return OK;
}
Status GetTop(LinkStack S, SElemType &e){
if (S==NULL){
exit(1);
} else {
return S->data;
}
}
3. 共享空间
#include <iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INTEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType data[MAXSIZE];
int top1;
int top2;
} SqDoubleStack;
Status Pop(SqDoubleStack &S, SElemType &e, int stackNum) {
if (stackNum == 1 && S.top1 != -1) {
e = S.data[S.top1];
S.top1--;
return OK;
}
if (stackNum == 2 && S.top2 != MAXSIZE) {
e = S.data[S.top2];
S.top2++;
return OK;
}
return ERROR;
}
Status Push(SqDoubleStack &S, SElemType e, int stackNum) {
if (stackNum == 1 && S.top1 + 1 < S.top2) {
S.top1++;
S.data[S.top1] = e;
return OK;
}
if (stackNum == 2 && S.top1 + 1 < S.top2) {
S.top2--;
S.data[S.top2] = e;
return OK;
}
return ERROR;
}
void ShowStack(SqDoubleStack &S) {
for (int i = 0; i <= S.top1; i++) {
cout << S.data[i] << endl;
}
for (int i = S.top2; i <= MAXSIZE - 1; i++) {
cout << S.data[i] << endl;
}
cout << endl;
}
Status ClearSeqDoubleStack(SqDoubleStack &S) {
S.top1 = -1;
S.top2 = MAXSIZE;
return OK;
}
Status InitSeqDoubleStack(SqDoubleStack &S) {
S.top1 = -1;
S.top2 = MAXSIZE;
return OK;
}
int GetLengthSeqDoubleStack(SqDoubleStack &S) {
return S.top1 + 1 + MAXSIZE - S.top2;
}
int main() {
int n,e;
SqDoubleStack S;
cout << "创建顺序栈(分享空间)成功!\n";
if (InitSeqDoubleStack(S)) {
cout << "初始化顺序栈(分享空间)成功!\n";
}
Push(S, 1, 1);
Push(S, 2, 1);
Push(S, 3, 1);
Push(S, 4, 1);
Push(S, 5, 1);
Push(S, 9, 2);
Push(S, 8, 2);
Push(S, 7, 2);
Push(S, 6, 2);
puts("展示元素:");
ShowStack(S);
printf("元素个数:%d\n", GetLengthSeqDoubleStack(S));
SElemType e1;
SElemType e2;
cout<<"弹栈\n";
cout<<"--------------------------------\n";
for (int i = 0; i < n; ++i) {
Pop(S,i,e);
cout<<"栈顶数据为"<<e<<endl;
}
ShowStack(S);
ClearSeqDoubleStack(S);
cout << endl;
return 0;
}