算法研究之——链表的一些操作(创建、打印,在头部、中间、尾部插入节点)

本文详细介绍了链表的各种操作,包括链表的初始化、正序和反序创建、打印、清除、获取长度、获取指定位置元素、地址查找、修改节点值、在头部和尾部插入节点等。提供了相应的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>

using namespace std;

struct Node
{
	int data;
	Node* next;
};

typedef class Node List_Node;


//1.链表初始化
void init_list(Node *head)
{
	head = nullptr;
	cout << "the list have been inited" << endl;
}


/*********************************************************************/
//2.创建单链表:该方法生成的节点顺序与输入的顺序一致而且表头不为空即包含数据
/***********************************************************************/
List_Node *creat_list()
{
	List_Node *head = nullptr;
	
	List_Node *p1, *p2;
	p1 = p2 = new Node;//p1为工作指针,p2为移动指针

	cout << "plz input data:";
	cin >> p1->data;//创建一个新的节点,从cin读入数据
	p1->next = nullptr;//新的节点的next指向null

	while (p1->data > 0)//这里把输入的数据
	{
		if (head == nullptr)
		{
			head = p1;
		}
		else
		{
			p2->next = p1;
		}
		
		p2 = p1;

		p1 = new Node;
		cin >> p1->data;
		p1->next = nullptr;
	}

	return head;
}

/******************************************/
//3.第二种方法建立链表,节点和输入的顺序是相反的
/*******************************************/
List_Node* creat_list2()
{
	Node *head=nullptr;
	Node*p1;

	p1 = new Node;
	cin >> p1->data;

	while (p1->data > 0)
	{
		p1->next = head;
		head = p1;

		p1 = new Node;
		cin >> p1->data;
	}
	return head;
}

/*************************************************/
//4.输出链表:即大家所说的打印链表print the list
/*************************************************/
void print_list(Node *Head)
{
	if (Head == nullptr)
	{
		cout << "empty list" ;
	}
	else
	{
		cout << "the list is:";
		while (NULL != Head)
		{
			cout << Head->data<<" ";
			Head = Head->next;
		}
	}

	cout << endl;
}


/**************************************************/
//5.清除链表:clear the list
/**************************************************/
void clear_list(Node* head)
{
	Node *pNext;

	if (head = nullptr)
	{
		cout << "the list is empty" << endl;
	}
	else
	{
		while (nullptr != head)
		{
			pNext = head->next;
			delete head;

			head = pNext;//表头下移动
		}
		cout << "the list had been emptied" << endl;
	}
}

/**************************************************/
//6.return the list zize
/**************************************************/
int size_list(Node* head)
{
	int size = 0;
	while (head != nullptr)
	{
		size++;
		head = head->next;//the head move forward next step
	}

	return size;
}

/**************************************************/
//7.return the  n element
/**************************************************/
int get_element(Node*head,int n)
{
	int i = 0;
	if (n < 0)
	{
		cout << "n  is  not a available value" << endl;
	}
	if (head == nullptr)
	{
		cout << "the list is empty" << endl;
	}

	while (head != NULL)
	{
		++i;
		if (i == n)
		{
			break;
		}
		head = head->next;
	}

	if (i < n)
	{
		cout << "the list's len is shorter then n" << endl;
	}

	return head->data;

}

/*****************************************************************************************/
//8.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL 
/*****************************************************************************************/
int *get_element_addr(Node*head, int x)
{
	if (nullptr == head)
	{
		cout << "the list is empty" << endl;
	}

	while ((head->data != x) && (head->next != nullptr))
	{
		head = head->next;
	}

	if (head->data == x)
	{
		cout << x << " " << &(head->data) << endl;
	}
	
	return &(head->data);
}

/*****************************************************************************************/
//9. 把链表中第n个节点的值修改为x,成功返回1,失败返回0
/*****************************************************************************************/
int modify_list(Node *head, int n, int x)
{

	int i = 0;

	if (nullptr == head)
	{
		cout << "the list is empty" << endl;
	}

	while (head != nullptr)
	{
		++i;
		if (i == n)
		{
			break;
		}

		head = head->next;
	}

	if (i < n)
	{
		cout << "n out of the range" << endl; 
	}

	head->data = x;

	return 1;
}

/*****************************************************************************************/
//10. 在表的头部插入一个新的节点成功返回true insert a new node into the head of the list
/*****************************************************************************************/
bool  insert_head_Node(Node **head, int insertelem)//这里把整个链表作为输入才能修改整个链表,不然就
{                                                   //想当与传值参数而无法修改链表
	Node *insert_node=new Node;
	insert_node->data = insertelem;

	insert_node->next = *head;
	*head = insert_node;

	return 1;
}

/*****************************************************************************************/
//11. insert the new node at the end of the list
/*****************************************************************************************/
bool  insert_end_Node(Node **head, int insertelem)
{
	Node *pHead = *head;
	Node *pTemp = new Node;//临时的节点用来存储数据

	Node *insert_node = new Node;
	insert_node->data = insertelem;
	insert_node->next = nullptr;
	pTemp = pHead;

	while (pHead->next != nullptr)
	{
		pHead = pHead->next;
	}

	pHead->next = insert_node;

	*head = pTemp;

	return true;
}

/*****************************************************************************************/
//11. 
/*****************************************************************************************/

int main()
{
	Node *my_list;
	my_list = creat_list2();//use the second way to creat forward list.
	print_list(my_list);//print the list.

	int len = size_list(my_list);//output the length of the list.
	cout << len << endl;

	int value = get_element(my_list,3);//输出第三个节点的数据
	cout << value << endl;

	cout << "修改后的";
	modify_list(my_list, 3, 20);
	print_list(my_list);

	cout << "在头部插入新的节点后";
	insert_head_Node(&my_list, 30);
	print_list(my_list);

	cout << "在尾部插入元素后";
	insert_end_Node(&my_list, 100);
	print_list(my_list);
}


//还有待解决的问题
/* 向有序单链表中插入元素x结点,使得插入后仍然有序 */
/* 从单链表中删除表头结点,并把该结点的值返回,若删除失败则停止程序运行 */
/* 从单链表中删除表尾结点并返回它的值,若删除失败则停止程序运行 */
/* 从单链表中删除第pos个结点并返回它的值,若删除失败则停止程序运行 */
/* 从单链表中删除值为x的第一个结点,若删除成功则返回1,否则返回0 */
/* 交换2个元素的位置 */
/* 将线性表进行快速排序 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值