双向链表的操作问题
1 /*双向链表的操作问题*/ 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 typedef struct DLNode 6 { 7 int data; 8 struct DLNode *prior,*next; 9 }DLinkList; 10 11 void InitList(DLinkList *&L) 12 { 13 L =(DLinkList*)malloc(sizeof(DLinkList)); 14 L->prior = L; 15 L->next = L; 16 } 17 18 void CreateList(DLinkList *&L, int n) 19 { 20 DLinkList * p,*r; 21 r = L; 22 for (int i = 0; i < n; ++i) 23 { 24 p =(DLinkList*)malloc(sizeof(DLinkList)); 25 scanf("%d",&p->data); 26 p->next = r->next; 27 r->next->prior = p; 28 r->next = p; 29 p->prior = r; 30 L->prior = p; 31 r = p; 32 } 33 } 34 35 void Dis(DLinkList *L) 36 { 37 DLinkList *r; 38 r = L; 39 while(r->next!=L) 40 { 41 r = r->next; 42 printf("%d ",r->data); 43 } 44 } 45 46 void sor(DLinkList *&L) 47 { 48 DLinkList *f,*s; 49 int tem; 50 f = L->next; 51 s = f->next; 52 while(f!=L) 53 { 54 while(s!=L) 55 { 56 if (f->data > s->data) 57 { 58 tem = f->data; 59 f->data = s->data; 60 s->data = tem; 61 } 62 s = s->next; 63 } 64 65 f = f->next; 66 s = f->next; 67 } 68 } 69 70 int main(int argc, char const *argv[]) 71 { 72 DLinkList * L; 73 int n; 74 scanf("%d",&n); 75 InitList(L); 76 CreateList(L,n); 77 sor(L); 78 Dis(L); 79 return 0; 80 }
注:代码中的排序方法为冒泡排序