不改变链表结构,反向打印链表

本文介绍了一种使用栈来实现链表逆序遍历的方法。通过将链表节点逐个压入栈中,再依次弹出并打印,从而达到逆序输出链表元素的效果。该方法适用于计算机科学领域的数据结构学习。
void PrintListReversignly_Iteratively(ListNode* pHead)
{
	std::stack<ListNode*> nodes;
	
	ListNode * pNode = pHead;
	while(pNode != NULL)
	{
		nodes.push(pNode);
		pNode = pNode->m_pNext;
	}
	while(!nodes.empty())
	{
		pNode = nodes.top();
		printf("%d\t",pNode->m_pData);
		nodes.pop();
	}
}


 

在双链表中,正向和反向打印的实现原理与双链表结构特点紧密相关。双链表的每个节点除了包含数据域,还包含指向前一个节点的指针(通常称为 `prev` 或 `bwd`)和指向后一个节点的指针(通常称为 `next` 或 `fwd`)。 ### Java 实现双链表正向和反向打印 以下是 Java 语言实现双链表正向和反向打印的示例代码: ```java class Node { int data; Node prev; Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } class DoublyLinkedList { private Node head; private Node tail; public DoublyLinkedList() { this.head = null; this.tail = null; } // 向链表添加元素 public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } // 正向打印链表 public void printForward() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } // 反向打印链表 public void printBackward() { Node current = tail; while (current != null) { System.out.print(current.data + " "); current = current.prev; } System.out.println(); } public static void main(String[] args) { DoublyLinkedList dll = new DoublyLinkedList(); int[] a = {1, 2, 3, 4, 5}; for (int num : a) { dll.add(num); } System.out.print("正向:"); dll.printForward(); System.out.print("反向:"); dll.printBackward(); } } ``` 在上述 Java 代码中,`printForward` 方法从链表的头节点开始,通过 `next` 指针逐个访问节点并打印节点的数据,直到链表末尾。`printBackward` 方法从链表的尾节点开始,通过 `prev` 指针逐个访问节点并打印节点的数据,直到链表头部。 ### C 语言实现双链表正向和反向打印 以下是 C 语言实现双链表正向和反向打印的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义双链表节点结构 typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; // 创建新节点 Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; return newNode; } // 向链表添加元素 void addNode(Node** head, Node** tail, int data) { Node* newNode = createNode(data); if (*head == NULL) { *head = newNode; *tail = newNode; } else { (*tail)->next = newNode; newNode->prev = *tail; *tail = newNode; } } // 正向打印链表 void printForward(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } // 反向打印链表 void printBackward(Node* tail) { Node* current = tail; while (current != NULL) { printf("%d ", current->data); current = current->prev; } printf("\n"); } int main() { Node* head = NULL; Node* tail = NULL; int a[] = {1, 2, 3, 4, 5}; int size = sizeof(a) / sizeof(a[0]); for (int i = 0; i < size; i++) { addNode(&head, &tail, a[i]); } printf("正向:"); printForward(head); printf("反向:"); printBackward(tail); return 0; } ``` 在上述 C 语言代码中,`printForward` 函数从链表的头节点开始,通过 `next` 指针逐个访问节点并打印节点的数据,直到链表末尾。`printBackward` 函数从链表的尾节点开始,通过 `prev` 指针逐个访问节点并打印节点的数据,直到链表头部。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值