Data structure C language implementation-Linear Linked List
Declaration. h
#ifndef DECLARATION_H_INCLUDED#define DECLARATION_H_INCLUDED#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define ElemType inttypedef ElemType* Triplet;typedef int Status;typedef struct LNode{ ElemType data; struct LNode *next;}LNode, *LinkList;#endif // DECLARATION_H_INCLUDED
Function. h
# Ifndef FUNCTION_H_INCLUDED # define FUNCTION_H_INCLUDEDvoid CreateList_L (LinkList * L, int n); // enter values of n elements in reverse order. The LStatus ListInsert_L (LinkList * L, int I, ElemType e); // Insert the element eStatus ListDelete_L (LinkList * L, int I, ElemType e) before position I in the single-chain linear table of the lead node ); // In the single-chain linear table of the leading node, delete the I-th element and e returns its value Status GetElem_L (LinkList L, int I, ElemType * e ); // L is the header pointer of the single-chain table of the leading node // when the I-th element exists, the value is paid to e and OK is returned; otherwise, ERRORStatus MergeList_L (LinkList * La, linkList * Lb, LinkList * Lc); // merge the La AND Lb tables to Lc and arrange void PrintList_L (LinkList L) in a non-decreasing sequence ); // output the content in the single-chain table # endif // FUNCTION_H_INCLUDED
Function. c
# Include
# Include
# Include "declaration. h "void CreateList_L (LinkList * L, int n) {Status I; LinkList p; * L = (LinkList) malloc (sizeof (LNode); (* L) -> next = NULL; // create a single-chain table of the lead node first.-> do not omit the parentheses for (I = n; I> 0 if the priority is higher; I --) {p = (LinkList) malloc (sizeof (LNode); // generate a new node scanf ("% d", & p-> data ); p-> next = (* L)-> next; // insert it to the header (* L)-> next = p; // The data is inserted into the connected table backwards, that is, the last number entered is in the header} // CreateList_LStatus ListInsert_L (LinkList * L, int I, ElemType e) {Lin KList p = * L, s; ElemType j = 0; while (p & j <i-1) {p = p-> next; j ++;} if (! P | j> i-1) return ERROR; // I less than 1 or greater than the table length plus 1 s = (LinkList) malloc (sizeof (LNode )); s-> data = e; s-> next = p-> next; p-> next = s; // connect first and insert return OK ;} status ListDelete_L (LinkList * L, int I, ElemType * e) {LinkList p = * L, q; ElemType j = 0; while (p-> next & j <i-1) {p = p-> next; j ++;} if (! (P-> next) | j> i-1) return ERROR; // deletion location unreasonable q = p-> next; p-> next = q-> next; * e = q-> data; free (q); return * e;} Status GetElem_L (LinkList L, int I, ElemType * e) {LinkList p; ElemType j = 1; p = L-> next; while (p & j
Next; j ++;} if (! P | j> I) return ERROR; // the I-th element does not exist * e = p-> data; return * e;} // GetElem_L () status MergeList_L (LinkList * La, LinkList * Lb, LinkList * Lc) {// the data in the two linked lists La and Lb are arranged in non-descending order // The La AND Lb tables are merged into the Lc and the LinkList pa, pb, pc are arranged in the non-descending sequence; int I = 0, j = 0; pa = (* La)-> next; pb = (* Lb)-> next; printf ("% x", (int) pa, (int) pb); (* Lc) = (LinkList) malloc (sizeof (LNode); pc = (* Lc); while (int) pa & (int) pb) {if (pa-> data) <= (pb-> data) {pc-> next = pa; pc = pa; pa = pa-> next ;} else {pc-> next = pb; pc = pb; pb = pb-> next ;}while (pa) {pc-> next = pa; // Insert the remaining segments pc = pa; pa = pa-> next;} while (pb) {pc-> next = pb; pc = pb = NULL;} return OK ;} // MergeList_L () void PrintList_L (LinkList L) {LinkList p; for (p = L-> next; p = p-> next) // L The data field of the head node of the linked list is not assigned printf ("% d", p-> data); printf ("\ n ");}
Main. c
# Include
# Include
# Include "declaration. h "# include" function. h "int main () {ElemType e; LinkList * La, * Lb, * Lc; La = (LinkList *) malloc (sizeof (LinkList); Lb = (LinkList *) malloc (sizeof (LinkList); Lc = (LinkList *) malloc (sizeof (LinkList); printf ("create la, please enter 5 number:"); CreateList_L (La, 5); printf ("la ="); PrintList_L (* La); printf ("the 3rd count in the la table is % d \ n", GetElem_L (* La, 3, & e); printf ("deleting the first number in the la table is % d \ n", ListDelete_L (La, 1, & e); printf ("after delete first member, la = "); PrintList_L (* La); printf (" create lb, please enter 4 number: "); CreateList_L (Lb, 4); printf (" lb = "); printList_L (* Lb); ListInsert_L (Lb, 2, 3); printf ("after insert 3, lb ="); PrintList_L (* Lb); printf ("MergeList function, lc = \ n "); if (MergeList_L (La, Lb, Lc) = OK) {printf (" merget success \ n "); PrintList_L (* Lc );} else printf ("merget failed"); return 0 ;}
Define the second-level pointers La, Lb, and Lc respectively, and pass them as parameters to the CreateList_L () function, so that we can
* La, * Lb, * Lc allocate storage space, which will not disappear when the function call is completed. In C language, parameter passing is a value passing method: if the value of the variable is used as the form parameter, a copy of the value is passed in. When the function does not return a type, the variable value change is only valid in the called function. If the parameter is a pointer or array name, the address of the variable is passed, similarly, the changes to this address in the called function will not spread out of this function, but the changes to the memory space represented by this address are assigned permanently, it will not be released when the call ends.
When implementing MergeList_L (LinkList * La, LinkList * Lb, LinkList * Lc), I forgot to allocate address space for * Lc at first, making the pc a wild pointer, as a result, the program has been suspended for a long time. Remember to initialize the pointer when using it.
Running result: