// Storage structure of a single-chain table for a linear c2-2.h table
Struct lnode
{
Elemtype data;
Lnode * next;
};
Typedef lnode * linklist; // another method for defining the linklist
// Bo2-2.cpp Single-Chain linear table (storage structure defined by c2-2.h) basic operations (12)
Status initlist (linklist & L)
{// Operation result: Construct an empty linear table L
L = (linklist) malloc (sizeof (lnode); // generate a header node and point l to this header Node
If (! L) // storage allocation failed
Exit (overflow );
L-> next = NULL; // the pointer field is null.
Return OK;
} Status destroylist (linklist & L)
{// Initial condition: the linear table l already exists. Operation Result: The linear table L is destroyed.
Linklist Q;
While (l)
{
Q = L-> next;
Free (L );
L = Q;
}
Return OK;
} Status clearlist (linklist L) // do not change L
{// Initial condition: the linear table l already exists. Operation Result: reset L to an empty table.
Linklist p, q;
P = L-> next; // P points to the first node
While (p) // not at the end of the table
{
Q = p-> next;
Free (P );
P = Q;
}
L-> next = NULL; // the header node pointer field is null.
Return OK;
} Status listempty (linklist L)
{// Initial condition: the linear table l already exists. Operation Result: if l is empty, true is returned; otherwise, false is returned.
If (L-> next) // non-null
Return false;
Else
Return true;
} Int listlength (linklist L)
{// Initial condition: the linear table l already exists. Operation Result: returns the number of data elements in L.
Int I = 0;
Linklist P = L-> next; // P points to the first node
While (p) // not at the end of the table
{
I ++;
P = p-> next;
}
Return I;
} Status getelem (linklist L, int I, elemtype & E) // algorithm 2.8
{// L is the header pointer of the single-chain table of the leading node. When the I-th element exists, the value is assigned to E and OK is returned. Otherwise, error is returned.
Int J = 1; // J is the counter
Linklist P = L-> next; // P points to the first node
While (P & J <I) // follow the pointer to search backward until P points to the I element or P is null
{
P = p-> next;
J ++;
}
If (! P | j> I) // the I-th element does not exist.
Return Error;
E = p-> data; // obtain the I-th element.
Return OK;
} Int locateelem (linklist L, elemtype E, status (* compare) (elemtype, elemtype ))
{// Initial condition: the linear table l already exists, and compare () is the data element judgment function (1 is satisfied, otherwise 0)
// Operation Result: return the order of the 1st data elements that meet the compare () Relationship with E in L.
// If such a data element does not exist, the return value is 0.
Int I = 0;
Linklist P = L-> next;
While (P)
{
I ++;
If (compare (p-> data, E) // locate such a Data Element
Return I;
P = p-> next;
}
Return 0;
} Status priorelem (linklist L, elemtype cur_e, elemtype & pre_e)
{// Initial condition: the linear table l already exists
// Operation Result: If cur_e is a data element of L and is not the first, use pre_e to return its precursor,
// Return OK; otherwise, the operation fails. If pre_e is not defined, infeasible is returned.
Linklist Q, P = L-> next; // P points to the first node
While (p-> next) // P indicates that the node has a successor
{
Q = p-> next; // Q is the successor of P
If (Q-> DATA = cur_e)
{
Pre_e = p-> data;
Return OK;
}
P = Q; // P moves backward.
}
Return infeasible;
} Status nextelem (linklist L, elemtype cur_e, elemtype & next_e)
{// Initial condition: the linear table l already exists
// Operation Result: If cur_e is a data element of L and is not the last one, use next_e to return its successor,
// Return OK; otherwise, the operation fails and next_e is not defined. infeasible is returned.
Linklist P = L-> next; // P points to the first node
While (p-> next) // P indicates that the node has a successor
{
If (p-> DATA = cur_e)
{
Next_e = p-> next-> data;
Return OK;
}
P = p-> next;
}
Return infeasible;
} Status listinsert (linklist L, int I, elemtype e) // algorithm 2.9. Do not change L
{// Insert element E before position I in the single-link linear table l of the leading Node
Int J = 0;
Linklist P = L, S;
While (P & J <i-1) // find the I-1 Node
{
P = p-> next;
J ++;
}
If (! P | j> i-1) // I less than 1 or greater than the table length
Return Error;
S = (linklist) malloc (sizeof (lnode); // generate a new node
S-> DATA = E; // insert to L
S-> next = p-> next;
P-> next = s;
Return OK;
} Status listdelete (linklist L, int I, elemtype & E) // algorithm 2.10. Do not change L
{// In the single-chain linear table l of the leading node, delete the I-th element and E returns its value.
Int J = 0;
Linklist P = L, Q;
While (p-> next & J <i-1) // find the I node and point P to its front
{
P = p-> next;
J ++;
}
If (! P-> next | j> i-1) // The deletion location is unreasonable
Return Error;
Q = p-> next; // Delete and release the node
P-> next = Q-> next;
E = Q-> data;
Free (Q );
Return OK;
} Status listtraverse (linklist L, void (* VI) (elemtype ))
// The vi parameter type is elemtype, which is different from the elemtype of the corresponding function in the bo2-1.cpp.
{// Initial condition: the linear table l already exists
// Operation result: Call the VI () function for each data element of L in sequence (). Once VI () fails, the operation fails.
Linklist P = L-> next;
While (P)
{
VI (p-> data );
P = p-> next;
}
Printf ("/N ");
Return OK;
}