我正在研究c中的双向链表,我有一个带有20个节点的双重链接tepm2,我想删除用户插入单词的节点。
struct node {
struct node *prev;
char word[100];
int repeatTime;
struct node *next;
} *h, *temp, *temp1, *temp2;
每个节点都有唯一的单词。
printf("\n Enter word to delete : ");
scanf("%s", &word);
Delete(word);
int delete(char data[200]) { //unable to delete
if (h == NULL)
return;
temp2 = next = previous = h;
while (temp2->next != NULL) {
if ((strcmp(temp2->word, data) == 0)) {
if (temp2->prev == NULL) {
h = temp2->next;
free(temp2);
return;
} else if (temp2->prev == NULL) {
previous->next = temp2;
free(temp2);
previous->next = NULL;
return;
} else {
previous->next = temp2->next;
next->prev = temp2->next;
}
}
temp2 = temp->next;
}
}
我无法删除用户输入的特定节点
答案 0 :(得分:0)
试试这个:
class Page {
private $TEMPLATE_PATH = '/application/templates/';
private $VIEW_PATH = '/application/views/';
protected $header;
protected $footer;
protected $body;
public function __construct($header_file, $body_file, $footer_file) {
$this->header = $this->TEMPLATE_PATH . $header_file;
$this->body = $this->VIEW_PATH . $body_file;
$this->footer = $this->TEMPLATE_PATH . $footer_file;
}
public function render(){
$page = [$this->header, $this->body, $this->footer];
foreach($page as $file) {
require_once($file);
}
}
}
答案 1 :(得分:0)
首先,我不认为这是正确的temp2 = next = previous = h;
现在你所要做的就是通过遍历找到你要删除的节点,然后将它的prev节点链接到它的下一个节点即(temp2-> prev) - > next = next和(temp2-> next) - > prev = prev并释放它。
现在真正的问题在于 1.第一个节点后面有其他节点 2.在其前面有其他节点的最后一个节点 3.仅节点
您可以通过将它们转换为前一个问题(即我们刚刚解决的中间问题中的节点)来简化这三个问题。
为简化起见,你可以将头部和尾部都设为NULL。