当前位置:   article > 正文

单链表的创建及插入,删除,查找数据

单链表的创建及插入,删除,查找数据

创建链表,插入数据,查找数据,删除数据(算法笔记)

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. const int N=10;
  5. struct node
  6. {
  7. int data;
  8. node* next;
  9. };
  10. node* create(int array[]);
  11. int search(node* head,int x);
  12. void insert(node* head,int pos,int x);
  13. void del(node* head,int x);
  14. int main()
  15. {
  16. int array[N]={1,2,3,4,5,5,6,5,3,9};
  17. node* l=create(array);
  18. node* s=create(array);
  19. node* i=create(array);
  20. node* d=create(array);
  21. l=l->next;
  22. printf("建立链表:\n");
  23. while(l!=NULL)
  24. {
  25. printf("%d ",l->data);
  26. l=l->next;
  27. }
  28. printf("\n查找:\n");
  29. int ans=search(s,5);
  30. printf("%d",ans);
  31. printf("\n插入:\n");
  32. insert(i,3,9);
  33. i=i->next;
  34. while(i!=NULL)
  35. {
  36. printf("%d ",i->data);
  37. i=i->next;
  38. }
  39. printf("\n删除\n");
  40. del(d,3);
  41. d=d->next;
  42. while(d!=NULL)
  43. {
  44. printf("%d ",d->data);
  45. d=d->next;
  46. }
  47. cout << "\nHello world!" << endl;
  48. return 0;
  49. }
  50. node* create(int array[])
  51. {
  52. node *p,*head,*pre;
  53. head=new node;
  54. head->next=NULL;
  55. pre=head;
  56. for(int i=0;i<N;i++)
  57. {
  58. p=new node;
  59. p->data=array[i];
  60. p->next=NULL;
  61. pre->next=p;
  62. pre=p;
  63. }
  64. return head;
  65. }
  66. int search(node* head,int x) //查找单链表中x的个数
  67. {
  68. node *p=head->next;
  69. int count=0;
  70. while(p!=NULL)
  71. {
  72. if(p->data==x)
  73. count++;
  74. p=p->next;
  75. }
  76. return count;
  77. }
  78. void insert(node* head,int pos,int x) //在第pos号插入数据x
  79. {
  80. node* p=head;
  81. for(int i=0;i<pos;i++)
  82. {
  83. p=p->next;
  84. }
  85. node *q=new node;
  86. q->data=x;
  87. q->next=p->next;
  88. p->next=q;
  89. }
  90. void del(node* head,int x) //删除值为x的结点
  91. {
  92. node* p=head->next;
  93. node* q=head;
  94. while(p!=NULL)
  95. {
  96. if(p->data==x)
  97. {
  98. q->next=p->next;
  99. delete(p);
  100. p=q->next;
  101. }
  102. else
  103. {
  104. q=p;
  105. p=p->next;
  106. }
  107. }
  108. }

下面个代码是看c和指针写出来的,但我感觉有点问题!

书上最后一个代码是这样给出的,他想传递的应该是链表第一个结点的地址,而不是头结点的地址,否则头结点是没数据的,即不应该与new_value比较。然而,传递了第一个结点的地址,insert函数必然会移动linkp的位置,因为没有另外的变量存储数据,那么问题来了,在主函数中,输出数据的时候,怎么从第一个数据开始输出,这儿的头结点的位置已经移动到new_value的前面一个节点了。所以,妾以为,用一个变量代替linkp更加合适,此事此时递给linkp的是链表的头结点,即头结点便不用移动,方便后续数据处理或者输出,即我写的那个完整的代码!

