typedef struct dnode
{
int data;
struct dnode *prior;
struct dnode *next;
}DNode;
/*在所指定位置之前插入*/
void before_insert(DNode *head, int position, int value)
{
DNode *p = head;
DNode *q = NULL;
int i = 0;
while(p && i++ < position - 1)
p = p->next;
if((q = (DNode *)malloc(sizeof(DNode)))==NULL)
{
printf("分配内存失败\n");
exit(0);
}
q->data = value;
q->next = p->next;
p->next = q;
p->next->prior = q;
q->prior = p;
}
/*在所指定位置之后插入*/
void behind_insert(DNode *head, int position, int value)
{
DNode *p = head;
DNode *q = NULL;
int i = 0;
while(p && i++ < position)
p = p->next;
if((q = (DNode *)malloc(sizeof(DNode)))==NULL)
{
printf("分配内存失败\n");
exit(0);
}
q->data = value;
q->next = p->next;
p->next = q;
p->next->prior = q;
q->prior = p;
}
阅读(951) | 评论(1) | 转发(0) |