归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言📝
但行前路,不负韶华!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include "LinkList.h"
#define DataType int
int SearchReverseK(LinkList head, int k, DataType* e);
int main()
{
DataType a[] = { 10,20,30,40,50,60,70,80,90,100 };
LinkList L;
InitList(&L);
int n;
n = sizeof(a) / sizeof(a[0]);
for (int i = 1; i <= n; i++)
{
InsertElem(L, i, a[i-1]);
}
printf("单链表L中公有%d个元素:", ListLength(L));
for (int i = 1; i <= n; i++)
{
LNode* p;
p=GetElem(L, i);
printf("%d ", p->data);
}
printf("\n");
printf("请输入要查找L中的倒数第几个值:");
int k;
scanf("%d", &k);
DataType e;
int flag;
flag = SearchReverseK(L, k, &e);
if (flag == 1)
{
printf("L中倒数第%d个节点的元素值是:%d\n", k, e);
}
else
{
printf("L中不存在倒数第%d个元素!\n");
}
return 0;
}
int SearchReverseK(LinkList head, int k, DataType* e)
{
int count = 0;
LNode* p, * q;
p = q = head->next;
while (p)
{
if (count < k)
{
count++;
}
else
{
q = q->next;
}
p = p->next;
}
if (count < k)
{
return 0;
}
else
{
*e = q->data;
return 1;
}
}