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

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

單循環(huán)鏈表的創(chuàng)建,搜索,遍歷

2019-11-11 05:44:02
字體:
供稿:網(wǎng)友
#include<iostream>#include<stdlib.h>using namespace std;typedef struct _node{            int data;    struct _node *next;}Node;//鏈表節(jié)點(diǎn)結(jié)構(gòu)。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);//創(chuàng)建一個(gè)p指針,如果不需要循環(huán)鏈表p->next=NULL即可。        Node *last=list->head;//last指針作為遍歷指針。    if(last)//判斷邊界,鏈表內(nèi)是否有數(shù)據(jù)?    {        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)            //這里判斷條件為空,所以如果鏈表中沒有尋找數(shù)據(jù)是不會(huì)跳出循環(huán)的,如果需要跳出循環(huán)條件為將代碼參照show()里的遍歷方式,或者判斷q指針也是可以的。        {            if(p->data==plc)            {                                if(q)//防止鏈表中第一個(gè)節(jié)點(diǎn)為刪除節(jié)點(diǎn),此時(shí)需要判斷q的值,因?yàn)槟愕谝粋€(gè)節(jié)點(diǎn)前沒有節(jié)點(diǎn)了。                {                    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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石嘴山市| 勃利县| 清水县| 武功县| 方山县| 霍城县| 布尔津县| 木里| 东港市| 北票市| 碌曲县| 和田县| 红安县| 确山县| 明光市| 修文县| 铁岭县| 津市市| 西和县| 义乌市| 万全县| 大邑县| 龙南县| 丽江市| 淳化县| 澄迈县| 罗平县| 囊谦县| 黄冈市| 襄垣县| 米泉市| 平原县| 皮山县| 凯里市| 西充县| 图们市| 广水市| 襄汾县| 绥滨县| 竹北市| 天门市|