打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
链表删除yc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct test{
    int dat;
    struct test *next;
}node;

node *create_node()
{
    node *p,*s,*head;
    int d,cycle=1;

    head = (node *)malloc(sizeof(node));
    p = head;
    printf("Please input your link_list data:\n");
    while(cycle) {
        scanf("%d",&d);
        if(d!=0) {
            s = (node *)malloc(sizeof(node));
            s->dat = d;
            p->next = s;
            p = s;
        }

        else cycle = 0;
    }

    head = head->next;
    p->next = NULL;

    return (head);
}

int list_len(node *head)
{
    int n = 0;
    node *p;
    p = head;
    while(p!=NULL) {
        p = p->next;
        n++;
    }

    return n;
}

void print_list(node *head)
{
    node *p;

    p = head;
    if(p!=NULL) {
        while(p!=NULL) {
            printf("%d\t",p->dat);
            p = p->next;
        }
    }
}

node *list_del(node **head,int data)
{
    node *p,*p_bak;
    p = *head;
    while((p->dat!=data)&&(p->next!=NULL)) {
        p_bak = p;
        p = p->next;
    }

    if(p->dat==data) {
        if(p==*head) {  //如果数据为链表头,则删除链表头,这里需要用双重指针,不然会出现无法删除而用0代替
            *head = p->next;
            free(p);
        }

        else {
            p_bak->next = p->next;
        }
    }

    else printf("No such node \n");

    return head;
}

    else printf("No such node \n");

    return head;
}


int main(void)
{
    node *t;
    int len;

    t=create_node();
    len = list_len(t);
    printf("len=%d\n",len);
    printf("The created list:\t");
    print_list(t);
    printf("\nChecking the link list for data '2'\n");
    list_del(&t,2); //注意用的双重指针,这里需取地址
    printf("After check:\n");
    print_list(t);
    return 0;

}
运行结果:



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C/C++面试题大汇总6
美团网 笔试 2014
单向链表应用函数
单链表判交问题,即是否有环,且环入口点,且两条链表是否相交
剑指offer之找到链表里面包含环的入口节点
3.21
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服