Data Structure-C language description of a single-chain table

Source: Internet
Author: User

// The C language description of the single-chain table, which is selected from data structure (C language description), Xu xiaokai, He Guiying, and Tsinghua University Press.

# Include <stdio. h>
# Include <stdlib. h>

# Define NN 12
# Define mm 20

// List struct Definition
Typedef int elemtype;

Struct snode {
Elemtype data;/* value range */
Struct snode * Next;/* link pointer field */
};

// 1. initialize the linear table, that is, leave the header pointer of the single-chain table empty.
Void initlist (struct snode ** HL)
{
* HL = NULL;
}

// 2. Clear all elements in the linear table, that is, release all nodes in the single-link table to make it an empty table.
Void clearlist (struct snode ** HL)
{
/* Use CP and NP as pointers to two adjacent nodes respectively */
Struct snode * CP, * NP;
/* The header pointer of a single-chain table is assigned to * Cp */
CP = * HL;
/* Traverse a single-chain table and release each node in sequence */
While (CP! = NULL)
{
NP = CP-> next;
Free (CP );
CP = NP;
}
/* Set the header pointer of a single-chain table to null */
* HL = NULL;
}

// 3. Return the length of the linear table l, that is, the length of a single-chain table
Int sizelist (struct snode * HL)
{
Int I;/* used to count the number of nodes */
While (hl! = NULL)
{
I ++;
Hl = HL-> next;
}
Return I;
}

// 4. Check whether the single linked list is empty
Int emptylist (struct snode * HL)
{
If (HL = NULL) return 1; else return 0;
}

// 5. Return the element in the POs node of the single-chain table. If the POs node is out of the range, stop running the program.
Elemtype getelem (struct snode * HL, int POS)
{
Int I = 0;/* count the number of nodes that have been traversed */
If (Pos <1)
{
Printf ("invalid POS value, quit running! \ N ");
Exit (1 );
}
While (hl! = NULL)
{
I ++;
If (I = POS) break;
Hl = HL-> next;
}
If (hl! = NULL)
Return HL-> data;
Else
{
Printf ("invalid POS value, quit running! \ N ");
Exit (1 );
}
}

// 6. traverse a single-chain table
Void traverselist (struct snode * HL)
{
While (hl! = NULL)
{
Printf ("5d %", HL-> data );
Hl = HL-> next;
}
Printf ("\ n ");
}

// 11. Insert a node whose element is X to the position of the POs node in the single-chain table. If the insertion is successful, 1 is returned; otherwise, 1 is returned.
Int insertposlist (struct snode ** HL, int POs, elemtype X)
{
Int I = 0;
Struct snode * newp;
Struct snode * CP = * HL, * ap = NULL;
/* Process POS values smaller than or equal to 0 */
If (Pos <= 0)
{
Printf ("the POs value is incorrect. If 0 is returned, insertion fails! \ N ");
Return 0;
}
/* Find the POs node */
While (CP! = NULL)
{
I ++;
If (Pos = I) break;
Else {ap = CP; CP = CP-> next ;}
}
/* Create a new node. If the allocation fails, stop inserting */
Newp = malloc (sizeof (struct snode ));
If (newp = NULL)
{
Printf ("the memory dynamic space is used up and cannot be inserted! \ N ");
Return 0;
}
/* Assign the value of X to the data field of the new node */
Newp-> DATA = X;
/* Insert the new node to the header */
If (AP = NULL)
{
Newp-> next = CP;/* or change to newp-> next = * HL */
* HL = newp;
}
/* Insert the new node into the AP and Cp */
Else
{
Newp-> next = CP;
AP-> next = newp;
}
/* 1 after successful insertion */
Return 1;
}

// 12. Insert the element X to the ordered single-chain table so that the inserted element is still ordered.
Void insertorderlist (struct snode ** HL, elemtype X)
{
/* Assign the header pointer of a single-chain table to CP and assign null to AP */
Struct snode * CP = * HL, * ap = NULL;
/* Create a new node */
Struct snode * newp;
Newp = malloc (sizeof (struct snode ));
If (newp = NULL)
{
Printf ("the memory dynamic space is used up, and the task stops running! \ N ");
Exit (1 );
}
Newp-> DATA = x;/* assign the value of X to the data field of the new node */
/* Insert the new node to the header */
If (Cp = NULL | x <CP-> data)
{
Newp-> next = CP;
* HL = newp;
Return;
}
/* Find the insertion position of the x node in sequence */
While (CP! = NULL)
{
If (x <CP-> data) break;
Else {ap = CP; CP = CP-> next ;}
}
/* Insert the x node into the AP and Cp */
Newp-> next = CP;
AP-> next = newp;
}

// 16. Delete the first node with the value of X from the single-chain table. If the deletion is successful, 1 is returned. Otherwise, 0 is returned.
Int deletevaluelist (struct snode ** HL, elemtype X)
{
/* Initialize the CP and AP pointers so that the CP points to the header node and the AP is empty */
Struct snode * CP = * HL;
Struct snode * ap = NULL;
/* Find the node with the value of X from the single-chain table, and then point it to the node by CP.
AP points to its precursor node */
While (CP! = NULL)
{
If (CP-> DATA = x) break;
AP = CP; CP = CP-> next;
}
/* If the query fails, that is, no node with the value of X exists in the single-link table, 0 */is returned */
If (Cp = NULL) return 0;
/* Process the deleted headers or non-headers separately */
If (AP = NULL)
* HL = (* HL)-> next;/* or * HL = CP-> next */
Else
AP-> next = CP-> next;
/* Recycle deleted nodes */
Free (CP );
/* 1 indicates that the deletion is successful */
Return 1;
}

Void main ()
{
Int A [NN];
Int I;
Struct snode * P; // * H, * s,
Initlist (& P );
For (I = 0; I <nn; I ++) A [I] = rand () % mm;
Printf ("random number sequence :");
For (I = 0; I <nn; I ++) printf ("% 5d", a [I]);
Printf ("\ n ");
Printf ("Random Number Sequence ");
Traverselist (P );
Printf ("single-chain table length: % 5d \ n", sizelist (p ));
Clearlist (& P );

}

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.