android复习路之手打链表篇

代码中很多细节都没有考虑,节点的释放,边界值问题,只是单纯的为了练手。

#include
#include 

#define LEN sizeof(struct student)

struct student{//结构体定义
    int a ;
    struct student* next;
};

struct student * Create()//创建链表
{
    struct student *head;
    struct student *p1;
    struct student *p2;
    head=NULL;
    p1=(struct student*)malloc(LEN);
    p2=(struct student*)malloc(LEN);
    printf("请输入信息");
    scanf("%d",&p1->a);
    while(p1->a!=0)
    {
        //p1->next=P2;
    if(head==NULL)
        head=p1;
    else
    {
        p2->next=p1;
        //p2=p1;
    }
     p2=p1;
     p1=(struct student*)malloc(LEN);
    printf("请输入信息");
    scanf("%d",&p1->a);
    }
    p2->next=NULL;
    return head;
}
void Print(struct student *head)//链表的输出
{
    //printf("ok");
    struct student*p1;
    p1=head;
    while(p1)
    {
        printf("%d",p1->a);
        p1=p1->next;
    }
    printf("\n");
}
struct student * add( struct student*head,int x)//添加节点
{
        struct student *p1;
        struct student *p2;
        struct student *p3;
        p1=head;
        while(p1->a<x)
        {
        p2=p1;
        p1=p1->next;
        }
        p3=(struct student *)malloc(LEN);
        p2->next=p3;
        p3->a=x;
        p3->next=p1;
        return head;
}
struct student * dele( struct student *head,int x)//删除节点
{
    struct student * p1;
    struct student *p2;
    p1=head;
    while(p1->a!=x)
    {
        p2=p1;
        p1=p1->next;
    }
    p2->next=p2->next->next;
    return head;
}
int main()
{
    struct student* start;
    start=Create();
    printf("创建的链表是:");
    Print(start);
    add(start,4);
    printf("添加节点后的链表是:");
    Print(start);
    dele(start,2);
     printf("删除节点后的链表是:");
    Print(start);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值