链表基本操作,查、改、增、删、头插法、尾插法

#include <stdio.h>
#include <stdlib.h>
struct Test
{
    int data;
    struct Test *next;
};

void printLink(struct Test *head)
{
    struct Test *p;

    p=head;
   while(p!=NULL){
    printf("%d ",p->data);
    p = p->next;

   }
   printf("\n");
}
int serchData(struct Test* p,int data)  //查
{
    while(p != NULL){
        if(p->data == data){

            return 1;
        }
        p = p->next;
    }
return 0;
}
struct Test* changeData(struct Test* head,int data,int newdata) //改
{
    struct Test *p = head;
     while(p != NULL){
        if(p->data == data){

            p->data = newdata;
        }
        p = p->next;
    }


    return head;
}

struct Test* inserthead(struct Test* head,int data,struct Test* new1)    //在前面插入一个节点
{
    struct Test* p = head;

    if(p->data == data){
        new1->next = head;
        return new1;
    }

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

struct Test* insertbehind(struct Test* head,int data,struct Test* new1)    //在后面插入一个节点
{
    struct Test* p = head;

    while(p!=NULL){
        if(p->data == data){

            new1->next = p->next;
            p->next = new1;
        }
        p = p->next;
    }
    return head;
}

struct Test* insertdelet(struct Test* head,int data)    //删除指定节点
{
    struct Test* p = head;
    if(p->data == data){
    head = head->next;
    return head;
    }
    while(p->next!=NULL){
        if(p->next->data == data){
            p->next = p->next->next;
            return head;
        }
        p = p->next;
    }
    return head;

}

struct Test* insertBehind(struct Test *head,struct Test *new)   //尾插法,1,2,3, 123
{
    struct Test *p = head;
    if(p == NULL){
        head = new;
        return head;
    }
    while(p->next != NULL){
        p = p->next;

        }
        p->next = new;
        return head;
}

struct Test *creatLink2(struct Test *head)  //创建链表
{
    struct Test *new;
    while(1){
        new = (struct Test *)malloc(sizeof(struct Test));
        printf("input your new node data:\n");
        scanf("%d",&(new->data));
        new->next = NULL;
        if(new->data == 0){
            printf("0 quit\n");
            return head;
        }
    head = insertBehind(head,new);
}
}
struct Test* insertHead(struct Test *head,struct Test *new)     //头插法功能
{
        if(head == NULL){
            head = new;
        }else{
            new->next = head;
            head = new;
        }
    return head;
}
struct Test* creatLink1(struct Test *head)   //创建链表
{
    struct Test *new;
    while(1){
        new = (struct Test *)malloc(sizeof(struct Test));
        printf("input your new node data:\n");
        scanf("%d",&(new->data));
        new->next = NULL;
        if(new->data == 0){
            printf("0 quit\n");
            return head;
        }
        head=insertHead(head,new);
    }
}
int main()
{
    struct Test *head = NULL;
    head = creatLink1(head);
    printLink(head);

    /*int i;
    int ret =0;
    int len;
    struct Test t1 = {1,NULL};
    struct Test t2 = {2,NULL};
    struct Test t3 = {3,NULL};
    struct Test t4 = {4,NULL};
    struct Test t5 = {5,NULL};


    t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    t4.next = &t5;

    //len = sizeof(struct Test);
    struct Test *head = NULL;

    head = &t1;

    printLink(&t1);

    //head = changeData(&t1,3,66);
    //printLink(head);

    struct Test new1 = {999,NULL};
    head = inserthead(&t1,2,&new1);
    printLink(head);

    struct Test new2 = {777,NULL};
    head = insertbehind(&t1,4,&new2);
    printLink(head);

    head = insertdelet(&t1,5);
    printLink(head);

    ret=serchData(&t1,3);
        if(ret =1){
            printf("恭喜你找到了\n");
        }else{
            printf("没有这个数\n");
    }
*/
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunshime.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值