//1.顺序建立链表
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=0;i<n;i++){
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
tail->next=p;
tail=p;
}
//2.逆序建立链表
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
for(i=0;i<n;i++){
p=(struct node*)malloc(sizeof(struct node);
p->next=head->next;
head->next=p;
}
//3.链表的合并
p=head1->next;
q=head2->next;
head1->next=NULL;
tail=head1;
while(p&&q){
if(p->data<q->data){
tail->next=p;
tail=p;
p=p->next;
tail->next=NULL;
}
else {
tail->next=q;
tail=q;
q=q->next;
tail->next=NULL;
}
}
if(p)tail->next=p;
else tail->next=q;
//4.链表的拆分
head1=(struct node*)malloc(sizeof(struct node));
head1->next=NULL;
tail1=head1;
head2=(struct node*)malloc(sizeof(struct node));
head2->next=NULL;
tail2=head2;
a=head->next;
while(a){
if(a->data%2){
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
p->data=a->data;
tail1->next=p;
tail1=p;
}
else{
q=(struct node*)malloc(sizeof(struct node));
q->next=NULL;
q->data=a->data;
tail2->next=q;
tail2=q;
}
a=a->next;
}
//5.链表的删除
d=head->next;
while(d){
p=d;
q=p->next;
while(q){
if(d->data=q->data){
p->next=q->next;
q=q->next;
}
else {
p=q;
q=q->next;
}
}
d=d->next;
}
//6.指定位置插入节点
p=head;
while(m--&&p->next){
p=p->next;
}
q=(struct node*)malloc(sizeof(struct node));
q->next=NULL;
q->data=x;
q->next=p->next;
p->next=q;
//7.双向链表
struct node{
int data;
struct node*next;
struct node*prior;
}
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=0;i<n;i++){
p=(struct node*)malloc(sizeof(struct node));
p->next=NULL;
tail->next=p;
p->prior=tail;
tail=p;
}
**//8.约瑟夫问题**
tail->next=head;
p=head;
q=tail;
int k=0;
while(p!=p->next)
{
k++;
if(k==m)
{
**q->next=p->next;
p=p->next;**//删除元素操作
k=0;
}
else
{
**q=p;
p=p->next;**
}
}
区别插入与删除
删除:p->next=q->next;
q=q->next;
插入:q->next=p->next;
p->next=q;