ultravnc 反向连接
Problem statement: Write a program to display the linked list in reverse order. Note that the original linked list will not change.
问题陈述:编写一个程序以相反的顺序显示链接列表。 请注意,原始链表不会更改 。
Solution
解
Display in reverse order
反向显示
Displaying in reverse order can be done using recursive function.
可以使用递归功能以相反的顺序显示。
Function reverse_display(node)
IF (node!= NULL)
reverse_display(node->next)
display node value
END IF
Example with Explanation:
解释示例:
Let's check how the program runs...
让我们检查程序的运行方式...
Let the input linked list to be: 1->2->3->4->NULL with head at 1
让输入链接列表为: 1-> 2-> 3-> 4-> NULL,head为1
In Main() it calls
reverse_display(1)
----------------------------------------------------------------
reverse_display(1):
node is not null
reverse_display(1->next) thus it calls reverse_display(2)
----------------------------------------------------------------
reverse_display(2):
node is not null
reverse_display(2->next) thus it calls reverse_display(3)
----------------------------------------------------------------
reverse_display(3):
node is not null
reverse_display(3->next) thus it calls reverse_display(4)
----------------------------------------------------------------
reverse_display(4):
node is not null
reverse_display(4->next) thus it calls reverse_display(NULL)
----------------------------------------------------------------
reverse_display(NULL):
node is null
no further call, control returned to reverse_display(4)
----------------------------------------------------------------
At reverse_display(4)
Control returned from reverse_display(4->next)
So it prints the node value that is 4
Control returns to reverse_display(3)
----------------------------------------------------------------
At reverse_display(3)
Control returned from reverse_display(3->next)
So it prints the node value that is 3
Control returns to reverse_display(2)
----------------------------------------------------------------
At reverse_display(2)
Control returned from reverse_display(2->next)
So it prints the node value that is 2
Control returns to reverse_display(1)
----------------------------------------------------------------
At reverse_display(1)
Control returned from reverse_display(1->next)
So it prints the node value that is 1
Control returns to Main function
Thus it displays 4 3 2 1
C实现以反向显示链接列表 (C implementation to display Linked List in Reverse)
#include <stdio.h>
#include <stdlib.h>
struct node{
int data; // data field
struct node *next;
};
void display(struct node* head){
struct node* current=head; // current node set to head
printf("traversing the list...\n");
while(current!=NULL){ //traverse until current node isn't NULL
printf("%d ",current->data);
current=current->next; // go to next node
}
}
void reverse_display(struct node* head){
if(head){
//recursive call to display in reverse order
reverse_display(head->next);
printf("%d ",head->data);
}
}
struct node* creatnode(int d){
struct node* temp=malloc(sizeof(struct node));
temp->data=d;
temp->next=NULL;
return temp;
}
int main(){
printf("creating the linked list by inserting new nodes at the end\n");
printf("enter 0 to stop building the list, else enter any integer\n");
int k,count=1,x;
struct node* curr,*temp;
scanf("%d",&k);
struct node* head=creatnode(k); //buliding list, first node
scanf("%d",&k);
temp=head;
///inserting at the end//
while(k){
curr=creatnode(k);
temp->next=curr;//appending each node
temp=temp->next;
scanf("%d",&k);
}
display(head); // displaying the list
printf("\ndisplaying in reverse order...\n");
reverse_display(head);//display in reverse order
return 0;
}
Output
输出量
First run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
1 2 3 4 0
traversing the list...
1 2 3 4
displaying in reverse order...
4 3 2 1
Second run:
creating the linked list by inserting new nodes at the end
enter 0 to stop building the list, else enter any integer
34 55 2 4 76 -8 6 0
traversing the list...
34 55 2 4 76 -8 6
displaying in reverse order...
6 -8 76 4 2 55 34
翻译自: https://www.includehelp.com/c-programs/display-a-linked-list-in-reverse.aspx
ultravnc 反向连接