Basic operation example of linear table

Source: Internet
Author: User

#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

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.