"Data structure (c)" Linear table order representation and implementation

Source: Internet
Author: User

1. Linear table (Linear_list):

Characteristics:

In a non-empty finite set of data elements

    1. There is only one data element known as the "first one";
    2. There is only one data element known as the "last";
    3. Except for the first one, each data element in the collection has a single precursor
    4. Except for the last one, each data element in the collection has only one successor
2. Sequential representation of linear tables: storing data elements of linear tables sequentially with a contiguous set of storage units
#include <bits/stdc++.h>using namespace std; #define LIST_INIT_SIZE 100///Linear table storage space Initial allocation # # Listincrement 10 Linear table Storage Allocation Increment # define Elemtype int#define Status int#define OK 1#define error-1#define overflow-2typedef struc    t{Elemtype *elem;    int length; int listsize;} SqList;    Status initlist_sq (sqlist &l) {L.elem = (Elemtype *) malloc (list_init_size*sizeof (elemtype)); if (!    L.elem) {return OVERFLOW;    } l.length = 0;    L.listsize = list_init_size; return OK;}       Status destroylist_sq (sqlist &l) {if (L.length > 0) {free (L.elem);   Free (&l); } return OK;    Status clearlist_sq (sqlist &l) {l.length = 0; return OK;}    Status isemptylist_sq (sqlist &l) {if (l.length = = 0) return OK; else return ERROR;} Status getlengthlist_sq (sqlist &l) {return l.length;}    Status Getelem (sqlist &l,int i,elemtype &e) {if (I < 1 | | i > l.length + 1) return ERROR; else {e = L.eleM[I-1];    return OK;    }}status listinsert_sq (sqlist &l, int i, elemtype e) {if (I < 1 | | i > l.length + 1) return ERROR; if (l.length >= l.listsize) {elemtype *newbase = (Elemtype *) realloc (l.elem,l.listsize + listincrement * s)        Izeof (Elemtype));        if (!newbase) {return OVERFLOW;        } L.elem = Newbase;    L.listsize + = listincrement;    } int *last = &L.elem[i-1];    for (int *index = & (L.elem[l.length-1]), index >= last; index--) {* (index + 1) = *index;    } *last = e;    ++l.length; return OK;}    Status listdeletebypos_sq (sqlist &l,int i,elemtype &e) {if (I < 1 | | i > l.length) return ERROR;    int *pos = &L.elem[i-1];    e = *pos;    int *last = L.elem + l.length-1;    For (pos++, POS <= last; pos++) {* (pos-1) = *pos;    } l.length--; return OK;}    Status listdeletebyval_sq (sqlist &l,elemtype e) {int *index = &L.elem[0]; InchT *last = &L.elem[L.length-1];  for (; index <=, index++) {if (*index = = e) {for (Int. *k = index +1; k <= last;         k++) * (k-1) = *k;         l.length--; index--;///because the value of P has been updated and needs to be determined once}} return OK; Status listtraverse_sq (SqList &l) {for (int i=0; i<l.length; i++) {i<l.length-1? printf ("%d", L.el    Em[i]):p rintf ("%d\n", L.elem[i]); } return OK;    int main () {sqlist sq_list;    INITLIST_SQ (sq_list);    int n;    scanf ("%d", &n);        for (int i=0; i<n; i++) {int val;        scanf ("%d", &val);    LISTINSERT_SQ (Sq_list,i+1,val);    } listtraverse_sq (Sq_list);     int dele_val,dele_pos;     scanf ("%d", &dele_pos);     LISTDELETEBYPOS_SQ (Sq_list,dele_pos,dele_val);    LISTTRAVERSE_SQ (sq_list); return 0;}

  

"Data structure (c)" Linear table order representation and implementation

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.