国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

單循環鏈表的創建,搜索,遍歷

2019-11-11 05:09:04
字體:
來源:轉載
供稿:網友
#include<iostream>#include<stdlib.h>using namespace std;typedef struct _node{            int data;    struct _node *next;}Node;//鏈表節點結構。typedef struct{    Node *head;    Node *end;}List;void add(List *list,int d){    Node *p=(Node*)malloc(sizeof(Node));        p->data=d;    p->next=(list->head);//創建一個p指針,如果不需要循環鏈表p->next=NULL即可。        Node *last=list->head;//last指針作為遍歷指針。    if(last)//判斷邊界,鏈表內是否有數據?    {        while((last->next)!=(list->head))        {            last=last->next;                    }        last->next=p;    }else{        list->head=p;        p->next=(list->head);    }        }void show(List *list){        if(list->head==NULL)    {        cout<<"nothing"<<endl;    }    else{                Node *p=(Node*)malloc(sizeof(Node));        p=list->head;        do{            cout<<p->data;            p=p->next;        }while(p!=(list->head));                    }    }void remove(List *list,int plc){        if(list->head==NULL)    {        cout<<"NULL";    }else{                Node *q=(Node*)malloc(sizeof(Node));                Node *p=(Node*)malloc(sizeof(Node));                p=list->head;        for(q=NULL;;q=p,p=p->next)            //這里判斷條件為空,所以如果鏈表中沒有尋找數據是不會跳出循環的,如果需要跳出循環條件為將代碼參照show()里的遍歷方式,或者判斷q指針也是可以的。        {            if(p->data==plc)            {                                if(q)//防止鏈表中第一個節點為刪除節點,此時需要判斷q的值,因為你第一個節點前沒有節點了。                {                    q->next=p->next;                                    }else{                    list->head=p->next;                }                                free(p);                break;            }                                }                    }    }int main(){            List list;    int n;    int m;    list.head=NULL;    cin>>n>>m;    for(int i=0;i<n;i++)    {        add(&list,i);            }    show(&list);    remove(&list,2);    cout<<endl;    show(&list);            return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 卢氏县| 木里| 广宗县| 枣强县| 两当县| 南溪县| 伊金霍洛旗| 宜城市| 武冈市| 潼关县| 丽江市| 博罗县| 尚志市| 富锦市| 白玉县| 右玉县| 西畴县| 永济市| 忻城县| 平罗县| 休宁县| 石景山区| 长子县| 武城县| 海兴县| 蒙城县| 固阳县| 金山区| 磐石市| 象山县| 金昌市| 定远县| 中卫市| 滕州市| 阿拉善左旗| 博白县| 鹤庆县| 会宁县| 南川市| 芒康县| 德钦县|