C. Implementation of sequential storage of data structures

Source: Internet
Author: User

This article is original, reproduced please indicate the source: http://blog.csdn.net/j903829182/article/details/38173819


# Include <stdio. h> # define maxsize 100 // define the size of the element typedef int datatype; // define a type typedef struct {// define a struct datatype list [maxsize]; int size; // size of struct element} seqlist; // struct object // initialize void initiate (seqlist * l) {L-> size = 0; // define the number of initialization elements} // calculate the number of current elements int getlength (seqlist L) {return L. size; // return length} // insert data element int insertdata (seqlist * l, int I, datatype X) {// Insert the data element x before the position I (0 <= I <= size) of the sequence table L // If the insertion is successful, 1 is returned. If the output fails, 0 Int J is returned; if (L-> size> = maxsi Ze) {printf ("the sequence table is full and cannot be inserted !! \ N "); Return 0;} else if (I <0 | I> L-> size) {printf (" the inserted position is invalid and is not in the specified range, the parameter I is invalid! \ N "); Return 0;} else {// move data from the back to the forward in a consistent manner to prepare for insertion (j = L-> size; j> I; j --) {L-> list [J] = L-> list [J-1];} l-> list [I] = x; L-> size ++; return 1 ;}} // Delete int deletedata (seqlist * l, int I, datatype * X) {// Delete the data in the sequence table where I is located. I> = 0 & I <= size-1. Save the data to X. // If deletion is successful, 1 is returned, otherwise, the return value is 0int J; If (L-> size <= 0) {printf ("the sequence table is empty and no data elements can be deleted! \ N "); Return 0;} else if (I <0 | I> L-> size-1) {printf (" parameter I is invalid and cannot be deleted! \ N "); Return 0;} else {* x = L-> list [I]; for (j = I + 1; j <= L-> size-1; j ++) {// forward from the previous one L-> list [J-1] = L-> list [J];} l-> size --; // subtract one from the data element return 1 ;}// retrieve the data element int getdata (seqlist L, int I, datatype * X) {if (I <0 | I> L. size-1) {printf ("parameter I is invalid and cannot be deleted! \ N "); Return 0;} else {* x = L. list [I]; return 1 ;}} int main () {seqlist mylist; int I, X; Initiate (& mylist); for (I = 0; I <10; I ++) {insertdata (& mylist, I, I + 1) ;}deletedata (& mylist, 4, & X); for (I = 0; I <getlength (mylist); I ++) {getdata (mylist, I, & X); printf ("% d", x) ;}return 0 ;}







# Include <stdio. h>/** linear structure definition: Except for the first and last elements, each element has only one unique precursor data element and a unique successor data element. Tree Structure Definition: except for the root node, each element has only one unique precursor data element, which can have zero or several successor Data Element Graph structures: each element can have zero or several precursor data elements and a linear table of zero or several successor data elements: it is the simplest linear structure, A linear table can insert or delete a data element at any location. A linear table can be stored in a sequential storage or chained storage structure. * // representation of the sequential storage of a linear table # define maxsize 100 # define true 1 # define false 0 typedef int datatype; typedef struct {datatype list [maxsize]; int size;} seqlist; // initialize void listinitiate (seqlist * l) {L-> size = 0 ;// Set the number of initial elements to 0} // calculate the number of current data elements int getlistlength (seqlist L) {return L. size; // number of returned elements} // insert data element int listinsert (seqlist * l, int I, datatype data) {Int J; // define the variable Jif (L-> size> = maxsize) {printf ("the sequence table is full and cannot be inserted !! \ N "); Return false; // return} if (I <0 | I> L-> size + 1) {printf (" the input parameter is invalid, cannot be inserted !! \ N "); Return false; // return} else {for (j = L-> size; j> = I; j --) {L-> list [J] = L-> list [J-1]; // move element} l-> list [I-1] = data; // assign a value of L-> size ++; // Add a return true to the number of elements;} // Delete the data element int deletelist (seqlist * l, int I, datatype * Data) {Int J = 0; If (L-> size = 0) {printf ("the linear table is empty and cannot be deleted !! \ N "); Return false;} if (I <0 | I> L-> size) {printf (" the deleted location I is incorrect. It cannot be deleted !! \ N "); Return false;} else {* Data = L-> list [I-1]; for (j = I; j <L-> size; j ++) {L-> list [J-1] = L-> list [J];} l-> size --; return true ;}} // get the data element int listget (seqlist * l, int I, datatype * Data) {if (I <0 | I> L-> size) {printf ("The position is incorrect. The value cannot be taken !! \ N "); Return false ;}else {* Data = L-> list [I-1]; return true ;}} // operation int empty (seqlist L) {If (L. size = 0) {return true;} else {return false ;}// print all output functions void displaydata (seqlist L) {int I; for (I = 0; I <L. size; I ++) {printf ("% d", L. list [I]);}/* anthor solitary nine swords time * // main function int main () {int I; int data; seqlist L; listinitiate (& L); for (I = 1; I <10; I ++) {listinsert (& L, I, I) ;}// displaydata (L ); // listinsert (& L, 5, 45); displaydata (l); printf ("\ n"); deletelist (& L, 5, & data ); printf ("\ n"); displaydata (l); printf ("\ n"); printf ("% d \ n", sizeof (seqlist), L. size); Return 0;} // the main advantage of the sequence table is that the algorithm is simple and the content unit utilization is high: // The main disadvantage is that the maximum number of data elements needs to be determined in advance.






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.