链表c++实现【学习笔记】

  1. Fundation knowledge
    characteristic:
    链表是一种由节点组成的数据结构,每个节点都包含某些信息以及指向链表中另一个节点的指针,用一个变量就能访问整个节点序列。
    【attention】链表并不是只能通过指针实现,方法有多种,但是最灵活的实现方法是通过指针
//***************************intSLList.h********************************/
#ifndef INTSLLIST_H_
#define INTSLLIST_H_

class IntSLLNode
{
public:
	IntSLLNode():next(0){};
	IntSLLNode(int el, IntSLLNode* ptr = 0) :info(el), next(ptr){};

public:	
	int info;
	IntSLLNode *next;
	//	friend class IntSLLList;
};

class IntSLLList
{
public:
	IntSLLList():head(0), tail(0){};
	void addToHead(int el);
	void addToTail(int el);
	int deleteFromHead();	//delete node and return its values
	int deleteFromTail();
	void deleteNode(int el);	
	bool isInList(int el);	//judge whether the node is in the linked list
private:
	IntSLLNode *head,*tail;
};

#endif
//********************************initSLList.cpp***********************************//
#include "intSLList.h"

void IntSLLList::addToHead(int el)
{
	head = new IntSLLNode(el,head);	//头部指针移位
	if (tail == 0)
	{
		tail = head;
	}
}

void IntSLLList::addToTail(int el)
{	
	if (tail == 0)
	{
		tail = new IntSLLNode(el);
		head = tail;
	}
	tail->next = new IntSLLNode(el);
	tail = tail->next;		//尾部指针移位
}

int IntSLLList::deleteFromHead()
{	
	int el = head->info;
	IntSLLNode *tmp = head->next; //create a temporary variable to save second node
	if (head == tail)
	{
		head = tail = 0;
	}
	else
	{
		delete head;	//here just delete data, the point name still exists;  
	}
	head = tmp;
	return el;
}

int IntSLLList::deleteFromTail()
{
	int el = tail->info;
	if (tail == head)
	{
		tail = head = 0;
	}
	else
	{
		for (IntSLLNode *i = head; i != tail;i=i->next)
		{
			if (i->next == tail)
			{	
				IntSLLNode *tmp = tail;
				tail = i;	//tail points to second last node
				delete tmp;	//delete last node
			}
		}
	}
	return el;
}

void IntSLLList::deleteNode(int el)
{
	if (head!=0)
	{
		if (tail == head && head->info == el)	//linked list only has one node and is be selected
		{
			delete head;
			head = tail = 0;
		}
		else
		{
			IntSLLNode *pred;
			for (pred = head; pred != tail;pred=pred->next)
			{
				if (pred->info == el && pred == head)		//first node
				{
					IntSLLNode *tmp = head->next;
					delete head;
					head = tmp;
				}
				else if (pred->next->info==el && pred->next == tail)	//last node
				{
					tail = pred;
					delete tail->next;
					tail->next = 0;
				}
				else
				{
					if (pred->next->info == el)		//node among the list
					{
						IntSLLNode *tmp = pred->next->next;
						delete pred->next;
						pred->next = tmp;
					}
				}
			}
		}
	}
}

bool IntSLLList::isInList(int el)
{
	IntSLLNode *tmp = head;
	for (tmp; tmp != tail->next; tmp = tmp->next)
	{
		if (tmp->info == el)
		{
			return true;
		}
	}
	return false;
}
//******************************main.cpp*****************************************//
#include <iostream>
#include "intSLList.h"
using namespace std;

int main()
{	
	
	//primary realization single linked list 
	IntSLLNode *head = new IntSLLNode(10);
	head->next = new IntSLLNode(20);
	head->next->next = new IntSLLNode(30);
	head->next->next->next = new IntSLLNode(40);

	//create a new class to operate single linked list
	IntSLLList my_list;
	my_list.addToHead(20);
	my_list.addToHead(10);
	my_list.addToTail(30);
	my_list.addToTail(40);
	my_list.deleteNode(30);
	if (my_list.isInList(40))
	{
		cout << "数据存在" << endl;
	}
	else
	{
		cout << "数据不存在" << endl;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值