小白


私信TA

用户名:yise35508

访问量:455

签 名:

等  级
排  名 5489
经  验 1474
参赛次数 0
文章发表 2
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

解题思路:
如下
注意事项:
1.数组的长度设置

2.插入和删除的循环注意是否遍历一遍

3.格式要求,数字前面有空格,分割线****是20个以及只设show的下方
参考代码:

#include#include#include#define SIZE 11

typedef struct NodeName {
	char ar[8];
	int next;
}NodeName;

void MyCreateArrList(NodeName * str, int m) {
	int i;
	str[0].next = 2;
	str[1].next = 0;
	for (i = 2; i < m - 1; i++)
	{
		str[i].next = i + 1;
	}
	str[m - 1].next = 0;
	for (int j = 0; j < m; j++)
	{
		str[j].ar[0] = '\0';
	}
}

int DeleteArrList(NodeName* str, int n, int m) {
	int i;
	int temp1 = 1; //头节点,数组最后
	for (i = 0; i < n - 1; i++)
	{
		temp1 = str[temp1].next;
	}
	int temp2 = str[temp1].next;// 1-2-3,此处为2

	str[temp1].next = str[temp2].next;
	//下面是free
	str[temp2].next = str[0].next;
	str[0].next = temp2;
	return 1;
}

void InsertArrList(NodeName* str, int n, int m) {
	NodeName p;
	int temp0 = str[0].next; //temp0是空位的位置,也是p的编号
	//上面是malloc

	int temp1 = 1; //头节点,数组最后
	for (int i = 0; i < n - 1; i++)
	{
		temp1 = str[temp1].next;
	}
	scanf("%s", p.ar);

	p.next = str[temp1].next;
	str[temp1].next = temp0;
	
	str[temp0] = p; //把新的结构体放入空位
}

int SearchArrList(NodeName* str, char * search, int m) {
	int i;
	int temp = 1; //头节点,数组最后
	for (i = 0; i < m ; i++)
	{
		temp = str[temp].next;
		if (!strcmp(str[temp].ar, search))
			break;
	}
	return temp;
}

void ShowArrList(NodeName* str, int m)
{
	int i;
	for (i = 0; i < m; i++)	
	{
		if (str[1].next == 10)
			str[0].next = 0;
		printf("%-8s%2d\n", str[i].ar, str[i].next);
	}
}

void PrintList(NodeName* str, int m)
{
	char check[7];
	char check_search[7];
	int a;
	int count = 0;
	while(scanf("%s", check) != EOF)
	{
		if (!strcmp(check, "delete"))
		{
			scanf("%d", &a);
			DeleteArrList(str, a, SIZE);
		}
		else if(!strcmp(check, "insert"))
		{
			scanf("%d", &a);
			InsertArrList(str, a, m);
			str[0].next++;
		}
		else if (!strcmp(check, "search"))
		{
			scanf("%s", check_search);
			printf("%2d\n", SearchArrList(str, check_search, m));
			printf("********************\n");
		}
		else if (!strcmp(check, "show"))
		{
			ShowArrList(str, m);
			printf("********************\n");
		}	
	}
}

int main(void)
{
	NodeName str[SIZE];
	MyCreateArrList(str, SIZE);
	
	PrintList(str, SIZE);
	return 0;
}


 

0.0分

1 人评分

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

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区