Thực hành lập trình bao gồm tất cả các chủ đề này, và nhiều hơn nữa. Cuốn sách này là đầy đủ các lời khuyên thực tế và thực tế thế giới ví dụ trong C, C + +, dung nham, và một loạt các ngôn ngữ mục đích đặc biệt. Nó bao gồm các chương về: gỡ lỗi: việc tìm kiếm lỗi một cách nhanh chóng | 46 ALGORITHMS AND DATA STRUCTURES CHAPTER 2 The routine emal 1 oc is one we ll use throughout the book it calls mal 1 oc and if the allocation fails it reports the error and exits the program. We ll show the code in Chapter 4 for now it s sufficient to regard emal loc as a memory allocator that never returns failure. The simplest and fastest way to assemble a list is to add each new element to the front ft addfront add newp to front of listp Nameval addfront Nameval Alistp Nameval newp newp- next l i s t p return newp I When a list is modified it may acquire a different first element as it does when addfront is called. Functions that update a list must return a pointer to the new first element which is stored in the variable that holds the list. The function and other functions in this group all return the pointer to the first element as their function value a typical use is nvlist addf rontfnvli s t newitemC smiley Ox263A This design works even if the existing list is empty null and makes it easy to combine the functions in expressions. seems more natural than the alternative of passing in a pointer to the pointer holding the head of the list. Adding an item to the end of a list is an procedure since we must walk the list to find the end addend add newp to end of listp a Nameval addend Nameval Alistp Nameval Anewp Nameval if Clistp NULL return newp for p l i s tp p- next NULL p p- next newp return listp I If we want to make addend an 0 1 operation. we can keep a separate pointer to the end of the list. The drawback to this approach besides the bother of maintaining the end pointer is that a list is no longer represented by a single pointer variable. We ll stick with the simple style. To search for an item with a specific name follow the next pointers SECTION LISTS 47 lookup sequential search for name in listp Nameval lookup Nameval listp char name for listp NULL listp listp- next if strcmpfname listp- name 0 return l ist p return NULL no match This takes ơ ỉ .