Interview 4 (single-chain table)

Source: Internet
Author: User

After so many interviews, if you take a test on the data structure, you will find it easier to take a test on the single-chain table and sorting table. Of course, it seems that there are many stacks. Let's take a look at the single-chain table today. Thanks to Yan Weimin's Data Structure last year, the data structure is still very confident.

Single-chain table

// Storage structure of a single-chain table of a linear table
Typedef struct lnode
{
Elemtype data;
Struct lnode * next;
} Lnode, * linklist;

// The algorithm used to reverse create a single-chain table from the end of the table to the header, O (N)
Void createlist_l (linklist & L, int N)
{
// Input values of n elements in reverse order to create a single-chain linear table with table header nodes.
L = (linklist) malloc (sizeof (lnode ));
L-> next = NULL; // create a single-chain table with the leading Node
For (I = N; I> 0; -- I)
{
P = (linklist) malloc (sizeof (lnode); // generate a new node
Scanf (& P-> data); // input element value
P-> next = L-> next;
L-> next = P; // insert to the header
}
}

// Search
Status getelem_l (linklist L, int I, elemtype & E)
{
// L is the header pointer of a single-chain table with the leading Node
// When element I exists, the value is assigned to E and OK is returned; otherwise, error is returned.
P = L-> next;
J = 1; // initialization. P points to the first node and J is the counter.
While (P & J <I)
{
P = p-> next;
++ J;
}
If (! P | j> I)
Return Error; // the I-th element does not exist.
E = p-> data;
Return OK;
}

// Insert
Status listinsert_l (linklist & L, int I, elemtype E)
{
// Insert element E before position I in the single-chain linear table l of the leading Node
P = L;
J = 0;
While (P & J <i-1)
{
P = p-> next;
++ J;
} // Find the I-1 Node
If (! P | j> i-1)
Return Error; // I is greater 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;
Return OK;
}

// Delete
Status listdelete_l (linklist & L, elemtype & E)
{
// In the single-chain linear table of the leading node, delete the I-th element and E returns its value.
P = L;
J = 0;
While (p-> next & <i-1)
{
// Find the I-th node and point P to its frontend
P = p-> next;
++ J;
}
If (! (P-> next) | j> i-1)
Return Error; // The deletion location is incorrect.
Q = p-> next;
P-> next = Q-> next; // Delete and release the node
E = Q-> data;
Free (Q );
Return OK;
}

// Merge
Void mergelist_l (linklist & la, linklist & Lb, linklist & lc)
{
// The elements of the known Single-Chain linear table la and lB are sorted by values in non-descending order.
// Merge La and lb to obtain the new single-chain linear table LC. The LC elements are also arranged in descending order of values.
Pa = La-> next;
PB = LB-> next;
Lc = pc = La; // use the La header node as the LC header Node
While (PA & Pb)
{
If (Pa-> data <= Pb-> data)
{
PC-> next = PA;
PC = PA;
Pa = pa-> next;
}
Else
{
Pa-> next = Pb;
PC = Pb;
PB = Pb-> next;
}
}
PC-> next = pa? PA: Pb; // Insert the remaining segments
Free (LB); // release the LB header Node
}

Write such a single-chain table today.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/crescent_star/archive/2009/03/21/4011718.aspx

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.