Single-Chain tables are output in reverse or reverse order, and single-chain tables are output in reverse order.
There are two situations: one is only reverse output, but not reverse output; the other is reverse output of the linked list.
****************** *****
1 # include <iostream> 2 # include <stack> 3 # include <assert. h> 4 using namespace std; 5 6 7 typedef struct node {8 int data; 9 node * next; 10} node; 11 12 // add 13 node * add (int n, node * head) {14 node * t = new node; 15 t-> data = n; 16 t-> next = NULL; 17 if (head = NULL) {18 head = t; 19} 20 else if (head-> next = NULL) {21 head-> next = t; 22} 23 else {24 node * p = head-> next; 25 while (p-> next! = NULL) {26 p = p-> next; 27} 28 p-> next = t; 29} 30 return head; 31} 32 33 // output 34 void print (node * head) {35 node * p = head; 36 while (p! = NULL) {37 cout <p-> data <""; 38 p = p-> next; 39} 40 cout <endl; 41} 42 43 // recursion 44 void reversePrint (node * p) {45 if (p! = NULL) {46 reversePrint (p-> next); 47 cout <p-> data <""; 48} 49} 50 51 // stack 52 void reversePrint2 (node * head) {53 stack <int> s; 54 while (head! = NULL) {55 s. push (head-> data); 56 head = head-> next; 57} 58 59 while (! S. empty () {60 cout <s. top () <"; 61 s. pop (); 62} 63} 64 65 int main () {66 67 node * head = NULL; 68 for (int I = 1; I <= 5; I ++) {69 head = add (I, head); 70} 71 print (head); 72 reversePrint (head); 73 reversePrint2 (head); 74 system ("pause "); 75 return 0; 76}
There are three methods for reverse output: recursion, stack, and backward output. The last one will be discussed later.
* ******************* Reverse sequence of a single-chain table **************** ****
1 # include <iostream> 2 # include <stack> 3 # include <assert. h> 4 using namespace std; 5 6 7 typedef struct node {8 int data; 9 node * next; 10} node; 11 12 node * add (int n, node * head) {13 node * t = new node; 14 t-> data = n; 15 t-> next = NULL; 16 if (head = NULL) {17 head = t; 18} 19 else if (head-> next = NULL) {20 head-> next = t; 21} 22 else {23 node * p = head-> next; 24 while (p-> next! = NULL) {25 p = p-> next; 26} 27 p-> next = t; 28} 29 return head; 30} 31 32 // loop 33 node * reverse (node * head) {34 35 if (head = NULL | head-> next = NULL) {36 return head; 37} 38 39 node * p1 = head; 40 node * p2 = head-> next; 41 node * p3 = NULL; 42 head-> next = NULL; 43 44 while (p2! = NULL) {45 p3 = p2; 46 p2 = p2-> next; 47 p3-> next = p1; 48 p1 = p3; 49} 50 head = p1; 51 return head; 52} 53 54 void print (node * head) {55 node * p = head; 56 while (p! = NULL) {57 cout <p-> data <""; 58 p = p-> next; 59} 60 cout <endl; 61} 62 63 64 // recursive 65 node * reverse2 (node * p) {66 if (p = NULL | p-> next = NULL) {67 return p; 68} 69 70 node * newHead = reverse2 (p-> next); 71 p-> next = p; 72 p-> next = NULL; 73 return newHead; 74} 75 76 77 int main () {78 79 node * head = NULL; 80 for (int I = 1; I <= 5; I ++) {81 head = add (I, head); 82} 83 print (head); 84 head = reverse (head); 85 print (head ); 86 head = reverse2 (head); 87 print (head); 88 89 system ("pause"); 90 return 0; 91}
Here, the linked list adopts two methods in reverse order: loop and recursion. You can draw a picture on the paper.
C language dynamic one-way linked list reverse output ???
# Include <stdio. h>
# Include <stdlib. h>
Struct node {
Int num;
Struct node * next;
};
Void display (node * p)
{
If (p = NULL)
{
Return;
}
Else
{
Display (p-> next );
Printf ("% d \ n", p-> num );
Return;
}
Return;
}
Int main ()
{
Struct node * head, * tail, * p;
Int num;
Int size = sizeof (struct node );
Head = tail = NULL;
Printf ("input number: \ n ");
Scanf ("% d", & num );
While (num! = 0 ){
P = (struct node *) malloc (size );
P-> num = num;
P-> next = NULL;
If (head = NULL)
Head = p;
Else
Tail-> next = p;
Tail = p;
Scanf ("% d", & num );
}
Display (head );
Return 0;
}
How can I reverse the order of a one-way linked list?
Output A linked list in reverse order
If the header node is L, yes;
P = q = L;/* p, q is the two pointers pointing to the header node */
While (p-> next! = NULL)
P = p-> next;/* point p to the last node in the key table */
While (1)
{
While (q-> next! = P)
Q = q-> next;/* Ask q to find the last node to be printed */
Printf ("% d \ n", p-> data );
P = q;/* p moves one forward */
Q = L;/* q points to the header node again */
If (p = L)/* exit after access */
Break;
}
Your reference