双向循环链表

来源:互联网 发布:开单软件 编辑:程序博客网 时间:2024/04/28 21:21
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<string.h>typedef struct node{int data;struct node *next,*prior;}*list;bool is_empty(list head){if(head->next==NULL)return true;elsereturn false;}list  creat(list head,int x){list new=(struct node*)malloc(sizeof(struct node));new->data=x;if(head==NULL){head=new;head->prior=head;head->next=head;}else{new->prior=head->prior;(head->prior)->next=new;new->next=head;head->prior=new;}return head;}void show(list tmp){if(is_empty(tmp))puts("the list is empty");else{list p=tmp;printf("%d ",tmp->data);tmp=tmp->next;while(tmp!=p){printf("%d ",tmp->data);tmp=tmp->next;}printf("\n");}}void insert(list head,list tmp){tmp->prior=head->prior;(head->prior)->next=tmp;tmp->next=head;head->prior=tmp;}void convert(list head){list tmp;list p;tmp=head->prior;while(tmp!=head){if((tmp->data%2)==0){p=tmp;tmp=tmp->prior;(p->prior)->next=p->next;(p->next)->prior=p->prior;insert(head,p);}elsetmp=tmp->prior;}}int main(void){list L=NULL;//init(&L);puts("please input the data ");puts("odd data in increase order and turned if even data ");while(1){int ret,tmp;ret=scanf("%d",&tmp);if(ret!=1){break;}L=creat(L,tmp);}convert(L);show(L);return 0;}


原创粉丝点击