编程之美 - 无头链表删除节点
2016-08-10 09:05
274 查看
问题描述:
一个没有头指针的链表,从其中删除任意一个中间节点(非第一个,也不是最后一个)。
思路:
没有头指针,所以无法使用遍历的方式找到目标节点的上一个节点。但可以使用的是目标节点的下一个节点。
因为当前节点是要被删除的,所以当前节点的值已经不需要被保存了,可以直接用下一个节点的值将其替换。
然后,直接删除下一个节点即可。
代码:
void delete_node(stnode* &target)
{
stnode *p = NULL;
if (NULL == target)
return;
if (NULL == target->_next)
{
cout << "it's last node will not delete" << endl;
return;
}
p = target;
target = target->_next;
p->_data = p->_next->_data;
p->_next = p->_next->_next;
delete target;
target = NULL;
}
扩展问题:
给一个链表和它的头指针,只遍历一次,将链表反转。
思路:
将链表反转,考虑将指针的指向反转了即可,然后将头指针指向原来的最后一个节点即可。
例如:
1->2->3->4->NULL
将指针的指向反转
NULL<-1<-2<-3<-4
代码:
void swap_nodes(stnode* &head)
{
stnode *p1 = NULL;
stnode *p2 = NULL;
stnode *p3 = NULL;
if ((NULL == head) || (NULL == head->_next))
return;
p1 = head;
p2 = p1->_next;
p1->_next = NULL;
while (p2 != NULL)
{
p3 = p2->_next;
p2->_next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
}
完整代码:
#include <iostream>
using namespace std;
typedef struct _stnode
{
int _data;
_stnode *_next;
} stnode;
void printlink(stnode* link)
{
stnode *p = link;
if (p == NULL)
{
cout << "NULL" << endl;
}
while (p != NULL)
{
cout << p->_data << " -> ";
p = p->_next;
}
cout << "NULL" <<endl<<endl;
}
void delete_node(stnode* &target)
{
stnode *p = NULL;
if (NULL == target)
return;
if (NULL == target->_next)
{
cout << "it's last node will not delete" << endl;
return;
}
p = target;
target = target->_next;
p->_data = p->_next->_data;
p->_next = p->_next->_next;
delete target;
target = NULL;
}
void swap_nodes(stnode* &head)
{
stnode *p1 = NULL;
stnode *p2 = NULL;
stnode *p3 = NULL;
if ((NULL == head) || (NULL == head->_next))
return;
p1 = head;
p2 = p1->_next;
p1->_next = NULL;
while (p2 != NULL)
{
p3 = p2->_next;
p2->_next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
}
void main()
{
int i = 0, test = 0;
int arr[] = {1,2,3,4,5,6,7,8,9,10,};
int len = sizeof(arr)/sizeof(arr[0]);
stnode *ptest = NULL;
stnode *pnew = NULL;
stnode *phead = NULL;
for (i = 0; i < len; i++)
{
pnew = new stnode;
pnew->_data = arr[i];
pnew->_next = NULL;
if (i == 0)
{
phead = pnew;
ptest = pnew;
}
else
{
ptest->_next = pnew;
ptest = pnew;
}
}
cout << "Initial link ================" << endl;
printlink(phead);
cout << endl;
test = 6;
ptest = phead;
for(i = 0; i < test-1; i++)
ptest = ptest->_next;
delete_node(ptest);
cout << "After deleting node ================" << endl;
printlink(phead);
cout << endl;
cout << "\n\nSwap Links ================" << endl;
swap_nodes(phead);
printlink(phead);
ptest = phead;
while(ptest != NULL)
{
pnew = ptest;
ptest = ptest->_next;
delete pnew;
pnew = NULL;
}
cin >> i;
}
一个没有头指针的链表,从其中删除任意一个中间节点(非第一个,也不是最后一个)。
思路:
没有头指针,所以无法使用遍历的方式找到目标节点的上一个节点。但可以使用的是目标节点的下一个节点。
因为当前节点是要被删除的,所以当前节点的值已经不需要被保存了,可以直接用下一个节点的值将其替换。
然后,直接删除下一个节点即可。
代码:
void delete_node(stnode* &target)
{
stnode *p = NULL;
if (NULL == target)
return;
if (NULL == target->_next)
{
cout << "it's last node will not delete" << endl;
return;
}
p = target;
target = target->_next;
p->_data = p->_next->_data;
p->_next = p->_next->_next;
delete target;
target = NULL;
}
扩展问题:
给一个链表和它的头指针,只遍历一次,将链表反转。
思路:
将链表反转,考虑将指针的指向反转了即可,然后将头指针指向原来的最后一个节点即可。
例如:
1->2->3->4->NULL
将指针的指向反转
NULL<-1<-2<-3<-4
代码:
void swap_nodes(stnode* &head)
{
stnode *p1 = NULL;
stnode *p2 = NULL;
stnode *p3 = NULL;
if ((NULL == head) || (NULL == head->_next))
return;
p1 = head;
p2 = p1->_next;
p1->_next = NULL;
while (p2 != NULL)
{
p3 = p2->_next;
p2->_next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
}
完整代码:
#include <iostream>
using namespace std;
typedef struct _stnode
{
int _data;
_stnode *_next;
} stnode;
void printlink(stnode* link)
{
stnode *p = link;
if (p == NULL)
{
cout << "NULL" << endl;
}
while (p != NULL)
{
cout << p->_data << " -> ";
p = p->_next;
}
cout << "NULL" <<endl<<endl;
}
void delete_node(stnode* &target)
{
stnode *p = NULL;
if (NULL == target)
return;
if (NULL == target->_next)
{
cout << "it's last node will not delete" << endl;
return;
}
p = target;
target = target->_next;
p->_data = p->_next->_data;
p->_next = p->_next->_next;
delete target;
target = NULL;
}
void swap_nodes(stnode* &head)
{
stnode *p1 = NULL;
stnode *p2 = NULL;
stnode *p3 = NULL;
if ((NULL == head) || (NULL == head->_next))
return;
p1 = head;
p2 = p1->_next;
p1->_next = NULL;
while (p2 != NULL)
{
p3 = p2->_next;
p2->_next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
}
void main()
{
int i = 0, test = 0;
int arr[] = {1,2,3,4,5,6,7,8,9,10,};
int len = sizeof(arr)/sizeof(arr[0]);
stnode *ptest = NULL;
stnode *pnew = NULL;
stnode *phead = NULL;
for (i = 0; i < len; i++)
{
pnew = new stnode;
pnew->_data = arr[i];
pnew->_next = NULL;
if (i == 0)
{
phead = pnew;
ptest = pnew;
}
else
{
ptest->_next = pnew;
ptest = pnew;
}
}
cout << "Initial link ================" << endl;
printlink(phead);
cout << endl;
test = 6;
ptest = phead;
for(i = 0; i < test-1; i++)
ptest = ptest->_next;
delete_node(ptest);
cout << "After deleting node ================" << endl;
printlink(phead);
cout << endl;
cout << "\n\nSwap Links ================" << endl;
swap_nodes(phead);
printlink(phead);
ptest = phead;
while(ptest != NULL)
{
pnew = ptest;
ptest = ptest->_next;
delete pnew;
pnew = NULL;
}
cin >> i;
}
相关文章推荐
- 编程之美-3.4-从无头单链表中删除节点-单链表逆置
- 读书笔记之编程之美 - 3.4 从无头链表中删除节点
- 编程之美-从无头单链表中删除节点方法整理
- [编程之美] PSet3.4 从无头单链表中删除节点
- 编程之美--从无头单链表中删除节点
- 编程之美3.4 从无头单链表中删除节点
- 编程之美-从无头单链表中删除节点
- 从无头单链表中删除节点--扩展:反转单链表
- 巧妙删除无头单链表中的节点(算法中的“狸猫换太子”)
- 从无头单链表中删除节点
- 从无头单链表中删除节点及单链表的反转操作
- 从无头单链表中删除节点
- 单链表应用——从无头单链表中删除节点
- 从无头单链表中删除节点及单链表的反转操作
- 从无头单链表中删除节点
- 从无头单链表中删除节点
- 从无头单链表中删除节点
- 一个单向链表,只知道某个节点的指针p,但是p不是尾节点,请编程删除节点p.
- 【编程之美】读书笔记:从无头单链表中删除结点
- 从无头单链表中删除节点