Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

483 views

Published on

git

Published in: Education
  • Be the first to comment

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

  1. 1. 寫程式?那些老師沒教的事 Code Smart, Don’t Code Hard Release 2012/07/15 @StudyArea-Taichung 畢玉泉 (小畢/CrBoy) <crboy@crboy.net>
  2. 2. 老師:「努力是會有回報的! ...........多寫一個功能加10分」
  3. 3. 你又開始努力....
  4. 4. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); }
  5. 5. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 新增! 新增!
  6. 6. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 新增! 刪除!
  7. 7. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 新增! 刪除! 刪除!
  8. 8. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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("NULLn"); } void help(){ * printf("0.µ≤ßÙn"); * printf("1.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); 新增! 刪除!
  9. 9. 你花了兩天
  10. 10. 結果功能寫爛了
  11. 11. 又花兩天把code改回來
  12. 12. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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("NULLn"); } void help(){ * printf("0.µ≤ßÙn"); * printf("1.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn");
  13. 13. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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("NULLn"); } void help(){ * printf("0.µ≤ßÙn"); * printf("1.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); 改回來!
  14. 14. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 改回來!
  15. 15. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 改回來改回來改回來!
  16. 16. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 統統改回來!
  17. 17. /*¶®•§jæ«∏Í∞T§uµ{æ«®t98Ø≈§AØZ≤¶•…¨u æ«∏π°GF74942294*/ / ****************************************************************************** * FileName: hw7_s.c Programmer: CrBoy Purpose: ≥–≥y§@≠”singly linked list®√∞ı¶Ê¶U∫ÿ•Ø‡ Input: Output: the standard out Compilation: gcc hw7_s.c -o hw7_s Run: ./hw7_s Date: 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 °Cn", 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¶≥ª~°In"); * * * 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("NULLn"); } 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.Insertn"); * printf("2.Deleten"); * printf("3.Searchn"); * printf("4.Countn"); * printf("5.Reversen"); * printf("6.Mergen"); * printf("7.Splitn"); * printf("8.helpn"); } 真的改回來了嗎? 有沒有哪裡改錯?
  18. 18. 還有用Excel記錄改了什麼的...
  19. 19. ◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣
  20. 20. 很多人合作Project的時候
  21. 21. 我的比較好! 我比較早寫好!!!! 聽我的就對了啦 幹! 哇洗國寶捏!
  22. 22. 有時候還會不小心蓋掉別人的
  23. 23. ◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣ 超
  24. 24. 程式設計師的美德
  25. 25.
  26. 26. GIT The Stupid Content Tracker
  27. 27. 有好多人在用Git Linux Android CakePHP Drupal GIMP jQuery GTK+ Qt phpBB Ruby on Rails PostgreSQL Git
  28. 28. 什麼是Git The Stupid Content Tracker 愚蠢的內容追蹤器? 時光機器 回到過去 與 平行時空 版本控制系統 什麼是版本?怎麼控制?
  29. 29. 這就是版本
  30. 30. 有八成重複的檔案 而且不知道差在哪裡
  31. 31. 所以要控制
  32. 32. 為什麼要版本控制? 記住孩子的成長過程 每次的更新都知道做了什麼 改爛程式不用怕 可以同時新增不同功能,又不互相影響 簡單方便的異地備援 (防意外,還可以防小人) 容易合作,改爛程式的人還推不掉責任
  33. 33. 不再用檔名說故事
  34. 34. 不懂?Let’s Do Git!
  35. 35. 對了...
  36. 36. 版本控制系統 不是 FTP!!
  37. 37. 「可不可以把 code 壓縮打包之後上傳到 git?」 『建議千萬不要這樣做,上次有人這樣做,結果...』 「結果呢?」 『結果追蹤不了版本,很不方便...』
  38. 38. 本簡報由畢玉泉 (CrBoy) <crboy@crboy.net> 製作 以創用CC 姓名標示-相同方式分享 3.0 台灣 授權條款釋出 歡迎任何形式之重製、散佈與修改,但請明顯標註作者姓名、ID與E-mail,並以相同授權條款將衍生作品釋出 簡報中所引用之圖片與商標部分擷取自網際網路,其權利皆分屬各該公司或著作人所有

×