首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

一个单链表有关问题

2012-04-08 
一个单链表问题Howtowritecodetodeleteaspecificnodeinasinglelinklist(单链表)thattakesO(1)time?Thatis,

一个单链表问题
How   to   write   code   to   delete   a   specific   node   in   a   single   link   list   (单链表)   that   takes   O(1)   time?   That   is,   the   time   deleting   a   node   is   the   same   (independent   from   the   length   of   the   list.)   Link   list   uses   pointers,
not   hash.   Input   is   a   pointer   of   the   deleting   node.   Show   your   algorithm   with   pseudo   code.   Hint:   just   3   steps.
  我回复了   q   =   p;
                  p   =   p-> next;
                    free(q);
他又回复说can   you   rework   #7?   a   node   in   single   link   list   has   two   fields:   data,   pointer   (pointing   to   the   next   node).
our   answer   "p   =   p-> next;   "   should   be   more   specific.
请高手帮忙.
问题的关键是不是怎么找出指向p指针的前一个节点的指针?
 


[解决办法]
就是用p的下一个节点(p-> next())的覆盖当前要删除的节点的data并完成链表的连接,然后删除当前节点p的下一个节点.
即:
q = p-> next();
p-> data = q-> data;
p-> next() = q-> next();
delete q;

热点排行