ultravnc 反向连接_C程序以反向显示链接列表

ultravnc 反向连接

Problem statement: Write a program to display the linked list in reverse order. Note that the original linked list will not change.

问题陈述:编写一个程序以相反的顺序显示链接列表。 请注意,原始链表不会更改

Solution

  1. Create and build the linked list

    创建并建立链接列表

  2. Display the original linked list

    显示原始链表

  3. 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 反向连接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值