鏈結串鍊程式#include <cstdlib> #include <iostream> #include <cmath> using namespace std; struct box{ int apple; box *next; box *prev; }; box *head=NULL; box *tail=NULL; void show(){ box *temp; if(head !=NULL){ cout<<"由大到小進行排序"<<endl; temp =head; while(temp !=NULL){ cout<<temp->apple<<","; temp=temp->next; } cout<<endl; cout<<"由小到大進行排序"<<endl; temp=tail; while(temp !=NULL){ cout<<temp->apple<<","; temp=temp->prev; } } else{ 術後面膜cout<<"無資料"<<endl; } } void del(){ box *temp; box *flag=NULL; if(head==NULL){ cout<<"目前串鏈無資料"<<endl; } else{ int x; cout<<"輸入要刪除的數字"<<endl; cin>>x; temp=head; if((temp->prev==NULL)&&(temp->next==NULL)){//只剩一個的狀態 if(temp->apple==x){ delete(temp); head=NULL; tail=NULL; } else{ cout<<"數字輸入錯誤"<<endl; } } else if((head->apple)==x || (tail->apple)==x){ 房屋買賣 if(head->apple==x){//解決剩2個和head的部分 head=head->next; head->prev=NULL; delete(temp); } else{ //解決掉是tail的部分 temp=tail; tail=tail->prev; tail->next=NULL; delete(temp); } } else{ //其他狀況  if(fabs((head->apple)-(x))>fabs((tail->apple)-(x))){//若數字比較靠近tail由tail開始找 temp=tail; while(temp!= NULL){ if(temp->apple==x){ flag=temp; break; } temp=temp->prev; 帛琉 } if(flag !=NULL){ flag->next->prev=flag->prev; flag->prev->next=flag->next; delete(flag); } else{ cout<<"數字輸入錯誤"<<endl; } } else{ //其他的狀況皆由這裡處理 temp=head; while(temp!=NULL){ if(temp->apple==x){ flag=temp; break; } temp=temp->next; } if(flag !=NULL){ 東森房屋flag->next->prev=flag->prev; flag->prev->next=flag->next; delete(flag); } else{ cout<<"數字輸入錯誤"<<endl; } } } } } void add(){ box *flag; //找位置 box *start; box *temp; int applePrice; cout<<"請輸入蘋果的價錢"<<endl; cin>>applePrice; temp=new box; temp->apple=applePrice; temp->next=NULL; temp->prev=NULL; if(head==NULL){ head=temp; tail=temp; } else{ if(head->next==NULL && tail->prev==NULL){ //先將2烤肉點建立起來 if((temp->apple) > (head->apple)){ //如果數字比head大 head->prev=temp; head=temp; head->next=tail; } if((temp->apple) < (head->apple)){ //如果數字比head小 tail->next=temp; tail=temp; tail->prev=head; } } else{//以下為2點建立完後的繼續增加 帛琉if(temp->apple>head->apple){//比head還大 temp->next=head; head->prev=temp; head=temp; } else if(temp->apple<tail->apple){//比tail還小 tail->next=temp; temp->prev=tail; tail=temp; } else{ if(fabs((head->apple)-(temp->apple)) > fabs((tail->apple)-(temp->apple))){ //判定由tail(小的一邊)開始出發 start=tail; while(start !=NULL){ //從tail開始往前找適合的位置 if(start->apple>temp->apple){ flag=start; break; } start=start->prev; 好房網 } temp->next=flag->next; flag->next->prev=temp; temp->prev=flag; flag->next=temp; } else{//其他的狀況皆由head開始出發 start=head; while(start!=NULL){ if(start->apple<temp->apple){ flag=start; break; 酒店兼職 } start=start->next ; } temp->prev=flag->prev; flag->prev->next=temp; temp->next=flag; flag->prev=temp; } } //以上為2點建立完後的繼續增加 } } } int main(int argc, char *argv[]) { int c=1; while(c!=0){ cout<<endl; seo cout<<"輸入0.結束"<<endl; cout<<"輸入1.新增一筆資料"<<endl; cout<<"輸入2.秀出目前的資料"<<endl; cout<<"輸入3.刪除資料"<<endl; cin>>c; if(c==1){ add(); } if(c==2){ show(); } if(c==3){ del(); } } system("PAUSE"); return EXIT_SUCCESS; }


.msgcontent .wsharing ul li { text-indent: 永慶房屋0; }



分享

Facebook
Plurk
YAHOO!

arrow
arrow
    全站熱搜

    wu87wubthb 發表在 痞客邦 留言(0) 人氣()