写在前面:
相关的解题方法网上已经很多了,我就不班门弄斧防止误导大家。
这里仅展现下我的代码,如果有不认同的可以选择其他大佬。
另外,重要的事情说三遍:
不要直接copy!!!
不要直接copy!!!
不要直接copy!!!
请务必在理解的基础上写出自己的代码,这样才有可能发现自己的某些遗漏点!
OJ-2132-建立学生信息链表:
void input()
{
typedef struct stud_node stu;
stu *p=(stu*)malloc(sizeof(stu));
while(scanf("%d",&p->num)&&p->num!=0)
{
scanf("%s%d",p->name,&p->score);
p->next=NULL;
if(!head)head=p;
else tail->next=p;
tail=p;
p=(stu*)malloc(sizeof(stu));
}
}
OJ-2133-学生成绩链表处理:
typedef struct stud_node stu;
struct stud_node *deletelist( struct stud_node *head, int min_score )
{
while(head&&head->score<min_score)
{
head=head->next;
}
if(!head)return NULL;
stu *pt=head,*pu=head->next;
while(pu)
{
if(pu->score<min_score)
{
pt->next=pu->next;
}
else pt=pu;
pu=pt->next;
}
return head;
}
struct stud_node *createlist()
{
stu* head=NULL,*tail=NULL;
stu *p=(stu*)malloc(sizeof(stu));
while(scanf("%d",&p->num)&&p->num!=0)
{
scanf("%s%d",p->name,&p->score);
p->next=NULL;
if(!head)head=p;
else tail->next=p;
tail=p;
p=(stu*)malloc(sizeof(stu));
}
return head;
};
OJ-2134-逆序数据建立链表:
struct ListNode *createlist()
{
struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode)),*head=NULL;
int num;
scanf("%d",&num);
while(num!=-1)
{
p->data=num;
p->next=head;
head=p;
scanf("%d",&num);
p=(struct ListNode*)malloc(sizeof(struct ListNode));
}
return head;
};
OJ-2135-链表拼接 :
法一:
#define swap(a,b) {int t=a;a=b;b=t;}
void kuai(int a[],int l,int r)
{
int mid=a[l+(r-l)/2];
int left=l,right=r;
while(left<=right)
{
while(a[left]<mid)left++;
while(a[right]>mid)right--;
if(left<=right)
{
swap(a[left],a[right]);
left++;
right--;
}
}
if(l<right)kuai(a,l,right);
if(r>left)kuai(a,left,r);
}
typedef struct ListNode ln;
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
int a[2000],n=0;
while(list1)
{
a[n++]=list1->data;
list1=list1->next;
}
while(list2)
{
a[n++]=list2->data;
list2=list2->next;
}
kuai(a,0,n-1);
ln *head=NULL,*tail=NULL,*p1=(ln*)malloc(sizeof(ln));
for(int i=0; i<n; i++)
{
p1->data=a[i],p1->next=NULL;
if(!head)head=p1;
else tail->next=p1;
tail=p1;
p1=(ln*)malloc(sizeof(ln));
}
return head;
};
法二:
typedef struct ListNode ln;
struct ListNode *mergelists(struct ListNode *list1, struct ListNode *list2)
{
if(!list1)return list2;
if(!list2)return list1;
ln *head=NULL,*tail=NULL;
while(list2&&list1)
{
if(list1->data<list2->data)
{
if(!head)head=list1;
else tail->next=list1;
tail=list1;
list1=list1->next;
}
else
{
if(!head)head=list2;
else tail->next=list2;
tail=list2;
list2=list2->next;
}
}
if(!list1&&list2)
{
tail->next=list2;
tail=list2;
list2=list2->next;
}
if(!list2&&list1)
{
tail->next=list1;
tail=list1;
list1=list1->next;
}
return head;
};
OJ-2136-奇数值结点链表:
法一:
struct ListNode *readlist()
{
struct ListNode *p=(struct ListNode *)malloc(sizeof(struct ListNode)),*head=NULL,*tail=NULL;
while(scanf("%d",&p->data)&&p->data!=-1)
{
p->next=NULL;
if(head==NULL)head=p;
if(tail!=NULL)tail->next=p;
tail=p;
p=(struct ListNode *)malloc(sizeof(struct ListNode));
}
return head;
};
struct ListNode *getodd( struct ListNode **L )
{
struct ListNode *evenhead=NULL,*eventail=NULL,*oddhead=NULL,*oddtail=NULL;
while(*L!=NULL)
{
if((*L)->data%2)
{
struct ListNode *p= (struct ListNode*)malloc(sizeof(struct ListNode));
p->data=(*L)->data;
p->next=NULL;
if(evenhead==NULL)evenhead=p;
else eventail->next=p;
eventail=p;
}
else
{
struct ListNode *p= (struct ListNode*)malloc(sizeof(struct ListNode));
p->data=(*L)->data,p->next=NULL;
if(oddhead==NULL)oddhead=p;
else oddtail->next=p;
oddtail=p;
}
*L=(*L)->next;
}
*L=oddhead;
return evenhead;
}
法二:
typedef struct ListNode lt;
struct ListNode *readlist()
{
lt *p=(lt*)malloc(sizeof(lt)),*head=NULL,*tail=NULL;
while(scanf("%d",&p->data)&&p->data!=-1)
{
p->next=NULL;
if(head==NULL)head=p;
else tail->next=p;
tail=p;
p=(lt*)malloc(sizeof(lt));
}
return head;
};
struct ListNode *getodd( struct ListNode **L )
{
lt* jihead=NULL,*jitail=NULL,*ouhead=NULL,*outail=NULL,*p=*L;
while(p)
{
if(p->data%2)
{
if(!jihead)jihead=p;
else jitail->next=p;
jitail=p;
p=p->next;
jitail->next=NULL;
}
else
{
if(!ouhead)ouhead=p;
else outail->next=p;
outail=p;
p=p->next;
outail->next=NULL;
}
}
*L=ouhead;
return jihead;
};
OJ-2137-单链表结点删除
法一:
typedef struct ListNode lt;
struct ListNode *readlist()
{
lt *p=(lt*)malloc(sizeof(lt)),*head=NULL,*tail=NULL;
while(scanf("%d",&p->data)&&p->data!=-1)
{
p->next=NULL;
if(head==NULL)head=p;
else tail->next=p;
tail=p;
p=(lt*)malloc(sizeof(lt));
}
return head;
};
struct ListNode *deletem( struct ListNode *L, int m )
{
while(L&&L->data==m)L=L->next;
if(!L)return NULL;
lt *p=L,*pn=L->next;
while(pn)
{
if(pn->data==m)
{
p->next=pn->next;
pn=p->next;
}
else p=pn,pn=pn->next;
}
return L;
};
OJ-2138-链表逆置
typedef struct ListNode ln;
struct ListNode *reverse( struct ListNode *head )
{
ln *p=head;
head=NULL;
while(p)
{
ln* temp=(ln*)malloc(sizeof(ln));
temp->data=p->data;
temp->next=head;
head=temp;
p=p->next;
}
return head;
};
OJ-2139-统计专业人数
int countcs( struct ListNode *head )
{
int count=0;
while(head)
{
if(head->code[1]=='0'&&head->code[2]=='2')count++;
head=head->next;
}
return count;
}
**
写在最后:
就这样结束了,over