《数据结构》 一、链表

印度的顶尖程序员Harsha Suryanarayana为你图文讲解数据结构,视频链接【强烈推荐】深入浅出数据结构 - 顶尖程序员图文讲解 - UP主翻译校对 (已完结)_哔哩哔哩_bilibili

1.头插法

链表.cpp

#include<iostream>
using namespace std;

//定义一个节点
class Node
{
public:
	int data;
	Node* next;
};

Node* head;//头节点 全局变量

//在链表开头插入一个节点
void Insert(int x)
{
	Node* temp = new Node();
	temp->data = x;
	temp->next = head;
	head = temp;
}

void Print()
{
	Node* temp = head;//因为不能修改头节点,使用临时节点temp
	cout << "List is: ";
	while (temp != NULL)
	{
		cout << temp->data << " ";//打印该节点的值
		temp = temp->next;//转到下一个节点
	}
	cout << endl;
}

int main()
{
	head = NULL;//该指针变量不指向任何东西,此时链表为空
	//让用户输入一些数字,将数字插入链表
	cout << "How many numbers?" << endl;
	int n,x;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cout << "Enter the number" << endl;
		cin >> x;
		Insert(x);
		Print();
	}
	system("pause");
	return 0;
}

2.任意位置插入

#include<iostream>
using namespace std;

//定义一个节点
class Node
{
public:
	int data;
	Node* next;
};

Node* head;//头节点 全局变量

//任意位置插入一个节点
void Insert(int data, int n)
{
	//创建需要插入的新节点
	Node* temp1 = new Node();
	temp1->data = data;
	temp1->next = NULL;

	if (n == 1)//头插
	{
		temp1->next = head;
		head = temp1;
		return;
	}
	//转到第n-1个节点
	Node* temp2 = head;
	for (int i = 0; i < n - 2; i++)
		temp2 = temp2->next;
	//插入节点
	temp1->next = temp2->next;
	temp2->next = temp1;
}

void Print()
{
	Node* temp = head;//因为不能修改头节点,使用临时节点temp
	cout << "List is: ";
	while (temp != NULL)
	{
		cout << temp->data << " ";//打印该节点的值
		temp = temp->next;//转到下一个节点
	}
	cout << endl;
}

int main()
{
	head = NULL;//empty list
	Insert(2,1);//List: 2
	Insert(3,2);//List: 2 3
	Insert(4,1);//List: 4 2 3
	Insert(5,2);//List: 4 5 2 3
	Print();
	system("pause");
	return 0;
}

3.任意位置删除节点

#include<iostream>
using namespace std;

//定义一个节点
class Node
{
public:
	int data;
	Node* next;
};

Node* head;//头节点 全局变量

//任意位置插入一个节点
void Insert(int data, int n)
{
	//创建需要插入的新节点
	Node* temp1 = new Node();
	temp1->data = data;
	temp1->next = NULL;

	if (n == 1)//头插
	{
		temp1->next = head;
		head = temp1;
		return;
	}
	//转到第n-1个节点
	Node* temp2 = head;
	for (int i = 0; i < n - 2; i++)
		temp2 = temp2->next;
	//插入节点
	temp1->next = temp2->next;
	temp2->next = temp1;
}

void Print()
{
	Node* temp = head;//因为不能修改头节点,使用临时节点temp
	cout << "List is: ";
	while (temp != NULL)
	{
		cout << temp->data << " ";//打印该节点的值
		temp = temp->next;//转到下一个节点
	}
	cout << endl;
}

void Delete(int n)
{
	Node* temp1 = head;
	if (n == 1)
	{
		head = temp1->next;
		delete temp1;
		return;
	}
	for (int i = 0; i < n - 2; i++)
		temp1 = temp1->next;
	Node* temp2 = temp1->next;
	temp1->next = temp2->next;
	delete temp2;
}

int main()
{
	head = NULL;//empty list
	Insert(2,1);//List: 2
	Insert(3,2);//List: 2 3
	Insert(4,1);//List: 4 2 3
	Insert(5,2);//List: 4 5 2 3
	Print();
	int n;
	cout << "Enter a position to delete" << endl;
	cin >> n;
	Delete(n);
	Print();
	system("pause");
	return 0;
}

4.反转链表(迭代方式实现)

void Reverse()
{
	Node *current, *prev, *next;
	current = head;
	prev = NULL;
	while (current != NULL)
	{
		next = current->next;//记录当前节点的下一个节点地址,以便能够遍历到达下一个位置
		current->next = prev;//调整当前节点的链接指向实现反转
		prev = current;
		current = next;//移动当前节点,实现遍历
	}
	head = prev;
	//return head;
}

5.打印链表(递归方式实现),理解 Print 和 ReversePrint 的区别(正反向打印链表元素)

void Print(Node* p)
{
	if (p == NULL)//递归的退出条件
		return;
	//先打印值再递归调用
	cout << p->data << " ";
	Print(p->next);
}

void ReversePrint(Node* p)
{
	if (p == NULL)//递归的退出条件
		return;
	//先递归调用再打印值
	ReversePrint(p->next);
	cout << p->data << " ";
}

6.反转链表(递归方式实现)

void Reverse(Node* p)
{
	if (p->next == NULL)//递归的退出条件
	{
		head = p;
		return;
	}
	Reverse(p->next);
	//反转链表
	Node* q = p->next;
	q->next = p;
	//前两行可合并为p->next->next = p;
	p->next = NULL;
}

7.双向链表

#include<iostream>
using namespace std;

//定义一个节点(双向)
class Node
{
public:
	int data;
	Node* next;
	Node* prev;
};

Node* head;//头节点 全局变量

//创建节点
Node* GetNewNode(int x)
{
	Node* newNode = new Node();//在堆中创建节点
	newNode->data = x;
	newNode->next = NULL;
	newNode->prev = NULL;
	return newNode;
}

//创建链表(头插法)
void InsertAtHead(int x)
{
	Node* temp = GetNewNode(x);
	if (head == NULL)
	{
		head = temp;
		return;
	}
	head->prev = temp;
	temp->next = head;
	head = temp;
}

//创建链表(尾插法)
void InsertAtTail(int x)
{
	Node* temp = GetNewNode(x);
	if (head == NULL)
	{
		head = temp;
		return;
	}
	//Going to last Node
	Node* temp1 = head;
	while (temp1->next != NULL)
		temp1 = temp1->next;
	temp1->next = temp;
	temp->prev = temp1;
}

void Print()
{
	Node* temp = head;
	cout << "Forward: ";
	while (temp != NULL)
	{
		cout << temp->data;
		temp = temp->next;
	}
	cout << endl;
}

void ReversePrint()
{
	Node* temp = head;
	if (temp == NULL)
		return;
	//Going to last Node
	while (temp->next != NULL)
		temp = temp->next;
	//打印并利用prev指针向前遍历
	cout << "Reverse: ";
	while (temp != NULL)
	{
		cout << temp->data;
		temp = temp->prev;
	}
	cout << endl;
}

int main()
{
	head = NULL;
	InsertAtHead(2);
	InsertAtHead(4);
	InsertAtHead(6);
	InsertAtTail(8);
	Print();
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值