#ifndef _DLIST_H_
#define _DLIST_H_
#include <stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *prior, *next;
}dlist_node;
extern dlist_node* dlist_create();
extern void show_dlist(dlist_node *H);
extern int empty_dlist(dlist_node *H);
extern dlist_node * dlist_get(dlist_node *H,int pos);
extern dlist_node *dlist_insert(dlist_node *H,int data,int pos);
extern dlist_node *dlist_delete(dlist_node *H,int pos);
#include "dlist.h"
dlist_node* dlist_create()
{
dlist_node*H = (dlist_node*)malloc(sizeof(dlist_node));
if(H == NULL)
{
printf("creat H error\n");
exit(-1);
}
H->prior = H;
H->next = H;
dlist_node *r = H;
int n;
while(1)
{
printf("input>>");
scanf("%d",&n);
getchar();
if(n == -1)
{
break;
}
dlist_node *new;
if((new = (dlist_node*)malloc(sizeof(dlist_node))) == NULL)
{
printf("create node error\n");
exit(-1);
}
new->data = n;
new->prior = r;
new->next = r->next;
H->prior = new;
r->next = new;
r = new;
}
return H;
}
void show_dlist(dlist_node *H)
{
dlist_node *p = H->next;
if(empty_dlist(H))
{
printf("empty");
return ;
}
while(p != H)
{
printf("%5d",p->data);
p = p->next;
}
putchar(10);
}
int empty_dlist(dlist_node *H)
{
return (H->next == NULL ? 1:0);
}
dlist_node *dlist_get(dlist_node *H,int pos)
{
if(pos < 0)
{
printf("pos < 0 ,pos < invailed\n");
return NULL;
}
dlist_node *p = H;
int i = -1;
while(i<pos)
{
i++;
p = p->next;
if(p == H)
{
printf("pos is invailed\n");
return NULL;
}
}
return p;
}
dlist_node *dlist_insert(dlist_node *H,int data,int pos)
{
if(pos < 0)
{
printf("pos < 0 ,pos < invailed\n");
return NULL;
}
dlist_node *p = H;
int i = -1;
while(i < pos)
{
i++;
p = p->next;
if(p == H)
{
printf("pos is invailed\n");
return NULL;
}
}
dlist_node *new;
if((new = (dlist_node*)malloc(sizeof(dlist_node))) == NULL)
{
printf("no menory");
return NULL;
}
new->data = data;
new->next = p;
new->prior = p->prior;
p->prior->next = new;
p->prior = new;
return H;
}
dlist_node *dlist_delete(dlist_node *H,int pos)
{
if(pos < 0)
{
printf("pos < 0 ,pos < invailed\n");
return NULL;
}
dlist_node *p = H;
int i = -1;
while(i < pos)
{
i++;
p = p->next;
if(p == H)
{
printf("pos is invailed\n");
return NULL;
}
}
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
return H;
}