链表的增删改查

创建链表:

头插法:

struct Link* init_head_insert(struct Link* head)
{

        struct Link* new;
        struct Link* p=head;
        new=(struct Link*)malloc(sizeof(struct Link));
        puts("input node");
        scanf("%d",&new->date);
        new->next=NULL;

        if(p!=NULL){
                new->next=p;
                p=new;
        }else{
                p=new;
        }
        return p;
}

 尾插法:

struct Link* init_tail_insert(struct Link* head)
{

        struct Link* new;
        struct Link* p=head;
        new=(struct Link*)malloc(sizeof(struct Link));
        puts("input node");
        scanf("%d",&new->date);
        new->next=NULL;

        if(head==NULL){
                head=new;
                return head;
        }

        while(p!=NULL){
                if(p->next==NULL){
                        p->next=new;
                        return head;
                }
                p=p->next;
        }
}

遍历链表:

void printLink(struct Link* head)
{
        struct Link* p=head;
        while(p!=NULL){
                printf("%d  ",p->date);
                p=p->next;
        }
        printf("\n");
}

 增加新节点:

 在指定节点前加入新节点:

struct Link* add_head_insert(struct Link* head)
{
        int N;
        struct Link* new=(struct Link*)malloc(sizeof(struct Link));
        struct Link* p=head;
        puts("where is insert new node");
        scanf("%d",&N);
        puts("input new node");
        scanf("%d",&new->date);
        new->next=NULL;
        if(p->date==N){
                new->next=p;
                p=new;
                return p;
        }

        while(p->next!=NULL){
                if(p->next->date==N){
                        new->next=p->next;
                        p->next=new;
                        return head;
                }
                p=p->next;
        }

}

在指定节点后加入新节点:

void add_tail_insert(struct Link* head)
{
        int N;
        struct Link* new=(struct Link*)malloc(sizeof(struct Link));
        struct Link* p=head;
        puts("where is insert new node");
        scanf("%d",&N);
        puts("input new node");
        scanf("%d",&new->date);
        new->next=NULL;
        while(p!=NULL){
                if(p->date==N){
                        new->next=p->next;
                        p->next=new;
                        break;
                }
                p=p->next;
        }
}

删除节点:

第一种情况:

 第二种情况:

struct Link* delete_Link(struct Link* head)
{
        int N;
        struct Link* p=head;
        puts("input delete node");
        scanf("%d",&N);
        if(p->date==N){
                p=p->next;
                return p;
        }
        while(p->next!=NULL){
                if(p->next->date==N){
                        p->next=p->next->next;
                        return head;
                }
                p=p->next;
        }
}

 修改节点:

void modify_Link(struct Link* head)
{
        int N;
        int new;
        struct Link* p=head;
        puts("modify that node");
        scanf("%d",&N);
        puts("enter the modified link ");
        scanf("%d",&new);
        while(p!=NULL){
                if(p->date==N){
                        p->date=new;
                        break;
                }
                p=p->next;
        }
}

查找节点:

void find_Link(struct Link* head)
{
        int N;
        struct Link* p=head;
        puts("input your find node");
        scanf("%d",&N);
        while(p!=NULL){
                if(p->date==N){
                        printf("Find node\n");
                }
                p=p->next;
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值