SDUTOJ-3331 資料結構實驗之連結串列八:Farey序列
阿新 • • 發佈:2018-11-12
連結串列節點插入練習題 題目連結
#include <stdio.h> #include <cstdlib> using namespace std; typedef int ElementType; typedef struct node { ElementType x, y; struct node * next; } Lnode; typedef Lnode * List; void PrintL(List head) { List p = head; int n = 0; if(p) { while(p->next) { if(++n % 10 == 0) printf("%d/%d\n", p->x, p->y); else printf("%d/%d\t", p->x, p->y); p = p->next; } printf("%d/%d\n", p->x, p->y); } else { printf("This list is empty!\n"); } } int main() { int n; scanf("%d", &n); List head = (List) malloc( sizeof(Lnode) ); head->x = 0; head->y = 1; List rear = (List) malloc( sizeof(Lnode) ); head->next = rear; rear->next = NULL; rear->x = rear->y = 1; int w = n-1; while(w--) { List p = head; while(p->next) { if(p->y + p->next->y <= n) { List q = (List) malloc( sizeof(Lnode) ); q->next = p->next; q->x = p->x + p->next->x; q->y = p->y + p->next->y; p->next = q; p = q->next; continue; } p = p->next; } } PrintL(head); return 0; }