Implementation of continuous storage array for data structure linear storage

Source: Internet
Author: User

Induction:

Linear

Continuous storage of "arrays"

Advantage: Fast access (element can be directly located)

Cons: Insertion of deleted elements is slow (because you want to move other elements), space is usually limited

Discrete storage "linked list"

Advantage: No space limit, insert delete element quickly

Cons: Access speed is slow (to one traversal, one for one)

Application of linear structure:

1. Stack

2. Queues

Nonlinear

Tree

Figure

#include <stdio.h>#include<malloc.h>#include<stdlib.h>#defineBOOL int#defineFalse 0#defineTrue 1/*The algorithm of continuous storage array represents this program, using data structure to realize the initialization of the array (open memory, determine the length of the array), append, insert the element in a certain position, delete the element, get the element, determine whether it is empty, is full, sort the array, traverse the array element, and then reverse the array function. *///defines a data type whose name is called struct ARR, which contains three membersstructarr{int*pbase;//The first address of the array is stored    intLen//The maximum number of elements the array can hold    intCnt//The number of valid elements in the current array};voidInit_arr (structARR *parr,intlength);BOOLAppend_arr (structARR *parr,intVal);//AppendBOOLInsert_arr (structARR *parr,intPosintVal);//the value of POS starts from 1BOOLDelete_arr (structARR *parr,intPosint*pVal);//int get ();BOOLIs_empty (structARR *PARR);BOOLIs_full (structARR *PARR);voidSort_arr (structARR *PARR);voidShow_arr (structARR *PARR);voidInverse_arr (structARR *PARR);intMainvoid){    structarr arr; intVal; Init_arr (&arr,6); if(Append_arr (&arr,7) {printf ("Added success! \ n"); } Append_arr (&arr,-5); Append_arr (&arr,2); Append_arr (&arr,8); Append_arr (&arr, One); //Insert_arr (&arr, 3, 384);    /*if (Delete_arr (&arr, 1, &val)) {printf ("Delete succeeded!        \ n ");    printf ("The element you deleted is:%d\n", Val); } else {printf ("Delete failed!    "); }    */    //Inverse_arr (&arr);Sort_arr (&arr); Show_arr (&arr); printf ("the length of the array is:%d\n", Arr.len); printf ("the number of valid elements in the array is:%d\n", arr.cnt); return 0;}voidInit_arr (structARR *parr,intlength) {    //(*parr). len = 99; //Parr->len =;Parr->pbase = (int*)malloc(sizeof(int)*length); if(Parr->pbase = =NULL) {printf ("dynamic memory allocation failed! \ n"); Exit (-1);//terminating the entire program requires a header file Stdlib.h    }    Else{PARR->len =length; PARR->cnt =0; }    return;//the delegate program terminated.}BOOLIs_empty (structARR *PARR) {    if(parr->cnt = =0)        return true; Else        return false;}BOOLIs_full (structARR *PARR) {    if(Parr->len = = parr->CNT)return true; Else        return false;}voidShow_arr (structARR *PARR) {    if(Is_empty (PARR)) {printf ("the array is empty! \ n"); }    Else    {        //valid contents of the output array        inti;  for(i =0; I < parr->cnt;i++) {printf ("%d\n", parr->Pbase[i]); }    }}BOOLAppend_arr (structARR *parr,intval) {    if(Is_full (PARR)) {return false; }    Else{PARR-&GT;PBASE[PARR-&GT;CNT] =Val; (PArr-&GT;CNT) + +; return true; }}BOOLInsert_arr (structARR *parr,intPosintval) {    inti; //to ensure the rationality of the program, write a robust code:    if(Is_full (PARR))return false; if(Pos <1|| POS > (parr->cnt +1))        return false;  for(i = (parr->cnt)-1; I >= (pos-1); i--)    {        //printf ("%d\n", I);Parr->pbase[i +1] = parr->Pbase[i]; } PARR->pbase[pos-1] =Val; (PArr-&GT;CNT) + +; return true;}BOOLDelete_arr (structARR *parr,intPosint*pVal) {    inti; //to put an illegal platoon.    if(Is_empty (PARR))return false; if(Pos <1|| POS > parr->CNT)return false; *pval = Parr->pbase[pos-1];  for(i = pos; i < parr->cnt; i++) {PARR->pbase[i-1] = parr->Pbase[i]; } (PARR-&GT;CNT)--; return true;}voidInverse_arr (structARR *PARR) {    inti =0; intj = (parr->cnt)-1; intT;  while(I <j) {T= parr->Pbase[i]; PARR->pbase[i] = parr->Pbase[j]; PARR-&GT;PBASE[J] =T; I++; J--; }    return;}//Bubble SortvoidSort_arr (structARR *PARR) {    intI, J; intT;  for(i =0; I < parr->cnt;i++)    {         for(j = i +1; J < parr->cnt;j++)        {            if(Parr->pbase[i] > parr->Pbase[j]) {T= parr->Pbase[i]; PARR->pbase[i] = parr->Pbase[j]; PARR-&GT;PBASE[J] =T; }        }    }}

Implementation of continuous storage array for data structure linear storage

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.