The back-interpolation method establishes the linked list, and then starts with the second element (if any) to insert after the end node to achieve the reverse list effect.
Topic Description: Enter a list, reverse the linked list, output linked list of all elements.
(Hint: Make sure to use the linked list)
Input:
The input may contain more than one test sample, and the input ends with EOF.
For each test case, enter the first behavior of an integer n (0<=n<=1000): Represents the number of linked lists that will be entered.
The second line entered contains n integers t (0<=t<=1000000): Represents a list element.
Output:
corresponding to each test case,
The element that is reversed with this output list, or null if there are no elements.
Sample input:
5
1 2 3 4 5
0
Sample output:
5 4 3 2 1
Null
#include <iostream> #include <cstdlib> using namespace std;
typedef struct LNODE {struct Lnode *next;
int data;
}*linklist;
Linklist createlist (linklist list,int n) {int num=0;
Linklist P=null,q=null;
p= (linklist) malloc (sizeof (linklist *));
list=p;
p->next=null;
for (int i=0;i<n;i++) {q= (linklist) malloc (sizeof (linklist *));
cin>>num;
q->data=num;
q->next=null;
p->next=q;
p=q;
} return list;
} linklist desendlist (linklist list) {linklist p=list->next,q=p->next,s=null;
p->next=null;
while (q)//q points to the element {s=q that needs to be inserted after the header node;
q=q->next;
s->next=p;
list->next=s;
P=s;
} return list;
} void Traveselist (linklist list) {linklist p=list->next;
if (p) {cout<<p->data;
p=p->next;
else {cout<< "NULL" <<endl;return;}
while (p) {cout<< ' ' <<p->data;
p=p->next;
} cout<<endl;
int main () {int n;
while (cin>>n) {if (!n) {cout<< "NULL" <<endl;continue;}
Linklist List=null;
List=createlist (list, n);
List=desendlist (list);
Traveselist (list);
return 0;
}/************************************************************** problem:1518 USER:HNDXZTF language:c++ result:accepted time:250 Ms memory:2972 KB ****************************************************************/