在无头节点的单链表里删除元素

在无头节点的单链表里删除元素


个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:在无头节点的链表里删除元素;

博客时间:2014-4-15;

编程语言:C++ ;

编程坏境:Windows 7 专业版 x64;

编程工具:vs2008 32位编译器;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;


mine words

静净精镜. In the world, no patience and no road.

problem

无头节点的单链表里删除节点


solution

对于此指针 p ,分为两种情况

要么是尾节点,要么不是

1:p is the end of list

so, we need travel the list again, and find its left node p2. Then free the end form p2;

time cost: O(list.length)

process follows


t

2: p is not the end of list

so, copy its right node(we make it called p2) key and next point to p, and delete p2;

time cost: O(1)


experiment

picture follows



code

test.cpp

#include<iostream>
using namespace std;


template<typename T>
class Node
{
public:
	T data;
	Node<T>* next;
	Node()
	{
		data = NULL;
	    next = NULL;
	}
};


// function: make list
// input: an array
// output: a list
template<typename T>
Node<T>* _Make_list(T * D,size_t size);

// function: delete the key from list
// input: a no_head list and the key
// output: a bool
template<typename T>
bool _Delete_node_from_Nohead_list(Node<T> * & L,Node<T>* key);

// function: output list
// input: a list
// output: void
template<typename T>
void _Output_list(Node<T>* L);

// function: find the key 
// input: a list and the key
// output: the key node point or NULL
template<typename T>
Node<T>* _Search_from_list(Node<T> *& L,T key);



int main()
{
	size_t size = 10;
	int * D = new int[size];
	cout<<"data follows"<<endl;
	for(size_t i =0;i<size;i++ )
	{
		D[i] = rand();
		cout<<D[i]<<endl;
	}

	Node<int>* L = _Make_list(D,size);
	int key ;
	Node<int> *p;
	cout<<"the key you want delete"<<endl;
	while(cin>>key)
	{
		//_Output_list(L);
		p = _Search_from_list(L,key);
		cout<<"find key = "<<p->data<<endl;
		_Delete_node_from_Nohead_list(L,p);
		_Output_list(L);
		cout<<"the key you want delete"<<endl;
	}
	system("pause");
	return 0;
}

// function: delete the key from list
// input: a no_head list and the key
// output: a bool
template<typename T>
bool _Delete_node_from_Nohead_list(Node<T> * & L,Node<T> * key)
{
	if(L != NULL)
	{
		// first case: key is not the end of list
		if(key != NULL && key->next != NULL)
		{
			Node<T> * p = key->next;
			key->next = p->next;
			key->data = p->data;
			delete p;
		}
		// second case: key is the end of list
		else if(key != NULL && key->next == NULL)
		{
			Node<T>* p = L;
			while(p->next != NULL && p->next != key)
			{
				p = p->next;
			}
			if(p->next == NULL)
				return false;
			else if(p->next == key)
			{
				p->next = NULL;
				delete key;
			}
		}
	}
	else return false;
}

// function: make list
// input: an array
// output: a list
template<typename T>
Node<T>* _Make_list(T * D,size_t size)
{
	if(D != NULL && size >0)
	{
		Node<T> * L,*p = new Node<T> ;
		p->data = D[0];
		L = p;
		for(size_t i = 1;i < size; i++)
		{
			p->next = new Node<T> ;
			p = p->next;
			p -> data = D[i];
		}
		return L;
	}
	else return NULL;
}

// function: output list
// input: a list
// output: void
template<typename T>
void _Output_list(Node<T>* L)
{
	Node<T>* p = L;
	cout<<"list follows"<<endl;
	while( p != NULL )
	{
		cout<< p->data <<endl;
		p = p->next;
	}
	cout<<"list over"<<endl;
}

// function: find the key 
// input: a list and the key
// output: the key node point or NULL
template<typename T>
Node<T>* _Search_from_list(Node<T> *& L,T key)
{
	if(L != NULL)
	{
		Node<T> * p = L;

		while(p != NULL && p->data != key)
		{
			p = p->next ;
		}
		if(p == NULL)
		{
			return NULL;
		}
		else 
		{
			return p;
		}
	}
	else return NULL;
}


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值