双向循环链表代码实践

来源:互联网 发布:2015年酒店行业数据 编辑:程序博客网 时间:2024/06/16 11:24
 #include<iostream>using namespace std;struct id{    id *prev;    int num;    id*next;};id *tem = NULL;void cre(id *guard){    int insert;    cout << "你想插在在第几个"<<endl;    cin >> insert;    if (guard->next == guard&&guard->prev == guard)    {        tem = new id;        tem->num = insert;        guard->next = tem;        guard->prev = tem;        tem->next = guard;        tem->prev = guard;        tem = NULL;    }    else    {        id*head = guard->next;        for (;insert > head->num;head = head->next)        {            if (head== guard)            {                break;            }        }        if (head == guard)        {            head = guard -> prev;            tem = new id;            tem->num = insert;            tem->next = guard;            tem->prev = head;            tem->next->prev = tem;            tem->prev->next = tem;            tem = NULL;        }        else        {            tem = new id;            tem->num = insert;            tem->next = head;            tem->prev = head->prev;            tem->prev->next = tem;            tem->next->prev = tem;            tem = NULL;        }    }}void del(id *guard){    cout << "你想删除序号是多少的那个块"<<endl;    int del_;    cin >> del_;    id*head=guard->next;    for (;del_ != head->num;head = head->next)    {        if (head== guard)        {            cout << "没有,别删了"<<endl;            break;        }    }    if (head != guard)    {        head->next->prev = head->prev;        head->prev->next = head->next;        delete head;        cout << "删除成功"<<endl;    }}void mod(id *guard){    int modify;    cout << "你想修改序号是多少的数"<<endl;    cin >> modify;    id*head;    head = guard->next;    for (;modify != head->num;head = head->next)    {        if (head == guard)        {            cout << "你改啥"<<endl;            break;        }    }    if (head != guard)    {        int scan;        cout << "你想改成几" << endl;        cin >> scan;        head->num = scan;        cout << "修改成功" << endl;    }}void pri(id *guard){    id*head=guard->next;    for (;head != guard;head = head->next)    {        cout << head->num<<endl;    }}int main(){    int n;    id*guard = new id;    guard->next = guard;    guard->prev = guard;    guard->num = NULL;    while (cin.good())    {        cout << "1插入,2删除,3修改,4打印"<<endl;        cin >> n;        switch (n)        {        case 1:            {                cre(guard);                break;            }        case 2:            {                del(guard);                break;            }        case 3:            {                mod(guard);                break;            }        case 4:            {                pri(guard);                break;            }                       }    }    return 0;}
0 0
原创粉丝点击