倒数第k个结点

这篇文章我们来看看倒数第k个结点:

什么倒数第k个结点?就是立刻打印出链表中倒数第k个结点的数据。怎么实现呢?我们直接看代码:

  1 #define _CRT_SECURE_NO_WARNINGS 1
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 struct Node {
  5     int data;
  6     struct Node* next;
  7 };
  8 
  9 struct Node* CreateList()
 10 {
 11     struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
 12     headNode->next = NULL;
 13     return headNode;
 14 }
 15 
 16 struct Node* CreateNode(int data)
 17 {
 18     struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 19     newNode->data = data;
 20     newNode->next = NULL;
 21     return newNode;
 22 }
 23 
 24 int ListLength(struct Node* headNode)
 25 {
 26     int len = 0;
 27     while (headNode)
 28     {
 29         headNode = headNode->next;
 30         len++;
 31     }
 32     return len;
 33 }
 34 
 35 void InsertNode(struct Node* headNode, int index, int data)
 36 {
 37     if (!headNode || index <= 0 || index > ListLength(headNode)) {
 38         return -1;
 39     }
 40     else if (index == 1) {
 41         struct Node* newNode = CreateNode(data);
 42         newNode->next = headNode->next;
 43         headNode->next = newNode;
 44     }
 45     else {
 46         struct Node* temp = headNode;
 47         for (int i = 0; i < index - 1; i++) {
 48             temp = temp->next;
 49         }
 50         struct Node* newNode = CreateNode(data);
 51         newNode->next = temp->next;
 52         temp->next = newNode;
 53     }
 54 }
 55 
 56 void PrintList(struct Node* headNode)
 57 {
 58     struct Node* pMove = headNode->next;
 59     while (pMove)
 60     {
 61         printf("%d ", pMove->data);
 62         pMove = pMove->next;
 63     }
 64     printf("\n");
 65 }
 66 
 67 int PrintDataOfKFromTheLast(struct Node* headNode, int k)  //打印倒数第k个结点数据
 68 {
 69     int i, count = 0;
 70     int len = ListLength(headNode);   //len记录当前链表长度
 71     if (k >= ListLength(headNode)) {
 72         return -1;
 73     }
 74     struct Node* pMove, * pFollow;
 75     if (!headNode) {  //如果当前链表不存在,返回-1
 76         return -1;
 77     }
 78     pMove = headNode;  //这两个工作指针都默认指向headNode
 79     pFollow = headNode;
 80     while (pMove)
 81     {
 82         pMove = pMove->next;  //控制pMove指针移动
 83         count++;  //计数器自增
 84         if (count > k) {   //等到pMove指针向前走了k步之后,pFollow指针开始向前移动
 85             pFollow = pFollow->next;
 86         }
 87     }  //循环结束的时候,pFollow指针所指向的数据正好是倒数第k个结点
 88     printf("倒数第%d个结点为:%d\n", k,pFollow->data);
 89     return 1;
 90 }
 91 
 92 int main()
 93 {
 94     int i, pos, data, k;
 95     struct Node* pl = CreateList();
 96     while (1)
 97     {
 98         printf("1.往链表中输入数据;2.打印链表;3.打印倒数第k个结点;4.退出:");
 99         scanf("%d", &i);
100         if (i == 1) {
101             printf("请输入你想要添加的位置:");
102             scanf("%d", &pos);
103             printf("请输入你想要添加的数据:");
104             scanf("%d", &data);
105             InsertNode(pl, pos, data);
106         }
107         else if (i == 2) {
108             printf("链表为:\n");
109             PrintList(pl);
110         }
111         else if (i == 3) {
112             printf("请输入k:");
113             scanf("%d", &k);
114             PrintDataOfKFromTheLast(pl, k);
115         }
116         else if (i == 4) {
117             break;
118         }
119     }
120     return 0;
121 }

来看看运行效果:

 

posted @ 2021-05-16 11:12  EvanTheBoy  阅读(45)  评论(0编辑  收藏  举报