C + + Training: Language link Table Analysis

Source: Internet
Author: User

C + + training before a classmate again ask C language link table how broken? Today, small series of this knowledge points to share out, there are doubts can see more!

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161009/1-161009105516335.png "alt=" Training "style=" border:0px; "/>

Believe that the people who have learned C are more or less aware of the linked list, the list is also an important part of the data structure, today to talk about the simplest one-way dynamic linked list of the establishment and output. First of all to understand what is linked list, linked list is an important dynamic data structure in the program design, is a dynamic storage allocation of a structure. The dynamic main performance in the element position can be changed, that is, arbitrarily delete arbitrary insertion, etc., the number of elements can be increased and reduced, unlike the array after the length of the declaration is fixed. This reminds of some time ago someone in the group asked how to delete any element in the group, if there is no linked list just started to learn to feel will not start, here is a snippet of code you can see:


int main ()


int a[6]={1,2,3,4,5,6};

int n,i,j;

printf ("Enter the number to delete:");

scanf ("%d", &n);

for (i=0;i<6;i++)

if (a[i]==n)


for (j=i;j<6-1;j++)



for (i=0;i<6-1;i++)

printf ("%d\n", A[i]);

return 0;


The results of the operation are as follows:

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161009/1-161009105529313.png "alt=" Training "style=" border:0px; "/>

is to make the array element move forward one bit from the deleted position, but the last element still exists, you can try it yourself, this does not save memory, but the list is deleted is different. The so-called dynamic popular saying is used to open up space, not to release space. The functions of dynamic storage allocation are mainly (malloc (), Calloc (), realloc (), free ()) Everyone is interested to know for themselves. And then just look at how to set up the list, the first step we understand the list of elements called nodes, each node contains data fields and the two parts of the pointer field. This node data field can contain many aspects of information, which will be used in the previous structure, the data field is easy to understand, the pointer field is the next node to hold the pointer is the address, so that the connection between each node is established. The second step is to define the node of the linked list, at which point we have to understand that the definition of this struct breaks the limit of what is defined in the C language, that is, you can define yourself by yourself, such as the definition of a recursive function. Now that you want to point to the pointer to the next node, the member of the struct must contain a pointer variable, which can point to other types of struct data, or to the struct type data of its own. Cases:

Struct ST


int num;

Char name[20];/* can also be written as Char *name so it can be unlimited in length, but the compiler may not be able to allocate space for this, so here's a character array */

Struct St *next;//next is a member of the struct St type, which also points to the struct St type data.


Where the data in the node is defined as needed;

The third step is to create a linked list, you must be thinking of the last node (tail node) to do it? Since the last node is definitely not pointing, it points to null (a null pointer: that does not point to any position), and the tail node has a head node, so we can specify a head pointer ( This pointer name can be arbitrarily defined, not specified), to point to the list header; Define the function struct St *creat (void) to create a linked list; Here we define three struct ST struct pointer variables, as follows:

Struct St *head,*p1,*p2; /*head head pointer, p1 points to the new node, P2 points to the tail node */

First, the dynamic storage non-function malloc () is used to open up space for p1,p2, p1=p2= (struct St *) malloc (sizeof (struct st));

Then create a new node to point Head,p1,p2 to that node, define the global variable N to determine whether to point to the table header, and also set a node as the tail node of the flag, where the num==0 is set;

Create two new nodes, P1 point to the new node, and place the second node behind the first node p2->next=p1;

Make the second node the end of the table, and then follow this step to establish the node's connection until you enter NUM as 0 o'clock the last node p2->next=null; here's the idea. The data item of the last node is the data field is not into the linked list;

Finally, the definition of void print (struct St *head) function output: First find the head pointer and then format output the node data item, define the struct St *p; and then move the pointer back:p=p->next; how does the output end here? This is used in the loop to judge, here I choose do while, so the main () function calls the above two function program is executed, simple list creation, the output is over. Here's the code:



Struct ST


int num;

Char name[20];

Float score;

struct St *next;


int n;

struct St *creat (void)


struct St *head,*p1,*p2;


p1=p2= (Struct St *) malloc (sizeof (struct st));

scanf ("%d%s%f", &p1->num,p1->name,&p1->score);


while (p1->num!=0)



if (n==1) head=p1;

Else (P2->next) =p1;


p1= (Struct St *) malloc (sizeof (struct st));

scanf ("%d%s%f", &p1->num,p1->name,&p1->score);


(P2->next) =null;

return head;


void print (struct St *head)


struct St *p;


if (head!=null)



printf ("%d\t%s\t%f\n", P->num,p->name,p->score);


}while (P!=null);


void Main ()


struct St *head;

Head=creat ();

Print (head);


The results of the operation are as follows:

650) this.width=650; "src=" Http://www.cdtedu.com/uploads/allimg/161009/1-161009105544136.png "alt=" Training "style=" border:0px; "/>

Two examples of C-language scopes

A first example: #include

int a=0; Global variables

void foo (void);

int main (void) {

int a=2; Local variables within the main function

int b=3; Local variables within the main function

printf ("1. Main_b =%d\n ", b);

printf ("main_a =%d\n", a);

Foo ();

printf ("2. Main_b =%d\n ", b);


void foo (void) {

int b=4; Local variables within the Foo function

printf ("foo_a =%d\n", a);

printf ("Foo_b =%d\n", b);

} output: 1. Main_b = 3main_a = 2foo_a = 0foo_b = 42. Main_b = 3 A second example: #include

int x = 2;

int y = 3;

int z = 4;

void Moo (int x, int *y) {

int z;

x = x+3;

*y = *y+3;

z = z+3;


* Here Z is the local variable.

* Note: Z is not initialized manually.

* However, the output from the following shows that Z is automatically initialized to 0 by the compiler.

* In general, the compiler will have a warning telling you that Z is not initialized


printf ("moo:x =%1d, *y =%1d, y =%1d, z =%1d\n", x,*y,y,z);


int main (void) {

Moo (x, &y);

printf ("main:x =%1d1, y =%1d, z =%1d\n", x, Y, z);

Output: moo:x = 5, *y = 6, y = 1073742056, z = 3main:x =, y = 6, z = 4

C + + Training: Language link Table Analysis

Related Article

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.