线性链表的基本操作(反转,删除,插入等)

主要内容如题,由于感觉自己对链表这块的知识真的很薄弱.

我之前还时不时的去看链表相关的知识,也算是复习把,但是效果不尽人意.

时间可以淡忘一切,有的很慢,有的很快,而我感觉自己对链表的学习则是一瞬的时间.

哈哈,当然也没有这么夸张啦.

还是觉着把单链表的基本操作自己再撸一遍,这个感觉很有必要.

果然在编写的过程中发现很多问题,不过好在有些不太好理解的部分后面在草稿纸上画了一下才顺利编写出对应的代码.

 

我把代码贴上来也方便失忆后的恢复对链表的记忆.我估计后面自己看代码都有可能看不太明白.

 

以下便是自己实现的代码,估计会有很多毛病,但我自测的时候暂时没出现bug:

 

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

typedef struct Node{
    int data;
    struct Node *next;
}Node;

void print(Node *head)//打印
{
    Node *p;

    if (!head) { printf("empty!\n");return;}

    p = head;

    while(p){
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
}

Node *add_tail(Node *head)//尾插
{
    Node *p;
    int num = 0;

    if (!head) return     NULL;

    p = head;

    while (p->next) {
        p = p->next;
    }

    printf("\nhow many Node do you want to add?(tail_insert):");    
    scanf("%d", &num);

    while (num-- > 0) {
        Node *new = (Node *)malloc(sizeof(Node));
        printf("input your data for new Node:");
        scanf("%d", &new->data);
        p->next = new;
        new->next=NULL;
        p = new;
    }
    return head;
}

Node *add_head(Node *head)//头插
{
    int num = 0;

    if (!head) return NULL;


    printf("\nhow many Node do you want to add?(head_insert):");    
    scanf("%d", &num);
    while (num-- > 0) {
        Node *new = (Node *)malloc(sizeof(Node));
        printf("input your data for new Node:");
        scanf("%d", &new->data);
        new->next = head;
        head = new;
    }
    return head;
}

Node *del_pos(Node *head)//根据位置删除对应结点 
{
    Node *p, *del;
    int count = 1;    
    int pos = 0;

    if (!head) return NULL;

    printf("\ninput your delete's position:");
    scanf("%d", &pos);    

    p = head;
    
    if (pos == 1) {//pos 为 1 时 先把下一个结点作为头结点 并free掉原来的头结点 返回新头
            head = p->next;
            free(p);
            return head;
    }
    
    while(p && (count++) != (pos - 1)) {    
        p = p->next;    
    }
    if (count == pos) {
        del = p->next;
        p->next = del->next;
        free(del);        
    } else { printf("out of rang!\n");}
    return head;
}

Node *del_data(Node *head)//根据元素删除对应结点
{    
    Node *tmp, *p, *n;
    int data = 0;    
    int count = 0;//统计结点受影响数    

    if (!head) return NULL;
    
    printf("\ninput your delete's data:");
    scanf("%d", &data);    

    p = head;
    n = head->next;

    while (n) {
        if (n->data == data) {
            p->next = n->next;
            free(n);
            n = p->next;
            count++;
            continue;
        }
        p = p->next;
        n = p->next;
    }
    if (head->data == data) {//头结点需要另外处理
        tmp = head;
        head = tmp->next;    
        free(tmp);
        count++;
    }
    printf("%d rows effected!\n", count);
    return head;
}

Node  *insert(Node *head)//插入结点
{       
    Node *p, *new;
    int count = 1;    
    int pos = 0;

    if (!head) return NULL;

    printf("\ninput your insert's pos:");
    scanf("%d", &pos);

    p = head;

    new = (Node *)malloc(sizeof(Node));

    if (pos == 1) {//pos 为 1 时 进行头插
            printf("input your data for new Node:");
            scanf("%d", &new->data);
            new->next = head;    
            head = new;
            return head;
    } 

    while(p && (count++) != (pos - 1)) {    
        p = p->next;    
    }

    if (count == pos) {
            printf("input your data for new Node:");
            scanf("%d", &new->data);
            new->next = p->next;
            p->next = new;
    } else { printf("out of rang!\n");}

    return head;
    
}

int clean(Node *head)//释放资源
{
    Node *p;

    if (!head) return 0;
    p = head;
    while(p) {
        free(p);
        p = p->next;

    }
    return 1;
}

Node *turn(Node *head)//链表反转
{
    Node *p, *q, *n;
    if (!head) return NULL;
    p = head;
    q = head->next;
    head->next = NULL;
    while (q) {
        n = q->next;
        q->next = p;
        p = q;
        q = n;
    }
    return p; 
}

int main()
{
    Node *n = (Node *)malloc(sizeof(Node));    //定义一个头结点
    Node *tmp;//临时结点

    int count = 1;    
    
    n->data = 2;
    n->next = NULL;


    print(add_tail(n));

    tmp = add_head(n);
    print(tmp);

    while (count-- > 0) {//根据位置删除
        tmp = del_pos(tmp);
        print(tmp);
    }

    count = 1;

    while (count-- > 0) {//插入
        tmp = insert(tmp);
        print(tmp);
    }
    
    count = 1;

    while (count-- > 0) {//根据结点数据删除
        tmp = del_data(tmp);
        print(tmp);
    }
    
    printf("turn around:\n");
    print(turn(tmp));
    
    if (clean(tmp)) printf("clean up\n");
    else printf("it's empty! clean error!\n");
}

 

以下便是我在linux平台测试的结果:

 

我算是新手咯,如果有人点进来看到我写的代码,还请大大们多多指点代码的不足之处.

转载于:https://www.cnblogs.com/yumhoa/p/6634013.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值