单链表

#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST

//for nodes of the list
class IntSLLNode
{
public:  //all members of IntSLLNode are declared public,in order to accessible through pointers,
	IntSLLNode(){
		next=0;
	}
	IntSLLNode(int el, IntSLLNode *ptr = 0){
		info = el; next = ptr;
	}
	int info;
	IntSLLNode *next;
};

//for access to the list
class IntSLList{
public:
	IntSLList(){
		head = tail = 0;
	}
	~IntSLList();
	int isEmpty(){
		return head == 0;
	}

	void addToHead(int);
	void addToTail(int);
	int deleteFromHead();
	int deleteFromTail();
	void deleteNode(int);
	bool isInList(int) const;
	void print();
private:
	IntSLLNode *head, *tail;
};
#endif

#include <iostream>
#include "intSLList.h"

using namespace std;

IntSLList::~IntSLList(){
	for (IntSLLNode *p; !isEmpty();)
	{
		p = head->next;
		delete head;
		head = p;
	}
}

void IntSLList::addToHead(int el){
	head = new IntSLLNode(el,head);
	if(tail == 0)
		tail = head;
}

//void IntSLList::addToTail(int el)
//{
//	if(tail != 0){
//		tail ->next = new IntSLLNode(el);
//		tail = tail->next;
//	}
//	else head = tail = new IntSLLNode(el);
//}
void IntSLList::addToTail(int el){
	if(isEmpty()){
		tail = new IntSLLNode(el);
		tail->next=tail;
	}
	else{
		tail->next = new IntSLLNode(el, tail->next);
		tail = tail ->next;
	}
}


bool IntSLList::isInList(int el) const{
	IntSLLNode *tmp;
	for(tmp = head; tmp != 0 && !(tmp->info ==el);tmp = tmp ->next);
	return tmp !=0;
}
void IntSLList::print(){
	IntSLLNode *tmp = head;
	while(0!=tmp){
		cout<<tmp->info<<" ,";
		tmp = tmp ->next;
	}
	cout<<endl;
}

int main()
{
	IntSLList *list = new IntSLList();
	list->addToHead(1);
	list->addToHead(2);
	list->addToHead(3);
	list->addToTail(5);
	list->addToTail(7);
	list->print();

	system("pause");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值