鱼C论坛

 找回密码
 立即注册
查看: 1163|回复: 2

[已解决]单链表尾插法

[复制链接]
发表于 2018-10-12 13:07:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdlib.h>
#include<string.h>
#include <stdio.h>
#include <malloc.h>

struct Book
{
    char a[20];
    struct Book *next;
};

struct Book *initList(char *a)
{
    struct Book *p=(struct Book *)malloc(sizeof(struct Book));
    strcpy(p->a,a);
    p->next = NULL;
    return p;
}

void append(struct Book *head, char *a)
{
    struct Book *p = head;
    struct Book *pNew = (struct Book *)malloc(sizeof(struct Book));
        while(p->next)
                p = p->next;       //这个是循环定位尾部。还有个是小甲鱼老师说的指针定位法。我想学习下这个指针法。
        strcpy(pNew->a, a);
    pNew->next = p->next;
    p->next = pNew;
}

void printList(struct Book *head)
{
    struct Book *p = head;
    while(p)
    {
        printf("%s",p->a);
        p = p->next;
    }
}

int main(void)
{
    struct Book *p = initList("ABC");
    append(p,"DEF");
    append(p,"GHI");
    printList(p);
    return 0 ;
}
最佳答案
2018-10-12 13:33:33
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Book
  5. {
  6.     char a[20];
  7.     struct Book *next;
  8. };

  9. struct Book *initList(char *a)
  10. {
  11.     struct Book *p=(struct Book *)malloc(sizeof(struct Book));
  12.     strcpy(p->a,a);
  13.     p->next = NULL;
  14.     return p;
  15. }

  16. void append(struct Book *head, char *a)
  17. {
  18.     struct Book *p = head;
  19.     struct Book *pNew = (struct Book *)malloc(sizeof(struct Book));
  20.     while(p->next)               // <------------------------------------------------------重点,尾插法需要遍历
  21.         p = p->next;
  22.     strcpy(pNew->a, a);
  23.     pNew->next = p->next;
  24.     p->next = pNew;
  25. }

  26. void printList(struct Book *head)
  27. {
  28.     struct Book *p = head;
  29.     while(p)
  30.     {
  31.         printf("%s",p->a);
  32.         p = p->next;
  33.     }
  34. }

  35. int main(void)
  36. {
  37.     struct Book *p = initList("ABC");
  38.     append(p,"DEF");
  39.     append(p,"GHI");
  40.     printList(p);
  41.     return 0 ;
  42. }
复制代码
4.png
3.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-10-12 13:11:10 | 显示全部楼层
本帖最后由 风骚的小胖子。 于 2018-10-12 13:19 编辑

#include<stdlib.h>
#include<string.h>
#include <stdio.h>
#include <malloc.h>

struct Book
{
        char a[20];
        struct Book *next;
};

struct Book *list()
{
        struct Book *p1=(struct Book *)malloc(sizeof(struct Book));
        p1->next=NULL;
        return p1;

}

struct Book *xinlist(char *a)
{
        struct Book *p2=(struct Book *)malloc(sizeof(struct Book));
//        p2->a=a;
        strcpy(p2->a,a);
        p2->next=NULL;
        return p2;
}

void lianjie(struct Book *p1,char *a)
{
        struct Book *p3=xinlist(a);     // <--------
         static struct Book *p4;
         p1->next=p4;                   //我写的指针定尾。错的 !!  就根据我的代码能否实现指针定位?可以的话帮我改下。不可以的话那为什么小甲鱼老师的那个可以呢?单链表分为有头单链表和无头单链表  或者就是说有头单链表不可以实现指针定位?
        p4->next=p3;
        p3->next=NULL;
                                // p3->next=p1->next;
                                // p1->next=p3;
       p4=p3;
}

void dayin(struct Book *p1)
{
        struct Book *p4=p1->next;
        while(p4!=NULL)
        {
                printf("%s",p4->a);
                p4=p4->next;
        }

}

int main(void)
{
        struct Book *p=list();
        lianjie(p,"ABC");
        lianjie(p,"DEF");
        lianjie(p,"GHI");
        dayin(p);
        return 0 ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-12 13:33:33 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>

  4. struct Book
  5. {
  6.     char a[20];
  7.     struct Book *next;
  8. };

  9. struct Book *initList(char *a)
  10. {
  11.     struct Book *p=(struct Book *)malloc(sizeof(struct Book));
  12.     strcpy(p->a,a);
  13.     p->next = NULL;
  14.     return p;
  15. }

  16. void append(struct Book *head, char *a)
  17. {
  18.     struct Book *p = head;
  19.     struct Book *pNew = (struct Book *)malloc(sizeof(struct Book));
  20.     while(p->next)               // <------------------------------------------------------重点,尾插法需要遍历
  21.         p = p->next;
  22.     strcpy(pNew->a, a);
  23.     pNew->next = p->next;
  24.     p->next = pNew;
  25. }

  26. void printList(struct Book *head)
  27. {
  28.     struct Book *p = head;
  29.     while(p)
  30.     {
  31.         printf("%s",p->a);
  32.         p = p->next;
  33.     }
  34. }

  35. int main(void)
  36. {
  37.     struct Book *p = initList("ABC");
  38.     append(p,"DEF");
  39.     append(p,"GHI");
  40.     printList(p);
  41.     return 0 ;
  42. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 01:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表