Single-chain table implementation in C Language

Source: Internet
Author: User

This is my first blog post on csdn. This is my virgin post ~~~~

//List.h
# Ifndef list_h_included # define list_h_included /// the knowledge about typedef is not very well understood. Please strengthen it !! Typedef struct node; typedef struct node * ptrtonode;/*********** this linked list is different from the general implementation here, hiding pointers, that is, typedef ptrtonode list; the general definition is typedef struct node {int data; struct node * Next;} List; if so, the initialization function must use a second-level pointer, void listinitia (list ** list); ***************************/typedef ptrtonode list; typedef ptrtonode position; typedef int datatype; struct node {datatype data; position next;}; void listinitia (list * List); int listinsert (list, int index, datatype X ); void listprint (list); void listdel (list * List); # endif // list_h_included

// List. CPP # include <stdlib. h> # include <stdio. h> # include "list. H "/*************** note void listinitia (list * List) */void listinitia (list * List) {* List = (list) malloc (sizeof (node); (* List)-> DATA = 100; (* List)-> next = NULL;} int listinsert (list, int index, datatype X) {list tmplist = List; If (index <0) return 0; int I = 0; while (tmplist-> next! = NULL) & (index! = I) {I ++; tmplist = tmplist-> next;} List newlist = (list) malloc (sizeof (node); newlist-> DATA = X; newlist-> next = NULL; tmplist-> next = newlist; return 1;} void listprint (list) {list tmplist = List; while (tmplist-> next) {tmplist = tmplist-> next; printf ("% d \ t", tmplist-> data) ;}// is the implementation correct? Is there a memory leak ?? Void listdel (list * List) {list tmplist = * List; List TMP; while (! Tmplist-> next) {TMP = tmplist; tmplist = tmplist-> next; free (TMP );}}

// Test File

# Include <stdio. h> # include <stdlib. h> # include "list. H "int main () {list = NULL; // list is a pointer listinitia (& list); int I; for (I = 0; I <10; I ++) listinsert (list, I, I + 10); listprint (list); listdel (& list); Return 0 ;}

I personally wrote the above. I don't know if there are any errors. If so, please point out .....

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.