**双向循环链表实现 **
**example **
typedef struct node{
int data;
struct node *prior;
struct node *next;
}distnode;
dlistnode *dlist_create();
void dlist_show(distnode *H);
dlistnode *dlist_get(distnode *H,int pos);
int dlist_insert(distnode *H,int value, int pos);
int dlist_delete(distnode *H, int pos);
dlistnode *dlist_create()
{
int n;
distnode *H, *r, *p;
if((H = (distnode *)malloc(sizeof(distnode))) == NULL)
{
printf("malloc failed\n");
return NULL;
}
H->prior = H;
H->next = H;
while(1)
{
printf("exit when input: -1\n");
scanf("%d",&n);
if(n == -1)
break;
p->data = n;
p->prior = r->next;
r->next = p;
H->prior = P;
r = p;
}
retuin H;
}
void dlist_show(distnode *H)
{
dlistnode *p;
p = H->next;
while(p != H)
{
printf("%d", p->data);
p = p->next;
}
}
dlistnode *dlist_get(distnode *H,int pos)
{
int i =0;
if(pos < 0)
{
printf("POS < 0,invalid!\n");
return NULL;
}
while(i< pos)
{
p = p->next;
i++;
if(p == H)
{
printf("pos is invaild\n");
return NULL;
}
}
return p;
}
int dlist_insert(distnode *H,int value, int pos)
{
distnode *p, *q;
p = dlist_get(H, pos);
if(p == NULL)
{
return -1;
}
if((q = (distnode *)malloc(sizeof(distnode))) == NULL){
printf("malloc error");
return -1;
}
q->data = value;
q->prior = p-prior;
q->next = p;
p->prior->next = q;
p->prior = q;
return 0;
}
int dlist_delete(distnode *H, int pos)
{
dlistnode *p;
p = dlist_get(H, pos);
if(p == NULL)
{
return -1;
}
p->prior->next = p->next;
p->next->prior = p->prior;
return 0;
}
int main (int argc, const char *argv)
{
/*遍历显示双向链表 */
dlistnode *H;
H = dlist_create();
dlist_show(H);
return 0;
#if 0
/*查找某位置的值 */
dlistnode *p;
while(1)
{
printf("input pos:");
scanf("%d",&pos);
p = dlist_get(H,&pos);
if(p)
{
printf("%d",p->data);
}
}
#endif
/*在某个位置前面插入固定值100,并在插入后打印链表所有值 */
while(1)
{
printf("input pos:");
scanf("%d",&pos);
dlist_insert(H,100, &pos);
dlist_show(H);
}
return 0;
}
**1、双向循环链表的创建 **
1、malloc内存;
2、prior next
3、return
dlistnode *dlist_create()
{
int n;
distnode *H, *r, *p;
if((H = (distnode *)malloc(sizeof(distnode))) == NULL)
{
printf("malloc failed\n");
return NULL;
}
H->prior = H;
H->next = H;
while(1)
{
printf("exit when input: -1\n");
scanf("%d",&n);
if(n == -1)
break;
p->data = n;
p->prior = r->next;
r->next = p;
H->prior = P;
r = p;
}
retuin H;
}
**2、双向循环链表遍历 **
void dlist_show(distnode *H)
{
dlistnode *p;
p = H->next;
while(p != H)
{
printf("%d", p->data);
p = p->next;
}
}
**3、双向循环链表的查找 **
dlistnode *dlist_get(distnode *H,int pos)
{
int i =0;
distnode *p = H;
if(pos < 0)
{
printf("pos < 0, invalid!\n");
return NULL;
}
while(i < pos)
{
p = p->next;
i++;
if(p == H)
{
printf("pos is invaild\n");
return NULL;
}
}
return p;
}
**4、双向循环链表的插入 **
int dlist_insert(distnode *H,int value, int pos)
{
distnode *p, *q;
p = dlist_get(H, pos);
if(p == NULL)
{
return -1;
}
if((q = (distnode *)malloc(sizeof(distnode))) == NULL){
printf("malloc error");
return -1;
}
q->data = value;
q->prior = p-prior;
q->next = p;
p->prior->next = q;
p->prior = q;
return 0;
}
**5、双向循环链表的删除 **
int dlist_delete(distnode *H, int pos)
{
dlistnode *p;
p = dlist_get(H, pos);
if(p == NULL)
{
return -1;
}
p->prior->next = p->next;
p->next->prior = p->prior;
return 0;
}