原来用循环链表写的现在用数组写(删除数据的,我找到了一个规律)

#include<iostream>
using namespace std;
#define TRUE 1
#define ERROR 0

class Array
{
private:
	int *array;
	int length;
public:
	void Initlist(int n);
	void Destorylist();
	void output();
	int Listlength();
	int Insert(int s,int n);
	int Listdelete(int s,int &n,int s1);
};
void Array::Initlist(int n)
{
	int i;
	length=n;
	array=new int [n];
	for(i=0;i<n;i++)
		array[i]=i+1;
}
void Array::Destorylist()
{
	delete [] array;
}
void Array::output()
{
	int i;
	cout<<"剩余孩子的个数为:"<<length<<endl<<"编号为:";
	for(i=0;i<length;i++)
		cout<<array[i]<<" ";
}
int Array::Listlength()
{
	return length;
}
int Array::Insert(int s,int n)//当链表中没有数据,则不执行循环
{
	int i;
	for(i=length;i>=s;i--)
		array[i]=array[i-1];
	array[s-1]=n;
	length++;
	return TRUE;
}
int Array::Listdelete(int s,int &n,int s1)
{
	if(length==0&&s==0) return ERROR;
	int i,j;
	n=array[s-1];
	j=s-1;
	for(i=j;i<length;i++)
		array[i]=array[i+1];
	length--;
	j=(j+s1-1)%length+1;
	return j;
}
int main()
{
	int Game1(Array a,int m,int k);
	int Game2(Array a,int m,int k);
	int m,n,k,k1;
	cout<<"请输入小孩的个数为:";
		cin>>n;
	cout<<"请输入你要淘汰几号编号孩子的间隔数:";
	cin>>m;
	cout<<"请输入你想要获胜者的数量:";
	cin>>k;
	Array child;
	child.Initlist(n);
	child.output();
	cout<<endl;
	cout<<"是否中途要增加小孩的数量,不是请输入1,是请输入2:";
	cin>>k1;
	if(k1==1)Game1(child,m,k);
	else
		if(k1==2)Game2(child,m,k);
		else
			cout<<"输入有问题!"<<endl;
	return 0;
}
int Game1(Array a,int m,int k) //对象的传递是双向的
{
	int t,m1;
	m1=m;
	while(a.Listlength()>k) //在这改变获胜者的数量
	{
		m=a.Listdelete(m,t,m1);
		cout<<"淘汰孩子的编号为:"<<t<<endl;
		a.output();
	}
	cout<<"获胜的孩子的编号为:";
	a.output();
	cout<<endl;
	return 0;
}
int Game2(Array a,int m,int k)
{
	int t,s=0,i,m1;
	int k1;
	int n=a.Listlength();
	m1=m;
	while(a.Listlength()>k) //在这改变获胜者的数量
	{
		m=a.Listdelete(m,t,m1);
		cout<<"淘汰孩子的编号为:"<<t<<endl;
		a.output();
		cout<<endl;
		cout<<"是否要插入小孩,是请输入1,否请输入0:";
		cin>>k1;
		if(k1==1)
		{
			int s1,s2;
			cout<<"请输入你要插入小孩的个数:";
			cin>>s1;
			for(i=0;i<s1;i++)
			{
				cout<<"请输入你要插入小孩的第"<<i+1<<"个小孩的插入位置:";
				cin>>s2;
				a.Insert(s2,++n);
				cout<<"插入后的小孩排队的序列为:";
				a.output();
				cout<<endl;
			}
		}
		else
			if(k1==0)continue;
			else
			{
				cout<<"输入错误,跳出程序!"<<endl;
				break;
			}
	}
	a.output();
	cout<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
由于“数组的链表”一词理解起来有些困难,这里我提供两个数组相关的链表代码。 1. 数组实现的链表 这种链表的实现方式是使用一个指针数组一个可用位置的指针来表示链表。节点的数据存放在数组中,通过数组下标来访问。链表的头节点指针指向第一个可用位置的指针,链表的尾节点指针指向最后一个非可用位置的指针。 ```c #define MAX_NODE 100 int data[MAX_NODE]; int next[MAX_NODE]; int head; int tail; int free_pos; void init() { for (int i = 0; i < MAX_NODE; i++) { next[i] = i + 1; } next[MAX_NODE - 1] = -1; free_pos = 0; head = -1; tail = -1; } int allocate_node() { if (free_pos == -1) { return -1; // 标记无可用节点 } int pos = free_pos; free_pos = next[pos]; return pos; } void free_node(int pos) { next[pos] = free_pos; free_pos = pos; } void insert(int val) { int pos = allocate_node(); if (pos == -1) { return; // 链表已满 } data[pos] = val; next[pos] = -1; if (head == -1) { head = pos; } else { next[tail] = pos; } tail = pos; } void remove(int val) { int curr = head; int prev = -1; while (curr != -1 && data[curr] != val) { prev = curr; curr = next[curr]; } if (curr == -1) { return; // 没有找到值为val的节点 } if (prev == -1) { head = next[curr]; } else { next[prev] = next[curr]; } free_node(curr); } void show() { int curr = head; while (curr != -1) { printf("%d ", data[curr]); curr = next[curr]; } printf("\n"); } ``` 2. 动态数组实现的链表 这种链表的实现方式是使用动态数组数组中的元素是节点指针,每个节点指针指向一个节点。链表的头节点指针指向第一个节点,链表的尾节点指针指向最后一个节点。当链表需要扩容时,动态数组会重新分配更大的内存,并将原有元素复制到新的内存中。 ```c #include <stdlib.h> typedef struct node_t { int val; struct node_t* next; } node_t; int size; int capacity; node_t** nodes; void init() { size = 0; capacity = 1; nodes = (node_t**)malloc(capacity * sizeof(node_t*)); nodes[0] = NULL; } void insert(int val) { node_t* node = (node_t*)malloc(sizeof(node_t)); node->val = val; node->next = NULL; nodes[size] = node; size++; if (size == capacity) { capacity *= 2; nodes = (node_t**)realloc(nodes, capacity * sizeof(node_t*)); } if (size == 1) { nodes[0] = node; } else { nodes[size - 2]->next = node; } } void remove(int val) { node_t* curr = nodes[0]; node_t* prev = NULL; while (curr != NULL && curr->val != val) { prev = curr; curr = curr->next; } if (curr == NULL) { return; // 没有找到值为val的节点 } if (prev == NULL) { nodes[0] = curr->next; } else { prev->next = curr->next; } free(curr); size--; if (size <= capacity / 2) { capacity /= 2; nodes = (node_t**)realloc(nodes, capacity * sizeof(node_t*)); } } void show() { node_t* curr = nodes[0]; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值