Data structure routine-create a single-chain table and single-chain data structure routine

Source: Internet
Author: User

Data structure routine-create a single-chain table and single-chain data structure routine

This article is a basic series of online courses on data structures (2): routines used to create a single-link table in a linear table during the first lesson.

[Routine]
Defines the storage structure of a single-chain table, creates a single-chain table by using the header plug-in and tail plug-in, and displays the created results.

# Include <stdio. h> # include <malloc. h> typedef int ElemType; typedef struct LNode // defines the node type {ElemType data; struct LNode * next; // points to the next node} LinkList; void CreateListF (LinkList * & L, ElemType a [], int n); // create a single-chain table void CreateListR (LinkList * & L, ElemType a [], int n); // create a single-chain table void DestroyList (LinkList * & L); // destroy the single-chain table void DispList (LinkList * L) // output single-chain table int main () {LinkList * L1, * L2; ElemType a [8] = {7, 9, 8, 2, 0, 4, 6, 3}; CreateListF (L1, a, 8); printf ("result of table creation by header insertion:"); DispList (L1); CreateListR (L2,, 6); printf ("result of table creation by means of tail insertion:"); DispList (L2); DestroyList (L1); DestroyList (L2); return 0 ;} void CreateListF (LinkList * & L, ElemType a [], int n) // create a single-chain table using the header Insertion Method {LinkList * s; int I; L = (LinkList *) malloc (sizeof (LinkList); // create the header node L-> next = NULL; for (I = 0; I <n; I ++) {s = (LinkList *) malloc (sizeof (LinkList); // create a new node s-> data = a [I]; s-> nex T = L-> next; // insert * s before the original Start node, and then L-> next = s;} void CreateListR (LinkList * & L, elemType a [], int n) // create a single-chain table {LinkList * s, * r; int I; L = (LinkList *) malloc (sizeof (LinkList) by means of end insertion )); // create a header node L-> next = NULL; r = L; // r always points to the terminal node. At the beginning, it points to the header node for (I = 0; I <n; I ++) {s = (LinkList *) malloc (sizeof (LinkList); // create a new node s-> data = a [I]; r-> next = s; // after inserting * s into * r, r = s;} r-> next = NULL; // set the next field of the terminal node to NULL} void DestroyList (LinkList * & L) // destroy a single-chain table {LinkList * p = L, * q = p-> next; while (q! = NULL) {free (p); p = q; q = p-> next;} free (p); // q is NULL, p points to the end node, release it} void DispList (LinkList * L) // output the single-chain table {LinkList * p = L-> next; while (p! = NULL) {printf ("% d", p-> data); p = p-> next;} printf ("\ n ");}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.