Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to realize train scheduling algorithm recursively

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use C language recursive train scheduling algorithm, the text is very detailed, has a certain reference value, interested friends must read!

1. Code

The title is as follows:

2.8 Four trains numbered 1, 2, 3 and 4 pass through a stack of train dispatching stations. What are the possible dispatching results? If there are n trains passing through the dispatching station, please design an algorithm to output all possible dispatching results.

The idea of algorithm is to use stack + recursion, and the difficulty of algorithm lies in this. First the code:

#include #define MAX 100typedef struct s{ char a[MAX]; int top;}Stack;/* Define stack data *//* Define some global variables */Stack S;/* Define global stack */char d[MAX],seq[MAX];/*d[MAX] is used to store the original stack sequence, seq[MAX] is used to store the output sequence */int len;/* Defines the number of elements that will pass through the stack */ int count=0;/* is used to count the number of output sequences */void initStack(Stack *S) /* initializes the empty stack */{ S->top=-1;}void push(Stack *S,char x) /* push */{ if(S->top>=MAX) return; S->top++; S->a[S->top]=x;}char pop(Stack *S) /* pop */{ if (S->top==-1) { printf("ERROR, POP Empty Stack"); return -1; } S->top--; return S->a[S->top+1]; } int isEmpty(Stack *S)/* Determine whether the stack is empty */ { if (S->top==-1) return 1; return 0; } void outSeq(char *seq, int len)/* Output vertex sequence */ { int i; for(i=0; i

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report