单向链表基础

单向升序链表的创建,结点插入,删除,逆转,合并

#include <iostream>
#include <cstdio>
using namespace std;

template <typename Type>
class Listline{
public:
	typedef struct NODE{
		NODE(){next = NULL;}
		Type val;
		NODE *next;
	}Node;
	Listline():head(NULL), length(0){}
	~Listline(){
		Node *cur = head;
		while(cur){
			Node *tmp = cur->next;
			delete cur;
			cur = tmp;
		}
	}
	void Insert(const Type &val);
	void Delete(const Type &val);
	void Reverse();
	void Union(Listline &);
	void Display() const;
	int Size() const {return length;}
private:
	Node *head;
	int length;
};

template <typename Type>
void Listline<Type>::
Insert(const Type &val){ // 升序插入
	Node **link = &head;
	Node *cur;
	while((cur = *link) != NULL && val > cur->val)
		link = &cur->next;
	Node *new_node = new Node;
	new_node->val = val;
	new_node->next = cur;
	*link = new_node;
	length++;
}

template <typename Type>
void Listline<Type>::
Delete(const Type &val){ // 删除链表中的第一个 val
	Node **link = &head;
	Node *cur;
	while((cur = *link) != NULL && val != cur->val)
		link = &cur->next;
	if(cur){
		*link = cur->next;
		delete cur;
		length--;
	}
}

template <typename Type>
void Listline<Type>::
Reverse(){ // 反转链表
	Node *cur = head->next;
	Node *per = head;
	while(cur){
		per->next = cur->next;
		cur->next = head;
		head = cur;
		cur = per->next;
	}
}

template <typename Type>
void Listline<Type>::
Union(Listline &li){ // 合并升序链表
	Node *cur1 = this->head;
	Node *cur2 = li.head;

	Node *new_head = cur1 ? cur1 : cur2;
	if(new_head == NULL)
		return;
	Node **link = &new_head;

	while(cur1 && cur2){
		if(cur1->val < cur2->val){
			*link = cur1;
			link = &cur1->next;
			cur1 = cur1->next;
			if(cur1 == NULL){
				*link = cur2;
				break;
			}
		}
		else{
			*link = cur2;
			link = &cur2->next;
			cur2 = cur2->next;
			if(cur2 == NULL){
				*link = cur1;
				break;
			}
		}
	}
	this->head = new_head;
	this->length += li.length;
	li.head = NULL;
}

template <typename Type>
void Listline<Type>::
Display() const{
	Node *cur = head;
	while(cur){
		cout << cur->val << ' ';
		cur = cur->next;
	}
	cout << endl;
}

void separate(){
	cout << "---------------------------------" << endl;
}

void test(){
	Listline<int> li1;
	Listline<int> li2;
	const int na = 8;
	const int nb = 6;
	int a[na] = {4, 2, -6, 0, 1, 5, 9, 7};
	int b[nb] = {3, -9, -3, 19, 90, 1};
	int c[na+nb];
	int clen = 0;

	for(int i = 0; i < na; i++){
		li1.Insert(a[i]);
		c[clen++] = a[i];
	}
	for(int i = 0; i < nb; i++){
		li2.Insert(b[i]);
		c[clen++] = b[i];
	}

	li1.Display();
	li2.Display();
	li1.Union(li2);
	li1.Display();
	separate();

	for(int i = 0; i < clen; i++){
		li1.Display();
		li1.Reverse();
		li1.Display();
		li1.Delete(c[i]);
		separate();
	}
}

int main()
{
	test();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值