# 数据结构->双链表的操作

1、利用尾插法建立一个双向链表。

2、遍历双向链表。

3、实现双向链表中删除一个指定元素。

4、在非递减有序双向链表中实现插入元素e仍有序算法。

5、判断双向链表中元素是否对称若对称返回1否则返回0。

6、设元素为正整型，实现算法把所有奇数排列在偶数之前。

``````#include <stdio.h>
#include <stdlib.h>
#define Elemtype int
#define dx struct node
typedef struct node
{
Elemtype data;
struct node *next,*pre;
} DLnode,*Dnode;
void creat(DLnode *h,DLnode *t)
{
DLnode *p;
Elemtype data;
scanf("%d",&data);
while(data!=-1)
{
p=(Dnode)malloc(sizeof(dx));
p->data=data;
p->next=h;
p->pre=h->pre;
h->pre->next=p;
h->pre=p;
scanf("%d",&data);
}
}
void bianli(DLnode *h,DLnode *t)
{
DLnode *p=t->next;
printf("\t\n向后遍历\n");
while(p!=h)
{
printf("%d ",p->data);
p=p->next;
}
puts("");
printf("\t\n向前遍历\n");
p=t->pre->pre;
while(p!=t)
{
printf("%d ",p->data);
p=p->pre;
}
puts("");
}
void Delete(DLnode *h,DLnode *t,Elemtype data)
{
DLnode *p=t->next;
while(p!=h)
{
if(p->data!=data)
p=p->next;
else
{
p->next->pre=p->pre;
p->pre->next=p->next;
free(p);
break;
}
}
}
void charu(DLnode *h,DLnode *t,DLnode *s)
{
DLnode *p=t->next;
while(p!=h&&p->data<s->data)
{
p=p->next;
}
s->next=p;
s->pre=p->pre;
p->pre->next=s;
p->pre=s;
}
void creat2(DLnode *h,DLnode *t)
{
DLnode *p;
Elemtype data;
scanf("%d",&data);
while(data!=-1)
{
p=(Dnode)malloc(sizeof(dx));
p->data=data;
charu(h,t,p);
scanf("%d",&data);
}
}
int duichen(DLnode *h,DLnode *t)
{
Dnode p1,p2;
p1=t->next;
p2=h->pre;
while(p1!=p2&&p2->next!=p1)
{
if(p1->data!=p2->data)
return 0;
p1=p1->next;
p2=p2->pre;
}
return 1;
}
void jo(DLnode *h,DLnode *t)
{
DLnode *p1,*p2;
p1=t->next;
p2=h->pre;
while(p1!=p2)
{
if(p1->data%2==0&&p2->data%2==1)
{
int p;
p=p1->data;
p1->data=p2->data;
p2->data=p;
}
else if(p1->data%2==0&&p2->data%2==0)
{
p2=p2->pre;
}
else if(p1->data%2==1&&p2->data%2==0)
{
p1=p1->next;
p2=p2->pre;
}
else if(p1->data%2==1&&p2->data%2==1)
{
p1=p1->next;
}
}
}
int main()
{
Dnode h,t;
h=(Dnode)malloc(sizeof(dx));
t=(Dnode)malloc(sizeof(dx));
h->pre=t;
h->next=t;
t->pre=h;
t->next=h;
creat(h,t);
bianli(h,t);
printf("\n输入要删除的数\n");
Elemtype data;
scanf("%d",&data);
Delete(h,t,data);
printf("\n删除后遍历\n\n");
bianli(h,t);
Dnode h1,t1;
h1=(Dnode)malloc(sizeof(dx));
t1=(Dnode)malloc(sizeof(dx));
h1->pre=t1;
h1->next=t1;
t1->pre=h1;
t1->next=h1;
creat2(h1,t1);
printf("非递减有序双向链表\n");
bianli(h1,t1);
printf("\n输入要插入的数\n");
scanf("%d",&data);
Dnode s=(Dnode)malloc(sizeof(dx));
s->data=data;
s->next=NULL;
s->pre=NULL;
printf("插入后链表\n");
charu(h1,t1,s);
bianli(h1,t1);

if(duichen(h,t))
{
printf("第一个无序的表是对称\n");
}
else
{
printf("第一个无序的表是不对称\n");
}
jo(h1,t1);
printf("奇偶排序后\n");
bianli(h1,t1);
return 0;
}``````

;