Create, initialize, search, delete, insert, and merge sequence tables

Source: Internet
Author: User

Create, initialize, search, delete, insert, and merge sequence tables

This article was written by referring to the data structure of the national excellent course Geng Guohua, and found some problems in the book, and made some simple corrections to supplement the entire process. If there are any deficiencies, please follow the instructions !!!


# Include
 
  
# Include
  
   
Using namespace std; # define MAXSIZE 100 # define ERROR 0 # define OK 1 typedef int ElemType; typedef struct {ElemType * elem; int last;} SqList; int Locate (SqList * L, elemType e) {int I = 0;/* I is the scanning counter, the initial value is 0, that is, compare from the first element */while (I <= L-> last) & (L-> elem [I]! = E)/* scan the table sequentially until the element with the value of e is found or the end of the table is scanned but not */I ++ is found; if (I <= L-> last) return (I + 1);/* if an element with a value of e is found, its serial number */elsereturn (-1) is returned ); /* if no value is found, an empty Sequence Number */}/* is returned, and an element e is inserted before the I Data Element in the sequence table L. The length of the table before insertion is n = L-> last + 1. The valid value range of I is 1 ≤ I ≤ L-> last + 2 */int InsList (SqList * L, int I, ElemType e) {int k; if (I <1) | (I> L-> last + 2 )) /* first determine whether the insert position is valid */{printf ("the insert position I value is invalid"); return (ERROR);} if (L-> last> = MAXSIZE-1) {printf ("the table is full and cannot be inserted"); return (ERROR) ;}for (k = L-> last; k> = I-1; k --) /* move position for element insertion */L-> elem [k + 1] = L-> elem [k]; L-> elem [I-1] = e; /* in the C language array, the subscript of element I is I-1 */L-> last ++; return (OK);} int DelList (SqList * L, int I) /* Delete the I-th Data Element in sequence table L. The valid value of I is 1 ≤ I ≤ L. last + 1 */{int k; if (I <1) | (I> L-> last + 1) {printf ("the deletion location is invalid! "); Return (ERROR) ;}for (k = I; k <= L-> last; k ++) l-> elem [k-1] = L-> elem [k];/* move the following elements in sequence */L-> last --; return (OK );} void print (SqList * L) {int I; for (I = 0; I <= L-> last; I ++) cout <
   
    
Elem [I] <"; cout <
    
     
Elem = new ElemType [MAXSIZE]; if (! L-> elem) {exit (0);} int n, I = 0; cout <"Enter the number of elements in the sequence table" <
     
      
> N; while (I
      
        > L-> elem [I ++];} L-> last = I-1;} void merge (SqList * LA, SqList * LB, SqList * LC) {int I, j, k; I = 0; j = 0; k = 0; while (I <= LA-> last & j <= LB-> last) if (LA-> elem [I] <= LB-> elem [j]) {LC-> elem [k] = LA-> elem [I]; I ++; k ++;} else {LC-> elem [k] = LB-> elem [j]; j ++; k ++ ;} while (I <= LA-> last)/* When the table LA has the remaining elements, then, the remaining elements of the table LA are assigned to the table LC */{LC-> elem [k] = LA-> elem [I]; I ++; k ++ ;} while (j <= LB-> last)/* When the LB table has the remaining elements, then, the remaining LB elements of the table are assigned to the LC */{LC-> elem [k] = LB-> elem [j]; j ++; k ++;} table ;} LC-> last = LA-> last + LB-> last + 1; print (LC);} int main () {SqList L, L1, L2; ElemType e; int I, t; cout <"sequence table operation sequence: (1. initialize 2. search 3. insert 4. delete) "<
       
         > E; t = Locate (& L, e); if (t) cout <"no." <
        
          > E> I; t = InsList (& L, I, e); if (t) {cout <"the inserted sequence table is" <
         
           > I; t = DelList (& L, I); if (t) {cout <"the sequence table after deletion is" <
          
           

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.