头文件
#ifndef __DOUBLELOOP__
#define __DOUBLELOOP__
#include <stdio.h>
#include <stdlib.h>
typedef struct doubleloop
{
int data;
struct doubleloop *pri;
struct doubleloop *next;
}node,*node_p;
node_p create_doubleloop();
node_p create_node(int data);
int empty_doubleloop(node_p H);
void insert_head(node_p H,int data);
void show(node_p H);
void delete_head(node_p H);
void insert_tail(node_p H,int data);
void delete_tail(node_p H);
void insert_location(node_p H,int location,int data);
void delete_location(node_p H,int location);
#endif
主函数
#include "doubleloop.h"
int main(int argc, const char *argv[])
{
node_p H=create_doubleloop();
insert_head(H,10);
insert_head(H,8);
insert_head(H,6);
insert_head(H,4);
insert_head(H,2);
insert_head(H,0);
show(H);
printf("add -2 at the head\n");
insert_head(H,-2);
show(H);
printf("delete the head\n");
delete_head(H);
show(H);
printf("add 12 in the end\n");
insert_tail(H,12);
show(H);
printf("delete the end\n");
delete_tail(H);
show(H);
printf("add 3 at the location 3\n");
insert_location(H,3,3);
show(H);
printf("delete location 4\n");
delete_location(H,4);
show(H);
return 0;
}
自定义函数
#include "doubleloop.h"
node_p create_doubleloop()
{
node_p H=(node_p)malloc(sizeof(node));
if(H==NULL)
{
printf("apply fail\n");
return NULL;
}
H->data=0;
H->next=H;
H->pri=H;
return H;
}
node_p create_node(int data)
{
node_p new=(node_p)malloc(sizeof(node));
new->data=data;
return new;
}
int empty_doubleloop(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return -1;
}
return H->next==H?1:0;
}
void insert_head(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
node_p new=create_node(data);
new->next=H->next;
new->pri=H;
H->next=new;
new->next->pri=new;
H->data++;
}
void show(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_doubleloop(H))
{
printf("void\n");
return;
}
node_p p=H->next;
printf("H->");
while(p!=H)
{
printf("%d->",p->data);
p=p->next;
}
printf("H");
putchar(10);
printf("upside down\n");
node_p q=H->pri;
printf("H->");
while(q!=H)
{
printf("%d->",q->data);
q=q->pri;
}
printf("H");
putchar(10);
putchar(10);
}
void delete_head(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_doubleloop(H))
{
printf("void\n");
return;
}
node_p q=H->next;
H->next=q->next;
q->next->pri=H;
free(q);
H->data--;
}
void insert_tail(node_p H,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
node_p p=H->pri;
node_p new=create_node(data);
new->next=H;
new->pri=p;
p->next=new;
H->pri=new;
H->data++;
}
void delete_tail(node_p H)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_doubleloop(H))
{
printf("void\n");
return;
}
node_p p=H->next;
while(p->next->next!=H)
{
p=p->next;
}
node_p q=p->next;
p->next=q->next;
H->pri=p;
free(q);
H->data--;
}
void insert_location(node_p H,int location,int data)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(location<=0||location>data)
{
printf("wrong location\n");
return;
}
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
node_p new=create_node(data);
new->next=p->next;
new->pri=p;
p->next=new;
new->next->pri=new;
H->data++;
}
void delete_location(node_p H,int location)
{
if(H==NULL)
{
printf("apply fail\n");
return;
}
if(empty_doubleloop(H))
{
printf("void\n");
return;
}
if(location<=0||location>H->data)
{
printf("wrong location\n");
return;
}
node_p p=H;
for(int i=0;i<location-1;i++)
{
p=p->next;
}
node_p q=p->next;
p->next=q->next;
q->next->pri=p;
free(q);
H->data--;
}
实现