• 29.单链表的C语言完整实现


    主要包含:

    单链表的数据结构,初始化,打印输出,建立单链表(尾插法),元素按位插入,按位删除,按值查找。

    1. #include
    2. #include
    3. #include
    4. #define MAXSIZE 100
    5. #define ElemType int
    6. #define Status int
    7. //单链表的数据结构
    8. typedef struct LNode
    9. {
    10. ElemType data;
    11. struct LNode *next;
    12. }LNode, *LinkList;
    13. //初始化
    14. int InitList(LinkList &L)
    15. {
    16. L = (LNode *)malloc(sizeof(LNode));
    17. L->next = NULL;
    18. return 1;
    19. }
    20. //输出
    21. void PrintList(LinkList L)
    22. {
    23. printf("当前单链表的所有元素:");
    24. LNode *p;
    25. p = L->next;
    26. while (p != NULL)
    27. {
    28. printf("[%d] ", p->data);
    29. p = p->next;
    30. }
    31. printf("\n");
    32. }
    33. //尾插法创建单链表
    34. int Create(LinkList &L)
    35. {
    36. int n, e;
    37. LNode *temp = L;//声明一个指针指向头结点,用于遍历链表   
    38. printf("请输入要输入元素的个数:");
    39. scanf("%d", &n);
    40. for (int i = 1; i <= n; i++)
    41. {
    42. LNode *a = (LNode*)malloc(sizeof(LNode));
    43. printf("请输入第%d元素的值:", (i));
    44. scanf("%d", &e);
    45. a->data = e;
    46. temp->next = a;
    47. a->next = NULL;
    48. temp = temp->next;
    49. }
    50. return 1;
    51. }
    52. //插入元素
    53. int InsertList(LNode *L, int i, ElemType e)
    54. {
    55. LNode *p = L;
    56. int j = 0;
    57. while (p && (j < i - 1)) //寻找要插入位置的前驱结点,让p指向它
    58. {
    59. p = p->next;
    60. ++j;
    61. }
    62. if (!p || j > i - 1) return 0; //插入位置非法,返回0
    63. LNode *s = (LNode *)malloc(sizeof(LNode));
    64. s->data = e; //创建一个新结点存放要插入的元素e
    65. s->next = p->next; //把新结点的指针域指向p->next
    66. p->next = s; //把p->next指向要插入的新结点
    67. return 1;
    68. }
    69. //删除元素
    70. int DeleteList(LNode *L, int i)
    71. {
    72. LNode *p = L;
    73. int j = 0;
    74. while (p->next && (j < i - 1)) //寻找要删除结点的前驱结点,让p指向它
    75. {
    76. p = p->next;
    77. ++j;
    78. }
    79. if (!p->next || j > i - 1) return 0; //删除位置非法,返回0
    80. LNode *q;
    81. q = p->next; //暂存删除结点,以便随后释放
    82. p->next = q->next; //把p->next指向p->next->next,即q->next
    83. free(q); //释放结点
    84. return 1;
    85. }
    86. //按值查找元素
    87. int LocateElem(LNode *L, ElemType e)
    88. {
    89. int i = 1;
    90. LNode *p = L->next;
    91. while (p&&p->data != e) //从第一个结点开始,依次向后遍历比较
    92. {
    93. p = p->next;
    94. i++;
    95. }
    96. if(p) return i;
    97. else return 0;
    98. }
    99. int main(){
    100. LinkList L;
    101. InitList(L);
    102. Create(L);
    103. PrintList(L);
    104. InsertList(L, 4, 5);
    105. PrintList(L);
    106. DeleteList(L, 3);
    107. PrintList(L);
    108. printf("%d\n",LocateElem(L, 2));
    109. return 0;
    110. }

    输出:

    1. 请输入要输入元素的个数:5
    2. 请输入第1元素的值:1
    3. 请输入第2元素的值:2
    4. 请输入第3元素的值:3
    5. 请输入第4元素的值:4
    6. 请输入第5元素的值:5
    7. 当前单链表的所有元素:[1] [2] [3] [4] [5]
    8. 当前单链表的所有元素:[1] [2] [3] [5] [4] [5]
    9. 当前单链表的所有元素:[1] [2] [5] [4] [5]
    10. 2
  • 相关阅读:
    kafka面试题(基础-进阶-高阶)
    level2行情接口中的TickRecord有什么作用?
    互联网摸鱼日报(2023-10-13)
    【附源码】计算机毕业设计SSM网上花店销售系统
    谷粒商城项目
    Sigma中的数字增益放大/降低方法
    res_config_settings.py文件详解
    智云通CRM:销售的黄金法则,尊重客户的意见
    好看的货架效果(含3D效果)
    LINUX的XEN和KVM到底区别在什么地方?
  • 原文地址:https://blog.csdn.net/qq_54708219/article/details/132946948