Data structure-bidirectional circular linked list (headless node) correlation algorithm

Source: Internet
Author: User

#include <stdio.h>
#include <stdlib.h>
#define OVERFLOW-2
#define OK 1
#define ERROR 0

//This bidirectional loop linked list headless node
typedef int ELEMTYPE;
typedef struct DULNODE {
Elemtype data;
struct Dulnode *prior;
struct Dulnode *next;
}dulnode,*dullinklist;

dullinklist Initlist_dul (dullinklist L);
dullinklist Createelemp_dul (dullinklist L);
void Printlist_dul (Dullinklist L);
int Getelemp1_dul (dullinklist l,int i,elemtype *e);
dullinklist Listinsert_dul (dullinklist l,int i,elemtype e);
int Listlength_dul (dullinklist L);
dullinklist Getelemp2_dul (dullinklist l,int i);
dullinklist Listinsert_dul (dullinklist l,int i,elemtype e);
dullinklist Listdelete_dul (dullinklist l,int i,elemtype *e);
int Getlocate_dul (dullinklist l,elemtype e);
int main ()
{
Dullinklist Dul;
int len,loc;
Elemtype e;
Dul = Initlist_dul (Dul);
Dul = Createelemp_dul (Dul);
Len = Listlength_dul (Dul);
printf ("The length of the doubly linked list is:%d\n", Len);
Printlist_dul (Dul);
Getelemp1_dul (dul,3,&e);//3 is the bit sequence where the element resides
printf ("The element of the first position is:%d\n", e);
printf ("After inserting an element into the list:");
Dul = Listinsert_dul (dul,1,0);//1 The position of the insertion element
Printlist_dul (Dul);
printf ("After deleting an element from a linked list:");
Dul = Listdelete_dul (dul,2,&e);//2 the bit order to delete the element
Printlist_dul (Dul);
printf ("The deleted element is:%d\n", e);
loc = Getlocate_dul (dul,0);//0 is the value of the element
printf ("The sequence of the two-way cyclic linked list element i is:%d\n", loc);
return 0;
}

//Initialize a two-way loop linked list
Dullinklist Initlist_dul (dullinklist L) {
L = (dullinklist) malloc (sizeof (Dulnode));
if (L! = NULL) {
L->next = L->prior = L;
} else {
Exit (OVERFLOW);
}
return L;

}


//Create a two-way circular linked list
Dullinklist Createelemp_dul (dullinklist L) {
int n,i;
Dullinklist P,s;
s = L;
printf ("Please enter the number of elements of the bidirectional loop list:");
scanf ("%d", &n);
printf ("Please enter the value of the element in the doubly-looped list:");
for (i=0; i<n; i++) {
p = (dullinklist) malloc (sizeof (Dulnode));
scanf ("%d", &p->data);
P->prior = s->prior;
S->prior->next = p;
P->next = s;
S->prior = p;
}
return L;
}

//Output the bidirectional loop linked list
void Printlist_dul (Dullinklist L) {
int i;
Dullinklist p;
p = L;
printf ("The element in the loop-linked list is:");
while (p->next! = L) {
p = p->next;
printf ("%d", p->data);
}
printf ("\ n");
}

//Returns the length of the double-loop linked list
int Listlength_dul (dullinklist L) {
Dullinklist p;
int i;
p = l->next;
i = 0;
while ((p!=l) && (p!=null)) {
++i;
p = p->next;
}
return i;
}

//Get the value of the element on the I-position in the two-way loop linked list
int Getelemp1_dul (dullinklist l,int i,elemtype *e) {
Dullinklist p;
Int J;
p = L;
j = 0;
while (P->next && j<i) {
++j;
p = p->next;
}
if (!p | | j>i) return ERROR;
*e = p->data;
return OK;
}

//Returns the pointer on the I position on the double-loop linked list
Dullinklist Getelemp2_dul (dullinklist l,int i) {
Dullinklist p;
Int J;
p = L;
j = 0;
while (P->next && j<i) {
++j;
p = p->next;
}
if (!p | | j>i) return ERROR;
return p;
}

//To insert element e before the I position of the two-way loop linked list
Dullinklist Listinsert_dul (dullinklist l,int i,elemtype e) {
Dullinklist P,s;
int m;
if (! ( P=getelemp2_dul (l,i))) {
return ERROR;
}

if (! ( s = (dullinklist) malloc (sizeof (Dulnode)))) {
return ERROR;
}
S->data = e;
S->prior = p->prior;
P->prior->next = s;
S->next = p;
P->prior = s;
return L;
}

//Delete the element on the I-position of the two-way loop linked list and return the value of the element
Dullinklist Listdelete_dul (dullinklist l,int i,elemtype *e) {
Dullinklist p;
if (! ( P=getelemp2_dul (l,i))) {
return ERROR;
}
*e = p->data;
P->prior->next = p->next;
P->next->prior = p->prior;
return L;
}

//Returns the ordinal of element e on a doubly-linked circular list
int Getlocate_dul (dullinklist l,elemtype e) {
Dullinklist p;
Int J;
p = l->next;
j = 0;
while ((P! = L) && (p->data!=e)) {
j + +;
}
Return J;
}






Data structure-bidirectional circular linked list (headless node) correlation algorithm

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.