aalexx 10 發表於 August 10, 2011 檢舉 Share 發表於 August 10, 2011 (已編輯) 以下為在下某日深液爆肝弄出來的東西編寫語言為C++,使用code::blocks打到昏昏沉沉總算是弄完了總覺得程式碼好像太繁複而且不知道為甚麼記憶體空間用工作管理員看怪怪的不知各位大大可否幫忙精簡or指正(尤其是那個count函數和list_switch函數)或是借看一下各位大大的鍊表寫法copy到Dev-c++或code::blocks應該可以直接編繹執行#include<iostream>using namespace std;int list_plus(void);int list_delete(void);int list_switch(void);int list_list(void);int num=0,point=1;//表單宣告struct list{ string data; list *aft; }head,null;list *count(struct list*,int,int);int main(){ int x; //初使化頭尾 head.aft=&null; head.data="head"; null.data="null"; //主體 do{ system("cls"); cout<<"1.插入\n2.刪除\n3.交換\n4.列印\n"; do{ cin>>x; }while(x<1 || x>4); switch(x){ case 1: list_plus(); break; case 2: list_delete(); break; case 3: list_switch(); break; case 4: system("cls"); list_list(); cout<<'\n'; system("pause"); break; } }while(1); system("pause"); return 0;}//數list *count(struct list *a,int r,int now){ if(now<r) { now++; a=count(a->aft,r,now); };return a;}//插入int list_plus(void){ system("cls"); list *q=new list; cout<<"請問輸入數值?\n"; cin>>q->data; cout<<"請問插入位置?\n(目前"<<num<<"筆資料,可用位置為1~"<<num+1<<")\n"; do{ cin>>point; }while(point<1 || point>num+1); q->aft=count(&head,point,0); list *temp; temp=count(&head,point-1,0); temp->aft=q; num+=1;}//刪除int list_delete(void){ system("cls"); if(num==0) { cout<<"沒有任何資料!\n\n"; system("pause"); return 1; } list_list(); cout<<"要刪除哪一個數?\n(資料位置1~"<<num<<")\n"; do{ cin>>point; }while(point<1 || point>num); list *temp2; temp2=count(&head,point,0); list *temp; temp=count(&head,point-1,0); temp->aft=count(&head,point+1,0); delete temp2; num-=1;}//交換int list_switch(void){ system("cls"); if(num<2) { cout<<"資料數量不足喔!\n\n"; system("pause"); return 1; } list_list(); cout<<"要交換兩筆資料,第一個?\n(資料位置1~"<<num<<")\n"; do{ cin>>point; }while(point<1 || point>num); list *temp; list *temp4; temp4=count(&head,point-1,0); temp=count(&head,point,0); cout<<"第二個?\n(資料位置1~"<<num<<")\n"; do{ cin>>point; }while(point<1 || point>num); list *temp2; list *temp3; temp2=count(&head,point,0); temp3=temp2->aft; if(temp==temp2) { cout<<"同一筆資料無法交換喔!\n\n"; system("pause"); return 1; }if(temp->aft==temp2) { temp2->aft=temp; temp4->aft=temp2; temp->aft=temp3; } else { if(temp2->aft==temp) { temp4=count(&head,point-1,0); temp2=temp4->aft; temp=temp2->aft; temp3=temp->aft; temp4->aft=temp; temp->aft=temp2; temp2->aft=temp3; } else { list *temp5; temp5=count(&head,point-1,0); temp2->aft=temp->aft; temp->aft=temp3; temp4->aft=temp2; temp5->aft=temp; } }}//列印int list_list(void){ if(num==0) { cout<<"沒有任何資料!\n"; return 1; } int print=0; list *temp; do{ print+=1; temp=count(&head,print,0); cout<<print<<". "<<temp->data<<'\n'; }while(print<num);} 此內容已被編輯, August 11, 2011 ,由 aalexx 鏈接文章 分享到其他網站
Recommended Posts
請登入後來留意見
在登入之後,您才能留意見
立即登入