现在的位置: 首页 > 综合 > 正文

C++链表

2013年08月22日 ⁄ 综合 ⁄ 共 3430字 ⁄ 字号 评论关闭

之前看过一些C语言版数据结构,正好开学也要学,就试着用C++写了一下,其实也就是一点语法的稍微变动。功能可能不全,代码也可能不完善,仅供参考。

#include <iostream>
using namespace std;

class Node
{
public:
	int data;
	Node* next;
};

//带头节点的后插法
Node* create()
{
	Node* head = new Node;
	
	Node* first;
	Node* tail;

	head->next = NULL;
	tail = head;
	int x;
	cin>>x;
	while(x != 8888)
	{
		first = new Node;
		first->data = x;
		first->next = NULL;
		tail->next = first;
		tail = first;
		cin>>x;
	}
	return head;
}

void showList(Node* head)
{
	Node* nd;
	if(head == NULL)
	{
		cout<<"该表为空表。\n";
	}
	else
	{
		nd = head->next;
		while(nd)
		{
			cout<<nd->data<<" ";
			nd = nd->next;
		}
		cout<<endl;
	}
}

//查找(按序号)
void searchByCapcity(Node* head, int n)
{
	Node* p = head->next;
	int j = 1;
	while(p && j < n)
	{
		p = p->next;
		++j;
	}
	if(! p || j > n)
	{
		cout<<"您输入的序号不再范围内\n";
		return ;
	}
	cout<<"您查找的第"<<n<<"个元素为:"<<p->data<<endl;
}

//查找(按值)
void searchByValue(Node* head, int e)
{
	Node* p = head->next;int j = 1;
	while(p && p->data != e)
	{
		p = p->next;
		++j;
	}
	if(! p || p == NULL)
	{
		cout<<"该值不在表中\n";
		return ;
	}
	cout<<"您查找的"<<e<<"是表中的第 "<<j<<" 个元素\n";
}

//插入(按序直接插入在该元素位置)
void insertBefore(Node* head, int n, int e)
{
	Node* p = head;int j = 1;
	while(p && j < n)
	{
		p = p->next;
		++j;
	}
	if(! p || j > n)
	{
		cout<<"超出范围\n";
		return ;
	}

	Node* s = new Node;
	s->data = e;
	s->next = p->next;
	p->next = s;
}

//插入(按序号在要插入的元素之后,同理可改为之前)
void insertNext(Node* head, int n, int e)
{
	Node* p = head;int j = 0;
	while(p && j < n)
	{
		p = p->next;
		++j;
	}
	if(! p || j > n)
	{
		cout<<"超出范围\n";
		return ;
	}

	Node* s = new Node;
	s->data = e;
	s->next = p->next;
	p->next = s;
}

//删除(通过第n - 1个节点)
void shanChu(Node* head, int n)
{
	Node* p = head;int j = 0;
	while(p->next && j < n - 1)//如果节点p的直接后躯存在
	{
		p = p->next;
		++j;
	}
	if(! p->next || j > n - 1)
	{
		cout<<"要删除的元素不存在\n";
		return ;
	}
	Node* q = p->next;
	p->next = q->next;
	delete q;
}

//删除(通过值)
void shanChuByValue(Node* head, int e)
{
	Node* p = head;
	while(p->next && p->next->data != e)
	{
		p = p->next;
	}
	if(! p->next)
	{
		cout<<"该元素不存在\n";
		return ;
	}
	Node* q = p->next;
	p->next = q->next;
	delete q;
}

void changeByNumber(Node* head, int changeNumber, int changeValue)
{
	Node* p = head->next;int j = 1;
	while(p && j < changeNumber)
	{
		p = p->next;
		++j;
	}
	if(! p || j > changeNumber)
	{
		cout<<"该元素不存在\n";
		return ;
	}
	p->data = changeValue;
}

void changeByValue(Node* head, int beforeChange, int afterChange)
{
	Node* p = head;
	while(p && p->data != beforeChange)
	{
		p = p->next;
	}
	if(! p)
	{
		cout<<"该元素不存在\n";
		return ;
	}
	p->data = afterChange;
}

int main()
{
	Node* head = NULL;
	int n;
	
	cout<<"1、创建链表                 2、输入编号查找元素  \n";
	cout<<"3、直接用值查找元素         4、插入元素          \n";
	cout<<"5、输入编号删除元素         6、直接输入要删除的数\n";
	cout<<"7、输入编号替换元素         8、直接输入要替换的数\n";
	cout<<"9、退出\n\n";
	do
	{
		cout<<"请选择操作:";
		cin>>n;
		switch(n)
		{
		case 1:
		    cout<<"请输入数据元素,按8888结束输入:\n";
			head = create();
			cout<<"该链表为:";showList(head);
			break;
		case 2:
			int capcity;//编号
			cout<<"请输入您要查找的数据的序号:";
			cin>>capcity;
			searchByCapcity(head, capcity);
			break;
		case 3:
			int value;
			cout<<"请输入您要查找的数:";
			cin>>value;
			searchByValue(head, value);
			break;
		case 4:
			int address, valueInsert;
			cout<<"请输入您要把元素插入到表的位置:";
			cin>>address;
			cout<<"请输入您要插入的元素:";
			cin>>valueInsert;
			insertBefore(head, address, valueInsert);
			//insertNext(head, address, valueInsert);
			cout<<"新链表为:";showList(head);
			break;
		case 5:
			int number;
			cout<<"请输入您要删除的元素的序号:";
			cin>>number;
			shanChu(head, number);
			cout<<"新链表为:";showList(head);
			break;
		case 6:
			int valueDelete;
			cout<<"请输入您要删除的数:";
			cin>>valueDelete;
			shanChuByValue(head, valueDelete);
			cout<<"新链表为:";showList(head);
			break;
		case 7:
			int changeNumber, changeValue;
			cout<<"请输入您想要改变的数的序号:";
			cin>>changeNumber;
			cout<<"请输入您想把这个数改为:";
			cin>>changeValue;
			changeByNumber(head, changeNumber, changeValue);
			cout<<"新链表为:";showList(head);
			break;
		case 8:
			int beforeValue, afterValue;
			cout<<"请输入您想要改变的数:";
			cin>>beforeValue;
			cout<<"请输入您想把这个数改为:";
			cin>>afterValue;
			changeByValue(head, beforeValue, afterValue);
			cout<<"新链表为:";showList(head);
			break;
		case 9:
			delete head;
			return 0;
		default:
			cout<<"请输入正确的操作序号\n";
		}
	}while(n != 9);
	delete head;
	return 0;
}

对其中的输入类型控制可以参看这篇博客http://blog.csdn.net/a1245527104/article/details/9984619


抱歉!评论已关闭.