【PTA】【C语言】链表操作-插入、查找和删除

请编写创建链表和输出链表的函数。对于以下数据结点的结构定义,

针对带头结点的链表,请编程完成以下功能。

struct LNode{
int data;                  //数据域
struct LNode *next;        //指针域
};
struct LNode *head;            //头指针

输入数据包含若干组命令和数据,一组数据中的第1个字符代表命令,

接下来的是该命令需要的数据。

(1)如果命令是I,功能为创建空链表,对应函数:void List_Init(head);

(2)如果命令是A,后跟一个整数data,功能为向链表尾部追加一个数据data,
对应函数:void List_Append(head,data);

(3)如果命令是C,后跟一个整数N,再跟N个整数,功能为向链表尾部追加N个
数据,可通过调用List_Append()函数实现;

(4)如果命令是P,功能遍历输出链表中所有数据,数据间用一个空格分隔,对应
函数:void List_print(head),如果链表未建立,输出“List not defined!”,
如果链表为空输出:“List is empty!”。

(5)如果命令是N,后跟一个整数n和d,功能为向链表的第n个位置插入数据d,
可通过调用List_Insert(head,n,d)函数实现;

(6)如果命令是F,后跟一个整数d,功能为在链表查找数据d,返回其位序,若
找不到返回-1。可通过调用List_Find(head,d)函数实现;

(7)如果命令是D,后跟一个整数n,功能为删除链表第n个位置的数据,可通过
调用List_Delete(head,n)函数实现。

输入格式:
若干组命令和数据,很多命令和数据写在一起请注意识别。

输出格式:
根据输入命令输出相应内容,详见输出样例。

输入样例:

I C 5 100 200 300 400 500 P
F 500 N 3 23 N 5 31 P
D 4 P F 99
A 6 P


输出样例:

100 200 300 400 500
index:5
100 200 23 300 31 400 500
100 200 23 31 400 500
Not Found!
100 200 23 31 400 500 6


输入样例:

I A 100 A 200 A 300 C 4 400 500 600 700 P
F 800 F 500
N 4 4 N 5 5 P
D 2 D 2 D 2 P
D 2 D 2 D 2 P
D 1 D 1 P
D 1 D 1 P


输出样例:

100 200 300 400 500 600 700
Not Found!
index:5
100 200 300 4 5 400 500 600 700
100 5 400 500 600 700
100 600 700
700
List is empty!

代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
struct LNode
{
	int data;                  //数据域
	struct LNode *next;        //指针域
};
struct LNode *head = NULL;
int len = sizeof(struct LNode);

struct LNode *List_Init(struct LNode *head)	// 创建空链表
{
	head = (struct LNode *) malloc(len);
	head -> next = NULL;
    head -> data = INT_MAX;
    return head;
}

void List_Append(struct LNode *head, int data)	// 向链表的尾部追加一个数据data
{
	struct LNode *p1, *p2;
	p1 = p2 = (struct LNode *) malloc(len);
	p1 = head;
	p2 -> data = data;
	p2 -> next = NULL;
	while (p1 -> next != NULL)
	{
		p1 = p1 -> next;
	}
	p1 -> next = p2;
}

void List_print(struct LNode *head)	// 遍历输出链表中所有数据
{
	struct LNode *p;
    if(head == NULL)
	{
        printf("List not defined!\n");
        return;
    }
    if(head -> next == NULL)
	{
        printf("List is empty!\n");
        return;
    }
    p = head -> next;
    while (p != NULL)
    {
    	printf("%d", p -> data);
        p = p -> next;
        if (p != NULL)
        printf(" ");
    }
    printf("\n");
}

void List_Insert(struct LNode *head, int n, int d)	// 向链表的第n个位置插入数据d
{
	struct LNode *p1, *p2;
	p1 = p2 = (struct LNode *) malloc(len);
	p1 = head;
	p2 -> data = d;
	p2 -> next = NULL;
	for (int i = 1; i < n; i++)
	{
		p1 = p1 -> next;
	}
	p2 -> next = p1 -> next;
	p1 -> next = p2;
}

int List_Find(struct LNode *head, int d)	// 在链表查找数据d,返回其位序
{
	int count = 1;
    struct LNode *p;
    p = head -> next;
    while(p != NULL)
	{
        if(p -> data == d)
			return count;
        p = p -> next;
        count++;
    }
    return -1;
}

void List_Delete(struct LNode *head, int n)	// 删除链表第n个位置的数据
{
	struct LNode *p1, *p2;
	p1 = p2 = (struct LNode *) malloc(len);
	p1 = head;
	p2 = p1 -> next;
	for (int i = 1; i < n; i++)
	{
		if(p1 != NULL)
			p1 = p1 -> next;
	}
	if (p1 == NULL || p1 -> next == NULL)
		return;
    p2 = p1 -> next;
    p1 -> next = p1 -> next -> next;
    free(p2);
}

int main()
{
	char ch;
	int n, data, N, i;
	while (scanf("%c", &ch) != EOF)
	{
		if (ch == 'I')
			head = List_Init(head);
		if (ch == 'A')
		{
			scanf("%d", &data);
			List_Append(head, data);
		}
		if (ch == 'C')
		{
			scanf("%d",&N);
            for(i = 0; i < N; i++)
			{
                scanf("%d", &data);
                List_Append(head, data);
            }
		}
		if (ch == 'P')
			List_print(head);
		if (ch == 'N')
		{
			scanf("%d %d", &n, &data);
            List_Insert(head, n, data);
		}
		if (ch == 'F')
		{
			scanf("%d", &data);
            if (List_Find(head, data) == -1)
				printf("Not Found!\n");
            else
				printf("index:%d\n", List_Find(head, data));
		}
		if (ch == 'D')
		{
			scanf("%d%d",&n);
            List_Delete(head,n);
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸢想睡觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值