栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

链表的增删改查

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链表的增删改查

创建链表:

头插法:

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 noden");
                }
                p=p->next;
        }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1014652.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号