單向鏈表 頭尾插法

來源:互聯網
上載者:User

單向鏈表  頭尾插法

#include<iostream.h>#include<conio.h> struct node{   int data;   node *next;              };node *createlist(int );node *createlistinvert(int );void outputlist(node *head); int main(){  int n;  node *listhead=NULL;    cout<<"請輸入節點數目: ";  cin>>n;  cout<<"**************下面請您輸入"<<n<<"個整數*****************\n"<<endl;   if(n>0)    {         cout<<"      ***************尾差法********************\n"<<endl;         listhead=createlist(n);               outputlist(listhead);                         cout<<" \n\n    *****************頭插法*********************\n"<<endl;          cout<<"**************下面請您輸入"<<n<<"個整數*****************\n"<<endl;                  listhead=createlistinvert(n);                outputlist(listhead);      }       getch();   return 0; }//尾插法 node *createlist(int n){   node *temp=NULL,*tail=NULL,*head=NULL;      int num;      cout<<"請輸入資料: ";    cin>>num;      head=new node;      if(head==NULL)     {        cout<<"記憶體配置不成功,重試吧!";                            return NULL;                               }             else    {       head->data=num;              head->next=NULL;       tail=head;                          }        for(int i=0;i<n-1;i++)      {               cin>>num;                  temp=new node;                  if(temp==NULL)           {              cout<<"記憶體配置不成功";                                        return head;                                           }                         else            {                temp->data=num;                temp->next=NULL;                tail->next=temp;                                tail=temp;                                   }                  }          return head;     }//頭插法node *createlistinvert(int n){      node *temp=NULL,*head=NULL;            cout<<"請輸入資料:";       int num;      cin>>num;             head=new node;            if(head==NULL)        {           cout<<"記憶體配置不成功!";             return NULL;                                                 }          else        {           head->data=num;                          head->next=NULL;                                 }        for(int i=0;i<n-1;i++)     {           cin>>num;                      temp=new node;                      if(temp==NULL)          {          cout<<"記憶體配置不成功!";                              return head;                                    }         else        {          temp->data=num;                 temp->next=head;                     head=temp;                      }        }    return head;  }    void outputlist(node *head) {      cout<<"List:   ";       node *curnode=head;            while(curnode)       {         cout<<curnode->data;         if(curnode->next)              cout<<"  -> ";                      curnode=curnode->next;               }             cout<<endl;           } 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.