Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use C language to realize train scheduling algorithm recursively

Shulou Source: shulou.com Published: 2022-06-01 09:46:47 09月12日 Update

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

Tags: Code train train function element parameter station array output scheduling result algorithm recursion at the same time bit that is sequence railway station original number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Huawei macOS OPPO Reno