51
寫程式?那些老師沒教的事 Code Smart, Don’t Code Hard Release 2012/07/15 @StudyArea-Taichung 畢玉泉 (小畢/CrBoy) <[email protected]>

寫程式?那些老師沒教的事 (Git 部分節錄)

  • Upload
    -

  • View
    398

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 寫程式?那些老師沒教的事 (Git 部分節錄)

寫程式?那些老師沒教的事Code Smart, Don’t Code Hard

Release

2012/07/15 @StudyArea-Taichung畢玉泉 (小畢/CrBoy) <[email protected]>

Page 2: 寫程式?那些老師沒教的事 (Git 部分節錄)

老師:「努力是會有回報的!...........多寫一個功能加10分」

Page 3: 寫程式?那些老師沒教的事 (Git 部分節錄)

你又開始努力....

Page 4: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);

* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){

* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;* while(c!=NULL){* * b->next = a;* * a = b;* * b = c;* * c = c->next;* }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

Page 5: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);

* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;* while(c!=NULL){* * b->next = a;* * a = b;* * b = c;* * c = c->next;* }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

新增!新增!

Page 6: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);

* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

新增!

刪除!

Page 7: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; ! ! ! case 1:! ! ! ! printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order)! ! ! ! ! h = Insert(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Insert(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;! ! ! case 2:! ! ! ! printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order) ! ! ! ! ! h = Delete(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Delete(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;! node *h=NULL, *h1=NULL, *h2=NULL;! help();* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){

* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){! node *p=h; ! while(p!=NULL && data!=p->data)! p = p->next;! return p;}

int Count(node *h){! int count=0;! while(h!=NULL){! ! count += 1;! ! h = h->next;! }! return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

新增!

刪除!

刪除!

Page 8: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; ! ! ! case 1:! ! ! ! printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order)! ! ! ! ! h = Insert(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Insert(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;! ! ! case 2:! ! ! ! printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order) ! ! ! ! ! h = Delete(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Delete(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;! node *h=NULL, *h1=NULL, *h2=NULL;! help();* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){

* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){! node *p=h; ! while(p!=NULL && data!=p->data)! p = p->next;! return p;}

int Count(node *h){! int count=0;! while(h!=NULL){! ! count += 1;! ! h = h->next;! }! return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void print_list(node *h){! node *temp;! temp = h;! while(temp != NULL){! ! printf("%d -> ", temp->data);! ! temp = temp->next;! }! printf("NULL\n");}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n");

新增!

刪除!

Page 9: 寫程式?那些老師沒教的事 (Git 部分節錄)

你花了兩天

Page 10: 寫程式?那些老師沒教的事 (Git 部分節錄)

結果功能寫爛了

Page 11: 寫程式?那些老師沒教的事 (Git 部分節錄)

又花兩天把code改回來

Page 12: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; ! ! ! case 1:! ! ! ! printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order)! ! ! ! ! h = Insert(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Insert(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;! ! ! case 2:! ! ! ! printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order) ! ! ! ! ! h = Delete(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Delete(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;! node *h=NULL, *h1=NULL, *h2=NULL;! help();* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){

* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){! node *p=h; ! while(p!=NULL && data!=p->data)! p = p->next;! return p;}

int Count(node *h){! int count=0;! while(h!=NULL){! ! count += 1;! ! h = h->next;! }! return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void print_list(node *h){! node *temp;! temp = h;! while(temp != NULL){! ! printf("%d -> ", temp->data);! ! temp = temp->next;! }! printf("NULL\n");}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n");

Page 13: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; ! ! ! case 1:! ! ! ! printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order)! ! ! ! ! h = Insert(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Insert(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;! ! ! case 2:! ! ! ! printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order) ! ! ! ! ! h = Delete(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Delete(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;! node *h=NULL, *h1=NULL, *h2=NULL;! help();* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){

* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){! node *p=h; ! while(p!=NULL && data!=p->data)! p = p->next;! return p;}

int Count(node *h){! int count=0;! while(h!=NULL){! ! count += 1;! ! h = h->next;! }! return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void print_list(node *h){! node *temp;! temp = h;! while(temp != NULL){! ! printf("%d -> ", temp->data);! ! temp = temp->next;! }! printf("NULL\n");}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n");

改回來!

Page 14: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; ! ! ! case 1:! ! ! ! printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order)! ! ! ! ! h = Insert(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Insert(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;! ! ! case 2:! ! ! ! printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");! ! ! ! if(scanf("%d", &data)==0) continue;! ! ! ! if(order) ! ! ! ! ! h = Delete(data, h);! ! ! ! else{! ! ! ! ! h = Reverse(h);! ! ! ! ! h = Delete(data, h);! ! ! ! ! h = Reverse(h);! ! ! ! } ! ! ! ! break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;! node *h=NULL, *h1=NULL, *h2=NULL;! help();* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){

* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){! node *p=h; ! while(p!=NULL && data!=p->data)! p = p->next;! return p;}

int Count(node *h){! int count=0;! while(h!=NULL){! ! count += 1;! ! h = h->next;! }! return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

改回來!

Page 15: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);

* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }! while(c!=NULL){! ! b->next = a;! ! a = b;! ! b = c;! ! c = c->next;! }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

改回來改回來改回來!

Page 16: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){! node *h1=NULL, *h2=NULL;! while(*h!=NULL){! ! if((*h)->data%2){! ! ! if(h1==NULL) h1 = *h1_ptr = *h;! ! ! else *h1_ptr = (*h1_ptr)->next = *h;! ! }else{! ! ! if(h2==NULL) h2 = *h2_ptr = *h;! ! ! else *h2_ptr = (*h2_ptr)->next = *h;! ! }! ! *h = (*h)->next;! }! (*h1_ptr)->next = (*h2_ptr)->next = NULL;! *h1_ptr = h1;! *h2_ptr = h2;! *h = NULL;* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);

* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }! temp = (node*)malloc(sizeof(node));! temp->data = data;! temp->next = p->next;! p->next = temp;* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);

* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;* while(c!=NULL){* * b->next = a;* * a = b;* * b = c;* * c = c->next;* }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

統統改回來!

Page 17: 寫程式?那些老師沒教的事 (Git 部分節錄)

/*¶®•\§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/

/*******************************************************************************FileName: hw7_s.cProgrammer: CrBoyPurpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•\؇ Input: Output: the standard outCompilation: gcc hw7_s.c -o hw7_s Run: ./hw7_sDate: 2006/1/8 /******************************************************************************/

#include <stdio.h>

struct node{* int data;* struct node *next;};

typedef struct node node;

void print_list(node*);/*print_list(h); ¶C¶L•Hh∂}¿Y™∫linked list*/node *Insert(int, node*);/*Insert(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥°§Jdata®√¶^∂«∑s™∫h*/ node *Delete(int, node*);/*Delete(data, h); ¶b•Hh∂}¿Y™∫linked list§§ßR∞£data®√¶^∂«∑s™∫h*/node *Search(int, node*);/*Search(data, h); ¶b•Hh∂}¿Y™∫linked list§§¥M߉data®√∂«¶^data©“¶b™∫¶Ï∏m*/int Count(node*);/*Count(h); ≠p∫‚•Hh∂}¿Y™∫linked list§§™∫∏`¬I≠”º∆*/node *Reverse(node*);/*Reverse(h); §œß«±∆¶C•Hh∂}¿Y™∫linked list®√∂«¶^∑s™∫h*/node *Merge(node*, node*);/*Merge(h1, h2); ¶X®÷h1ªPh2≥o2≠”linked list®√¶^∂«¶X®÷´·™∫™∫head*/void Split(node**, node**, node**);/*Split(&h, &h1, &h2); §¿≥Œh≥o≠”linked list¨∞©_∞∏º∆®√§¿ßO¶s®Ïh1ªPh2§§*/void help();

int order=1;

int main(void){* int sel=8, data=0;* node *h=NULL, *h1=NULL, *h2=NULL;* help();* while(sel){* * printf("\n•ÿ´e™∫Linked list°G");* * print_list(h); * * printf("Ω–øÔæ‹•\\؇°G");* * if(scanf("%d", &sel)==0){* * * sel=-1;* * * fflush(stdin);* * } * * switch(sel){* * * case 0: break; * * * case 1:* * * * printf("Ω–øȧJ±˝¥°§J™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order)* * * * * h = Insert(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Insert(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 2:* * * * printf("Ω–øȧJ±˝ßR∞£™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * if(order) * * * * * h = Delete(data, h);* * * * else{* * * * * h = Reverse(h);* * * * * h = Delete(data, h);* * * * * h = Reverse(h);* * * * } * * * * break;* * * case 3:* * * * printf("Ω–øȧJ±˝∑j¥M™∫愺∆≠»°G");* * * * if(scanf("%d", &data)==0) continue;* * * * h1 = Search(data, h);* * * * printf("•H%dß@¨∞∞_¬I™∫Linked list°G", data);* * * * print_list(h1);* * * * h1 = NULL;* * * * break;* * * case 4:* * * * printf("•ÿ´e™∫Linked list¶@¶≥%d≠”node°C\n", Count(h));* * * * break;* * * case 5:* * * * h = Reverse(h);* * * * break;* * * case 6:* * * * printf("h1°G"); * * * * print_list(h1);

* * * * printf("h2°G"); * * * * print_list(h2);* * * * h = Merge(h1, h2);* * * * h1 = h2 = NULL; * * * * break;* * * case 7:* * * * if(order) Split(&h, &h1, &h2);* * * * else{* * * * * h = Reverse(h);* * * * * Split(&h, &h1, &h2);* * * * }* * * * printf("h1°G"); * * * * print_list(h1);* * * * printf("h2°G");* * * * print_list(h2); * * * * break;* * * default: printf("±z™∫øȧJ¶≥ª~°I\n"); * * * case 8: help(); * * }* }* return 0;}

void print_list(node *h){* node *temp;* temp = h;* while(temp != NULL){* * printf("%d -> ", temp->data);* * temp = temp->next;* }* printf("NULL\n");}

node *Insert(int data, node *h){* node *p, *t, *temp;* if(h==NULL){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = NULL;* * return temp;* }* p = h;* t = h->next;* if(data<p->data){* * temp = (node*)malloc(sizeof(node));* * temp->data = data;* * temp->next = p;* * return temp;* }* if(t!=NULL)* * while(data<p->data || data>t->data){* * * p = p->next;* * * t = t->next;* * * if(t==NULL) break;* * }* temp = (node*)malloc(sizeof(node));* temp->data = data;* temp->next = p->next;* p->next = temp;* return h;}

node *Delete(int data, node *h){* node *p, *t;* if(h==NULL) return h;* p = h;* t = h->next;* if(data == p->data){* * p = p->next;* * free(h);* * return p;* }* while(data!=t->data){* * p = p->next;* * t = t->next;* * if(t==NULL) return h;* }* p->next = t->next;* free(t);* return h;}

node *Search(int data, node *h){* node *p=h; * while(p!=NULL && data!=p->data)* p = p->next;* return p;}

int Count(node *h){* int count=0;* while(h!=NULL){* * count += 1;* * h = h->next;* }* return count;}

node *Reverse(node *h){

* node *a, *b, *c;* if(h==NULL || h->next==NULL) return h;* a = h;* b = a->next;* c = b->next;* a->next = NULL;* while(c!=NULL){* * b->next = a;* * a = b;* * b = c;* * c = c->next;* }* b->next = a;* order = !order; * return b;}

node *Merge(node *h1, node *h2){* node *h, *temp;* h = temp = h1->data < h2->data ? h1 : h2;* h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* while(h1!=NULL && h2!=NULL){* * temp = temp->next = h1->data < h2->data ? h1 : h2;* * h1->data < h2->data ? (h1=h1->next) : (h2=h2->next);* }* h2 == NULL ? (temp->next=h1) : (temp->next=h2);* return h;}

void Split(node **h, node **h1_ptr, node **h2_ptr){* node *h1=NULL, *h2=NULL;* while(*h!=NULL){* * if((*h)->data%2){* * * if(h1==NULL) h1 = *h1_ptr = *h;* * * else *h1_ptr = (*h1_ptr)->next = *h;* * }else{* * * if(h2==NULL) h2 = *h2_ptr = *h;* * * else *h2_ptr = (*h2_ptr)->next = *h;* * }* * *h = (*h)->next;* }* (*h1_ptr)->next = (*h2_ptr)->next = NULL;* *h1_ptr = h1;* *h2_ptr = h2;* *h = NULL;}

void help(){* printf("0.µ≤ßÙ\n"); * printf("1.Insert\n");* printf("2.Delete\n");* printf("3.Search\n");* printf("4.Count\n");* printf("5.Reverse\n");* printf("6.Merge\n");* printf("7.Split\n");* printf("8.help\n"); }

真的改回來了嗎?

有沒有哪裡改錯?

Page 18: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 19: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 20: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 21: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 22: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 23: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 24: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 25: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 26: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 27: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 28: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 29: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 30: 寫程式?那些老師沒教的事 (Git 部分節錄)

還有用Excel記錄改了什麼的...

Page 31: 寫程式?那些老師沒教的事 (Git 部分節錄)

◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣

Page 32: 寫程式?那些老師沒教的事 (Git 部分節錄)

很多人合作Project的時候

Page 33: 寫程式?那些老師沒教的事 (Git 部分節錄)

我的比較好!我比較早寫好!!!!

聽我的就對了啦 幹!哇洗國寶捏!

Page 34: 寫程式?那些老師沒教的事 (Git 部分節錄)

有時候還會不小心蓋掉別人的

Page 35: 寫程式?那些老師沒教的事 (Git 部分節錄)

◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣

Page 36: 寫程式?那些老師沒教的事 (Git 部分節錄)

程式設計師的美德

Page 37: 寫程式?那些老師沒教的事 (Git 部分節錄)

Page 38: 寫程式?那些老師沒教的事 (Git 部分節錄)

GITThe Stupid Content Tracker

Page 39: 寫程式?那些老師沒教的事 (Git 部分節錄)

有好多人在用Git

Linux

Android

CakePHP

Drupal

GIMP

jQuery

GTK+

Qt

phpBB

Ruby on Rails

PostgreSQL

Git

Page 40: 寫程式?那些老師沒教的事 (Git 部分節錄)

什麼是Git

The Stupid Content Tracker

愚蠢的內容追蹤器?

時光機器

回到過去 與 平行時空

版本控制系統

什麼是版本?怎麼控制?

Page 41: 寫程式?那些老師沒教的事 (Git 部分節錄)

這就是版本

Page 42: 寫程式?那些老師沒教的事 (Git 部分節錄)

有八成重複的檔案而且不知道差在哪裡

Page 43: 寫程式?那些老師沒教的事 (Git 部分節錄)
Page 44: 寫程式?那些老師沒教的事 (Git 部分節錄)

所以要控制

Page 45: 寫程式?那些老師沒教的事 (Git 部分節錄)

為什麼要版本控制?

記住孩子的成長過程

每次的更新都知道做了什麼

改爛程式不用怕

可以同時新增不同功能,又不互相影響

簡單方便的異地備援 (防意外,還可以防小人)

容易合作,改爛程式的人還推不掉責任

Page 46: 寫程式?那些老師沒教的事 (Git 部分節錄)

不再用檔名說故事

Page 47: 寫程式?那些老師沒教的事 (Git 部分節錄)

不懂?Let’s Do Git!

Page 48: 寫程式?那些老師沒教的事 (Git 部分節錄)

對了...

Page 49: 寫程式?那些老師沒教的事 (Git 部分節錄)

版本控制系統

不是

FTP!!

Page 50: 寫程式?那些老師沒教的事 (Git 部分節錄)

「可不可以把 code 壓縮打包之後上傳到 git?」

『建議千萬不要這樣做,上次有人這樣做,結果...』

「結果呢?」

『結果追蹤不了版本,很不方便...』

Page 51: 寫程式?那些老師沒教的事 (Git 部分節錄)

本簡報由畢玉泉 (CrBoy) <[email protected]> 製作以創用CC 姓名標示-相同方式分享 3.0 台灣 授權條款釋出歡迎任何形式之重製、散佈與修改,但請明顯標註作者姓名、ID與E-mail,並以相同授權條款將衍生作品釋出

簡報中所引用之圖片與商標部分擷取自網際網路,其權利皆分屬各該公司或著作人所有