线性表

线性表的顺序存储(顺序表)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. const int MAXSIZE = 50;
  4. typedef struct SequenceList
  5. {
  6. int* data;
  7. int length;
  8. }SqList;
  9. void Initialize(SqList*, int); //初始化顺序表
  10. bool AppendElem(SqList*, int); //添加元素
  11. bool InsertElem(SqList*, int, int); //插入元素
  12. bool DeleteElem(SqList*, int, int*); //删除元素
  13. void Destroy(SqList*); //销毁顺序表
  14. bool IsEmpty(SqList*); //是否为空
  15. bool IsFull(SqList*); //是否已满
  16. int GetLength(SqList*); //获取长度
  17. int GetElem(SqList*, int); //按位查找
  18. int LocateElem(SqList*, int); //按值查找
  19. void Traverse(SqList*); //遍历顺序表
  20. int main(void)
  21. {
  22. SqList L;
  23. return 0;
  24. }
  25. void Initialize(SqList* L)
  26. {
  27. L->data = (int*)malloc(sizeof(int) * MAXSIZE);
  28. L->length = 0;
  29. }
  30. bool AppendElem(SqList* L, int val)
  31. {
  32. if (IsFull(L))
  33. {
  34. return false;
  35. }
  36. else
  37. {
  38. L->data[L->length] = val;
  39. L->length++;
  40. return true;
  41. }
  42. }
  43. bool InsertElem(SqList* L, int pos, int val)
  44. {
  45. if (IsFull(L))
  46. {
  47. return false;
  48. }
  49. else
  50. {
  51. for (int i = L->length; i >= pos; i--)
  52. {
  53. L->data[i] = L->data[i - 1];
  54. }
  55. L->data[pos - 1] = val;
  56. L->length++;
  57. return true;
  58. }
  59. }
  60. bool DeleteElem(SqList* L, int pos, int* backVal)
  61. {
  62. if (IsEmpty(L))
  63. {
  64. return false;
  65. }
  66. else
  67. {
  68. *backVal = L->data[pos - 1];
  69. for (int i = pos; i < L->length; i++)
  70. {
  71. L->data[i - 1] = L->data[i];
  72. }
  73. L->length--;
  74. return true;
  75. }
  76. }
  77. void Destroy(SqList* L)
  78. {
  79. free(L->data);
  80. L->data = NULL;
  81. }
  82. bool IsEmpty(SqList* L)
  83. {
  84. if (L->length <= 0)
  85. {
  86. return true;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. bool IsFull(SqList* L)
  94. {
  95. if (L->length >= MAXSIZE)
  96. {
  97. return true;
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. int GetLength(SqList* L)
  105. {
  106. return L->length;
  107. }
  108. int GetElem(SqList* L, int pos)
  109. {
  110. return L->data[pos - 1];
  111. }
  112. int LocateElem(SqList* L, int val)
  113. {
  114. for (int i = 0; i < L->length; i++)
  115. {
  116. if (L->data[i] == val)
  117. {
  118. return i + 1;
  119. }
  120. }
  121. return -1;
  122. }
  123. void Traverse(SqList* L)
  124. {
  125. for (int i = 0; i < L->length; i++)
  126. {
  127. printf("%d ", L->data[i]);
  128. }
  129. printf("\n");
  130. }

线性表的链式存储(单链表)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. typedef struct ElemType
  4. {
  5. char name[8];
  6. int score;
  7. }ElemType;
  8. typedef struct LinkNode
  9. {
  10. ElemType data;
  11. struct LinkNode* pNext;
  12. }LinkNode, * LinkList;
  13. void HeadInitialize(LinkList*); //采用头插法初始化单链表
  14. void TailInitialize(LinkList*); //采用尾插法初始化单链表
  15. void AppendElem(LinkList, ElemType); //添加结点
  16. bool Insert(LinkList, int, ElemType); //插入新结点
  17. bool Delete(LinkList, int, ElemType*); //删除结点
  18. void Destroy(LinkList); //销毁链表
  19. bool IsEmpty(LinkList); //是否为空
  20. int GetLength(LinkList); //计算表长
  21. LinkNode* GetElem(LinkList, int); //按位查找结点
  22. LinkNode* LocateElem(LinkList, ElemType); //按值查找结点
  23. void Traverse(LinkList); //遍历单链表
  24. int main(void)
  25. {
  26. LinkList L;
  27. TailInitialize(&L);
  28. Traverse(L);
  29. AppendElem(L, { "sprout",100 });
  30. Traverse(L);
  31. Insert(L, 2, { "Insert",100 });
  32. printf("在第2个结点处插入一个新节点\n");
  33. Traverse(L);
  34. ElemType e;
  35. Delete(L, 4, &e);
  36. printf("删除第4个结点,结点的名字是:%s,结点的成绩是:%d\n", e.name, e.score);
  37. Traverse(L);
  38. Destroy(L);
  39. printf("销毁链表");
  40. return 0;
  41. }
  42. void HeadInitialize(LinkList* pL)
  43. {
  44. int length;
  45. *pL = (LinkNode*)malloc(sizeof(LinkNode));
  46. (*pL)->pNext = NULL;
  47. printf("请输入你想要创建的结点个数:");
  48. scanf("%d", &length);
  49. for (int i = 0; i < length; i++)
  50. {
  51. LinkNode* pNew = (LinkNode*)malloc(sizeof(LinkNode));
  52. printf("请输入第%d个结点的姓名:", i + 1);
  53. scanf("%s", &pNew->data.name);
  54. printf("请输入第%d个结点的成绩:", i + 1);
  55. scanf("%d", &pNew->data.score);
  56. pNew->pNext = (*pL)->pNext;
  57. (*pL)->pNext = pNew;
  58. }
  59. printf("\n");
  60. }
  61. void TailInitialize(LinkList* pL)
  62. {
  63. int length; //存储链表结点个数
  64. *pL = (LinkNode*)malloc(sizeof(LinkNode));
  65. LinkNode* pTail = *pL;
  66. pTail->pNext = NULL;
  67. printf("请输入您要创建的链表节点个数:");
  68. scanf("%d", &length);
  69. for (int i = 0; i < length; i++)
  70. {
  71. LinkNode* pNew = (LinkNode*)malloc(sizeof(LinkNode));
  72. printf("请输入第%d个结点的姓名:", i + 1);
  73. scanf("%s", &pNew->data.name);
  74. printf("请输入第%d个结点的成绩:", i + 1);
  75. scanf("%d", &pNew->data.score);
  76. pNew->pNext = NULL;
  77. pTail->pNext = pNew;
  78. pTail = pNew;
  79. }
  80. printf("\n");
  81. }
  82. void AppendElem(LinkList L, ElemType e)
  83. {
  84. LinkNode* p = L;
  85. while (p->pNext != NULL)
  86. {
  87. p = p->pNext;
  88. }
  89. int length = GetLength(L);
  90. printf("在链表的第%d个位置新添一个结点\n", length + 1);
  91. LinkNode* q = (LinkNode*)malloc(sizeof(LinkNode));
  92. q->data = e;
  93. q->pNext = NULL;
  94. p->pNext = q;
  95. }
  96. bool Insert(LinkList L, int pos, ElemType e)
  97. {
  98. LinkNode* p = GetElem(L, pos - 1);
  99. LinkNode* q = (LinkNode*)malloc(sizeof(LinkNode));
  100. q->data = e;
  101. q->pNext = p->pNext;
  102. p->pNext = q;
  103. return true;
  104. }
  105. bool Delete(LinkList L, int pos, ElemType* backElem)
  106. {
  107. if (IsEmpty(L))
  108. {
  109. return false;
  110. }
  111. LinkNode* p = GetElem(L, pos - 1);
  112. LinkNode* q = p->pNext;
  113. *backElem = q->data;
  114. p->pNext = p->pNext->pNext;
  115. free(q);
  116. q = NULL;
  117. return true;
  118. }
  119. void Destroy(LinkList L)
  120. {
  121. LinkNode* p = L;
  122. while (L != NULL)
  123. {
  124. L = L->pNext;
  125. free(p);
  126. p = L;
  127. }
  128. }
  129. bool IsEmpty(LinkList L)
  130. {
  131. if (L->pNext == NULL)
  132. {
  133. return true;
  134. }
  135. return false;
  136. }
  137. int GetLength(LinkList L)
  138. {
  139. if (IsEmpty(L))
  140. {
  141. return 0;
  142. }
  143. int i = 1;
  144. LinkNode* p = L->pNext;
  145. while (p->pNext != NULL)
  146. {
  147. p = p->pNext;
  148. i++;
  149. }
  150. return i;
  151. }
  152. LinkNode* GetElem(LinkList L, int pos)
  153. {
  154. if (pos < 1)
  155. {
  156. return NULL;
  157. }
  158. if (pos == 0)
  159. {
  160. return L;
  161. }
  162. int i = 1;
  163. LinkNode* p = L->pNext;
  164. while (i < pos && p != NULL)
  165. {
  166. p = p->pNext;
  167. i++;
  168. }
  169. return p;
  170. }
  171. LinkNode* LocateElem(LinkList L, ElemType e)
  172. {
  173. for (LinkNode* p = L->pNext; p != NULL; p = p->pNext)
  174. {
  175. if (p->data.name == e.name && p->data.score == e.score)
  176. {
  177. return p;
  178. }
  179. }
  180. return NULL;
  181. }
  182. void Traverse(LinkList L)
  183. {
  184. int i = 1;
  185. for (LinkNode* p = L->pNext; p != NULL; p = p->pNext)
  186. {
  187. printf("链表第%d个结点的名字是:%s,成绩是:%d\n", i, p->data.name, p->data.score);
  188. i++;
  189. }
  190. }

栈的顺序存储(顺序栈)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. const int MaxSize = 50;
  4. typedef struct SequenceStack
  5. {
  6. int* data;
  7. int top;
  8. }SqStack;
  9. void Initialize(SqStack*);
  10. bool Push(SqStack*, int);
  11. bool Pop(SqStack*, int*);
  12. void Destroy(SqStack*);
  13. bool IsEmpty(SqStack*);
  14. bool IsFull(SqStack*);
  15. int GetTop(SqStack*);
  16. int main(void)
  17. {
  18. SqStack s;
  19. return 0;
  20. }
  21. void Initialize(SqStack* pS)
  22. {
  23. pS->data = (int*)malloc(sizeof(int) * MaxSize);
  24. pS->top = 0;
  25. }
  26. bool Push(SqStack* pS, int val)
  27. {
  28. if (pS->top == MaxSize)
  29. {
  30. return false;
  31. }
  32. else
  33. {
  34. pS->data[pS->top] = val;
  35. pS->top++;
  36. return true;
  37. }
  38. }
  39. bool Pop(SqStack* pS, int* backVal)
  40. {
  41. if (pS->top == 0)
  42. {
  43. return false;
  44. }
  45. else
  46. {
  47. pS->top--;
  48. *backVal = pS->data[pS->top];
  49. return true;
  50. }
  51. }
  52. void Destroy(SqStack* pS)
  53. {
  54. free(pS->data);
  55. pS->top = 0;
  56. }
  57. bool IsEmpty(SqStack* pS)
  58. {
  59. if (pS->top == 0)
  60. {
  61. return true;
  62. }
  63. return false;
  64. }
  65. bool IsFull(SqStack* pS)
  66. {
  67. if (pS->top == MaxSize)
  68. {
  69. return true;
  70. }
  71. return false;
  72. }
  73. int GetTop(SqStack* pS)
  74. {
  75. return pS->data[pS->top - 1];
  76. }

栈的链式存储(链栈)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. typedef struct LinkNode
  4. {
  5. int data;
  6. struct LinkNode* next;
  7. }LNode;
  8. typedef struct LinkStack
  9. {
  10. LNode* top;
  11. LNode* bottom;
  12. }LStack;
  13. void Initialize(LStack*);
  14. void Push(LStack*, int);
  15. bool Pop(LStack*, int*);
  16. void Destroy(LStack*);
  17. bool IsEmpty(LStack*);
  18. int GetTop(LStack* pS);
  19. int main(void)
  20. {
  21. LStack s;
  22. return 0;
  23. }
  24. void Initialize(LStack* pS)
  25. {
  26. pS->top = (LNode*)malloc(sizeof(LNode));
  27. pS->bottom = pS->top;
  28. pS->top->next = NULL;
  29. }
  30. void Push(LStack* pS, int val)
  31. {
  32. LNode* pNew = (LNode*)malloc(sizeof(LNode));
  33. pNew->data = val;
  34. pNew->next = pS->top;
  35. pS->top = pNew;
  36. }
  37. bool Pop(LStack* pS, int* backVal)
  38. {
  39. if (pS->top == pS->bottom)
  40. {
  41. return false;
  42. }
  43. LNode* p = pS->top->next;
  44. *backVal = pS->top->data;
  45. free(pS->top);
  46. pS->top = p;
  47. return true;
  48. }
  49. void Destroy(LStack* pS)
  50. {
  51. LNode* p = pS->top->next;
  52. while (pS->top != pS->bottom)
  53. {
  54. free(pS->top);
  55. pS->top = p;
  56. p = p->next;
  57. }
  58. }
  59. bool IsEmpty(LStack* pS)
  60. {
  61. if (pS->top == pS->bottom)
  62. {
  63. return true;
  64. }
  65. return false;
  66. }
  67. int GetTop(LStack* pS)
  68. {
  69. return pS->top->data;
  70. }

队列

队列的顺序存储(顺序队)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. const int MaxSize = 50;
  4. typedef struct SequenceQueue
  5. {
  6. int* data;
  7. int front;
  8. int rear;
  9. }SqQueue;
  10. void Initialize(SqQueue*);
  11. bool EnQueue(SqQueue*, int);
  12. bool DeQueue(SqQueue*, int*);
  13. bool IsEmpty(SqQueue*);
  14. bool IsFull(SqQueue*);
  15. int GetFront(SqQueue*);
  16. int main(void)
  17. {
  18. SqQueue q;
  19. return 0;
  20. }
  21. void Initialize(SqQueue* pQ)
  22. {
  23. pQ->data = (int*)malloc(sizeof(int) * MaxSize);
  24. pQ->front = pQ->rear = 0;
  25. }
  26. bool EnQueue(SqQueue* pQ, int val)
  27. {
  28. if ((pQ->rear + 1) % MaxSize == pQ->front) //rear的下一个位置是front,说明队列已满
  29. {
  30. return false;
  31. }
  32. else
  33. {
  34. pQ->data[pQ->rear] = val;
  35. pQ->rear = (pQ->rear + 1) % MaxSize;
  36. return true;
  37. }
  38. }
  39. bool DeQueue(SqQueue* pQ, int* backVal)
  40. {
  41. if (pQ->rear == pQ->front) //rear和front的位置重合,说明队列为空
  42. {
  43. return false;
  44. }
  45. else
  46. {
  47. *backVal = pQ->data[pQ->front];
  48. pQ->front = (pQ->front + 1) % MaxSize;
  49. return true;
  50. }
  51. }
  52. bool IsEmpty(SqQueue* pQ)
  53. {
  54. if (pQ->rear == pQ->front)
  55. {
  56. return true;
  57. }
  58. return false;
  59. }
  60. bool IsFull(SqQueue* pQ)
  61. {
  62. if ((pQ->rear + 1) % MaxSize == pQ->front)
  63. {
  64. return true;
  65. }
  66. return false;
  67. }
  68. int GetFront(SqQueue* pQ)
  69. {
  70. return pQ->data[pQ->front];
  71. }

队列的链式存储(链队)

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. typedef struct LinkNode
  4. {
  5. int data;
  6. struct LinkNode* next;
  7. }LNode;
  8. typedef struct LinkQueue
  9. {
  10. LNode* front;
  11. LNode* rear;
  12. }LQueue;
  13. void Initialize(LQueue*);
  14. void EnQueue(LQueue*, int);
  15. bool DeQueue(LQueue*, int*);
  16. int main(void)
  17. {
  18. LQueue q;
  19. return 0;
  20. }
  21. void Initialize(LQueue* pQ)
  22. {
  23. pQ->rear = (LNode*)malloc(sizeof(LNode));
  24. pQ->front = pQ->rear;
  25. pQ->front->next = NULL;
  26. }
  27. void EnQueue(LQueue* pQ, int val)
  28. {
  29. LNode* pNew = (LNode*)malloc(sizeof(LNode));
  30. pNew->data = val;
  31. pNew->next = NULL;
  32. pQ->rear->next = pNew;
  33. pQ->rear = pNew;
  34. }
  35. bool DeQueue(LQueue* pQ, int* backVal)
  36. {
  37. if (pQ->rear == pQ->front)
  38. {
  39. return false;
  40. }
  41. else
  42. {
  43. *backVal = pQ->front->next->data;
  44. LNode* p = pQ->front->next;
  45. pQ->front->next = p->next;
  46. if (p == pQ->rear) //如果队列中此时只有一个结点
  47. {
  48. pQ->rear = pQ->front;
  49. }
  50. free(p);
  51. p = NULL;
  52. return true;
  53. }
  54. }