当前位置:   article > 正文

【数据结构】PTA 带头结点的链式表操作集 C语言

【数据结构】PTA 带头结点的链式表操作集 C语言

本题要求实现带头结点的链式表操作集

函数接口定义:

  1. List MakeEmpty();
  2. Position Find( List L, ElementType X );
  3. bool Insert( List L, ElementType X, Position P );
  4. bool Delete( List L, Position P );

其中List结构定义如下:

  1. typedef struct LNode *PtrToLNode;
  2. struct LNode {
  3. ElementType Data;
  4. PtrToLNode Next;
  5. };
  6. typedef PtrToLNode Position;
  7. typedef PtrToLNode List;

各个操作函数的定义为:

List MakeEmpty():创建并返回一个空的线性表

Position Find( List L, ElementType X ):返回线性表中X的位置。若找不到则返回ERROR;

bool Insert( List L, ElementType X, Position P ):将X插入在位置P指向的结点之前,返回true。如果参数P指向非法位置,则打印“Wrong Position for Insertion”,返回false;

bool Delete( List L, Position P ):将位置P的元素删除并返回true。若参数P指向非法位置,则打印“Wrong Position for Deletion”并返回false。

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define ERROR NULL
  4. typedef enum {false, true} bool;
  5. typedef int ElementType;
  6. typedef struct LNode *PtrToLNode;
  7. struct LNode {
  8. ElementType Data;
  9. PtrToLNode Next;
  10. };
  11. typedef PtrToLNode Position;
  12. typedef PtrToLNode List;
  13. List MakeEmpty();
  14. Position Find( List L, ElementType X );
  15. bool Insert( List L, ElementType X, Position P );
  16. bool Delete( List L, Position P );
  17. int main()
  18. {
  19. List L;
  20. ElementType X;
  21. Position P;
  22. int N;
  23. bool flag;
  24. L = MakeEmpty();
  25. scanf("%d", &N);
  26. while ( N-- ) {
  27. scanf("%d", &X);
  28. flag = Insert(L, X, L->Next);
  29. if ( flag==false ) printf("Wrong Answer\n");
  30. }
  31. scanf("%d", &N);
  32. while ( N-- ) {
  33. scanf("%d", &X);
  34. P = Find(L, X);
  35. if ( P == ERROR )
  36. printf("Finding Error: %d is not in.\n", X);
  37. else {
  38. flag = Delete(L, P);
  39. printf("%d is found and deleted.\n", X);
  40. if ( flag==false )
  41. printf("Wrong Answer.\n");
  42. }
  43. }
  44. flag = Insert(L, X, NULL);
  45. if ( flag==false ) printf("Wrong Answer\n");
  46. else
  47. printf("%d is inserted as the last element.\n", X);
  48. P = (Position)malloc(sizeof(struct LNode));
  49. flag = Insert(L, X, P);
  50. if ( flag==true ) printf("Wrong Answer\n");
  51. flag = Delete(L, P);
  52. if ( flag==true ) printf("Wrong Answer\n");
  53. for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
  54. return 0;
  55. }
  56. /* 你的代码将被嵌在这里 */

输入样例:

  1. 6
  2. 12 2 4 87 10 2
  3. 4
  4. 2 12 87 5

输出样例:

  1. 2 is found and deleted.
  2. 12 is found and deleted.
  3. 87 is found and deleted.
  4. Finding Error: 5 is not in.
  5. 5 is inserted as the last element.
  6. Wrong Position for Insertion
  7. Wrong Position for Deletion
  8. 10 4 2 5

AC代码:

  1. List MakeEmpty()
  2. {
  3. List head;
  4. head=(List)malloc(sizeof(struct LNode));
  5. head->Next=NULL;
  6. return head;
  7. }
  8. Position Find( List L, ElementType X )
  9. {
  10. List l2;
  11. l2=L->Next;//从头节点下一节点开始找
  12. while(l2)
  13. {
  14. if(l2->Data==X)
  15. return l2;
  16. l2=l2->Next;
  17. }
  18. return ERROR;
  19. }
  20. bool Insert( List L, ElementType X, Position P )
  21. {
  22. List l2,temp;
  23. temp=(List)malloc(sizeof(struct LNode));
  24. l2=L;
  25. while(l2)
  26. {
  27. if(l2->Next==P)
  28. {
  29. temp->Data=X;
  30. l2->Next=temp;
  31. temp->Next=P;
  32. return true;
  33. }
  34. l2=l2->Next;
  35. }
  36. printf("Wrong Position for Insertion\n");
  37. return false;
  38. }
  39. bool Delete( List L, Position P )
  40. {
  41. List l2;
  42. l2=L;
  43. while(l2)
  44. {
  45. if(l2->Next==P)
  46. {
  47. l2->Next=P->Next;
  48. return true;
  49. }
  50. l2=l2->Next;
  51. }
  52. printf("Wrong Position for Deletion\n");
  53. return false;
  54. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/1017921
推荐阅读
相关标签
  

闽ICP备14008679号