SlideShare a Scribd company logo
1 of 51
Download to read offline
寫程式?那些老師沒教的事
Code Smart, Don’t Code Hard
Release
2012/07/15	 @StudyArea-Taichung
畢玉泉	 (小畢/CrBoy)	 	 <crboy@crboy.net>
老師:「努力是會有回報的!
...........多寫一個功能加10分」
你又開始努力....
/*¶®•§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");
}
/*¶®•§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");
}
新增!
新增!
/*¶®•§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");
}
新增!
刪除!
/*¶®•§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");
}
新增!
刪除!
刪除!
/*¶®•§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");
新增!
刪除!
你花了兩天
結果功能寫爛了
又花兩天把code改回來
/*¶®•§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");
/*¶®•§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");
改回來!
/*¶®•§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");
}
改回來!
/*¶®•§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");
}
改回來改回來改回來!
/*¶®•§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");
}
統統改回來!
/*¶®•§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");
}
真的改回來了嗎?
有沒有哪裡改錯?
還有用Excel記錄改了什麼的...
◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣
很多人合作Project的時候
我的比較好!
我比較早寫好!!!!
聽我的就對了啦 幹!
哇洗國寶捏!
有時候還會不小心蓋掉別人的
◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣
超
程式設計師的美德
懶
GIT
The Stupid Content Tracker
有好多人在用Git
Linux
Android
CakePHP
Drupal
GIMP
jQuery
GTK+
Qt
phpBB
Ruby on Rails
PostgreSQL
Git
什麼是Git
The Stupid Content Tracker
愚蠢的內容追蹤器?
時光機器
回到過去 與 平行時空
版本控制系統
什麼是版本?怎麼控制?
這就是版本
有八成重複的檔案
而且不知道差在哪裡
所以要控制
為什麼要版本控制?
記住孩子的成長過程
每次的更新都知道做了什麼
改爛程式不用怕
可以同時新增不同功能,又不互相影響
簡單方便的異地備援 (防意外,還可以防小人)
容易合作,改爛程式的人還推不掉責任
不再用檔名說故事
不懂?Let’s Do Git!
對了...
版本控制系統
不是
FTP!!
「可不可以把	 code	 壓縮打包之後上傳到	 git?」
『建議千萬不要這樣做,上次有人這樣做,結果...』
「結果呢?」
『結果追蹤不了版本,很不方便...』
本簡報由畢玉泉 (CrBoy) <crboy@crboy.net> 製作
以創用CC 姓名標示-相同方式分享 3.0 台灣 授權條款釋出
歡迎任何形式之重製、散佈與修改,但請明顯標註作者姓名、ID與E-mail,並以相同授權條款將衍生作品釋出
簡報中所引用之圖片與商標部分擷取自網際網路,其權利皆分屬各該公司或著作人所有

More Related Content

What's hot

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
Sai Ef
 
Yy
YyYy
Yy
yygh
 

What's hot (18)

Myraytracer
MyraytracerMyraytracer
Myraytracer
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Numerical Methods with Computer Programming
Numerical Methods with Computer ProgrammingNumerical Methods with Computer Programming
Numerical Methods with Computer Programming
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing
 
Railwaynew
RailwaynewRailwaynew
Railwaynew
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
Event
EventEvent
Event
 
Linked lists
Linked listsLinked lists
Linked lists
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
C99.php
C99.phpC99.php
C99.php
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
Hospital management
Hospital managementHospital management
Hospital management
 
Nouveau document texte
Nouveau document texteNouveau document texte
Nouveau document texte
 
Yy
YyYy
Yy
 
Code em Poker
Code em PokerCode em Poker
Code em Poker
 
Ss
SsSs
Ss
 
Vcs28
Vcs28Vcs28
Vcs28
 
JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)JavaScript - i och utanför webbläsaren (2010-03-03)
JavaScript - i och utanför webbläsaren (2010-03-03)
 

Viewers also liked

Esperienze gruppo
Esperienze gruppoEsperienze gruppo
Esperienze gruppo
classeterza
 
Cac bai toan suy luan tieu hoc
Cac bai toan suy luan tieu hocCac bai toan suy luan tieu hoc
Cac bai toan suy luan tieu hoc
ngoquangbinh
 

Viewers also liked (13)

Inducción proceso servicio al cliente
Inducción proceso  servicio al clienteInducción proceso  servicio al cliente
Inducción proceso servicio al cliente
 
Capder
CapderCapder
Capder
 
PKI for the People - a Beame.io Research Initiative
PKI for the People - a Beame.io Research InitiativePKI for the People - a Beame.io Research Initiative
PKI for the People - a Beame.io Research Initiative
 
Esperienze gruppo
Esperienze gruppoEsperienze gruppo
Esperienze gruppo
 
CV EEGabison mars
CV  EEGabison marsCV  EEGabison mars
CV EEGabison mars
 
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
COSCUP 2016 Workshop: 用 Docker 架設班級 git-it 練習環境
 
Inducciòn proceso de contrataciòn
Inducciòn proceso de  contrataciòn Inducciòn proceso de  contrataciòn
Inducciòn proceso de contrataciòn
 
Inducciòn proceso de gestiòn ambiental
Inducciòn proceso de gestiòn ambientalInducciòn proceso de gestiòn ambiental
Inducciòn proceso de gestiòn ambiental
 
Tunisia
TunisiaTunisia
Tunisia
 
Inducción proceso financiero
Inducción proceso financiero Inducción proceso financiero
Inducción proceso financiero
 
Journal du Salon de l'Entrepreneuriat 2016
Journal du Salon de l'Entrepreneuriat 2016Journal du Salon de l'Entrepreneuriat 2016
Journal du Salon de l'Entrepreneuriat 2016
 
Cac bai toan suy luan tieu hoc
Cac bai toan suy luan tieu hocCac bai toan suy luan tieu hoc
Cac bai toan suy luan tieu hoc
 
выставка15:2
выставка15:2выставка15:2
выставка15:2
 

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

Program(Output)
Program(Output)Program(Output)
Program(Output)
princy75
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
kostikjaylonshaewe47
 
R57php 1231677414471772-2
R57php 1231677414471772-2R57php 1231677414471772-2
R57php 1231677414471772-2
ady36
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
galagirishp
 
Bai Giang 11
Bai Giang 11Bai Giang 11
Bai Giang 11
nbb3i
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
Moriyoshi Koizumi
 

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

Program(Output)
Program(Output)Program(Output)
Program(Output)
 
R57.Php
R57.PhpR57.Php
R57.Php
 
Cquestions
Cquestions Cquestions
Cquestions
 
Assignement c++
Assignement c++Assignement c++
Assignement c++
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
Java
JavaJava
Java
 
java-introduction.pdf
java-introduction.pdfjava-introduction.pdf
java-introduction.pdf
 
Tu1
Tu1Tu1
Tu1
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
R57php 1231677414471772-2
R57php 1231677414471772-2R57php 1231677414471772-2
R57php 1231677414471772-2
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
 
Bai Giang 11
Bai Giang 11Bai Giang 11
Bai Giang 11
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
SAS code sample
SAS code sampleSAS code sample
SAS code sample
 
Data structures
Data structuresData structures
Data structures
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 

More from 均民 戴

興大資訊社 CPE 訓練宣傳
興大資訊社 CPE 訓練宣傳興大資訊社 CPE 訓練宣傳
興大資訊社 CPE 訓練宣傳
均民 戴
 

More from 均民 戴 (14)

CKmates - AWS 三大 AI 解決方案:應用影像辨識、聊天機器人與語音轉換以及 Serverless 應用
CKmates - AWS 三大 AI 解決方案:應用影像辨識、聊天機器人與語音轉換以及 Serverless 應用CKmates - AWS 三大 AI 解決方案:應用影像辨識、聊天機器人與語音轉換以及 Serverless 應用
CKmates - AWS 三大 AI 解決方案:應用影像辨識、聊天機器人與語音轉換以及 Serverless 應用
 
CKmates - AWS 雲端運算 基礎服務介紹
CKmates - AWS 雲端運算 基礎服務介紹CKmates - AWS 雲端運算 基礎服務介紹
CKmates - AWS 雲端運算 基礎服務介紹
 
在 DigitalOcean 架設 Gitlab
在 DigitalOcean 架設 Gitlab在 DigitalOcean 架設 Gitlab
在 DigitalOcean 架設 Gitlab
 
Bootstrap個人網站 20141117
Bootstrap個人網站 20141117Bootstrap個人網站 20141117
Bootstrap個人網站 20141117
 
Bootstrap個人網站 20141027
Bootstrap個人網站 20141027Bootstrap個人網站 20141027
Bootstrap個人網站 20141027
 
資訊創意課程 - Create A Personal Website 1
資訊創意課程 - Create A Personal Website 1資訊創意課程 - Create A Personal Website 1
資訊創意課程 - Create A Personal Website 1
 
MIS MySQL 入門
MIS MySQL 入門MIS MySQL 入門
MIS MySQL 入門
 
SITCON 2014 - Regular Expression Introduce
SITCON 2014 - Regular Expression IntroduceSITCON 2014 - Regular Expression Introduce
SITCON 2014 - Regular Expression Introduce
 
興大資訊社 CPE 訓練宣傳
興大資訊社 CPE 訓練宣傳興大資訊社 CPE 訓練宣傳
興大資訊社 CPE 訓練宣傳
 
20130706閃電秀
20130706閃電秀20130706閃電秀
20130706閃電秀
 
Drupalize your data use entities
Drupalize your data use entitiesDrupalize your data use entities
Drupalize your data use entities
 
Drupal 7 Entity & Entity API
Drupal 7 Entity & Entity APIDrupal 7 Entity & Entity API
Drupal 7 Entity & Entity API
 
植基於個人本體論的新聞推薦系統
植基於個人本體論的新聞推薦系統植基於個人本體論的新聞推薦系統
植基於個人本體論的新聞推薦系統
 
JSON 和 Android 的火花
JSON 和 Android 的火花JSON 和 Android 的火花
JSON 和 Android 的火花
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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

  • 1. 寫程式?那些老師沒教的事 Code Smart, Don’t Code Hard Release 2012/07/15 @StudyArea-Taichung 畢玉泉 (小畢/CrBoy) <crboy@crboy.net>
  • 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. /*¶®•§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. /*¶®•§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. /*¶®•§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. /*¶®•§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"); 新增! 刪除!
  • 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. /*¶®•§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. /*¶®•§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. /*¶®•§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. /*¶®•§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. /*¶®•§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.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 37.
  • 40. 什麼是Git The Stupid Content Tracker 愚蠢的內容追蹤器? 時光機器 回到過去 與 平行時空 版本控制系統 什麼是版本?怎麼控制?
  • 43.
  • 50. 「可不可以把 code 壓縮打包之後上傳到 git?」 『建議千萬不要這樣做,上次有人這樣做,結果...』 「結果呢?」 『結果追蹤不了版本,很不方便...』
  • 51. 本簡報由畢玉泉 (CrBoy) <crboy@crboy.net> 製作 以創用CC 姓名標示-相同方式分享 3.0 台灣 授權條款釋出 歡迎任何形式之重製、散佈與修改,但請明顯標註作者姓名、ID與E-mail,並以相同授權條款將衍生作品釋出 簡報中所引用之圖片與商標部分擷取自網際網路,其權利皆分屬各該公司或著作人所有