#include <iostream>
using namespace std;
typedef struct LNode
{
	int data;
	struct LNode * next;

}LNode, * Listlist;

//bool init(Listlist & L,int i) //尾插法
//{
//	LNode *new_node, *temp_node;
//	temp_node = L;
//	for (int num = 0; num < i; num++)
//	{
//		new_node = new LNode;
//		new_node->next = NULL;
//		cin >> new_node->data;
//		temp_node->next = new_node;
//		temp_node = new_node;
//	}
//}

bool init(Listlist & L, int i)    //头插法
{
	LNode * new_item, * temp_item;//申明一个暂时的节点的指针,然后一个向下传的界点的指针
	temp_item = L;//将这个临时的节点当成是一个乡下传递的一个指针
	int num;//数量
	for (num = 0; num < i; num++)
	{
		new_item = new LNode;//申明一个新的节点
		new_item->next = NULL;//将末尾置为空
		cin >> new_item->data;//输入这个新节点的值
		new_item->next = L->next;//将新节点指向第一个节点的地址
		L->next = new_item;//将头节点指向这个新的节点
	}
	return true;//返回正确
}

bool destroy(Listlist &L)
{
	Listlist temp;//临时的指针
	temp = L->next;//指向第一个节点
	while (temp)//判断这个节点是不是空的,并作为一个结束条件
	{
		L->next = L->next->next;//将头结点的下一个指针,指向下下个节点
		free(temp);//释放单独提出来的节点
		temp = L->next;//临时节点指向下一个节点
	}
	return true;//返回正确
}

bool listempty(Listlist L)
{
	return L->next ? false : true;//如果头指针的指向下一个节点的指针为空的话呢,那么这个就是一个空的链表.
}

int listlength(Listlist & L)
{
	Listlist temp;
	temp = L->next;//将头指针指向的一个节点,当做是第一个节点。
	int num = 1;//并赋值为1
	while (temp)//将temp作为一个判断是否为空的一个条件
	{
		temp = temp->next;//指向下一个,就像在数数一样
		num++;//数目加一
	}
	return num - 1;//因为最后一个数到的数是空指针所索引的数
}

bool getitem(Listlist L, int index, int &item)
{
	if (index < 1 && index>listlength(L))//先判断这个条件是不是成立的,符不符合我们的输入的条件
	{
		return false;//如果不符合,我们就返回这个错误的
	}
	LNode * temp;//申明的是一个临时节点的指针
	temp = L->next;//指向第一个节点
	int num = 1;//并且将这个节点的标号为1
	while (temp&&num < index )//一个是将这个指针是不是空的作为一个判断条件,然后将这个是不是到了,这个后面当做是一个判断的条件
	{
		temp = temp->next;//将这个节点的值,指向下一个节点
		num++;//数目加一
	}
	if (!temp || num >= index )
	{
		return false;
	}
	item = temp->data;
	return true;
}

bool locateitem(Listlist L, int index, int item)
{
	if (index<1 && index>listlength(L))
	{
		return false;
	}
	LNode * temp=L->next;
	int num = 1;
	while (temp&&num < index )
	{
		temp = temp->next;
		num++;
	}
	return item == temp->data ? true : false;
}

bool listinsert(Listlist &L, int index, int item)
{
	if (index<1 && index>listlength(L))
	{
		return false;
	}
	LNode * temp=L;
	int num = 0;
	while (temp&&num < index -1)
	{
		temp = temp->next;
		num++;
	}
	LNode * new_node = new LNode;
	new_node->next = temp->next;
	cin >> new_node->data;
	temp->next = new_node;
	return true;
}

bool deleteitem(Listlist & L, int index, int &item)
{
	if (index<1 && index>listlength(L))
	{
		return false;
	}
	LNode * temp=L;
	int num = 0;
	while (temp->next&&num < index - 1)
	{
		temp = temp->next;
		num++;
	}
	item = temp->next->data;
	temp->next = temp->next->next;
	return true;
}

  

posted on 2018-10-02 16:19  一起_007  阅读(72)  评论(0编辑  收藏  举报