链表c++代码的实现
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                链表c++代码的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                //說明:各位測試數據的時候,請嚴格按照主函數來測試數據。因為這個鏈表是假設所有輸入數據正確的情況。。。假如隨便亂輸入的話。。我沒寫邊界檢查。
//本次寫法是比較簡潔的。方便大家學習。
?
?
?
?
?
#include "iostream" using namespace std; typedef struct Node { char ch; struct Node *nex; }List; void Init(List *&L)//初始化 { L=new List; L->nex=NULL; } void show(List *L)//顯示 { List *p=L->nex; while(p!=NULL) { cout<<p->ch<<" "; p=p->nex; } cout<<endl; } void Create(List *&L,int n)//給進N個數 { int i; List *p=L; for(i=0;i<n;i++) { p->nex=new List; p=p->nex; cin>>p->ch; p->nex=NULL; } } void Insert(List *&L,char key,int index)//插入key到下標為INDEX的鏈表中 { int j=0; List *p=L; while(j<index) { j++; p=p->nex; } List *s; s=new List; s->nex=NULL; s->ch=key; s->nex=p->nex; p->nex=s; } void Del(List *&L,int index)//刪除下標為index的數 { int j=0; List *p=L; while(j<index) { j++; p=p->nex; } p->nex=p->nex->nex; } int main() { int n,q; List *L; while(cin>>n>>q)//n個數Q個操作 { Init(L); Create(L,n); while(q--) { int x; cin>>x; if(x==1)//插入 { char key; int index; cin>>key>>index; Insert(L,key,index); show(L); } else if(x==2)//顯示 { show(L); } else//刪除 { int index; cin>>index; Del(L,index); show(L); } } } return 0; }?
總結
以上是生活随笔為你收集整理的链表c++代码的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Periodic Signal
- 下一篇: html中article、section
