[面试准备】数据结构--链表

#include <iostream>
using namespace std;

//链表节点定义
template <class T>
struct SLNode{
	T data;
	SLNode<T>* next;

	SLNode(SLNode* nextnode = NULL){
		next = nextnode;
	}

	SLNode(const T& item,SLNode* nextnode = NULL){
		data = item;
		next = nextnode;
	}
};

//链表类定义
template<class T>
class SLList{
private:
	SLNode<T>* head;
	int length;
public:
	SLList(){
		head = new SLNode<T> ();
	}

	~SLList();

	bool Isempty(){//判断链表是否为空
		return head==NULL;
	}

	int Length(){//返回链表的长度
		return length;
	}
	bool Find(int k,T& item );//将链表第K个节点的值赋給item
	int Search(const T& item);//在链表中查找值为item的节点并返回在表中的位置
	void Insert(int k,T item);//在第K个节点后插入值为item的节点
	void Delete(int k,T& item);//删除链表中第K个节点,并将其值赋給item
	void Show();
};

template<class T>
bool SLList<T> :: Find(int k , T& item){
	if(k<0){
		cout<<"存取位置不合法!"<<endl;
		return  false;
	}
	SLNode<T>* p = head;
	int i = 0;
	while(p!=NULL&& i<k-1){
		p=p->next;
		++i;
	}
	if(p==NULL){
		cout<<"无此节点!"<<endl;
		return false;
	}
	item = p->data;
	return true;
}

template<class T>
int SLList<T> :: Search(const T& item){
	SLNode<T>* p = head;
	int i = 0;
	while(p!=NULL&&p->data!=item){
		p = p->next;
		++i;
	}
	if(p==NULL){
		cout<<"无此节点!"<<endl;
		return -1;
	}
	return i;
}

template<class T>
void SLList<T> :: Delete(int k,T&item){
	SLNode<T>* p = head,*q;
	int  i = 0;
	if(k<0){
		cout<<"删除不合法!"<<endl;
		return ;
	}
	while(p!=NULL&&i<k-2){
		p = p->next;
		++i;
	}
	if(p->next==NULL){
		cout<<"无此节点!"<<endl;
		return ;
	}
	q = p->next;
	p->next = q->next;
	item = q->data;
	--length;
	delete q;
}

template<class T>
void SLList<T> :: Insert(int k,T item){
	if(k<0){
		cout<<"插入位置不合法!"<<endl;
		return ;
	}
	SLNode<T> *p,*q;
	int i = 0;
	if(k == 0 ){
		head->data = item;
		head->next = NULL;
		length++;
		return ;
	}
	p = head ;
	if(k == 1){
		SLNode<T>* s = new SLNode<T>(item);
		p->next = s;
		length++;
		return ;
	}
	while(p!=NULL&&i<k-1){
		p = p->next;
		++i;
	}
	if(p == NULL){
		cout<<"插入位置不合法!"<<endl;
		return ;
	}
	SLNode<T>* s = new SLNode<T>(item);
	q = p->next;
	p->next = s;
	s->next = q;
	++length;
	return ;
}

template<class T>
void SLList<T> :: Show(){
	SLNode<T>* p;
	if(head == NULL){
		cout<<"链表为空!"<<endl;
		return ;
	}
	p = head;
	while(p!=NULL){
		if(p->next == NULL){
			cout<<p->data<<endl;
			return ;
		}
		cout<<p->data<<"->";
		p = p->next;
	}
	return ;
}

template<class T>
SLList<T> :: ~SLList(){
	SLNode* p = head;
	SLNode* q = p;
	while(p!=NULL){
		p = p->next;
		delete q;
	}
}

int main() {
	SLList<int>* sllist = new SLList<int>();
	for(int i = 0;i< 10; i++){
		sllist->Insert(i,10-i);
	}
	sllist->Show();
	int a = sllist->Search(2);
	cout<<a<<endl;
	cout<<sllist->Length()<<endl;
	sllist->Find(2,a);
	cout<<a<<endl;
	sllist->Delete(3,a);
	cout<<a<<endl<<sllist->Length()<<endl;
	sllist->Show();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值