Thoughts on the relationship between global variables and local variables and linked lists

Source: Internet
Author: User
Tags data structures define local

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.