"Data structure (c)" Linear table--bidirectional cyclic linked list

Source: Internet
Author: User

/**** 2017.11.2** ahthor:799** Cyclic doubly linked list *****/#include <bits/stdc++.h>using namespace std; #define Elemtype Int#defi   Ne Status int#define error-1#define OK 1typedef struct lnode{elemtype data;   struct Lnode *prev; struct Lnode *next;};    typedef struct dlinklist{int len; Lnode *head;};    void Initlinklist (Dlinklist &l) {l.len = 0;    L.head = (Lnode *) malloc (sizeof (Lnode));    L.head->next = L.head;    L.head->prev = L.head; return;}    int getlengthlinklist (dlinklist &l) {Lnode *index = l.head->next;    int ct = 0;        while (Index! = l.head) {ct++;    index = index->next; } return CT;}    lnode* Getlinklistelem (dlinklist &l,int i) {lnode *index = L.head;    int ct = 0;        while (CT < i) {ct++;    index = index->next; } return index; Status insertlinklist (dlinklist &l,int i,elemtype data)///* before inserting {if (I <0 | | i > getlengthlinklist (L) + 1) re    Turn ERROR; Lnode *pointer = GetlinKlistelem (L,i);    Lnode *newnode = (Lnode *) malloc (sizeof (Lnode));    Newnode->data = data;    Newnode->prev = pointer->prev;    Pointer->prev->next = NewNode;    Newnode->next = pointer;    Pointer->prev = NewNode;    l.len++; return OK;}    Status Detelelinklistbypos (dlinklist &l,int i,elemtype &e)///bitwise DELETE {Lnode *pointer = Getlinklistelem (L,i);    E = pointer->data;    Pointer->prev->next = pointer->next;    Pointer->next->prev = pointer->prev;    free (pointer); return OK;}    Status Detelelinklistbyval (dlinklist &l,elemtype e)///bitwise DELETE {Lnode *pointer = l.head->next; for (;p ointer! = l.head; pointer = pointer->next) {if (Pointer->data = = e) {POINTER-&G            T;prev->next = pointer->next;            Pointer->next->prev = pointer->prev;        free (pointer); }} return OK;    void Trverselinklist (Dlinklist &l) {Lnode *index = l.head->next;; for (; INdex! = L.head;    index = index->next) {cout<<index->data<< ""; } Cout<<endl;}    int main () {int n;    cin>>n;    Dlinklist *DK = (dlinklist *) malloc (sizeof (dlinklist));    Initlinklist (*DK);        for (int i=1; i <= n; i++) {int tmp;        cin>>tmp;    Insertlinklist (*DK,I,TMP);    } trverselinklist (*DK);    int pos,val;    cin>>val;    Detelelinklistbyval (*dk,val); Trverselinklist (*DK);}

  

"Data structure (c)" Linear table--bidirectional cyclic linked list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.