学生信息
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct student)
struct student{
char name[20];
int num;
float score;
struct student * next;
};
int n=0;
int main()
{
struct student * creat();
struct student * change(struct student * head);
void print(struct student *);
struct student * head;
head=creat();
head=change(head);
print(head);
return 0;
}
struct student * creat()
{
struct student * head;
struct student * p1,* p2;
char a[2]={"*"};
p1=p2=(struct student *)malloc(LEN);
scanf("%s",p1->name);
if(strcmp(p1->name,a)!=0)
{
scanf("%d%f",&p1->num,&p1->score);
head=NULL;
while(strcmp(p1->name,a)!=0)
{
n++;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%s",p1->name);
if(strcmp(p1->name,a)!=0)
scanf("%d%f",&p1->num,&p1->score);
else
break;
}
p2->next=NULL;
}
else
head=NULL;
return (head);
}
struct student * change(struct student * head)
{
struct student * p1=head,*p2=head;
float sum=0,aver=0;
for(;p1!=NULL;)
{
sum+=p1->score;
p1=p1->next;
}
aver=1.0*sum/n;
p1=head;
for(;p1!=NULL;)
{
if(p1->score<aver&&p1==head)
{
head=p1->next;
free(p1);
p1=p2=head;
}
else if(p1->score<aver&&p1!=head)
{
p2->next=p1->next;
free(p1);
p1=p2->next;
}
else if(p1->score>=aver)
{
p2=p1;
p1=p1->next;
}
}
return head;
}
void print(struct student * head)
{
struct student * p;
p=head;
for(;p!=NULL;)
{
printf("%s\n",p->name);
p=p->next;
}
}
单向链表
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Node)
struct Node{
int data;
struct Node * next;
};
int number=0;
int main()
{
void creat(int n);
int n;
scanf("%d",&n);
creat(n);
return 0;
}
void creat(int n)
{
void del(struct Node * head);
void print(struct Node * head);
struct Node * head, * p1, * p2;
int i=0;
for(i=0;i<n;i++)
{
head=NULL;
p1=p2=(struct Node *)malloc(LEN);
scanf("%d",&p1->data);
for(;p1->data!=-1;)
{
number++;
if(number==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct Node *)malloc(LEN);
scanf("%d",&p1->data);
}
p2->next=NULL;
del(head);
number=0;
}
}
void del(struct Node * head)
{
void print(struct Node * head);
struct Node * p1, * p2;
p1=p2=head;
for(;p1!=NULL;)
{
if(p1->data%2!=0&&p1==head)
{
head=p1->next;
free(p1);
p1=p2=head;
}
else if(p1->data%2!=0&&p1!=head)
{
p2->next=p1->next;
free(p1);
p1=p2->next;
}
else if(p1->data%2==0)
{
p2=p1;
p1=p1->next;
}
}
print(head);
}
void print(struct Node * head)
{
struct Node * p=head;
if(p==NULL)
printf("NULL");
else
for(;p!=NULL;)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
链表的交集
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Arr)
struct Arr{
int a;
struct Arr * next;
};
int n=0,m=0;
int main()
{
struct Arr * seek(struct Arr * head1,struct Arr * head2);
void print(struct Arr * head);
struct Arr * head=NULL, * head1=NULL, * head2=NULL, * p1=NULL, * p2=NULL;
p1=p2=(struct Arr *)malloc(LEN);
scanf("%d",&p1->a);
for(;p1->a!=-1;)
{
n++;
if(n==1)
head1=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct Arr *)malloc(LEN);
scanf("%d",&p1->a);
}
p2->next=NULL;
p1=p2=(struct Arr *)malloc(LEN);
scanf("%d",&p1->a);
for(;p1->a!=-1;)
{
m++;
if(m==1)
head2=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct Arr *)malloc(LEN);
scanf("%d",&p1->a);
}
p2->next=NULL;
head=seek(head1,head2);
print(head);
return 0;
}
struct Arr * seek(struct Arr * head1,struct Arr * head2)
{
struct Arr * head=NULL, * p1=head1, * p2=head2;
struct Arr * p3, * p4;
n=m=0;
for(;p1!=NULL;)
{
for(p2=head2;p2!=NULL;)
{
if(p1->a==p2->a)
{
n++;
p3=p4=(struct Arr *)malloc(LEN);
if(n==1)
head=p1;
p3->a=p1->a;
}
p2=p2->next;
}
p1=p1->next;
}
p3->next=NULL;
return head;
}
void print(struct Arr * head)
{
struct Arr * p=head;
if(p==NULL)
printf("NULL");
else
for(;p!=NULL;)
{
printf("%d ",p->a);
p=p->next;
}
printf("\n");
}
算法2-8~2-11:链表的基本操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct link)
struct link{
int data;
struct link * next;
};
struct match{
char a[20];
int data1;
int data2;
}item;
int main()
{
struct link * creat(int num);
void show(struct link * head);
struct link * del(struct link * head,int n);
struct link * insert(struct link * head,int n,int data3);
void get(struct link * head,int n);
struct link * head;
int num,m,i; //num为初始数据个数。
scanf("%d",&num);
head=creat(num);
//show(head);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%s",item.a);
if(strcmp(item.a,"show")==0)
show(head);
else if(strcmp(item.a,"delete")==0)
{
scanf("%d",&item.data1);
head=del(head,item.data1);
}
else if(strcmp(item.a,"insert")==0)
{
scanf("%d%d",&item.data1,&item.data2);
head=insert(head,item.data1,item.data2);
}
else if(strcmp(item.a,"get")==0)
{
scanf("%d",&item.data1);
get(head,item.data1);
}
}
return 0;
}
struct link * creat(int num)
{
struct link * head, * p;
int i;
head=NULL;
for(i=0;i<num;i++)
{
p=(struct link *)malloc(LEN);
scanf("%d",&p->data);
p->next=head;
head=p;
}
return head;
}
void show(struct link * head)
{
struct link * p1=head;
if(p1==NULL)
printf("Link list is empty\n");
else
{
for(;p1!=NULL;)
{
printf("%d ",p1->data);
p1=p1->next;
}
printf("\n");
}
}
struct link * del(struct link * head,int n)
{
struct link * p1, * p2;
int i;
int sign=0;
p1=p2=head;
for(i=1;p1!=NULL;i++)
{
if(i==n&&n==1)
{
sign=1;
head=p1->next;
free(p1);
p1=p2=head;
break;
}
else if(i==n&&n!=1)
{
sign=1;
p2->next=p1->next;
free(p1);
p1=p2->next;
break;
}
p2=p1;
p1=p1->next;
}
if(sign)
printf("delete OK\n");
else
printf("delete fail\n");
return head;
}
struct link * insert(struct link * head,int n,int data3)
{
int sign=0,i;
struct link * p1, * p2;
p1=p2=head;
if(p1==NULL&&n==1)
{
sign=1;
p1=(struct link *)malloc(LEN);
p1->data=data3;
head=p1;
p1->next=NULL;
}
else
{
for(i=1;p1!=NULL;i++)
{
if(i==n&&n==1)
{
sign=1;
p1=(struct link *)malloc(LEN);
p1->data=data3;
head=p1;
p1->next=p2;
p2=p1;
break;
}
else if(i==n-1&&n!=1)
{
sign=1;
p1=(struct link *)malloc(LEN);
p1->data=data3;
p1->next=p2->next;
p2->next=p1;
p2=p1;
break;
}
p1=p2=p1->next;
}
}
if(sign)
printf("insert OK\n");
else
printf("insert fail\n");
return head;
}
void get(struct link * head,int n)
{
int i,sign=0;
struct link * p1;
p1=head;
for(i=1;p1!=NULL;i++)
{
if(i==n)
{
sign=1;
printf("%d\n",p1->data);
}
p1=p1->next;
}
}