欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

带头结点双循环链表

程序员文章站 2022-03-01 19:49:57
...

初始化

void DLinkListInit(DLinkLIst &L)
{
    L=new node;
    L->next=L;
    L->prior=L;
}

创建

void CreateDlinklist(DLinkLIst &L)
{
    int n,m;
    DLinkLIst s;
    DLinkLIst r=L;
    cout<<"please input a number:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        s=new node;
        cin>>m;
        s->data=m;
        r->next=s;
        s->prior=r;
        r=s;
    }
    r->next=L;
    L->prior=r;
}

计算长度

int  DlinkLength(DLinkLIst L)
{
    DLinkLIst p=L->next;
    int count=0;
    while(p!=L)
    {
        count++;
        p=p->next;
    }
    return count;
}

插入新的结点

void InsertDlinklist(DLinkLIst &L,int i,int e,int length)
{
    DLinkLIst p=L;
    int j=0;
    if(i<1||i>length+1)
    {
        cout<<"false";
    }
    while(j<i-1)
    {

        p=p->next;
        j++;
    }
    DLinkLIst s=new node;
    s->data = e;
    s->next=p->next;
    s->prior=p->next->prior;
    p->next=s;
    s->next->prior=s;

}

删除结点

void DeleteDlinklist(DLinkLIst &L,int e)
{
    DLinkLIst p=L->next;
    while(p!=L&&p->data!=e)
    {

        p=p->next;
    }
    if(p==L)
    {
        cout<<"can't find this number,delete fail";
        exit(0);
    }
    p->prior->next=p->next;
    p->next->prior=p->prior;
    delete p;
}

正序输出

void coutprint_H(DLinkLIst L)
{
    DLinkLIst p=L->next;
    while(p!=L)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}

逆序输出

void coutprint_E(DLinkLIst L)
{
    DLinkLIst p=L->prior;
    while(p!=L)
    {
        cout<<p->data<<" ";
        p=p->prior;
    }

完整代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;

typedef struct node
{
    int data;
    struct node *prior;
    struct node *next;
}*DLinkLIst;

void DLinkListInit(DLinkLIst &L)
{
    L=new node;
    L->next=L;
    L->prior=L;
}
void CreateDlinklist(DLinkLIst &L)
{
    int n,m;
    DLinkLIst s;
    DLinkLIst r=L;
    cout<<"please input a number:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        s=new node;
        cin>>m;
        s->data=m;
        r->next=s;
        s->prior=r;
        r=s;
    }
    r->next=L;
    L->prior=r;
}
int  DlinkLength(DLinkLIst L)
{
    DLinkLIst p=L->next;
    int count=0;
    while(p!=L)
    {
        count++;
        p=p->next;
    }
    return count;
}
void InsertDlinklist(DLinkLIst &L,int i,int e,int length)
{
    DLinkLIst p=L;
    int j=0;
    if(i<1||i>length+1)
    {
        cout<<"false";
        exit(0) ;
    }
    while(j<i-1)
    {

        p=p->next;
        j++;
    }
    DLinkLIst s=new node;
    s->data = e;
    s->next=p->next;
    s->prior=p->next->prior;
    p->next=s;
    s->next->prior=s;

}
void DeleteDlinklist(DLinkLIst &L,int e)
{
    DLinkLIst p=L->next;
    while(p!=L&&p->data!=e)
    {

        p=p->next;
    }
    if(p==L)
    {
        cout<<"can't find this number,delete fail";
        exit(0);
    }
    p->prior->next=p->next;
    p->next->prior=p->prior;
    delete p;
}
void coutprint_H(DLinkLIst L)
{
    DLinkLIst p=L->next;
    while(p!=L)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
void coutprint_E(DLinkLIst L)
{
    DLinkLIst p=L->prior;
    while(p!=L)
    {
        cout<<p->data<<" ";
        p=p->prior;
    }
}

int main(void)
{
    DLinkLIst L;
    int n,m1,m2;
    int number;
    DLinkListInit(L);
    CreateDlinklist(L);
    n=DlinkLength(L);
    cout<<"please input two numbers  you want insert and where:";
    cin>>m1>>m2;
    InsertDlinklist(L,m2,m1,n);
    coutprint_H(L);
    cout<<endl;
    cout<<"please input a number that you want to delete:";
    cin>>number;
    DeleteDlinklist(L,number);
    coutprint_H(L);
    cout<<endl;
    coutprint_E(L);
    return 0;
}

本人初学,理解不深,有错希望谅解,帮忙改正,谢谢!

相关标签: 链表