题目:
代码:
#include<bits/stdc++.h>
#define ph print(head)
using namespace std;
struct node{
int val;
node* next;
};
node* create(){
node *head,*tail,*temp;
head=new node;
head->next=NULL;
tail=head;
printf("请输入一串链表(以-1结尾):\n");
while(1){
temp=new node;
int x; cin>>x;
if(x==-1) break;
temp->val=x;
tail->next=temp;
tail=temp;
}
tail->next=NULL;
return head;
}
void print(node* head){
node* temp=head->next;
printf("现在的链表是:\n");
while(temp){
printf("%d ",temp->val);
temp=temp->next;
}
cout<<endl;
}
void deletefirst(node* head){
printf("请输入你要删除的值(本次只删除第一个):\n");
int x; cin>>x;
node* pre=head;
node* temp=head->next;
while(temp){
if(temp->val==x){
pre->next=temp->next;
return;
}
pre=temp;
temp=temp->next;
}
}
void reverse(node* head){
printf("倒置中...\n");
int num[100]={0},k=0;
node *temp1=head->next,*temp2=head->next;
while(temp1){
num[k++]=temp1->val;
temp1=temp1->next;
}
while(temp2){
temp2->val=num[--k];
temp2=temp2->next;
}
}
void insert(node *head){
printf("请输入你要插入的值:\n");
int x; cin>>x;
node *pre=head;
node *temp=head->next;
while(temp&&temp->val<=x){
pre=temp;
temp=temp->next;
}
node *newnode=new node;
newnode->val=x;
if(temp==NULL){
pre->next=newnode;
pre=newnode;
pre->next=NULL;
}
else{
newnode->next=pre->next;
pre->next=newnode;
}
}
void deleteall(node* head){
printf("请输入你要删除的值(本次删除所有该值):\n");
int x; cin>>x;
node *pre=head;
node *temp=head->next;
while(temp){
if(temp->val==x){
pre->next=temp->next;
temp=temp->next;
}
else{
pre=temp;
temp=temp->next;
}
}
}
void mysort(node *head){
printf("排序中...\n");
node *pre,*ergodic,*temp;
for(pre=head->next;pre->next!=NULL;pre=pre->next){
for(ergodic=pre->next;ergodic!=NULL;ergodic=ergodic->next){
if(ergodic->val<pre->val){
int temp=ergodic->val;
ergodic->val=pre->val;
pre->val=temp;
}
}
}
}
void merge(node* head1,node* head2){
printf("合并前的两个单链表是:");
node *tail=head1,*e1=head1->next,*e2=head2->next;
while(e1&&e2){
if(e1->val<e2->val){
tail->next=e1;
tail=e1;
e1=e1->next;
}
else{
tail->next=e2;
tail=e2;
e2=e2->next;
}
}
tail->next=e1?e1:e2;
printf("合并后:");
}
node* common(node* head1,node* head2)
{
node *head,*p,*q,*s,*pre;
head=new node;
head->next=NULL;
pre=head;
for(p=head1->next;p;p=p->next)
{
for(q=head2->next;q;q=q->next)
{
if(q->val==p->val)
{
s=new node;
s->val=p->val;
pre->next=s;
pre=s;
break;
}
}
}
pre->next=NULL;
return head;
}
void partion(node* head)
{
node *p,*s,*pre=head;
p=head->next;
while(p)
{
if(p->val%2==0)
{
pre=p;
p=p->next;
}
else
{
s=p;
pre->next=p->next;
p=pre->next;
s->next=head->next;
head->next=s;
}
}
}
void search(node* head,int k)
{
int cnt=0,i;
node *p,*q;
for(p=head->next;p;p=p->next) cnt++;
p=head;
for(i=1;i<=cnt-k+1;i++)
p=p->next;
printf("倒数第k个值是:%d\n",p->val);
return;
}
int main(){
node* head;
head=create(); ph;
deletefirst(head); ph;
reverse(head); ph;
deleteall(head); ph;
mysort(head); ph;
insert(head); ph;
node* head2;
head2=create();
merge(head,head2); ph;
node* newhead=common(head,head2); print(newhead);
partion(head); ph;
printf("请输入k:\n");
int k; cin>>k;
search(head,k);
return 0;
}