Sequential storage pseudo-algorithm codes and explanations for linear tables

Source: Internet
Author: User

#define LIST_INIT_SIZE 100//Initial allocation of the linear table storage space # define Listincrement 10//linear table storage allocation increment typedef struct {elemtype *elem;// Storage space base int length;//current length int listsize;//currently allocates storage capacity (in sizeof (Elemtype))}sqlist;//1. Initialize status initlist_sq (SqList &l)/ /Constructs an empty linear table {l.elem= (elemtype*) malloc (list_init_size*sizeof (Elemtype));//Allocate space, if allocated space, return Elem base address//list_init_ Size corresponds to the number of elements. And elemtype corresponds to the size of each element if (! L.elem) exit (OVERFLOW);//If empty, the request allocates memory space fails l.length=0;//empty table length is zero l.listsize=list_init_size//initial storage capacity reutn OK;} 2. Clear the table L.LENGTH=0;//3. Destroy table Free (L.elem),//4. The table long return (l.length),//5. Determine the empty table if (l.length==0) return OK;//6. Take the first element of the table, status Getelem (sqlist l,int i,elemtype &e) {if (i>l.length| | i<1) return (ERROR);p. l.elem+ (i-1),//p the value of the element I removed for the position of the element I e=*p;//is assigned to E}//7. Positioning: Find out if an element exists, present its position, otherwise 0int Locate_ Sq (sqlist l,elemtype e) {p=l.elem;//because elem points to the base address of the linear table, so p also points to the base of the linear table for (i=1;i<=l.length;i++) {if (*p++==e)// If p points to a data element that is equal to the element being found, it returns Ireturn I; else//if traversing the entire linear table is not found, return 0return 0;} 8. Insert: Insert a new element before the first element in the sequential linear table L estatus listinsert_sq (sqlist &l,int i,elemtyPE e) {if (i<1| | I&GT;L.LENGTH+1)//If the insertion position is not appropriate, return Errorreturn error;if (l.length>=l.listsize)//If the insertion position is appropriate {newbase= (elemtype*) ReAlloc (L.elem, (l.listsize+listincrement) *sizeof (elemtype));//Open a new address space from the virtual to the original and then add the append and then multiply the size of each element if (!newbase)// Exit exit (OVERFLOW) if the new space opens up errors; l.elem=newbase;//points The first address of the linear table to newbasel.listsize=l.listsize+listincrement;//the new linear table length equals the original plus the newly opened}q=& (L.elem[i-1] );//determines where element e needs to be inserted, the first element subscript is "i-1" for (p=& (l.elem[l.length-1]);p >=q;--p) * (p+1) =*p;//let the elements in the linear table move backwards, Until you move to the next bit of *q=e where the element e needs to be inserted; The length of the l.length++;//linear table plus 1return OK;} 9. Delete the operation. Status listdelete_sq (sqlist &l,int i,elemtype &e) {//delete the I element in the sequential linear table L, and return its value//i with E as the valid value of 1<=i<=listlength_ Sq (L) if (i<1| | (I>l.length)) Return error;p=& (L.elem[i-1])//p points to the deleted element e=*p;//the value of the deleted element is assigned to eq=l.elem+l.length-1;//q to the footer for (p++;p <=q;p++)/ /Move up from the next position to be deleted * (p-1) =*p; L.length--;return OK;}

Sequential storage pseudo-algorithm codes and explanations for 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.