使用链表进行奇偶分排 c语言
生活随笔
收集整理的這篇文章主要介紹了
使用链表进行奇偶分排 c语言
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目內容:
使用單向鏈構建一個有n個節點,輸入正整數序列并輸出序列,n由鍵盤輸入。然后將偶數節點依次移動到?所有的奇數后面,形成一個前面為奇正數序列,后面為偶整數序列的新序列,最后輸出這個序列
#include <stdio.h> #include <stdlib.h>typedef struct i {int n;struct i *next;struct i *past; }F; int pd(F *p) {for(;p!=NULL;p=p->next){if((p->n)%2==1){return 0;}}return 1; } int main() {F *ptemp,*head,*pnew,*pstore,*pw,*puse;head=(F*)malloc(sizeof(F));pnew=(F*)malloc(sizeof(F));head->next=pnew;head->past=NULL;pnew->past=head;head->n=1;pnew->n=2;int num,i;scanf("%d",&num);ptemp=pnew;for(i=2;i<num;i++){pnew=(F*)malloc(sizeof(F));ptemp->next=pnew;pnew->past=ptemp;ptemp=ptemp->next;ptemp->n=i+1;}pw=ptemp;ptemp->next=NULL;for(ptemp=head;ptemp!=NULL;ptemp=ptemp->next){printf(" %d",ptemp->n);}printf("\n");for(ptemp=pw;ptemp!=NULL;ptemp=ptemp->past){if((ptemp->n)%2==0){ puse=ptemp;while(pd(puse)==0){int t; t=ptemp->n; ptemp->n=ptemp->next->n; ptemp->next->n=t; ptemp=ptemp->next; puse=ptemp;}}}for(ptemp=head;ptemp!=NULL;ptemp=ptemp->next){printf(" %d",ptemp->n);}printf("\n");return 0; }單向的整不會了,整個雙向的哈哈
總結
以上是生活随笔為你收集整理的使用链表进行奇偶分排 c语言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序使用函数的三种方法
- 下一篇: 山东大学科技文献期末复习(个人速成向)