Data Structures-linear tables-sequential storage structure complete executable code

Source: Internet
Author: User
Tags data structures printf
Data Structures-linear tables-sequential storage structure complete executable code (C language Description)  
#include "stdio.h" #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20/* Storage space Initial allocation */Typede   f int elemtype;     /* The Elemtype type is based on the actual situation and is assumed to be an int */typedef int STATUS;             /* Status is the type of function, whose value is the function result status code, such as OK, etc. */typedef struct {elemtype data[maxsize];/* array, storing data element */int length;

/* Linear table current length */}sqlist;
    /* Initialize sequential linear table */Status initlist (sqlist *l) {l->length=0;
return OK;
    }//Order table Establishment SqList Create (SqList L) {int i;
	Srand ((unsigned) time (NULL));
        for (i=0; i <; i++) {L.data[i] = rand ()%100;
	l.length++;
} return L; }/* Initial conditions: Sequential linear table L already exists.
    */* Operation result: Resets L to empty table */Status clearlist (sqlist *l) {l->length = 0;
return OK;
    }/* Initial condition: listtraverse function call *//* operation result: print corresponding element */Status visit (elemtype c) {printf ("%d", c);
return OK;
    }/* Initial condition: sequential linear table L already exists */* operation result: output per data element of L */Status Listtraverse (sqlist L) {int i;
    For (I=0;i < l.length;i++) Visit (L.data[i]);
    printf ("\ n");
return OK;

}/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l), */* operation result: Insert new data element in L position before e,l length plus 1 */Status Listinsert (sqlist *l,int I,
	Elemtype e) {int k;
	if (l->length==maxsize)/* Sequential linear table is full */return ERROR;
    
	if (I < 1 | | i>l->length+1)//* When I is smaller than the first position or larger than the last position, */return ERROR;
			if (i <= l->length)/* If the insertion data location is not at the end of the table */{for (k=l->length-1;k>=i-1;k--)/* The data element after the insertion position is moved backward by one */
	l->data[k+1]=l->data[k];          } l->data[i-1]=e;
    
	/* Insert new element */l->length++;
return OK; }/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l)/*/////////* Operation Result: Delete the I data element of L and return its value with E, minus 1 */Status listdelete (sqlist *l,int I,elemtyp
    e *e) {int k;
    if (l->length==0)/* Linear table is empty */return ERROR;
    if (I < 1 | | i>l->length)/* Delete location incorrect */return ERROR;
    *e=l->data[i-1]; 
			if (I < l->length)/* If deletion is not the last position */{for (k=i; k < l->length; k++)/* will remove the successor element forward */
    l->data[k-1]=l->data[k];
   } l->length--;
return OK; }/* Initial conditions: Sequential linear table L already exists, 1≤i≤listlength (l)/*/* Operation result: Use E to return the value of the I data element in L, note that I refers to the position, the 1th position of the array is starting from 0 */Status getelem (sqlist l,int I,
    Elemtype *e) {if (l.length==0 | | < 1 | | i>l.length) return ERROR;
    
    *E=L.DATA[I-1];
return OK; }/* Initial conditions: Sequential linear table L already exists */* Operation result: Returns the length of L.

*/int Listlength (SqList L) {return l.length;} /* Initial conditions: Sequential linear table L already exists */* operation result: If L is empty table, return true, otherwise false */Status Listempty (sqlist L) {return (L.length = = 0)?
True:false; }/* Initial condition: sequential linear table L already exists */* Operation result: Returns the bit order of the 1th data element in L that satisfies the relationship with E.
    */* If such a data element does not exist, the return value is 0 */int locateelem (SqList l,elemtype e) {int i;
    if (l.length==0) return 0;
    For (I=0;i < l.length;i++) {if (l.data[i]==e) break;
    
    } if (I>=l.length) return 0;
return i+1;
    } int main () {sqlist L;
    Elemtype e;
    Status i;
    int opp;
    int j,k;
    int POS;
    
    Elemtype value;
    I=initlist (&AMP;L); printf ("Initialize successfully, l.length=%d\ n ", l.length); printf ("\n1. Traverse linear table \n2." Linear table Assignment \n3. Empty linear table \n4. A linear table is inserted into \n5. Finds elements in a table \n6. To determine whether an element is \n7 in a table. Deletes an element \n8. linear table Length \n9. The linear table is empty \n0. Exit \
    n Please select your action: \ n ");
        while (opp! = 0) {scanf ("%d", &opp);
                Switch (OPP) {case 1:listtraverse (L);
                printf ("\ n");
                
            Break
                Case 2:l = Create (L);
                printf ("Create random list: L.data=");
                Listtraverse (L);
                printf ("\ n");
                
            Break
                Case 3:i=clearlist (&AMP;L);
                printf ("Empty L after: l.length=%d\n", l.length);
                Listtraverse (L);
                printf ("\ n");
                
                
            Break
                Case 4:printf ("Please enter the insertion element position:");
                scanf ("%d", &pos);
                printf ("Please enter the value of the inserted element:");
                scanf ("%d", &value);
       i = Listinsert (&l,pos,value);         printf ("Insert complete, now linear table: \ n");
                Listtraverse (L);
                printf ("\ n");
                
            Break Case 5:printf ("You want to find the first few elements.")
                ");
                scanf ("%d", &pos);
                Getelem (l,pos,&e);
                printf ("The value of the%d element is:%d\n", pos,e);
                
            Break
                Case 6:printf ("Enter the value you want to know whether it is in the table:");
                scanf ("%d", &e);
                K=locateelem (l,e);
                This assumes that the elements in the random array do not repeat if (k) printf ("Value%d is the%d element in the table \ n", e,k);
                else printf ("no element with a value of%d \ n", e);
                
            Break Case 7:printf ("to delete the first few elements.
                ");
                scanf ("%d", &pos);
                Listdelete (&l,pos,&e);
                printf ("Delete complete, now linear table: \ n");
                Listtraverse (L);
                printf ("\ n");
                
Break            Case 8:k = listlength (L);
                printf ("Linear table of length:%d \ n", K);
                
            Break
                Case 9:i = Listempty (L);
                if (i) printf ("The linear table is empty. \ n");
                else printf ("The linear table is not empty \ n");
                
            Break
        Case 0:exit (0); }
    }
}

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.