#include "stdio.h"
#include <stdlib.h>
#define n 5
struct node {
char data;
struct node *next;
};
int main() {
struct node *head = NULL, *p, *q;
char ch;
while (ch = getchar(), ch!= '#') {
p = (struct node *)malloc(sizeof(struct node));
p->data = ch;
p->next = NULL;
if (head == NULL) {
head = p;
q = p;
} else {
q->next = p;
q = p;
}
}
if (head == NULL || head->next == NULL) {
printf("链表节点不足两个,无法输出倒数第二个节点内容。\n");
} else {
p = head;
while (p->next->next!= NULL) {
p = p->next;
}
printf("%c\n", p->data);
}
// 释放链表内存
while (head!= NULL) {
q = head;
head = head->next;
free(q);
}
return 0;
}
输出链表中倒数第二个结点的数据(以#结束)
最新推荐文章于 2024-10-08 17:22:14 发布