用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

单链表

2012-10-18 作者: LeiZhen举报

[c++]代码库

# include <iostream>
using namespace std;
typedef char ElemType;

typedef struct Node
{	// 结构体节点
	ElemType data;
	struct Node *next;
}LinkList;	

class MyList
{
private:
	LinkList* L;
public:
	bool InitList()	// 初始化链表空间
	{	
		L = new LinkList;	// 创建头指针
		if ( NULL == L )
		{
			cout << "Not Have Enough Memory!";
			return false;
		}
		L->next = NULL;
		return true;
	}
	bool CreatList( int site, ElemType e )	// 创建链表
	{
		int i = 0;
		LinkList* p = L;
		while ( i<site-1 && p!=NULL )
		{
			i ++;
			p = p->next;
		}
		if ( NULL == p )
			return false;
		else
		{
			LinkList*q = new LinkList;
			q->data = e;
			p->next = q;
			p = q;
			q->next = NULL;
		}
		return true;
	}
	void DispList()	// 输出顺序表
	{
		LinkList* p = L->next;
		while ( p!=NULL )
		{
			cout << p->data << ' ';
			p = p->next;
		}
		cout << endl;
	}
	int ListLength() // 求表长
	{
		int i = 0;
		LinkList* p = L->next;
		while ( p!=NULL )
		{
			i ++;
			p = p->next;
		}
		return i;
	}		
	bool ListEmpty()	// 判断是否为空表 
	{
		return NULL == L->next;
	}	
	bool GetElem( int site, ElemType &e )	// 求某位置上的元素
	{	
		int i =0;
		LinkList* p = L;
		while ( i<site && p!=NULL )
		{
			i ++;
			p = p->next;
		}
		if ( NULL == p || 0 == site)
			return false;
		else
		{
			e = p->data;
			return true;
		}
	} 
	bool LocateElem( int& site, ElemType e )	// 求元素的位置
	{
		int i = 1;
		LinkList* p = L->next;
		while ( p!=NULL && p->data!=e )
		{
			i ++;
			p = p->next;
		}
		if ( NULL == p )
			return false;
		else
			site = i;
		return true;
	}	
	bool ListInsert( int site, ElemType e )		// 插入元素
	{
		int i = 0;
		LinkList* p = L;
		while ( i<site-1 && p!=NULL )
		{
			i ++;
			p = p->next;
		}
		if ( NULL == p )
			return false;
		else
		{
			LinkList*q = new LinkList;
			q->data = e;
			q->next = p->next;
			p->next = q;

			return true;
		}
	}   
	bool ListDelete( int site, ElemType &e )	// 删除元素
	{
		int i = 0;
		LinkList* p = L;
		while ( i<site-1 && p!=NULL )
		{
			i++;
			p = p->next;
		}
		if ( NULL == p )
			return false;
		else
		{
			LinkList* q = p->next;
			if ( NULL == q )
				return false;
			e = q->data;
			p->next = q->next;
			delete q;

			return true;
		}
	}
	void DestoryList()	// 清空链表
	{ 
		LinkList*p, *q;
		p = L;
		while ( p!=NULL ) 
		{
			q = p->next;
			delete p;
			p = q;
		}
	}	
};

int main (void)
{
	MyList h;	

	cout << ">>>>>>>>> InitList Operation <<<<<<<<\n";
	h.InitList();	// 初始化链表
	int temp;
	ElemType e;
	cout << "Input The LinkList Length You Want To Creat : ";
	cin >> temp;
	cout << "Input Element : \n";
	cout << ">>>>>>>> CreatList Operation <<<<<<<\n";
	for ( int i=1;i <= temp;++i )
	{
		printf("No.%d : ", i);
		cin >> e;
		if ( h.CreatList( i, e ) ) 		// 创建链表
			printf("Creat No.%d element success!\n", i);
		else
			printf("Creat No.%d element failed!\n", i);
		cin.sync();		// 清除缓冲区
	}
	cout << "Now LinkList is : \n";
	cout << ">>>>>>>>> DispList Operation <<<<<<<<\n";
	h.DispList();	// 输出链表

	cout << ">>>>>>>> ListLength Operation <<<<<<<\n";
	printf("LinkList Length Is %d.\n", h.ListLength());	// 输出表长

	cout << ">>>>>>>> ListEmpty Operation <<<<<<<<\n";
	if ( h.ListEmpty() )	// 判断是否是空表
		cout << "LinkList Is Empty!\n";
	else
		cout << "Not a Empty LinkList!\n";

	cout << ">>>>>>>>> GetElem Operation <<<<<<<<<\n";
	cout << "Input Site To Check Element : ";
	cin >> temp;
	if ( h.GetElem( temp, e ) )		// 用 e 返回链表中第 temp 个元素
		printf("No.%d Element Is %c.\n", temp, e);
	else
		cout << "Overflow The LinkList!\n";

	cout << ">>>>>>>> LocateElem Operation <<<<<<<<\n";	
	cout << "Input Element To Check Site : ";
	cin >> e;
	if ( h.LocateElem( temp, e ) )		// 输入元素查看元素所在的位置
		printf("%c Exist In No.%d.\n", e, temp);
	else
		printf("Can't Find %c!\n", e);

	cout << ">>>>>>>> ListInsert Operation <<<<<<<<\n";
	cout << "Input Site : ";
	cin >> temp;
	cout << "Input Element : ";
	cin >> e;
	if ( h.ListInsert( temp, e ) )	// 在 temp 位置插入 e
	{
		cout << "Insert Success!\nNow LinkList Is : ";
		h.DispList();
	}
	else
		cout << "Insert Failed!\n";

	cout << ">>>>>>>> ListDelete Operation <<<<<<<<\n";
	cout << "Input Delete Site : ";
	cin >> temp;
	if ( h.ListDelete( temp, e ) )	// 删除 temp 位置的元素
	{
		printf("Delete %c Success!\nNow LinkList Is : ", e);
		h.DispList();
	}
	else
		cout << "Delete Failed!\n";

	h.DestoryList(); 	// 销毁链表

	return 0;
}

[代码运行效果截图]


单链表


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...