循环双向链表的表尾插入操作问题,问题已经注释在程序中了,哪位高手帮我解答,谢谢。
我感觉逻辑上是没有问题啊,哪位高手帮我解答一下,那四句和注释的7句是等价的啊????
typedef int data_type;
typedef struct double_listnode {
data_type data;
struct double_listnode *prior, *next;
}double_listnode_t, *double_listnode_p;
static double_listnode_t *init_double_list(int n)
{
int i;
double_listnode_p head, s;
double_listnode_p news, prev, next; //在表尾插入
head = (double_listnode_t *)malloc(sizeof(double_listnode_t));
head -> prior = head -> next = head;
for (i = 0; i < n; i++) {
s = (double_listnode_t *)malloc(sizeof(double_listnode_t));
s -> data = i + 1;
//news = s; //这7句在表尾插入 news为插入结点
//prev = head->prior; //prev为插入结点的前一个结点
//next = head; //next为插入结点的后一个结点
//next->prior = news;
//news->next = next;
//news->prior = prev;
//prev->next = news;
head->prior = s;//这个4句和上面7句等价,但是这边会出错??上面的7句是模仿内核链表来写的。
s->next = head;
s->prior = head->prior;
head->prior->next = s;
}
return head;
}
[解决办法]
head->prior = s
s->next = head;
s->prior = head->prior;//这个时候head->prior已经是s了
head->prior->next = s;
[解决办法]
s->prior = head->prior
s->next = head //先把s添加到链表中
head->prior->next = s
head->prior = s
上面的应该是可行的
[解决办法]
head->prior = s;
s->next = head;
s->prior = head->next;
head->next->next = s;
[解决办法]
head->prior = s;
s->next = head;
s->prior = head->next;
head->next->next = s;
这样就可以啦.