Introduced
Put all the knots in one line.
- continuous storage [Array]
- discrete storage [Linked list]
- One of the two common applications of linear structures is the stack
- Two common applications of linear structures (time-related operations)
Array algorithm Demo
#include <stdio.h>#include<malloc.h>#include<stdlib.h>#include<stdbool.h>structarr{int*pbase; intLen; intcnt;};voidInit_arr (structARR *,int);BOOLAppend_arr (structARR *,int);BOOLInsert_arr (structARR *,int,int);BOOLDelete_arr (structARR *,int,int*);BOOLIs_empty (structARR *);BOOLIs_full (structARR *);voidSort_arr (structARR *);voidShow_arr (structARR *);voidInversion_arr (structARR *);intMainvoid){ structarr arr; intVal; Init_arr (&arr,6); Show_arr (&arr); Append_arr (&arr,1); Append_arr (&arr,- -); Append_arr (&arr, the); Append_arr (&arr,4); Append_arr (&arr, the); Append_arr (&arr, the); /*if (Delete_arr (&arr, 5, &val)) {printf ("delete success\n"); printf ("Delete element is:%d\n", Val); } else {printf ("delete error\n"); }*/ //Append_arr (&arr, 6); //Append_arr (&arr, 7); //Insert_arr (&arr,,); /*if (Append_arr (&arr, 8)) printf ("Add success\n"); else printf ("Add error\n");*/Show_arr (&arr); Inversion_arr (&arr); Show_arr (&arr); return 0;}voidInit_arr (structARR * PARR,intlength) {PARR->pbase = (int*) malloc (sizeof(int) *length); if(NULL = = parr->pbase) {printf ("DM error\n"); Exit (-1); } Else{PARR->len =length; PARR->cnt =0; } return;}BOOLAppend_arr (structARR * PARR,intval) { if(Is_full (PARR))return false; PARR->PBASE[PARR->CNT] =Val; (PArr->CNT) + +; return true;}BOOLInsert_arr (structARR * PARR,intPosintval) { inti; if(Is_full (PARR))return false; if(Pos <1|| POS > parr->cnt+1) return false; for(i = parr->cnt-1; I >= pos-1; --i) {PARR->pbase[i+1] = parr->Pbase[i]; } PARR->pbase[pos-1] =Val; PARR->cnt++; return true;}BOOLDelete_arr (structARR * PARR,intPosint*pVal) { inti; if(Is_empty (PARR))return false; if(Pos <1|| POS >pArr->CNT)return false; *pval = parr->Pbase[pos]; for(i = pos; I < parr->cnt; + +)i) {PARR->pbase[i-1] = parr->Pbase[i]; } PARR->cnt--; return true;}BOOLIs_empty (structARR *PARR) { if(0= = Parr->CNT)return true; Else return false;}BOOLIs_full (structARR *PARR) { if(parr->cnt = = parr->len)return true; Else return false;}voidSort_arr (structARR *PARR) { inti; intJ; for(i =0; I < parr->cnt; i++) { for(j = i; J < parr->cnt; J + +) { if(Parr->pbase[i] > parr->Pbase[j]) { intT; T= parr->Pbase[i]; PARR->pbase[i] = parr->Pbase[j]; PARR->PBASE[J] =i; } } }}voidShow_arr (structARR *PARR) { if(Is_empty (PARR)) printf ("arr is empty\n"); Else { inti; for(i =0; I < parr->len; i++) printf ("%d\n", parr->Pbase[i]); printf ("\ n"); }}voidInversion_arr (structARR *PARR) { inti =0; intj = parr->cnt-1; intT; while(I <j) {T= parr->Pbase[i]; PARR->pbase[i] = parr->Pbase[j]; PARR->PBASE[J] =T; ++i; --J; } return;}
l3--Array Algorithm Demo