//c和指针给的代码
int insert(register node **linkp,int new_value)
    {
        register node *current;
        register node *p;
        while((current=*linkp)!=NULL && current->data<new_value)
               linkp=¤t->next;
        p =(node *)malloc(sizeof(node));
        if(p==NULL)
            return 0;
        p->data=new_value;
        p->next=current;
        *linkp=p;
        return 1;
    }

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <algorithm>
  4. using namespace std;
  5. struct node
  6. {
  7. int data;
  8. node *next;
  9. };
  10. node *create(int array[],int n);
  11. int insert(register node **linkp,int new_value);
  12. //node* insert(register node **linkp,int new_value);
  13. int main()
  14. {
  15. int a[]={1,4,5,8,9,12,15,2,36,5,9,8,5,5,98};
  16. int n=sizeof(a)/sizeof(a[0]);
  17. sort(a,a+n);
  18. for(int i=0;i<n;i++)
  19. {
  20. printf("%3d",a[i]);
  21. }
  22. printf("\n");
  23. printf("创建链表:\n");
  24. node *q=create(a,n);
  25. q=q->next;
  26. while(q!=NULL)
  27. {
  28. printf("%3d",q->data);
  29. q=q->next;
  30. }
  31. printf("\n");
  32. printf("插入一个数后:\n");
  33. node *i=create(a,n);
  34. insert(&i,-1);
  35. insert(&i,25);
  36. insert(&i,-10);
  37. i=i->next;
  38. while(i!=NULL)
  39. {
  40. printf("%3d",i->data);
  41. i=i->next;
  42. }
  43. printf("\n");
  44. }
  45. node *create(int array[],int n)
  46. {
  47. node *p,*pre,*head;
  48. head=new node;
  49. head->next=NULL;
  50. pre=head;
  51. for(int i=0;i<n;i++)
  52. {
  53. p=new node;
  54. p->data=array[i];
  55. p->next=NULL;
  56. pre->next=p;
  57. pre=p;
  58. }
  59. return head;
  60. }
  61. int insert(register node **linkp,int new_value)
  62. {
  63. register node *precious;
  64. precious=NULL;
  65. register node *current;
  66. register node *p;
  67. current=(*linkp)->next;
  68. while(current!=NULL&&current->data<new_value)
  69. {
  70. precious=current;
  71. current=current->next;
  72. }
  73. p =(node *)malloc(sizeof(node));
  74. if(p==NULL)
  75. return 0;
  76. p->data=new_value;
  77. p->next=current;
  78. if(precious==NULL)
  79. (*linkp)->next=p;
  80. else
  81. precious->next=p;
  82. free(p);
  83. return 1;
  84. }

经过苦心“修炼”,终于找出了解决办法,insert函数的返回值为 node*,返回头节点:


node* insert(register node **linkp,int new_value)
{
    node* pre = *linkp;

    register node *current;
    register node *p;

    *linkp=(*linkp)->next;  //因为是带头结点的链表,所以要取头结点的下一个结点
    while((current=*linkp)!=NULL && current->data<new_value)
           linkp=¤t->next;
    p =(node *)malloc(sizeof(node));
    if(p==NULL)
        return NULL;
    p->data=new_value;
    p->next=current;

    if(pre->next==current)  //如果插入的数是在头节点的下一个位置,也就是插入的数最小
        pre->next=p;
    else
        *linkp=p;   //不能写成 (*linkp->next)->nect=p ; 原因我尚未弄清楚,我只知道会出现死循环
    return pre;
}

整体代码如下:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <algorithm>
  4. using namespace std;
  5. struct node
  6. {
  7. int data;
  8. node *next;
  9. };
  10. node *create(int array[],int n); //带头节点的链表
  11. node* insert(register node **linkp,int new_value);
  12. int main()
  13. {
  14. int a[]={1,4,5,8,9,12,15,2,36,5,9,8,5,5,98};
  15. int n=sizeof(a)/sizeof(a[0]);
  16. sort(a,a+n);
  17. for(int i=0;i<n;i++)
  18. {
  19. printf("%3d",a[i]);
  20. }
  21. printf("\n");
  22. printf("创建链表:\n");
  23. node *q=create(a,n);
  24. q=q->next;
  25. while(q!=NULL)
  26. {
  27. printf("%3d",q->data);
  28. q=q->next;
  29. }
  30. printf("\n");
  31. printf("插入一个数后:\n");
  32. node *i=create(a,n);
  33. i=insert(&i,-1);
  34. i=insert(&i,25);
  35. i=insert(&i,-10);
  36. i=i->next;
  37. while(i!=NULL)
  38. {
  39. printf("%3d",i->data);
  40. i=i->next;
  41. }
  42. }
  43. node *create(int array[],int n)
  44. {
  45. node *p,*pre,*head;
  46. head=new node;
  47. head->next=NULL;
  48. pre=head;
  49. for(int i=0;i<n;i++)
  50. {
  51. p=new node;
  52. p->data=array[i];
  53. p->next=NULL;
  54. pre->next=p;
  55. pre=p;
  56. }
  57. return head;
  58. }
  59. node* insert(register node **linkp,int new_value) //返回头节点
  60. {
  61. node* pre = *linkp;
  62. register node *current;
  63. register node *p;
  64. *linkp=(*linkp)->next; //因为是带头结点的链表,所以要取头结点的下一个结点
  65. while((current=*linkp)!=NULL && current->data<new_value)
  66. linkp=&current->next;
  67. p =(node *)malloc(sizeof(node));
  68. if(p==NULL)
  69. return NULL;
  70. p->data=new_value;
  71. p->next=current;
  72. if(pre->next==current) //如果插入的数是在头节点的下一个位置,也就是插入的数最小
  73. pre->next=p;
  74. else
  75. *linkp=p; //不能写成 (*linkp->next)->nect=p ; 原因我尚未弄清楚,我只知道会出现死循环
  76. return pre;
  77. }

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

闽ICP备14008679号