链表的增删改查(带头节点)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


#define len sizeof(struct student)
#define ERROR  0
#define ok    1


struct student
{
    int num;
//    float score;
    struct student *next;
};


typedef struct student stu;
stu *initstu(stu *head);
stu *create_tail(stu *head, int n);
stu *create_head(stu *head, int n);
stu *delete(stu *head, int n);
stu *insert(stu *head, int n, int e);
stu *find(stu *head, int k);
stu *clear_list(stu *head);
void print(stu *head);




int main()
{
stu *head;
int n, m, j, k, e, l;

head=initstu(head);
print(head);

printf("input the num of create\n");
scanf("%d",&n);
head=create_tail(head,n);
print(head);


printf("input the num of create\n");
scanf("%d",&m);
head=create_head(head,m);
print(head);


printf("input the site of you want delete\n");
scanf("%d",&j);
head=delete(head,j);
print(head);


printf("input the site of you want insert\n");
scanf("%d",&k);
printf("input the num of you want insert\n");
scanf("%d",&e);
head=insert(head,k,e);
print(head);

printf("input yao find de lianbiao shu\n");
    scanf("%d",&l);
find(head,l);


printf("clear\n");
clear_list(head);
print(head);


return 0;
}




stu *initstu(stu *head)
{
head=(stu *)malloc(len);
if(NULL==head)
return ERROR;


head->next=NULL;


return head;
}


stu *create_tail(stu *head, int n)
{
stu *p, *tail;
int i;


p=head;
tail=head;
for(i=0; i<n; i++)
{
tail=(stu *)malloc(len);
scanf("%d",&tail->num);
p->next=tail;
p=tail;
}
tail->next=NULL;


return head;
}




stu *create_head(stu *head, int n)
{
stu *p, *tail;
int i;

head=(stu *)malloc(len);
head->next=NULL;
tail=head->next;

for(i=0; i<n; i++)
{
tail=(stu *)malloc(len);
scanf("%d",&tail->num);
tail->next=head->next;
head->next=tail;
}

return head;
}




stu *delete(stu *head, int n)
{
int i=1;
stu *p, *q;
p=head;




while(p->next&&i<n)
{
i++;
p=p->next;
}
if(p->next==NULL||i>n)
return ERROR;


q=p->next;
p->next=q->next;


free(q);


return head;
}


stu *insert(stu *head, int n, int e)
{
int i=1;
stu *p, *q;

p=head;
while(p && i < n)
{
i++;
p=p->next;
}
if(!p||i>n)
return ERROR;
q=(stu *)malloc(len);
q->num=e;
q->next=p->next;
p->next=q;


return head;
}


stu *clear_list(stu *head)
{
stu *p, *q;
p=head->next;

while(p)
{
q=p->next;
free(p);
p=q;
}
head->next=NULL;

return head;
}


stu *find(stu *head, int k)
{
    stu *p; 
    p=head;
    int count=0;
    while(p!=NULL)
    {   
        if(k==p->num)
        {   
        printf("%d\n",p->num);
        break;
        }   
        p=p->next;
    
    }   
    return 0;
}




void print(stu *head)
{
stu *p;
p=head->next;
if(p==NULL)
printf("This is a empty.\n");
    while(p!=NULL)
    {
        printf("%d\n",p->num);
        p=p->next;
    }


}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值