数据结构笔记

这篇博客探讨了链表数据结构在解决实际问题中的应用,包括实现约瑟夫环问题的循环链表算法和使用栈解决迷宫路径寻找。约瑟夫环问题中,通过循环链表删除节点来模拟过程;迷宫求解中,利用堆栈回溯寻找可行路径。这些实例展示了链表和堆栈在算法设计中的重要作用。

c数据结构

链表
链表的类实现
class Node//节点类
{
public:
	Node* next;
	int data;
	Node()
	{
		next = NULL;
	}
};

class LinkList//实例化得到一个带有空节点的头指针
{
public:
	Node* head;//头指针
	int len;
	LinkList()
	{
		head = new Node();//head头指针指向一个空节点
		len = 0;
	}
	~LinkList()//从头删到尾
	{
		Node* p, * q;
		p = head;
		while (p != NULL)
		{
			q = p;
			p = p->next;
			delete q;
		}
		len = 0;
		head = NULL;
	}
};
循环链表
Oj题目:约瑟夫环
#include <iostream>
using namespace std;

class Node//节点类
{
public:
	Node* next;
	int data;
	Node()
	{
		next = NULL;
	}
};

class LinkList//实例化得到一个带有空节点的头指针
{
public:
	Node* head;//头指针
	int len;
	LinkList()
	{
		head = new Node();//head头指针指向一个空节点
		len = 0;
	}
	~LinkList()//从头删到尾
	{
		delete head;
	}
	void initialize()
	{
		int num;
		cin >> num;
		Node* p = head;
		for (int i = 1; i <= num; i++)
		{
			Node* q = new Node();
			q->data = i;
			p->next = q;
			p = p->next;
		}
		p->next = head;//循环链表
	}
	void Joseph()
	{
		int k, s;
		cin >> k >> s;
		Node* start = head,*temp;
		for (int i = 1; i < s; i++)//到达指定节点之前
		{
			start = start->next;
		}
		while (head->next->next!= head)//只剩最后一个节点时退出循环
		{
			for (int i = 1; i < k; i++)//k-1次
			{
				if (start->next->next == head)//跳过头节点
				{
					start = start->next->next;
				}
				else
				{
					start = start->next;
				}
			}
			cout << start->next->data << ' ';
			temp = start->next;//删除节点
			start->next = temp->next;
			delete temp;
		}
		cout << start->next->data << ' ' << endl;
	}
};


int main()
{
	LinkList temp01;
	temp01.initialize();
	temp01.Joseph();
	return 0;
}
Oj题目:交换节点
#include <iostream>
using namespace std;

class Node
{
public:
	int data;
	Node* next;
	Node()
	{
		next = NULL;
	}
};

class LinkList
{
public:
	int len;
	Node* head;
	LinkList()
	{
		head = new Node();
		len = 0;
	}
	~LinkList()
	{ 
		Node* p, * q;
		p = head;
		while (p != NULL)
		{
			q = p;
			p = p->next;
			delete q;
		}
	}
	void initialize()
	{
		cin >> len;
		Node* p = head;
		for (int i = 0; i < len; i++)
		{
			int temp;
			cin >> temp;
			Node* q = new Node();
			q->data = temp;
			p->next = q;
			p = p->next;
		}
	}
	Node* num_point(int n)//返回所找节点的前一个指针
	{
		Node* temp = head;
		for (int i = 1; i < n; i++)
		{
			temp = temp->next;
		}
		return temp;
	}
	void swap(Node* p, Node* q)
	{/*未完成*/
		int t = p->data;
		p->data = q->data;
		q->data = t;
	}
	void display()
	{
		Node* k = head->next;
		while (k != NULL)
		{
			cout << k->data << ' ';
			k = k->next;
		}
		cout << endl;
	}
};

int main()
{
	LinkList temp;
	temp.initialize();
	temp.display();
	int t1, t2;
	cin >> t1 >> t2;
	Node* a, * b;
	a = temp.num_point(t1);
	b = temp.num_point(t2);
	temp.swap(a, b);
	temp.display();
	return 0;
}
堆栈
Oj题目:迷宫求解
#include <iostream>
using namespace std;

