首页 > 代码库 > 线性链表的基本操作(删除,插入等)

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

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

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

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

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

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

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

 

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

 

以下便是自己实现的代码,估计会有很多毛病,但我自测的时候暂时没出现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) 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 = http://www.mamicode.com/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 =http://www.mamicode.com/= data) {
            p->next = n->next;
            free(n);
            n = p->next;
            count++;
            continue;
        }
        p = p->next;
        n = p->next;
    }
    if (head->data =http://www.mamicode.com/= 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;
}

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

    int count = 3;    
    
    n->data = http://www.mamicode.com/2;
    n->next = NULL;


    print(add_tail(n));

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

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

    count = 3;

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

    while (count-- > 0) {//根据结点数据删除
        tmp = del_data(tmp);
        print(tmp);
    }

    if (clean(tmp)) printf("clean up\n");
    else printf("it‘s empty! clean error!\n");
}

 

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

技术分享

 

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

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