Variables can be divided into global variables and local variables by Scope. It is recommended that you do not use global variables when necessary ", so I thought that if I could find an algorithm in the programming process to make it run effectively by using local variables for programs originally using global variables, is my work really useful and effective ?? I am confused because I did some small experiments in the process of learning the data structure and encountered some difficulties. (I used the C language of "algorithm and data structure)
My problem is some algorithms related to the basic functions of linked lists and some common data structures based on linked lists. For example, the deletion and insertion algorithms of the sequence tables written in the textbooks I use (they are functions described in Class C), and their return values are void. Due to the function calling mechanism, we can only define a global sequence table in the main function to call those void functions. However, I made this change and changed the void type to a custom linked list type. In this way, I can define local custom linked list variables in the main function, the modified function can also be successfully called to implement some basic functions of the linked list: The following is an example after the individual changes: (I use VC6.0)
# Include <stdio. h>
# Include <stdlib. h>
# Define max size 500
Typedef int ElemType; // data element
Typedef struct // sequential linked list structure
{
ElemType data [MAXSIZE]; //
Int leght ;//
} MyList; // my linked list
MyList InitList () // initialization of the sequence table // This is changed
{
MyList L;
L. leght = 0;
Return L;
}
Int ListLeght (MyList L) // calculate the length of the linked list
{
Return L. leght;
}
ElemType GetElem (MyList L, int I) // Evaluate the elements of the linked list
{
Return L. data [i-1];
}
ElemType LocateElem (MyList L, ElemType x) // query linked list elements by value
{
Int I;
While (I <= L. leght & L. data [i-1]! = X)
I ++;
If (I <= L. leght)
Return I;
Else
Return 0;
}