"THE C Programming Language" Reading Note 4

xiaoxiao2021-03-06  99

Program design

So far, we have a complete understanding of the basic elements of the language, but always stay in the details of expressions, we are difficult to write the program, and today any program is a project, how to organize The basic elements we have mastered have made them have a function of organic overall. This requires an overall concept of design ideas. In the first step in C, it is a process of designing ideas. In other words, The design of the function, we have seen in the previous text, how is the core problem to break down the problem to be resolved, write a function with independent functions, and then enter the interface function (in the console environment, usually the main function ) Composition complete procedures. But the image, the problem we can solve is quite limited, because in practical applications, we have to deal with the simple built-in type (int, char, etc.), but rather than these complex multi-data types, so the second Step how to write abstract models for specific problems, that is, ADT (Abstract Data Type), which enables object-based design ideas, and learning pointers and structures need to explore such ideas, and will pass a simple list. The design of the linked list briefly explains how to create a complete program.

The first step is to establish an empty project. It is best not to choose the "Console Program" template, which makes your design ideas clearly understand, remember that you are studying now, convenient and fast is not your pursuit.

In the second step, calm down and think about it. Your linked list is to provide those interfaces that can be modified to users (such as specific data types), which are placed in the list of list.h files. In this article, we provide we provide initialization, destruction, increase nodes, delete nodes, insert nodes, find, and print output. Then add a file called llst.h in the project, the input code is as follows:

#ifndef list_h

#define list_h

/ * Define function status * /

#ifndef ERR

#define err -1

#define ok 1

#ENDIF

Typedef int stat;

TypeDef void type; / * Users can change this type according to the specific needs * /

Typedef struct ListItem {

TYPE DATE; / * Node Data * /

Struct ListItem * next; / * points to the next node * /

} list_node; // Link table node

Typedef struct {

Struct ListItem * PTR; / * Link table head pointer * /

INT size; / * Link table length * /

} list; // Link list

List * list_init (void); / * Initialization * /

Status List_Destroy (list *); / * Destroy * /

STATUS ADD_NODE (List *, const type); / * Add a node * /

STATUS DELETE_ALL (List *); // Clear

Status delete_node (list *, list_node *); / * Delete a node * /

STATUS INSERT_NODE (List *, const type); / * Insert a node * /

List_node * find_node (const list *, const type); / * Find * /

STATUS LIST_PRINT (const list *); / * Print * /

#ENDIF

In the third step, add the list.c file in the project. Include has just established header files and implements each pole. Since this file is not a user visible in normal case (except for maintenance problems), the author does not add any comments. Of course, this is not a good habit, it is too simple here, and the comment is excessive. The first is the header file required for include:

#include "stdio.h"

#include "stdlib.h"

/ * Strictly speaking, the above-mentioned angle brackets, because the webpage is displayed * /

#include "list.h"

Next is the implementation of initialization and destruction

List * list_init (void)

{

List * p = (list *) malloc (sizeof (list));

IF (p == 0)

Return 0;

P-> PTR = 0;

P-> size = 0;

Return P;

}

Status List_Destroy (List * PEV)

{

IF (PEV == 0)

Return ERR;

Delete_all (PEV);

Free (pev);

Return OK;

}

According to reason, the function cannot return a pointer, huh, huh, there is a number of initiators misunderstand, returning to the left value of the local object, the reference to the local object (the latter is the statement of C ) is indeed, because local objects Assignment in the activity record (ie, the function call stack) is assigned, once the function is ended, the returned will be invalid. Therefore, the following functions are wrong,

INT * f ()

{

INT * P, A;

P = & a;

Return P;

}

However, by malloc is allocated on the heap, he will not be recycled as the function is over. But this is quite careful, and memory leaks must be prevented. You must fall the space before the end of the program.

Here is the complete list.c

#include "stdio.h"

#include "stdlib.h"

/ * Strictly speaking, the use of angle brackets, because the webpage is displayed * /

#include "list.h"

List * list_init (void)

{

List * p = (list *) malloc (sizeof (list));

IF (p == 0)

Return 0;

P-> PTR = 0;

P-> size = 0;

Return P;

}

Status List_Destroy (List * PEV)

{

IF (PEV == 0)

Return ERR;

Delete_all (PEV);

Free (pev);

Return OK;

}

Status add_node (list * p, const type date)

{

List_node * pev =

(List_node *) Malloc (sizeof (list_node));

IF (PEV == 0)

Return ERR;

PEV-> DATE = DATE;

PEV-> Next = P-> PTR;

P-> PTR = pev;

P-> Size ;

Return OK;

}

Status delete_node (List * p, list_node * pev)

{

List_node * temp = pev; if (PEV == 0)

Return ERR;

PEV = TEMP-> NEXT;

Free (TEMP);

P-> size -

Return OK;

}

Status Delete_all (List * PEV)

{

IX;

IF (PEV == 0)

Return ERR;

IF (pev-> size = 0)

Return ERR;

For (ix = 0; ix size; ix, pev-> PTR)

Delete_node (pev, pev-> ptr);

Return OK;

}

Status INSERT_NODE (List * P, Const Type Date)

{

List_node * pev = p-> PTR;;

IF (p == 0)

Return ERR;

PEV = Find_Node (p, date);

IF (PEV == 0)

{

TYPE IA;

Printf ("Number / N of the input to be inserted);

Scanf ("% D", & IA);

Add_node (p, IA);

}

Else

{

TYPE IA;

List_node * pv =

(List_node *) Malloc (sizeof (list_node));

IF (PEV == 0)

Return ERR;

Printf ("Number / N of the input to be inserted);

Scanf ("% D", & IA);

PV-> DATE = IA;

PV-> Next = pev-> next;

PEV-> Next = PV;

P-> Size ;

}

Return OK;

}

List_node * find_node (const list * pev, const type date)

{

IX;

List_node * p = pev-> PTR;

For (ix = 0; ix size; ix)

IF (P-> Date == Date)

Return P;

Else

P = P-> next;

Return 0;

}

Status List_Print (const list * pev)

{

IX;

List_node * p = pev-> PTR;

IF (PEV == 0)

Return ERR;

IF (pev-> size == 0)

Return OK;

For (ix = 0; ix size; ix)

{

Printf ("% D / T", P-> Date);

P = P-> next;

}

Printf ("/ n");

Return OK;

}

The fourth step, write a main function, because the personal debugging method is different, this is not given here. Just make sure that each function can work properly.

转载请注明原文地址:https://www.9cbs.com/read-124578.html

New Post(0)