码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-28 16:29:46      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:inpu   ant   any   erro   count   方便   type   tin   for   

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

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

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

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

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

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

 

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

 

以下便是自己实现的代码,估计会有很多毛病,但我自测的时候暂时没出现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 = 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;
}

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

    int count = 3;    
    
    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 = 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平台测试的结果:

技术分享

 

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

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

标签:inpu   ant   any   erro   count   方便   type   tin   for   

原文地址:http://www.cnblogs.com/yumhoa/p/6634013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!