#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;
}
}
#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;
}
}