Linked list 使用C++

好吧…我要來獻醜一下

這也是幫學弟做的作業,再改的簡單一點的

因為是改的,所以也許會有些奇怪的地方或bug

還請多指教….

說一下這程式的功能好了

可以讀名字的文字檔(.txt),或手動加入名字

移除指定名字和列出所有名字

還有指定名字顯示位置….

其實原本作業要求很多功能,我覺得太瑣碎就拿掉了

資料結構原本是環狀串列,我改成單一普通的鏈結串列

以下就參考參考囉….

 

#include<iostream>
#include<fstream>
#include<string> 
#include<conio.h>



using namespace std;



int element=0;
struct node
{
    string name;
    node *next;
};



node *head=new node;
node *tail=new node;



void *readfile(){
     char filename[30];
     cout<<"enter filename:";
     cin>>filename;
     ifstream fin(filename);
     if(!fin) {
              cout << "無法讀入檔案\n";
              system("pause");
              exit(0);
     }
     node *pre = new node;
     fin>>pre->name;
     
     if(head->next==NULL)head->next=pre;
     else tail->next=pre;
     element++;
     
     while(!fin.eof())
     {
         node *tmp = new node;
         fin>>tmp->name;
         pre->next=tmp;
         pre=tmp;
         element++;
     }
     tail=pre;
}



void display(node *list){
      
     list=list->next;//跳過head
     
     if(list==NULL)
     {
         cout<<"空的"<<endl;
     }
     for(int i=0;i<element;i++)
    {
            cout<<"name: "<<list->name<<endl;
            list=list->next;
    }
}



//加入指定名字 
void add(node *list){
     
     node *tmp=new node;
     cout<<"enter the name:";
     cin>>tmp->name;
     if(head->next==NULL)
     {
          tmp->next=head->next;
          head->next=tmp;
          tail=tmp;
     }
     else
     {
     tmp->next=list->next;
     list->next=tmp;
     tail=tmp;
     }
     element++;
}



//刪除指定名字 
void del(node *list){
     string delname;
     node *pre=new node;
     
     cout<<"enter the delete name:";
     cin>>delname;
     
     for(int i=0;i<=element;i++)      
     {              
         if(list->name==delname)
         {
                 cout<<"delete the "<<list->name<<" !!"<<endl;
                 pre->next=list->next;
                 free(list); 
                 element--;
         }
         pre=list;
         list=list->next;
     }
}



//指定名字顯示位置 
void showposition(node *list){
     string fname;
     
     cout<<"enter the find name:";
     cin>>fname;
     
     for(int i=0;i<=element;i++)      
     {              
         if(list->name==fname)
         {
            cout<<"the "<<list->name<<" position is ["<<i<<"]"<<endl;
            break;
         }
         list=list->next;
     }
}



int main(){
    char comand;
    bool exit=false;
    
    while(!exit){
    cout<<"        ==MENU=="<<endl;
    cout<<"r、 Add name from file name.txt"<<endl;
    cout<<"a、 Add a new name into the list"<<endl;
    cout<<"d、 Delete a name from the list"<<endl;
    cout<<"f、 Find a name and return the position of the name in the list."<<endl;
    cout<<"p、 Print out all the names in the list."<<endl;
    cout<<"q、 Exit the program."<<endl;
    cout<<"    whitch one?";
    comand=getch();
    cout<<comand<<endl;
    
    switch(comand){
        case 'r':
             readfile();
             cout<<"Creat Success!"<<endl; 
             break;
        case 'a':
             add(tail);
             break;
        case 'd':
             del(head);
             break;
        case 'f':
             showposition(head);
             break;
        case 'p':
             display(head);
             break;
        case 'q':
             cout<<"bye bye ~!"<<endl;
             exit=true;
             break;
        default:
             break;
    }
    system("pause");
    system("cls");
    }
    return 0;
}

 

會了linked list真的世界都不同了欸~

真是恨自己太晚會阿~

一邊寫一邊回想以前有人說著它的一切

都一一的成真了….

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *