Self-built algorithm library of data structure--double linked list

Source: Internet
Author: User

This paper is aimed at the basic series of data Structure network course (2): Linear table in the 12th class double linked list.

Build your own professional infrastructure algorithm library according to the method suggested in the "0207 Algorithm Change Program" [VIDEO] section.

The double-linked list algorithm library uses the program's multi-file organization, including two files:
  
1. header file: Dlinklist.h, contains the code that defines the data structure of the double-linked list, the macro definition, the declaration of the function to implement the algorithm;

#ifndef dlinklist_h_included
#define dlinklist_h_included
typedef int ELEMTYPE;
typedef struct DNODE        //define doubly-linked table node type
{
    elemtype data;
    struct Dnode *prior;    The precursor junction
    struct Dnode *next;     Points to subsequent nodes
} dlinklist;
void Createlistf (Dlinklist *&l,elemtype a[],int N);//head interpolation double-linked list
void Createlistr (dlinklist *&l,elemtype a[] , int n);//tail interpolation method to build double-linked list
void Initlist (Dlinklist *&l);//Initialize double-linked list
void Destroylist (Dlinklist *&l);//Destroy double-linked list
bool Listempty (dlinklist *l);//Determine if the list is empty
int listlength (dlinklist *l);//The length of the list is
void Displist ( Dlinklist *l); Output List
bool Getelem (dlinklist *l,int i,elemtype &e);//Gets the value of the node
int Locateelem (dlinklist *l,elemtype e); Find a node
bool Listinsert (dlinklist *&l,int i,elemtype e);//Insert a node
bool Listdelete (dlinklist *&l,int I , Elemtype &e); Delete a node


#endif//dlinklist_h_included

2. source file: Linklist.cpp, which contains definitions of functions that implement various algorithms

#include <stdio.h> #include <malloc.h> #include "dlinklist.h" void Createlistf (Dlinklist *&l,elemtype A
    [],int N]//head interpolation method to build double-linked list {dlinklist *s;
    int i;   L= (Dlinklist *) malloc (sizeof (dlinklist));
    Create a head node l->prior=l->next=null;
        for (i=0; i<n; i++) {s= (dlinklist *) malloc (sizeof (dlinklist));//Create new node s->data=a[i];            s->next=l->next;
        Insert the *s before the original start node, after the head node if (l->next!=null) l->next->prior=s;
        l->next=s;
    s->prior=l;
    }} void Createlistr (Dlinklist *&l,elemtype a[],int N)//tail interpolation method to build double-linked list {dlinklist *s,*r;
    int i;   L= (Dlinklist *) malloc (sizeof (dlinklist));
    Create a head node l->prior=l->next=null;                    R=l; R always points to the terminal node, starting with the head node for (i=0; i<n; i++) {s= (dlinklist *) malloc (sizeof (dlinklist));//Creating a new node s-&
        Gt;data=a[i];
        r->next=s; s->prior=r;
    R=s after inserting the *s into the *r;        } r->next=null;   Terminal node next field is set to null} void Initlist (Dlinklist *&l) {l= (dlinklist *) malloc (sizeof (dlinklist));
Create a head node l->prior=l->next=null;
    } void Destroylist (Dlinklist *&l) {dlinklist *p=l,*q=p->next;
        while (Q!=null) {free (p);
        p=q;
    q=p->next;
} free (p);
    } bool Listempty (Dlinklist *l) {return (l->next==null);} int listlength (Dlinklist *l) {dlinklist *p=l;
    int i=0;
        while (p->next!=null) {i++;
    p=p->next;
} return (i);
    } void Displist (Dlinklist *l) {dlinklist *p=l->next;
        while (P!=null) {printf ("%d", p->data);
    p=p->next;
} printf ("\ n");
    } bool Getelem (dlinklist *l,int i,elemtype &e) {int j=0;
    Dlinklist *p=l;
        while (J<i && p!=null) {j + +;
    p=p->next;
    } if (P==null) return false;
        else {e=p->data;
    return true; }} int Locateelem (DlinKlist *l,elemtype e) {int n=1;
    Dlinklist *p=l->next;
        while (P!=null && p->data!=e) {n++;
    p=p->next;
    } if (P==null) return (0);
else return (n);
    } bool Listinsert (dlinklist *&l,int i,elemtype e) {int j=0;
    Dlinklist *p=l,*s;
        while (j<i-1 && p!=null) {j + +;
    p=p->next;
    } if (P==null)//did not find the first i-1 node return false;   else//Find the first i-1 node *p {s= (dlinklist *) malloc (sizeof (dlinklist));
        Create a new node *s s->data=e;        s->next=p->next;
        Insert the *s into the *p after if (p->next!=null) p->next->prior=s;
        s->prior=p;
        p->next=s;
    return true;
    }} bool Listdelete (dlinklist *&l,int i,elemtype &e) {int j=0;
    Dlinklist *p=l,*q;
        while (j<i-1 && p!=null) {j + +;
    p=p->next; } if (P==null)//i-1 node return not foundFalse              else//Find the first i-1 node *p {q=p->next;       Q Point to delete the node if (q==null) return false;
        There is no first node e=q->data;.        p->next=q->next;
        Remove the *q node if (p->next!=null) from the single-linked list p->next->prior=p;                Free (q);
    Release *q node return true; }
}

3. In the process of building the algorithm library, in order to complete the test, build a source file (such as main.cpp) in the same project, and compile the main function to complete the relevant testing work.
The test work can be "progressive" thinking, each of the functions involved should be as few as possible.
For example, the following design tests some of the functions:

#include <stdio.h>
#include "dlinklist.h"

int main ()
{
    dlinklist *a;
    Elemtype a[]= {1, 3, 2, 9, 0, 4, 5, 6, 7, 8};
    Initlist (A);
    CREATELISTF (A, A, ten);
    printf ("Length:%d\n", Listlength (A));
    Listinsert (A, 4,);
    printf ("After Insert:");
    Displist (A);
    Destroylist (A);
    return 0;
}

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.