Kangkang


私信TA

用户名:166Kangkang

访问量:762

签 名:

等  级
排  名 8449
经  验 1170
参赛次数 0
文章发表 3
年  龄 0
在职情况 学生
学  校 中国矿业大学
专  业 计算机科学与技术

  自我简介:

#include <iostream>
#include <string>
#include <iomanip>

#define MAXSIZE 11

using namespace std;



struct Node
{
	string elem;
	int next;
};


class StaticLinkList
{
private:
	Node space[MAXSIZE];
	//表头节点:1
	//备用链表表头节点:0
	//NULL :0
	int length;

public:
	StaticLinkList()//初始化链表
	{
		length = 0;
		space[1].next = 0;//带表头节点的空链表
		space[0].next = 2;//构成空闲链接表
		for (int i = 2; i < MAXSIZE - 1; i++)
		{
			space[i].next = i + 1;
		}
		space[MAXSIZE - 1].next = 0;
	}
	void Show()
	{
		for (int i = 0; i < MAXSIZE; i++)
		{
			cout << setiosflags(ios::left);
			cout << setw(8) << space[i].elem;
			cout << setiosflags(ios::right);
			cout << setw(2) << space[i].next << endl;
			cout << resetiosflags(ios::right);
		}
		cout << "********************" << endl;
	}
	int FindPrior(int a)//找到第a个节点的前一个节点
	{
		int prior = 1;
		while (--a)
		{
			prior = space[prior].next;
		}
		return prior;
	}
	int Malloc()
	{
		int i = space[0].next;
		if (i)
			space[0].next = space[space[0].next].next;//备用链表删去一个节点
		return i;//返回分配的节点下标
	}
	void Free(int a)//将下标为a的空闲节点回收到备用链表
	{
		space[a].next = space[0].next;
		space[0].next = a;
	}
	void Insert(int a, string e)
	{
		int priorNode = FindPrior(a);
		int newNode = Malloc();
		space[newNode].elem = e;

		space[newNode].next = space[priorNode].next;
		space[priorNode].next = newNode;
	}
	void Delete(int a)
	{
		int priorNode = FindPrior(a);
		int del = space[priorNode].next;

		space[priorNode].next = space[del].next;
		Free(del);
	}
	void Search(string e)
	{
		int i = space[1].next;
		while (i)
		{
			if (space[i].elem == e)
			{
				cout << setiosflags(ios::right) << setw(2) << i << endl;
				cout << "********************" << endl;
				return;
			}
			i = space[i].next;
		}
	}
};

int main()
{
	StaticLinkList sl;
	string select;
	while (cin >> select)
	{
		if (select == "show")
		{
			sl.Show();
		}
		else if (select == "insert")
		{
			int a;
			string e;
			cin >> a >> e;
			sl.Insert(a, e);
		}
		else if (select == "delete")
		{
			int a;
			cin >> a;
			sl.Delete(a);
		}
		else if (select == "search")
		{
			string e;
			cin >> e;
			sl.Search(e);
		}
	}
	return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区