雙向迴圈鏈表

來源:互聯網
上載者:User

標籤:鏈表   刪除元素   ios   nbsp   迴圈   cout   amp   申請   --   

定義一個迴圈鏈表結構

typedef struct LNode{    Elemtype data;    struct LNode *prev;    struct LNode *next;}*Linklist;
初始化鏈表

void CreatList(Linklist &L){    Linklist p, h;    cout << "請輸入鏈表的長度" << endl;    int n;    cin >> n;    L = (Linklist)malloc(sizeof(LNode));    L->next = L;    L->prev = L->next;    h = L;    if (!L)    {        cout << "申請空間失敗" << endl;    }    cout << "請輸入新節點的數值" << endl;    while (n != 0)    {        p = (Linklist)malloc(sizeof(LNode));        cin >> p->data;        p->next = h->next;        h->next = p;        p->prev = h;        h = h->next;        n--;    }    L->prev = h;}
按位置尋找
void GetElem(Linklist &L){    cout << "請輸入要尋找的位置" << endl;    int e;    cin >> e;    Linklist p = L;    while (p->next != L && e != 0)    {        p = p->next;        e--;    }    if (e != 0)    {        cout << "您所尋找的位置不在本鏈表中" << endl;    }    else    {        cout << "您所尋找的元素為:" << p->data << endl;    }}
插入一個元素

void InsertElem(Linklist &L){    Elemtype data;    int e;    cout << "請輸入插入的元素值和位置(以空格隔開)" << endl;    cin >> data;    cin >> e;    Linklist s, p = L;    s = (Linklist)malloc(sizeof(LNode));    s->data = data;    while (p->next != L && e != 1)    {        p = p->next;;        e--;    }    if (e > 1)    {        cout << "您輸入的位置超過本表的長度啦!" << endl;    }    s->next = p->next;    p->next = s;}
刪除一個元素

void DeleteElem(Linklist &L){    int e;    Linklist p = L, q = NULL;    cout << "請輸入刪除元素位置" << endl;    cin >> e;    while (p->next != L && e != 1)    {        p = p->next;        e--;    }    if (e > 1)    {        cout << "您輸入的位置超過本表的長度啦!" << endl;    }    else    {        q = p->next;        p->next = q->next;        free(q);    }}
遍曆一遍鏈表
void ShowList(Linklist &L){    cout << "遍曆一遍鏈表" << endl;    Linklist l = L;    while (l->next != L)    {        l = l->next;        cout << l->data << ‘ ‘;    }    cout << endl;}
完整代碼
#include"stdafx.h"#include <iostream>#include<stdlib.h>using namespace std;typedef int Elemtype;//定義一個迴圈鏈表結構 typedef struct LNode{    Elemtype data;    struct LNode *prev;    struct LNode *next;}*Linklist;//初始化鏈表void CreatList(Linklist &L){    Linklist p, h;    cout << "請輸入鏈表的長度" << endl;    int n;    cin >> n;    L = (Linklist)malloc(sizeof(LNode));    L->next = L;    L->prev = L->next;    h = L;    if (!L)    {        cout << "申請空間失敗" << endl;    }    cout << "請輸入新節點的數值" << endl;    while (n != 0)    {        p = (Linklist)malloc(sizeof(LNode));        cin >> p->data;        p->next = h->next;        h->next = p;        p->prev = h;        h = h->next;        n--;    }    L->prev = h;}//按位置尋找void GetElem(Linklist &L){    cout << "請輸入要尋找的位置" << endl;    int e;    cin >> e;    Linklist p = L;    while (p->next != L && e != 0)    {        p = p->next;        e--;    }    if (e != 0)    {        cout << "您所尋找的位置不在本鏈表中" << endl;    }    else    {        cout << "您所尋找的元素為:" << p->data << endl;    }}//插入一個元素void InsertElem(Linklist &L){    Elemtype data;    int e;    cout << "請輸入插入的元素值和位置(以空格隔開)" << endl;    cin >> data;    cin >> e;    Linklist s, p = L;    s = (Linklist)malloc(sizeof(LNode));    s->data = data;    while (p->next != L && e != 1)    {        p = p->next;;        e--;    }    if (e > 1)    {        cout << "您輸入的位置超過本表的長度啦!" << endl;    }    s->next = p->next;    p->next = s;}//刪除一個元素void DeleteElem(Linklist &L){    int e;    Linklist p = L, q = NULL;    cout << "請輸入刪除元素位置" << endl;    cin >> e;    while (p->next != L && e != 1)    {        p = p->next;        e--;    }    if (e > 1)    {        cout << "您輸入的位置超過本表的長度啦!" << endl;    }    else    {        q = p->next;        p->next = q->next;        free(q);    }}//遍曆一遍鏈表void ShowList(Linklist &L){    cout << "遍曆一遍鏈表" << endl;    Linklist l = L;    while (l->next != L)    {        l = l->next;        cout << l->data << ‘ ‘;    }    cout << endl;}

覺得文章不錯,點個贊和關注喲.

 

雙向迴圈鏈表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.