#include <iostream.h>//basic operation of linear table#include <stdlib.h>#include<stdio.h>#defineList_init_size 100//initial allocation of the linear table storage space#defineListincrement 10//allocation increment for linear table storage spacestypedefstruct{ int*elem;//Storage space Base intLength//Current Length intListsize;//Current allocated storage capacity in sizeof (Elemtype)}sqlist;intINITLIST_SQ (SqList &l) {//constructs an empty linear table L. L.elem =New int[List_init_size]; if(! L.elem) Exit (1);//Storage Allocation FailureL.length =0;//empty table length is 0L.listsize = list_init_size;//Initial storage capacity return 0; } //initlist_sqintLISTINSERT_SQ (Sqlist&l,intIinte) {if(I <1|| i > l.length+1)return 1;//I value is not legal if(l.length>= l.listsize) {//Current storage is full, increasing capacity int* Newbase = (int*) ReAlloc (L.elem, (l.listsize+listincrement) *sizeof(int)); if(!newbase) Exit (1); L.elem= Newbase;//New Base AddressL.listsize + = listincrement;//Increase storage capacity } int* q = & (l.elem[i-1]);//Q is the insertion position for(int*p = & (l.elem[l.length-1]); p>=q; --P) * (p+1) = *p;//insert position and subsequent element right shift*q = e;//Insert++l.length;//1 increase in table length return 0; }intLISTDELETE_SQ (Sqlist&l,intIint&e) {//Delete the I element in the sequential linear table L and return its value with E. //the legal value of I is 1≤i≤listlength_sq (L). int*p, *Q; if(i<1|| I>l.length)return 1;//I value is not legalp = & (l.elem[i-1]);//P is the location of the deleted elemente = *p;//the value of the deleted element is assigned to eQ = l.elem+l.length-1;//the position of the footer element for(++p; p<=q; ++p) * (P-1) = *p;//move the element left after the element is deleted--l.length;//table length minus 1 return 0; } voidTraverse (SqList L) {//outputs the elements of the sequential linear table l, individually. cout<<"La:"; for(intI=0; i<l.length;i++) cout<<L.elem[i]<<" ";}voidMain () {sqlist La; INITLIST_SQ (La); //construct linear table La inta[6] = {1,4,8,3,2,0}; for(intI=1;i<7; i++) {listinsert_sq (la,i,a[i-1]);//inserting elements} Traverse (La); //outputs the elements of the sequential linear table l, individually. cout<<Endl; intm; LISTDELETE_SQ (La,3, m);//Delete Elementcout<<"after deleting an element"; Traverse (La); cout<<"the deleted elements are:"<<m<<endl;//Output}
Basic operation example of linear table