最近很是秃头,感觉好难啊,每天一次祈求,希望C语言可以八十五分以上,求求上帝爸爸了
好了,话不多说,今天美丽刺激的链表环节开始了!!!首先,我们开始先写几道经典的题型
建立一个有头后插的链表,然后稍稍做一点变动
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head);
struct student *inset(struct student *head,int n);
void print(struct student *head);
int main()
{ struct student *head;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
head=creatlist(head);
print(head);
return 0;
}
struct student *creatlist(struct student *head)
{ int i,j,x,n;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<3;i++)
{
p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
scanf("%d",&n);
inset(head,n);
return head;
}
struct student *inset(struct student *head,int n)
{ int i;
struct student *pre,*p,*stu;
p=head;
stu=(struct student *)malloc(sizeof(struct student));
stu->num=n;
for(i=0;i<3;i++)
{
/*if(i==2)
{ stu->next=p->next;
p->next=stu;
}*/
pre=p;
p=p->next;
}
stu->next=p;
pre->next=stu;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{
printf("->%d",p->num);
p=p->next;
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
struct student *inset(struct student *head,int n);
void print(struct student *head);
int main()
{ struct student *head;
int n;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
print(head);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i,j,x;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{
p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
struct student *inset(struct student *head,int n);
void print(struct student *head);
int main()
{ struct student *head;
int n;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
print(head);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i,j,x;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{
p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=head->next;
head->next=p;
}
return head;
}
void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
}
前面两个真的是很经典基础的有头前插和有头尾插,很基础,很简单的。
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{ int num;
struct student *next;
};
struct student *creatlist(struct student *head);
void print(struct student *head,int j);
int main()
{ struct student *head;
int n;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
head=creatlist(head);
return 0;
}
struct student *creatlist(struct student *head)
{ int i,j=0,x;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
while(p->num!=0)
{
p->next=tp->next;
tp->next=p;
tp=p;
j++;
p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
} print(head,j);
return head;
}
void print(struct student *head,int j)
{
struct student *p;
p=head->next;
while(p)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n%d",j);
}
也是个简单的题,没啥好说的,下一个
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
void print(struct student *head,int m);
int main()
{ struct student *head;
int n,m;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
scanf("%d",&m);
print(head,m);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
void print(struct student *head,int m)
{ int t=0,i=0;
struct student *p;
p=head->next;
while(p)
{ i++;
if(p->num==m) {printf("%d %d",m,i);t=1;break;}
p=p->next;
}
if(t==0) printf("0");
}
这个题其实很简单,但是我码的时候遇到了一个愚蠢的问题,居然输入的时候没有用&,所以各种没输出,害!!!!!
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
void print(struct student *head,int m);
int main()
{ struct student *head;
int n,m;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
scanf("%d",&m);
print(head,m);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
void print(struct student *head,int m)
{ int t=0,i=0,k;
struct student *p;
p=head->next;
while(p)
{ i++;
if(p->num==m&&i!=1) {printf("%d",k);t=1;break;}
k=p->num;
p=p->next;
}
if(t==0) printf("没有前驱");
}
目前很顺利,比指针顺利多了,害,希望一如既往的顺利。
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
//void print(struct student *head);
int main()
{ struct student *head;
int n,m;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
//print(head);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i,j=0;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
if(p->num%2==0)
j++;
}
printf("%d",j);
return head;
}
/*void print(struct student *head,int m)
{ int t=0,i=0,k;
struct student *p;
p=head->next;
while(p)
{ i++;
if(p->num==m&&i!=1) {printf("%d",k);t=1;break;}
k=p->num;
p=p->next;
}
if(t==0) printf("没有前驱");
}*/
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
struct student *inset(struct student *head,int m,int n);
void print(struct student *head);
int main()
{ struct student *head;
int n,m;
head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
scanf("%d",&n);
head=creatlist(head,n);
scanf("%d",&m);
inset(head,m,n);
print(head);
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i,j=0;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
struct student *inset(struct student *head,int m,int n)
{ int i;
struct student *p,*pre;
p=head;
for(i=0;i<n;i++)
{ pre=p->next;
if(pre->num==m)
{
p->next=pre->next;
}
else p=p->next;
}
};
void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{ printf("%d ",p->num);
p=p->next;
}
}
这个题就是删除链表,所以需要掌握
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
int num;
struct student *next;
};
struct student *creatlist(struct student *head,int n);
struct student *inset(struct student *head,int m,int n);
void print(struct student *head);
int main()
{ struct student *head;
int n,m;
while(~scanf("%d",&n))
{head=(struct student *)malloc(sizeof(struct student));
if(!head) exit(0);
head=creatlist(head,n);
scanf("%d",&m);
inset(head,m,n);
print(head);}
return 0;
}
struct student *creatlist(struct student *head,int n)
{ int i,j=0;
struct student *tp; struct student *p;
head->next=NULL;
tp=head;
for(i=0;i<n;i++)
{p=(struct student *)malloc(sizeof(struct student));
scanf("%d",&(p->num));
p->next=tp->next;
tp->next=p;
tp=p;
}
return head;
}
struct student *inset(struct student *head,int m,int n)
{ int i,t=0;
struct student *p,*pre;
p=head;
struct student *stu;
stu=(struct student *)malloc(sizeof(struct student ));
stu->num=m;
for(i=0;i<n;i++)
{ pre=p->next;
if(pre->num>m&&i!=n-1)
{ stu->next=pre;
p->next=stu;t=1;break;
}
else
p=p->next;
}
if(t==0){pre->next=stu;
stu->next=NULL;
}
};
void print(struct student *head)
{
struct student *p;
p=head->next;
while(p)
{ printf("%d ",p->num);
p=p->next;
}
}
#include<stdlib.h>
#include<stdio.h>
typedef struct list
{ int data;
struct list* next;
};
void creatline(struct list *head,int n);
void inset(struct list *head,int x);
void print(struct list *head);
int main()
{ struct list *head; int n;
while(scanf("%d",&n)!=EOF)
{ head=(struct list *)malloc(sizeof(struct list));
head->next=NULL;
creatline(head,n);
print(head);
}
return 0;
}
void creatline(struct list *head,int n)
{ int i,x;
for(i=0;i<n;i++)
{
scanf("%d",&x);
inset(head,x);
}
}
void inset(struct list *head,int x)
{
struct list *pre,*p,*p2;
pre=head;
p=head->next;
while(p!=NULL&&p->data<x)
{
pre=p;p=p->next;
}
p2=(struct list *)malloc(sizeof(struct list));
p2->data=x;
p2->next=p;
pre->next=p2;
}
void print(struct list *head)
{
struct list *p;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
typedef struct student{
char name[10];
int num;
float a,b,c;
float sum,eva;
}stu;
typedef struct line
{
stu data;
struct line *next;
};
struct line *creatline(struct line *head,int n);
void print(struct line *head);
int main()
{ struct line *head;
int n;
while(~scanf("%d",&n))
{head=(struct line *)malloc(sizeof(struct line));
creatline(head,n);
}
return 0;
}
struct line *creatline(struct line *head,int n)
{
struct line *tp,*p;
head->next=NULL;
tp=head;
int i;
for(i=0;i<n;i++)
{ p=(struct line *)malloc(sizeof(struct line));
scanf("%d %s %f %f %f",&(p->data.num),&(p->data.name),&(p->data.a),&(p->data.b),&(p->data.c));
p->data.sum=(p->data.a)+(p->data.b)+(p->data.c);
p->data.eva=(p->data.sum)/3;
p->next=tp->next;
tp->next=p;
tp=p;
}
print(head);
return (head);
};
void print(struct line *head)
{
struct line *p;
int i=0;
p=head->next;
while(p!=NULL)
{
printf("%d %s %.2f %.2f %.2f %.2f %.2f",p->data.num,p->data.name,p->data.a,p->data.b,p->data.c,p->data.eva,p->data.sum);
p=p->next;printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
char name[30];
double a[3];
double aver;
double sum;
struct list *next;
}list;
void creat(list *rear,int n)
{
int i,j;
double temp;
for(i=0;i<n;i++)
{
temp=0;
list *knot;
knot=(list *)malloc(sizeof(list));
scanf("%d",&knot->num);
scanf("%s",knot->name);
for(j=0;j<3;j++)
{
scanf("%lf",&knot->a[j]);
temp=temp+knot->a[j];
}
knot->sum=temp;
temp=temp/3;
knot->aver=temp;
rear->next=knot;
rear=knot;
}
rear->next=NULL;
}
int main()
{
int n;
while(scanf("%d",&n)!=-1)
{
list *head,*rear;
head=(list *)malloc(sizeof(list));
rear=head;
creat(rear,n);
list *find;
find=head;
int flag,k=0,temp=0;
scanf("%d",&flag);
while(find->next!=NULL)
{
temp++;
find=find->next;
if(find->num==flag)
{
k=1;
break;
}
}
if(k==1)
{
printf("%d ",temp);
printf("%d %s ",find->num,find->name);
printf("%.2lf %.2lf %.2lf ",find->a[0],find->a[1],find->a[2]);
printf("%.2lf %.2lf",find->aver,find->sum);
printf("\n");
}
if(k==0)
{
printf("0\n");
}
}
}
#include<stdlib.h>
#include<stdio.h>
typedef struct{
unsigned int number;
char name[20];
double grade[3];
double sum,ave;
}Data;
typedef struct{
Data data;
struct List* next;
}List;
void fun(List* Head,List *insert){
List *pre=Head;
int flag=0;
while(pre->next!=NULL){
List* now=pre->next;
Data temp=now->data,target=insert->data;
if(temp.number>target.number){
pre->next=insert;
insert->next=now;
flag=1;break;
}
else
pre=pre->next;
if(!flag&&pre->next==NULL){
pre->next=insert;
insert->next=NULL;
}
}
List *find=Head->next;
while(find!=NULL){
Data temp=find->data;
printf("%u %s %.2lf %.2lf %.2lf %.2lf %.2lf\n",temp.number,temp.name,temp.grade[0],temp.grade[1],temp.grade[2],temp.ave,temp.sum);
find=find->next;
}
}
int main()
{
int n;
while(~scanf("%d",&n)){
List *Head,*Rear;
Head=(List*)malloc(sizeof(List));
Head->next=NULL;
Rear=Head;
while(n--){
int i;
Data temp;
List* node;
temp.sum=0;
scanf("%u%s",&temp.number,&temp.name);
for(i=0;i<3;i++){
scanf("%lf",&temp.grade[i]);
temp.sum+=temp.grade[i];
}
temp.ave=temp.sum/3.0;
node=(List*)malloc(sizeof(List));
node->data=temp;
Rear->next=node;
Rear=node;
}
Rear->next=NULL;
//以上为链表的创建
int i;
Data insert_data;
List* insert;
insert_data.sum=0;
scanf("%u%s",&insert_data.number,&insert_data.name);
for(i=0;i<3;i++){
scanf("%lf",&insert_data.grade[i]);
insert_data.sum+=insert_data.grade[i];
}
insert_data.ave=insert_data.sum/3.0;
insert=(List*)malloc(sizeof(List));
insert->data=insert_data;
fun(Head,insert);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int num;
char name[10];
float a,b,c;
float sum,eva;
}data;
typedef struct list{
data data;
struct list *next;
};
void creatlist(struct list *head,int x)
{ int j=1;
struct list *tp;
tp=head;
while(tp->next!=NULL)
{ struct list *p; p=tp->next;
data target=p->data;
if(target.num==x)
{ tp->next=p->next;
//free(p);
j=0;
break;
}
else{tp=tp->next;}
}
struct list *find;
find=head->next;
while(j==0&&find!=NULL)
{
printf("%d %s %.2f %.2f %.2f %.2f %.2f\n",find->data.num,find->data.name,find->data.a,find->data.b,find->data.c,find->data.eva,find->data.sum);
find=find->next;
}
if(j==1) printf("Error\n");
}
int main()
{
struct list *head;
head=(struct list *)malloc(sizeof(struct list ));
head->next=NULL;
int n,i,x;
while(~scanf("%d",&n))
{ struct list *tp,*p;
tp=head;
for(i=0;i<n;i++)
{
p=(struct list *)malloc(sizeof(struct list));
data temp;
scanf("%d %s %f %f %f",&temp.num,&temp.name,&temp.a,&temp.b,&temp.c);
temp.sum=temp.a+temp.b+temp.c;
temp.eva=temp.sum/3;
p->data=temp;
p->next=tp->next;
tp->next=p;
tp=p;
}
tp->next=NULL;
scanf("%d",&x);
creatlist(head,x);
}
}
#include<stdlib.h>
#include<stdio.h>
typedef struct{
unsigned int number;
char name[20];
double grade[3];
double sum,ave;
}Data;
typedef struct{
Data data;
struct List* next;
}List;
void List_output(List* Head){
List *find=Head->next;
while(find!=NULL){
Data temp=find->data;
printf("%u %s %.2lf %.2lf %.2lf %.2lf %.2lf\n",temp.number,temp.name,temp.grade[0],temp.grade[1],temp.grade[2],temp.ave,temp.sum);
find=find->next;
}
}
void List_insert(List*Head,List*insert){
List *pre=Head;
int flag=0;
while(1){
if(!flag&&pre->next==NULL){
pre->next=insert;
insert->next=NULL;
break;
}
List* now=pre->next;
Data temp=now->data,insert_data=insert->data;
if(temp.sum<insert_data.sum){
pre->next=insert;
insert->next=now;
flag=1;
break;
}
else
pre=pre->next;
}
}
int main()
{
int n;
while(~scanf("%d",&n)){
List *Head;
Head=(List*)malloc(sizeof(List));
Head->next=NULL;
while(n--){
int i;
Data temp;
List* node;
temp.sum=0;
scanf("%u%s",&temp.number,&temp.name);
for(i=0;i<3;i++){
scanf("%lf",&temp.grade[i]);
temp.sum+=temp.grade[i];
}
temp.ave=temp.sum/3.0;
node=(List*)malloc(sizeof(List));
node->data=temp;
List_insert(Head,node);
}
List_output(Head);
}
return 0;
}
终于完了,最后三个有点卡壳,不,是真的卡壳!!!!