C语言_链表操作
发布日期:2021-05-14 23:42:46 浏览次数:17 分类:精选文章

本文共 2794 字,大约阅读时间需要 9 分钟。

������������

���������������������������������������������������������������������������������������������������������������������������������������������

���������������

������������������������������������������������������������������������������������

  • ���������������������������������
  • ���������������������������������������������������
  • ������������������

    ���������������������

    • ���������������������������������������������
    • ������������������������������������������������������

    ������������

    typedef struct node {
    datatype data;
    struct node *next;
    } LNode, *LinkList;

    ������������������������

    ���������������������������������������������������������

    • ���������������������������������������������������������
    • ������������������������ head������������������������ NULL���

    ������������

    #include 
    #include
    #include
    struct student {
    int data;
    struct student *next;
    };
    typedef struct student stu;
    int main() {
    stu *head = NULL;
    int num = 0;
    printf("please enter the max num of students:");
    scanf("%d", &num);
    head = (stu *)malloc(num * sizeof(stu));
    stu *current = head;
    }

    ������������������

    1. ������������������

    lnode *createList() {
    lnode *l = (lnode *)malloc(sizeof(lnode));
    l->data = -1;
    l->next = NULL;
    return l;
    }

    2. ������������

    void insertAfter(int id) {
    lnode *p = (lnode *)malloc(sizeof(lnode));
    p->data = id;
    p->next = NULL;
    lnode *current = head;
    while (current->next != NULL) {
    current = current->next;
    }
    current->next = p;
    }

    3. ������������������

    void traverse() {
    lnode *current = head;
    while (current->next != NULL) {
    current = current->next;
    printf("%d ", current->data);
    }
    printf("\n");
    }

    4. ������������������

    int listlen() {
    int c = 0;
    lnode *current = head;
    while (current->next != NULL) {
    c++;
    current = current->next;
    }
    return c;
    }

    5. ������������

    lnode *find(int id) {
    lnode *current = head;
    while (current->next != NULL && current->data != id) {
    current = current->next;
    }
    return current;
    }

    6. ������������

    int delData(int id) {
    lnode *cur = head;
    lnode *p = NULL;
    while (cur->next != NULL) {
    if (cur->next->data == id) {
    p = cur->next;
    cur->next = p->next;
    free(p);
    return 1;
    }
    cur = cur->next;
    }
    return 0;
    }

    ���������������

    int main() {
    head = createList();
    insertAfter(10);
    insertAfter(20);
    insertAfter(30);
    traverse();
    printf("list length: %d\n", listlen());
    stu *t = find(30);
    if (t) {
    printf("search data = %d\n", t->data);
    } else {
    printf("not find!\n");
    }
    delData(20);
    traverse();
    }
    上一篇:C语言_动态内存分配练习
    下一篇:C语言_常用排序算法

    发表评论

    最新留言

    很好
    [***.229.124.182]2025年05月03日 07时19分40秒

    关于作者

        喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
    -- 愿君每日到此一游!

    推荐文章