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

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

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

2019-11-11 07:09:02
字體:
來源:轉載
供稿:網友
#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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河曲县| 抚松县| 鲜城| 杭锦旗| 上犹县| 饶平县| 精河县| 喀什市| 东乌| 左云县| 郸城县| 冕宁县| 盐山县| 新蔡县| 正蓝旗| 临武县| 靖西县| 乐陵市| 南汇区| 阜康市| 灌南县| 广东省| 张家口市| 石阡县| 安图县| 衡东县| 陵水| 沅陵县| 舒城县| 孝昌县| 忻城县| 平乐县| 攀枝花市| 易门县| 崇州市| 眉山市| 漾濞| 天镇县| 舟山市| 新野县| 开阳县|