链表逆置
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插法
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
void reverse(LNode *&L)
{
LNode *p,*q;
p=L->next;
L->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=L->next;
L->next=q;
}
}
int main()
{
LNode *L,*B;
int n,c[10000];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&c[i]);
creathead(L,c,n);
reverse(L);
while(L->next!=NULL)
{
printf("%d ",L->next->data);
L=L->next;
}
printf("\n");
return 0;
}
将一个链表分解为偶数链表和奇数链表
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
LNode *next;
};
void creathead(LNode *&L,int a[],int n)///尾插法
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
/把A分解为A里面节点值为偶数
B里面节点值为奇数*/
void split(LNode *A,LNode *&B)
{
LNode *p,*q,*r;
B=(LNode*)malloc(sizeof(LNode));
B->next=NULL;
p=A;
r=B;
while(p->next!=NULL)
{
if(p->next->data%2==0)
{
q=p->next;
p->next=q->next;
q->next=NULL;
r->next=q;///尾插
r=q;
}
else
p=p->next;
}
}
int main()
{
LNode *L,*B;
int n,c[10000];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&c[i]);
creathead(L,c,n);
split(L,B);
printf("奇数\n");
while(L->next!=NULL)
{
printf("%d ",L->next->data);
L=L->next;
}
printf("\n");
printf("偶数\n");
while(B->next!=NULL)
{
printf("%d ",B->next->data);
B=B->next;
}
return 0;
}
将两个递增链表归并为一个递减链表
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
LNode *next;
};
void merge(LNode *&A,LNode *B)
{
LNode *p=A->next;
LNode *q=B->next;
LNode *s;
A->next=NULL;
while(p!=NULL && q!=NULL)
{
if(p->data <= q->data)
{
s=p;
p=p->next;
s->next=A->next;
A->next=s;
}
else
{
s=q;
q=q->next;
s->next=A->next;
A->next=s;
}
}
while(p!=NULL)
{
s=p;
p=p->next;
s->next=A->next;
A->next=s;
}
while(q!=NULL)
{
s=q;
q=q->next;
s->next=A->next;
A->next=s;
}
}
void creathead(LNode *&L,int a[],int n)///尾插
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
// printf("1111\n");
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
int main()
{
LNode *A,*B;
int a[1000],b[1000];
int n,m;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=0; i<m; i++)
scanf("%d",&b[i]);
creathead(A,a,n);
creathead(B,b,m);
merge(A,B);
while(A->next!=NULL)
{
printf("%d ",A->next->data);
A=A->next;
}
return 0;
}
将一个链表分解为两个链表:
第一个链表中含有原链表中序号为奇数的节点
第二个链表中含有原链表中序号为偶数数的节点
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
int num;
LNode *next;
};
void bianhao(LNode *L)
{
int i=0;
while(L->next!=NULL)
{
i++;
L->next->num=i;
L=L->next;
}
}
void split(LNode *A,LNode *&B)
{
LNode *p,*q,*r;
B=(LNode*)malloc(sizeof(LNode));
B->next=NULL;
p=A;
r=B;
while(p->next!=NULL)
{
if(p->next->num%2==0)
{
q=p->next;
p->next=q->next;
q->next=NULL;
r->next=q;
r=q;
}
else
p=p->next;
}
}
void creathead(LNode *&L,int a[],int n)///头插法
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
int main()
{
LNode *A,*B;
int a[1000];
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
creathead(A,a,n);
bianhao(A);
split(A,B);
printf("奇数\n");
while(A->next!=NULL)
{
printf("%d ",A->next->data);
A=A->next;
}
printf("\n");
printf("偶数\n");
while(B->next!=NULL)
{
printf("%d ",B->next->data);
B=B->next;
}
return 0;
}
将一个链表分解成两个链表
第一个链表中含有原链表中小于0的节点
第二个链表中含有原链表中大于0的节点
#include<bits/stdc++.h>
using namespace std;
struct LNode
{
int data;
struct LNode *next;
};
void create(LNode *L,int a[],int n)///尾插
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
r=L;
for(int i=0; i<n; i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
void solve(LNode *L,LNode *&B,LNode *&C)
{
LNode *p,*q,*r,*h;
B=(LNode*)malloc(sizeof(LNode));
C=(LNode*)malloc(sizeof(LNode));
B->next=NULL;
C->next=NULL;
p=L->next;
q=B;
r=C;
while(p)
{
if(p->data < 0)
{
h=p;
p=p->next;//尾插
q->next=h;
q=h;
}
else
{
h=p;
p=p->next; ///尾插
r->next=h;
r=h;
}
}
while(B->next!=NULL)
{
printf("%d ",B->next->data);
B=B->next;
}
while(C->next!=NULL)
{
printf("%d ",C->next->data);
C=C->next;
}
}
int main()
{
int n,a[100];
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
LNode *L,*B,*C;
create(L,a,n);
solve(L,B,C);
return 0;
}