Status MazePath(MazeType maze,PosType start,PosType end)//墙是1,空白通道块是0,路径有编号,曾纳块为1 
{
	InitStack(S);
	curpos=start;
	curstep=1;
	do
	{
		if(Pass(curpos))//当前位置可通 (不是通道块,不是当前路径的通道块,也不是曾经纳入过路径的通道块) 
		{
			FootPrint(curpos);//(标记当前路径) 
			e=(curstep,curpos,1);
			Push(S,e);//将当前位置插入堆栈 
			if(curpos==end) return (TRUE);//如果当前位置是终点,直接得到路径堆栈 
			curpos=NextPos(curpos,1);//否则当前位置向东挪一位 
			curstep++;//序号加一 
		}
		else//当前位置不可通 
		{
			if(!StackEmpty(S))//堆栈不空,有前一个通道块 
			{
				Pop(S,e);//弹出前面的路 
				while(e.di==4&&!StackEmpty(S))//当栈不空但栈顶的四周均不可通,一步步后退并把那些通道块标记为墙 
				{
					MarkPrint(e.seat);//留下不能通过的标记(变成墙) 
					Pop(S,e);//直到弹出一个还有其他方向的 
				}
				if(e.di<4)//当栈不空且栈顶尚有其他方向未探索 
				{
					e.di++;//调个头 
					Push(S,e);//通道块收回去 
					curpos=NextPos(e.seat,e.di);//再将当前节点设为另一个方向的通道块 
				}
			}
		}
	}while(!StackEmpty(S));//当栈不空 
	return (FALSE)//栈空了 
}
#include <iostream>
#include <stack>
using namespace std;

struct position
{
	int x, y;
};

struct chunk
{
	int order;
	position seat;
	int direct;
};

class Maze
{
public:
	int** maze;//地图数组指针
	int size;//大小
	stack<chunk> path;//路径
	position end;

	Maze(int n)
	{
		size = n;
		maze = new int* [n];
		for (int i = 0; i < n; i++)
		{
			maze[i] = new int[n];
		}
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				cin >> maze[i][j];
		end = { size - 1,size - 1 };
	}
	~Maze()
	{
		for (int i = 0; i < size; i++)
			delete[] maze[i];
		delete[] maze;
	}
	bool Pass(position curpos)
	{
		if (curpos.x > size - 1 || curpos.y > size - 1 || curpos.x < 0 || curpos.y < 0)
			return false;

		if (maze[curpos.x][curpos.y] == 0)
			return true;
		else
			return false;
	}
	void FootPrint(position curpos)
	{
		maze[curpos.x][curpos.y] = -1;
	}
	position NextPos(position curpos, int direct)
	{
		if (direct == 1)
			curpos.y++;
		if (direct == 2)
			curpos.x++;
		if (direct == 3)
			curpos.y--;
		if (direct == 4)
			curpos.x--;
		return curpos;
	}
	void MarkPrint(position curpos)
	{
		maze[curpos.x][curpos.y] = 1;
	}
	void MazePath()//先得到路径堆栈,再打印路径
	{
		/*得到路径堆栈*/
		position curpos = { 0.0 };
		int curstep = 1;
		chunk e;//当前通道块
		do
		{
			if (Pass(curpos))
			{
				this->FootPrint(curpos);
				e = { curstep,curpos,1 };
				path.push(e);
				if (curpos.x == end.x && curpos.y == end.y)
					break;//得到堆栈,退出循环
				curpos = NextPos(curpos, 1);
				curstep++;
			}
			else
			{
				if (!path.empty())
				{
					e = path.top();
					path.pop();
					while (e.direct == 4 && !path.empty())
					{
						this->MarkPrint(e.seat);
						e = path.top();
						path.pop();
					}
					if (e.direct < 4)
					{
						e.direct++;
						path.push(e);
						curpos = NextPos(e.seat, e.direct);
					}
				}
			}
		} while (!path.empty());
		/*打印路径*/
		stack<chunk> path1;//临时堆栈
		if (!path.empty())
		{
			while (!path.empty())//将path的数据逆序导入path1
			{
				path1.push(path.top());
				path.pop();
			}
			int i = 0;
			while (!path1.empty())
			{
				position cpos = path1.top().seat;//?
				if ((++i) % 4 == 0)
				{
					cout << '[' << cpos.x << ',' << cpos.y << ']' << "--" << endl;
				}
				else
				{
					cout << '[' << cpos.x << ',' << cpos.y << ']' << "--";
				}
				path1.pop();
			}
			cout << "END" << endl;
		}
		else
			cout << "no path" << endl;
	}
};

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		Maze map(n);
		map.MazePath();//初始化地图之后,进行路径堆栈的寻找,并打印输出
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值