├── README.md ├── a(1).out ├── test01.cpp ├── 汉诺塔问题.cpp ├── 汉诺塔问题.exe ├── 第2章-线性表 ├── 1.静态顺序表.cpp ├── 2.动态顺序表.cpp ├── 3.带头节点的单链表.cpp ├── 4.不带头节点的单链表.cpp ├── 5.双链表.cpp ├── 6循环单链表.cpp ├── 7循环双链表.cpp ├── 8静态链表.cpp └── a.out └── 第3章-栈、队列、数组 ├── 1.顺序栈.cpp ├── 2.顺序栈的另一种实现方式.cpp ├── 3.共享栈.cpp ├── 4.链栈(不带头节点).cpp ├── 5.链栈(带头节点).cpp ├── 5链栈(带头节点).cpp ├── 6.队列的顺序实现.cpp ├── 7.队列的链式实现(不带头节点).cpp └── 8.队列的链式实现(带头节点).cpp /README.md: -------------------------------------------------------------------------------- 1 | # 王道408计算机数据结构代码 2 | 王道408计算机考研💯💻数据结构代码cpp具体实现(Updating🚀) 3 | 4 | ### 一、第一章 绪论 5 | 6 | 第一章是绪论,略 7 | 8 | ### 二、第二章 线性表 9 | 10 | 顺序表、单链表、双链表、循环链表、静态恋表的cpp代码具体实现 11 | 12 | ### 三、第三章 栈,队列,数组 13 | 14 | 顺序栈、共享栈、链栈的cpp代码具体实现 15 | 16 | ### 四、第四章 串 17 | 18 | 不考 19 | 20 | ### 五、第五章 树 21 | 22 | 看情况更新... 23 | 24 | ### 六、第六章 图 25 | 26 | 看情况更新... 27 | 28 | ### 七、第七章 排序 29 | 30 | 课本上基本都有... 31 | 32 | --- 33 | 34 | 火速updating中🚀🚀欢迎PR 35 | -------------------------------------------------------------------------------- /a(1).out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eglfiv/408-DataStructure-Study/cf48c8415d7461053e38435d587fd6d4ffefb701/a(1).out -------------------------------------------------------------------------------- /test01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef int ElemType; 4 | typedef struct SNode{ 5 | ElemType data; 6 | struct SNode *next; 7 | }SNode,*LinkStake; 8 | 9 | bool InitStake(LinkStake &S){ 10 | S=(SNode *)malloc(sizeof(SNode)); 11 | if(S==NULL) 12 | return false; 13 | S->next=NULL; 14 | return true; 15 | } 16 | 17 | bool InsertStake(LinkStake &S,ElemType e){ 18 | SNode *t=(SNode *)malloc(sizeof(SNode)); 19 | if(t==NULL) 20 | return false; 21 | t->data=e; 22 | t->next=S->next; 23 | S->next=t; 24 | return true; 25 | } 26 | 27 | int main(){ 28 | 29 | } -------------------------------------------------------------------------------- /汉诺塔问题.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int step; 3 | void hanoi(char a, char b, char c, int n) { 4 | if (n == 0) 5 | return; 6 | hanoi(a, c, b, n-1);//将n-1个盘子由A经过C移动到B 7 | printf ("step %d: move %d from %cto%c\n", step++, n, a, c); 8 | hanoi(b, a, c,n-1);//剩下的n-1盘子,由B经过A移动到C 9 | } 10 | int main() { 11 | int n; 12 | while( scanf ("%d", &n)) { 13 | step=1; 14 | hanoi('A', 'B', 'C',n); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /汉诺塔问题.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eglfiv/408-DataStructure-Study/cf48c8415d7461053e38435d587fd6d4ffefb701/汉诺塔问题.exe -------------------------------------------------------------------------------- /第2章-线性表/1.静态顺序表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MaxSize 10 3 | typedef struct{ 4 | int data[MaxSize]; 5 | int length; 6 | }SqList; 7 | 8 | void InitList(SqList &L){ 9 | for(int i;i=i;j++){ 18 | L.data[j]=L.data[j-1]; 19 | } 20 | L.data[i-1]=e; 21 | L.length=L.length+1; 22 | } 23 | 24 | //删除 25 | bool ListDelete(SqList &L,int i,int &e){ 26 | if(i<1||i>L.length){ 27 | return false; 28 | }; 29 | e=L.data[i-1]; 30 | for(int j=i;j<=L.length;j++){ 31 | L.data[j-1]=L.data[j]; 32 | } 33 | L.length--; 34 | return true; 35 | }for(int i;i 2 | #include 3 | #define InitSize 10 4 | typedef struct{ 5 | int *data; //这里等价int data[];但是cpp不支持未知长度数组,所以只能借助指针实现 6 | int MaxSize,length; 7 | }SeqList; 8 | 9 | void InitList(SeqList &L){ 10 | L.data=(int *)malloc(InitSize*sizeof(int)); 11 | L.MaxSize=InitSize; 12 | L.length=0; 13 | } 14 | 15 | void IncreaseSize(SeqList &L, int len){ 16 | int *p=L.data; 17 | L.data=(int *)malloc((L.MaxSize+len)*sizeof(int)); 18 | for(int i=0;i 3 | #include 4 | typedef struct LNode{ 5 | int data; 6 | struct LNode *next; 7 | }LNode,*LinkList; 8 | 9 | //带头节点单链表初始化(更常用) 10 | bool InitList(LinkList L){ 11 | L=(LNode *)malloc(sizeof(LNode)); 12 | if(L==NULL) 13 | return false; 14 | L->next=NULL; 15 | return true; 16 | } 17 | //带头节点插入 18 | bool ListInsert(LinkList &L,int i, int e){ 19 | if(i<1) 20 | return false; 21 | LNode *p=L; //指定p指向当前扫描到的结点,L指向头节点,头节点不存数据 22 | int j=0; //当前p指向的是第几个节点 23 | while(p!=NULL&&jnext; 25 | j++; 26 | } 27 | if(p==NULL) 28 | return false; 29 | LNode *s=(LNode *)malloc(sizeof(LNode)); 30 | s->data=e; 31 | s->next=p->next; 32 | p->next=s; 33 | return true; 34 | } 35 | //指定节点的后插操作(有无头节点都可) 36 | bool InsertNextNode(LNode *p,int e){ 37 | if(p==NULL) 38 | return false; 39 | LNode *s=(LNode *)malloc(sizeof(LNode)); 40 | if(s==NULL)//内存分配失败 41 | return false; 42 | s->data=e; 43 | s->next=p->next; 44 | p->next=s; 45 | return true; 46 | } 47 | //指定节点前插操作(有无头节点都可),时间复杂度 O(1) 48 | bool InsertPriorNode(LNode *p,int e){ 49 | if(p==NULL) 50 | return false; 51 | LNode *s=(LNode*)malloc(sizeof(LNode)); 52 | if(s==NULL) 53 | return false; 54 | s->next=p->next; 55 | p->next=s; 56 | s->data=p->data; 57 | p->data=e; 58 | return true; 59 | } 60 | //删除节点(带头节点) 61 | bool ListDelete(LinkList &L,int i,int &e){ 62 | if(i<1) 63 | return false; 64 | LNode *p=L; 65 | int j=0; 66 | while(p!=NULL&&jnext; 68 | j++; 69 | } 70 | if(p==NULL) 71 | return false; 72 | if(p->next==NULL) 73 | return false; 74 | LNode *q=p->next; 75 | e=q->data; 76 | p->next=q->next; 77 | free(q); 78 | return true; 79 | } 80 | //删除指定节点p(有无头节点都可) 81 | //当p是最后一个节点时有bug 82 | bool DeleteNode(LNode *p){ 83 | if(p==NULL) 84 | return false; 85 | LNode *q=p->next; 86 | p->data=p->next->data; 87 | p->next=q->next; 88 | free(q); 89 | return true; 90 | } 91 | //按位查找 92 | LNode *GetElem(LinkList L,int i){ 93 | if(i<0) 94 | return NULL; 95 | LNode *p=L; 96 | int j=0; 97 | while(p!=NULL&&jnext; 99 | j++; 100 | } 101 | return p; 102 | } 103 | //按值查找 104 | LNode *LocateElem(LinkList L,int e){ 105 | LNode *p=L->next; 106 | while(p!=NULL&&p->data!=e){ 107 | p=p->next; 108 | } 109 | return p; 110 | } 111 | //求表的长度 112 | int Length(LinkList L){ 113 | int len=0; 114 | LNode *p=L; 115 | while(p->next!=NULL){ 116 | p=p->next; 117 | len++; 118 | } 119 | return len; 120 | } 121 | 122 | //循环尾插法建立单链表 123 | LinkList List_TailInsert(LinkList &L){ 124 | int x; 125 | L=(LinkList)malloc(sizeof(LNode));//建立一个头节点 126 | LNode *s,*r=L; //r为尾指针 127 | scanf("%d",&x); 128 | while(x!=9999){ 129 | s=(LinkList)malloc(sizeof(LNode)); 130 | s->data=x; 131 | r->next=s; 132 | r=s; 133 | scanf("%d",&x); 134 | } 135 | return L; 136 | } 137 | //头插法 138 | LinkList List_HeadInsert(LinkList &L){ 139 | LNode *s; 140 | int x; 141 | L=(LinkList)malloc(sizeof(LNode)); 142 | L->next=NULL; 143 | scanf("%d",&x); 144 | while(x!=9999){ 145 | s=(LNode*)malloc(sizeof(LNode)); 146 | s->data=x; 147 | s->next=L->next; 148 | L->next=s; 149 | scanf("%d",&x); 150 | } 151 | return 0; 152 | } 153 | 154 | 155 | int main(){ 156 | LinkList L; 157 | List_TailInsert(L); 158 | 159 | } 160 | -------------------------------------------------------------------------------- /第2章-线性表/4.不带头节点的单链表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef struct LNode{ 4 | int data; 5 | struct LNode *next; 6 | }LNode,*LinkList; 7 | 8 | bool InitList(LinkList &L){ 9 | L=NULL; 10 | return true; 11 | } 12 | 13 | bool Empty(LinkList L){ 14 | return L==NULL; 15 | } 16 | 17 | //不带头节点插入 18 | bool ListInsert(LinkList &L,int i,int e){ 19 | if(i<1) 20 | return false; 21 | //插入第一个节点操作与其他节点不同 22 | if(i==1){ 23 | LNode *s=(LNode *)malloc(sizeof(LNode)); 24 | s->data=e; 25 | s->next=L->next; 26 | L=s; 27 | return true; 28 | } 29 | //当i!=1的情况 30 | LNode *p=L; 31 | int j=1; 32 | while(p!=NULL&&jnext; 34 | j++; 35 | } 36 | if(p==NULL) 37 | return false; 38 | LNode *s=(LNode *)malloc(sizeof(LNode)); 39 | s->data=e; 40 | s->next=p->next; 41 | p->next=s; 42 | return true; 43 | } 44 | 45 | int main(){ 46 | } -------------------------------------------------------------------------------- /第2章-线性表/5.双链表.cpp: -------------------------------------------------------------------------------- 1 | //带头节点 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct DNode{ 6 | ElemType data; 7 | struct DNode *prior,*next; 8 | }DNode,*DLinkList; 9 | 10 | bool InitDLinkList(DLinkList &L){ 11 | L=(DNode*)malloc(sizeof(DNode)); 12 | if(L==NULL) 13 | return false; 14 | L->prior=NULL; //这里指向头节点就是循环双链表的初始化操作 15 | L->next=NULL; 16 | return true; 17 | } 18 | bool Empty(DLinkList L){ 19 | if(L->next==NULL) 20 | return true; 21 | else 22 | return false; 23 | } 24 | bool InsertNextDNode(DNode *p,DNode *s){ 25 | if(p==NULL||s==NULL) 26 | return false; 27 | s->next=p->next; 28 | if(p->next==NULL) 29 | p->next->prior=s->prior; 30 | p->next=s; 31 | s->prior=p; 32 | return true; 33 | } 34 | //删除p结点的后继结点 35 | bool DeleteNextDNode(DNode *p){ 36 | if(p==NULL) 37 | return false; 38 | DNode *q=p->next; 39 | if(p==NULL) 40 | return false; 41 | p->next=q->next; 42 | if(q->next!=NULL) 43 | q->next->prior=p; 44 | free(q); 45 | return true; 46 | } 47 | //销毁双链表 48 | void DestroyList(DLinkList &L){ 49 | while(L->next!=NULL){ 50 | DeleteNextDNode(L); 51 | } 52 | free(L); 53 | L=NULL; 54 | } 55 | 56 | int main(){ 57 | DLinkList L; 58 | InitDLinkList(L); 59 | } -------------------------------------------------------------------------------- /第2章-线性表/6循环单链表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef int ElemType; 4 | typedef struct LNode{ 5 | ElemType data; 6 | struct LNode *next; 7 | }LNode,*LinkList; 8 | 9 | bool InitList(LinkList &L){ 10 | L=(LNode*)malloc(sizeof(LNode)); 11 | if(L==NULL)return false; 12 | L->next=L; 13 | return true; 14 | } 15 | 16 | //判断是否是尾结点 17 | bool isTail(LinkList L,LNode *p){ 18 | if(p->next==L) 19 | return true; 20 | else 21 | return false; 22 | } 23 | 24 | int main(){ 25 | 26 | } -------------------------------------------------------------------------------- /第2章-线性表/7循环双链表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | typedef int ElemType; 4 | typedef struct DNode{ 5 | ElemType data; 6 | struct DNode *prior,*next; 7 | }DNode,*DLinkList; 8 | bool InitDLinkList(DLinkList &L){ 9 | L=(DNode*)malloc(sizeof(DNode)); 10 | if(L==NULL) 11 | return false; 12 | L->prior=L; 13 | L->next=L; 14 | return true; 15 | } 16 | bool Empty(DLinkList &L){ 17 | if(L-next=L) 18 | return true; 19 | else 20 | return false; 21 | } 22 | bool isTail(DLinkList &L,DNode *p){ 23 | if(p->next==L) 24 | return true; 25 | else 26 | return false; 27 | } 28 | int main(){ 29 | DLinkList L; 30 | InitDLinkList(L); 31 | } -------------------------------------------------------------------------------- /第2章-线性表/8静态链表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | typedef int ElemType 3 | #define MaxSize 10 4 | typedef struct Node{ 5 | ElemType data;//数据元素 6 | int next;//下一个元素的数组下标 7 | }SLinkList[MaxSize]; 8 | 9 | int mian(){ 10 | SLinkList a; 11 | struct Node b[MaxSize];//和上面等价 12 | } -------------------------------------------------------------------------------- /第2章-线性表/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eglfiv/408-DataStructure-Study/cf48c8415d7461053e38435d587fd6d4ffefb701/第2章-线性表/a.out -------------------------------------------------------------------------------- /第3章-栈、队列、数组/1.顺序栈.cpp: -------------------------------------------------------------------------------- 1 | //初始化时候top=-1 2 | #include 3 | #define MaxSize 10 4 | typedef int ElemType; 5 | typedef struct{ 6 | ElemType data[MaxSize]; 7 | int top; 8 | }SqStack; 9 | 10 | void InitStack(SqStack &S){ 11 | S.top=-1;//初始化栈顶指针 12 | } 13 | bool StackEmpty(SqStack &S){ 14 | return S.top!=-1; 15 | } 16 | 17 | //进栈 18 | bool Push(SqStack &S,ElemType x){ 19 | if(S.top==MaxSize-1) 20 | return false; 21 | S.top=S.top+1; 22 | S.data[S.top]=x; 23 | //S.data[++S.top]=x; //区分另一种情况 24 | return true; 25 | } 26 | //出栈(数据还残留在内存中,只是逻辑上被删除) 27 | bool Pop(SqStack &S,ElemType &x){ 28 | if(S.top==-1) 29 | return false; 30 | x=S.data[S.top]; //栈顶元素先出栈 31 | S.top=S.top-1; //指针再减1 32 | //x=S.[S.top--]; 区分另一种情况 33 | return true; 34 | } 35 | //读栈顶元素 36 | bool GetTop(SqStack &S,ElemType &x){ 37 | if(S.top==-1) 38 | return false; 39 | x=S.data[S.top]; 40 | return true; 41 | } 42 | 43 | int main(){ 44 | 45 | } 46 | -------------------------------------------------------------------------------- /第3章-栈、队列、数组/2.顺序栈的另一种实现方式.cpp: -------------------------------------------------------------------------------- 1 | //top指针指向下一个可以插入元素的位置 2 | //初始化时top=0 3 | #include 4 | #define MaxSize 10 5 | typedef int ElemType; 6 | typedef struct{ 7 | ElemType data[MaxSize]; 8 | int top; 9 | }SqStack; 10 | 11 | void InitStack(SqStack &S){ 12 | S.top=0; 13 | } 14 | bool StackEmpty(SqStack &S){ 15 | if(S.top==0) 16 | return true; 17 | else 18 | return false; 19 | } 20 | //进栈 21 | bool Push(SqStack &S,ElemType x){ 22 | if(S.top==MaxSize-1) 23 | return false; 24 | S.data[S.top]=x; 25 | S.top=S.top+1; 26 | //S.data[S.top++]=x; //区分另一种情况 27 | return true; 28 | } 29 | //出栈(数据还残留在内存中,只是逻辑上被删除) 30 | bool Pop(SqStack &S,ElemType &x){ 31 | if(S.top==-1) 32 | return false; 33 | S.top=S.top-1; //指针再减1 34 | x=S.data[S.top]; //栈顶元素先出栈 35 | //x=S.[--S.top]; 区分另一种情况 36 | return true; 37 | } 38 | int main(){ 39 | 40 | } 41 | -------------------------------------------------------------------------------- /第3章-栈、队列、数组/3.共享栈.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #define MaxSize 10 3 | typedef int ElemType; 4 | typedef struct{ 5 | ElemType data[MaxSize]; 6 | int top0; //0号栈栈顶指针 7 | int top1; //1号栈栈顶指针 8 | //栈满条件:top0+1==top1 9 | }ShStack; 10 | void InitStack(ShStack &S){ 11 | S.top0=-1; 12 | S.top1=MaxSize; 13 | } 14 | 15 | int main(){ 16 | 17 | } -------------------------------------------------------------------------------- /第3章-栈、队列、数组/4.链栈(不带头节点).cpp: -------------------------------------------------------------------------------- 1 | //推荐使用不带头节点的情况 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct SNode{ 6 | ElemType data; 7 | struct SNode *next; 8 | }SNode,*LinkStack; 9 | //有不带头节点和带头节点两个版本 10 | //进栈/出栈都只能在栈顶一段进行(链头作为栈顶) 11 | bool InitStack(LinkStack &S){ 12 | S=NULL; 13 | return true; 14 | } 15 | bool Empty(LinkStack &S){ 16 | return S==NULL; 17 | } 18 | 19 | //插入,也就是单链表的头插操作,在p指针后插入元素e 20 | //头插法 21 | bool InsertNextNode(LinkStack S, ElemType e){ 22 | SNode *t = (SNode *)malloc(sizeof(SNode)); 23 | if (t==NULL) 24 | return false; 25 | t->data=e; 26 | t->next=S->next; 27 | S->next=t; 28 | return true; 29 | } 30 | 31 | int main(){ 32 | LinkStack S; 33 | InitStack(S); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /第3章-栈、队列、数组/5.链栈(带头节点).cpp: -------------------------------------------------------------------------------- 1 | //带头节点链栈 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct SNode{ 6 | ElemType data; 7 | struct SNode *next; 8 | }*LinkStake; 9 | 10 | bool InitStake(LinkStake &S){ 11 | S=(SNode *)malloc(sizeof(SNode)); 12 | if(S==NULL) 13 | return false; 14 | S->next=NULL; 15 | return true; 16 | } 17 | bool Empty(LinkStake &S){ 18 | return S->next==NULL; 19 | } 20 | //头插法 21 | bool InsertStake(LinkStake &S, ElemType e){ 22 | if(S==NULL) 23 | return false; 24 | SNode *t=(SNode *)malloc(sizeof(SNode)); 25 | if(S==NULL) 26 | return false; 27 | t->data=e; 28 | t->next=S->next; 29 | S->next=t; 30 | return true; 31 | } 32 | int main(){ 33 | //LinkStake S; 34 | //InitStake(S); 35 | } -------------------------------------------------------------------------------- /第3章-栈、队列、数组/5链栈(带头节点).cpp: -------------------------------------------------------------------------------- 1 | //带头节点链栈 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct SNode{ 6 | ElemType data; 7 | struct SNode *next; 8 | }*LinkStake; 9 | 10 | bool InitStake(LinkStake &S){ 11 | S=(SNode *)malloc(sizeof(SNode)); 12 | if(S==NULL) 13 | return false; 14 | S->next=NULL; 15 | return true; 16 | } 17 | bool Empty(LinkStake &S){ 18 | return S->next==NULL; 19 | } 20 | //头插法 21 | bool InsertStake(LinkStake &S, ElemType e){ 22 | if(S==NULL) 23 | return false; 24 | SNode *t=(SNode *)malloc(sizeof(SNode)); 25 | if(S==NULL) 26 | return false; 27 | t->data=e; 28 | t->next=S->next; 29 | S->next=t; 30 | return true; 31 | } 32 | int main(){ 33 | //LinkStake S; 34 | //InitStake(S); 35 | } -------------------------------------------------------------------------------- /第3章-栈、队列、数组/6.队列的顺序实现.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | # define MaxSize 10 //定义队列中元素的最大个数 3 | typedef int ElemType; 4 | typedef struct{ 5 | ElemType data[MaxSize]; //用静态数组存放队列元素 6 | int front,rear; //队列指针和对尾指针 7 | 8 | //不浪费存储空间判断队空/队满 9 | //int size //队列长度 10 | //int tag //最近进行的是删除/插入 11 | }SqQueue; 12 | //队列元素的个数:(rear+MaxSize-front)%MaxSize 13 | 14 | //初始化队列 15 | void InitQueue(SqQueue &Q){ 16 | //初始化时,对头、队尾指针指向0 17 | Q.rear=Q.front=0; 18 | //若队尾指针指向队尾元素 19 | //Q.front=0; 20 | //Q.front=Maxize-1; 21 | } 22 | 23 | //判断队列是否为空 24 | bool QueueEmpty(SqQueue Q){ 25 | if(Q.rear==Q.front) 26 | return true; 27 | else 28 | return false; 29 | } 30 | 31 | //插入数据元素 32 | bool EnQueue(SqQueue Q,ElemType x){ 33 | if((Q.rear+1)%MaxSize==Q.front) //判断队满 34 | return false; 35 | //如果rear指向队尾元素 36 | //Q.rear=(Q.rear+1)%MaxSize; 37 | //Q.data[Q.rear]=x; 38 | Q.data[Q.rear]=x; 39 | Q.rear=(Q.rear+1)%MaxSize; //循环队列 40 | return true; 41 | } 42 | 43 | //出队(删除一个队头元素,并返回x ) 44 | bool DeQueue(SqQueue Q,ElemType &x){ 45 | if(Q.rear==Q.front) 46 | return false; 47 | x=Q.data[Q.front]; 48 | Q.front=(Q.front+1)%MaxSize; 49 | return true; 50 | } 51 | 52 | //读取队头元素的值,用x返回 53 | bool GetHead(SqQueue Q,ElemType &x){ 54 | if(Q.rear=Q.front) 55 | return false; 56 | x=Q.data[Q.front]; 57 | return true; 58 | } 59 | 60 | int main(){ 61 | SqQueue Q; 62 | 63 | } -------------------------------------------------------------------------------- /第3章-栈、队列、数组/7.队列的链式实现(不带头节点).cpp: -------------------------------------------------------------------------------- 1 | //不带头节点队列 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct LinkNode{ 6 | ElemType data; 7 | LinkNode *next; 8 | }LinkNode; 9 | typedef struct{ 10 | LinkNode *front,*rear; 11 | }LinkQueue; 12 | 13 | //初始化队列(不带头节点队列) 14 | void InitQueue(LinkQueue &Q){ 15 | Q.front=NULL; 16 | Q.rear=NULL; 17 | } 18 | 19 | //判断队列是否为空 20 | bool IsEmpty(LinkQueue Q){ 21 | if(Q.front==NULL) 22 | return true; 23 | else 24 | return false; 25 | } 26 | 27 | //入队(不带头节点) 28 | void EnQueue(LinkQueue &Q,ElemType x){ 29 | LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode)); 30 | s->data=x; 31 | s->next=NULL; 32 | //判断是否为空 33 | if(Q.front==NULL){ 34 | Q.front=s; 35 | Q.rear=s; 36 | }else{ 37 | Q.rear->next=s; 38 | Q.rear=s; 39 | } 40 | } 41 | 42 | //出队(不带头节点) 43 | bool DeQueue(LinkQueue &Q,ElemType x){ 44 | if(Q.front==NULL) 45 | return false; 46 | LinkNode *p=Q.front; 47 | x=p->data; 48 | Q.front=p->next; 49 | //此次是最后一个节点出队 50 | if(Q.rear==p) 51 | Q.front=NULL; 52 | Q.rear=NULL; 53 | free(p); 54 | return(true); 55 | } 56 | 57 | int main(){ 58 | 59 | } 60 | -------------------------------------------------------------------------------- /第3章-栈、队列、数组/8.队列的链式实现(带头节点).cpp: -------------------------------------------------------------------------------- 1 | //带头节点 2 | #include 3 | #include 4 | typedef int ElemType; 5 | typedef struct LinkNode{ 6 | ElemType data; 7 | struct LinkNode *next; 8 | }LinkNode; 9 | typedef struct{ 10 | LinkNode *front,*rear; 11 | }LinkQueue; 12 | 13 | //初始化队列(带头节点) 14 | void InitQueue(LinkQueue &Q){ 15 | //初始时front、rear都指向头节点 16 | Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode)); 17 | } 18 | 19 | //判断队列是否为空 20 | bool IsEmpty(LinkQueue Q){ 21 | if(Q.front==Q.rear) 22 | return true; 23 | else 24 | return false; 25 | } 26 | 27 | //新元素入队(插入操作)(带头节点) 28 | void EnQueue(LinkQueue &Q,ElemType x){ 29 | LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode)); 30 | s->data=x; 31 | s->next=NULL; 32 | Q.rear->next=s; 33 | Q.rear=s; 34 | } 35 | 36 | //出队(带头节点) 37 | bool DeQueue(LinkQueue &Q,ElemType &x){ 38 | if(Q.front==Q.rear) 39 | return false; //空队 40 | LinkNode *p=Q.front->next; //头节点后的第一个元素 41 | x=p->data; 42 | Q.front->next=p->next; 43 | if(Q.rear==p) 44 | Q.rear=Q.front; 45 | free(p); 46 | return true; 47 | } 48 | 49 | int main(){ 50 | LinkQueue Q; 51 | InitQueue(Q); 52 | } 53 | --------------------------------------------------------------------------------