Data structure-sequential storage structure of linear tables

Source: Internet
Author: User

The sequential storage of linear tables requires three properties 1. The starting position of the storage space 2. The maximum storage capacity of a linear table 3. The current length of the linear table two, the time complexity of the linear table:The sequential storage structure of linear tables, in which the time complexity is O (1), regardless of the location of the data, the time complexity of insertion and deletion is O (n), so the linear table is suitable for the number of elements, and more is the application to access the data.
third, the structure of the linear table:
Four, code example:
/* Our metering method, in addition to the subscript starting from 0, the rest are counted from 1, so only when it comes to accessing the array, pay attention to whether to +1 or -1*/#include <stdio.h> #define MAXSIZE 20#define OK Define ERROR 0#define TRUE 1#define flase 0typedef struct{int No; int grade;} Elemtype;typedef struct{elemtype data[maxsize];//the max length of the list int length;} Sqlist;void initlist (sqlist* L) {l->length = 0;} int Getelem (sqlist* l,int i,elemtype* e) {if (L->length = = 0 | | i > L->length | | i < 1) return ERROR; *e = l-& GT;DATA[I-1]; return OK;} int creatlist (sqlist* L) {int i; printf ("Ready to create the List,please input the length\n"); scanf ("%d", &l->lengt h); if (L->length > MAXSIZE) fprintf (stderr, "Out of memory!\n");  for (i = 0; I <= l->length-1;i++) {printf ("Data%d=", i+1); scanf ("%d%d", &l->data[i]. No,&l->data[i].grade); }}int insertlist (Sqlist*l,int i,elemtype* e) {int k; if (l->length = = MAXSIZE) return ERROR; if (I < 1 | | i > l-& Gt;length + 1) return ERROR; if (i <= l->length) {for (k = l->Length K >= i;  k--) {L->data[k] = l->data[k-1]; }} L->data[i-1] = *e; l->length++; return OK;} int DeleteList (sqlist* l,int i) {int k; if (L = = NULL | | i > l->length) return ERROR; for (k = i; k <= L->lengt h;k++) {l->data[k-1] = l->data[k];} L->length--; return OK;} void Showlist (sqlist* L) {int i; for (i = 0; I <= l->length-1;i++) {printf ("%d%d \ n", L->data[i]. No,l->data[i].grade); }}int listlength (sqlist* L) {return l->length;} /*return the location (num) of the Element*/int Locateelem (sqlist* l,elemtype* e) {int. i,res; for (i = 0; i < L->lengt H i++) {if (L->data[i].  No = = E->no && L->data[i].grade = = e->grade) res = i+1; else res =-1; } return res; /*combine-Lists,if The element in L2 was not existent in L1,insert the element into L1*/void combinelist (sqlist* l1,sql ist* L2) {int l1_len,l2_len,i; Elemtype e; L1_len = Listlength (L1);  L2_len = Listlength (L2); for (i = 1; I <= L2_len; i++) {Getelem (l2,i,&e);  if (Locateelem (l1,&e) = =-1) {insertlist (l1,i,&e); }}}int Main () {sqlist L1; SqList L2; Initlist (&AMP;L1); Initlist (&AMP;L2); printf ("SqList l1:\n"); Creatlist (&AMP;L1); printf ("SqList l2:\n"); Creatlist (&AMP;L2); Combinelist (&AMP;L1,&AMP;L2); Showlist (&AMP;L1); return 0;}
The results indicate:

Data structure-sequential storage structure of linear tables

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.