查找单链表的倒数第k个节点,要求只能遍历一次链表 点击打开链接
删除倒数第k个节点(要求遍历一次)
#define _CRT_SECURE_NO_WARNINGS 1
typedef int DataType;
#define NULL 0
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef struct Node
{
DataType data;
struct Node*next;
}Node, *LinkList;
void InitList(LinkList *L)
{
*L = (Node*)malloc(sizeof(Node));
if (*L == NULL){
printf("申请内存空间失败");
}
(*L)->next = NULL;
}
LinkList BuyNode(DataType data)
{
LinkList NewNode = NULL;
NewNode = (LinkList)malloc(sizeof(Node));
if (NewNode == NULL){
printf("为节点创建空间失败");
}
NewNode->data = data;
NewNode->next = NULL;
return NewNode;
}
void PrintList(LinkList L)
{
LinkList Cur = L;
if (L == NULL){
printf("NULL");
}
Cur = Cur->next;
while (Cur)
{
if (Cur ==NULL)
return;
printf("%d--->", Cur->data);
Cur = Cur->next;
}
printf("NULL\n");
}
void PushBack(LinkList* L, DataType data)
{
assert(L);
LinkList Cur = *L;
if (Cur == NULL){
Cur = BuyNode(data);
}
while (Cur->next)
{
Cur = Cur->next;
}
Cur->next = BuyNode(data);
}
LinkList DeleteLastKNode(LinkList L, int k)
{
LinkList Fast = L;
LinkList Slow = L;
LinkList Pre = NULL;
LinkList Del = NULL;
if (L == NULL || k <= 0)
return NULL;
while (k--){
if (Fast == NULL){
return NULL;
}
Fast = Fast->next;
}
while (Fast){
Fast = Fast->next;
Pre = Slow;
Slow = Slow->next;
}
if (Slow->next == NULL)
{
Pre->next = NULL;
}
else{
Del = Slow->next;
Slow->data = Del->data;
Slow->next = Del->next;
free(Del);
}
}
test3()
{
LinkList L = NULL;
InitList(&L);
PrintList(L);
PushBack(&L, 2);
PushBack(&L, 4);
PushBack(&L, 6);
LinkList S = DeleteLastKNode(L, 2);
PrintList(L);
}
int main()
{
test3();
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
typedef int DataType;
#define NULL 0
#include<stdio.h>
#include<windows.h>
#include<assert.h>
typedef struct Node
{
DataType data;
struct Node*next;
}Node, *LinkList;
void InitList(LinkList *L)
{
*L = (Node*)malloc(sizeof(Node));
if (*L == NULL){
printf("申请内存空间失败");
}
(*L)->next = NULL;
}
LinkList BuyNode(DataType data)
{
LinkList NewNode = NULL;
NewNode = (LinkList)malloc(sizeof(Node));
if (NewNode == NULL){
printf("为节点创建空间失败");
}
NewNode->data = data;
NewNode->next = NULL;
return NewNode;
}
void PrintList(LinkList L)
{
LinkList Cur = L;
if (L == NULL){
printf("NULL");
}
Cur = Cur->next;
while (Cur)
{
if (Cur ==NULL)
return;
printf("%d--->", Cur->data);
Cur = Cur->next;
}
printf("NULL\n");
}
void PushBack(LinkList* L, DataType data)
{
assert(L);
LinkList Cur = *L;
if (Cur == NULL){
Cur = BuyNode(data);
}
while (Cur->next)
{
Cur = Cur->next;
}
Cur->next = BuyNode(data);
}
LinkList DeleteLastKNode(LinkList L, int k)
{
LinkList Fast = L;
LinkList Slow = L;
LinkList Pre = NULL;
LinkList Del = NULL;
if (L == NULL || k <= 0)
return NULL;
while (k--){
if (Fast == NULL){
return NULL;
}
Fast = Fast->next;
}
while (Fast){
Fast = Fast->next;
Pre = Slow;
Slow = Slow->next;
}
if (Slow->next == NULL)
{
Pre->next = NULL;
}
else{
Del = Slow->next;
Slow->data = Del->data;
Slow->next = Del->next;
free(Del);
}
}
test3()
{
LinkList L = NULL;
InitList(&L);
PrintList(L);
PushBack(&L, 2);
PushBack(&L, 4);
PushBack(&L, 6);
LinkList S = DeleteLastKNode(L, 2);
PrintList(L);
}
int main()
{
test3();
system("pause");
return 0;
}