Data structure. Sequence table, data structure sequence table

Source: Internet
Author: User

Data structure. Sequence table, data structure sequence table

# Include <stdio. h>
# Include <stdlib. h>

# Define LIST_INIT_SIZE 100
# Define LIST_INCREMENT 10
Typedef struct Point // element type
{
Int x;
Int y;
} Point;
Typedef Point ElemType;
Typedef struct // list type
{
ElemType * elem; // data
Int length; // current length
Int listsize; // maximum size
} Sqlist;
Int cpy (ElemType * des, ElemType data)
{
Des-> x = data. x;
Des-> y = data. y;
Return 0;
}
Int elem_cmp (ElemType data1, ElemType data2 );
Int list_init (Sqlist * mylist );
Int list_insert (Sqlist * mylist, ElemType data );
Int list_find (Sqlist * mylist, ElemType data );
Int list_delete (Sqlist * mylist, ElemType data );
Int list_disp (Sqlist mylist );
Int main ()
{
Sqlist mylist;
List_init (& mylist );
Printf ("% d, % d \ n", mylist. length, mylist. listsize );
Printf ("Hello world! \ N ");

Point tempt;
Tempt. x = 11;
Tempt. y = 12;

List_insert (& mylist, tempt );
List_disp (mylist );
Printf ("% d, % d \ n", mylist. length, mylist. listsize );
Getchar ();

Tempt. x = 21;
Tempt. y = 22;
List_insert (& mylist, tempt );
List_disp (mylist );
Printf ("% d, % d \ n", mylist. length, mylist. listsize );
Getchar ();

List_delete (& mylist, tempt );
List_disp (mylist );
Printf ("% d, % d \ n", mylist. length, mylist. listsize );
Getchar ();

Return 0;
}
Int elem_cmp (ElemType data1, ElemType data2)
{
If (data1.x = data2.x) & (data1.y = data2.y ))
{
Return 1;
}
Else
{
Return 0;
}
}
Int list_init (Sqlist * mylist)
{
Mylist-> elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType ));
If (mylist-> elem! = NULL)
{
Mylist-> length = 0;
Mylist-> listsize = LIST_INIT_SIZE;

Printf ("% d, % d \ n", mylist-> length, mylist-> listsize );
Return 0;
}
Else
{
Printf ("allocate memory is failed! Sorry \ n ");
Return 1;
}
}
Int list_insert (Sqlist * mylist, ElemType data)
{
If (mylist-> length> = mylist-> listsize)
{
// If the memo is not enougth to use, realloc
Mylist-> elem = (ElemType *) realloc (mylist-> elem, LIST_INIT_SIZE + LIST_INCREMENT );
If (mylist-> elem = NULL)
{
Printf ("memo realloc is failed! Sorry, friend \ n ");
Return 1;
}
Else
{
Mylist-> listsize + = LIST_INCREMENT;
}
}
Cpy (& mylist-> elem [mylist-> length], data );
Mylist-> length + = 1;
Return 0;
}
Int list_find (Sqlist * mylist, ElemType data)
{
Int I = 0;
For (I = 0; I <mylist-> length; I ++)
{
If (elem_cmp (mylist-> elem [I], data)> 0)
{
Return I + 1;
}
}
Return 0;
}
Int list_delete (Sqlist * mylist, ElemType data)
{
Int I = 0, result = list_find (mylist, data );
If (result = 0)
{
Printf ("not found the data \ n ");
Return 1;
}
Else
{
For (I = result-1; I <mylist-> length; I ++)
{
Mylist-> elem [I] = mylist-> elem [I + 1];
}
Mylist-> length --;
Return 0;
}
}
Int list_disp (Sqlist mylist)
{
System ("clear ");
Int j = 0;
For (j = 0; j <mylist. length; j ++)
{
Printf ("{% d, % d} \ t", mylist. elem [j]. x, mylist. elem [j]. y );
}
Return 0;
}


Data Structure sequence table

I don't know if this program can meet your requirements. This program uses the sequence table method to delete the elements of a linear table, and then inserts the elements into the linear table, with a running diagram attached to it:
The procedure is as follows:
# Include <iostream>
Using namespace std;
# Include <malloc. h>
# Define LIST_INIT_SIZE 100
# Define LISTINCREMENT 10
# Define OVERFLOW-1
# Define OK 1
# Define ERROR 0
Typedef int Status;
Typedef int ElemType;
Typedef int KeyType;
Typedef struct {
ElemType * elem;
KeyType * key;
Int length;
Int listsize;
} SqList;
Typedef struct {
KeyType key;
} SElemType;
Status InitList (SqList & L) {/* defines a linear table */
Int length1;
Printf ("determine the sequence table length :");
Scanf ("% d", & length1 );
L. listsize = length1;
L. elem = (ElemType *) malloc (length1 * sizeof (ElemType ));
If (! L. elem ){
Printf ("out of space ");
Exit (OVERFLOW );
}
L. length = 0;
Return OK;
}

Status Listinsert (SqList & L, int I, ElemType e) {/* insert element e before element I */
ElemType * p, * q, * newbase;
If (I <1 | I> L. length + 1 ){
Return ERROR;
}
If (L. length> L. listsize)
{
Newbase = (ElemType *) realloc (L. elem, (L. listsize + LISTINCREMENT) * sizeof (ElemType ));
If (newbase = NULL ){
Printf ("out of space ");
Return (OVERFLOW );
}
L. listsize + = LISTINCREMENT;
}
P = & (L. elem [I-1]);
For (q = & (L. elem [L. length-1]); q> = p; q --){
* (Q + 1) = * q;
}
L. elem [I-1] = e;
L. length ++;

Return OK;
}
Status DeleteList (SqList & L, int I) {/* Delete I elements */
ElemType * q, * p;
If (I <1 | I> L. length ){
Return ERROR;
}
Q = & (L. elem [I-1]);
P = L... the remaining full text>


Data Structure sequence table

# Include <stdio. h>
# Include <malloc. h> // header file inclusion command

# Define List_init_size 100 // definition of symbolic Constants
# Define Listincrement 100
# Define TRUE 1
# Define FALSE 0
# Define OK 1
# Define ERROR 0
# Define INFEASIBLE-1
# Define OVERFLOW-2

Typedef int Elemtype; // The abstract data type elemtype is embodied in the int type.
Typedef int Status;
Typedef struct {// define struct
Elemtype * elem;
Int length;
Int listsize;
} Sqlist;
Status Initlist_sq (Sqlist * l) // returns-2 If the function (initialization sequence table) fails to be initialized.
{
L-> elem = (Elemtype *) malloc (sizeof (Elemtype ));
If (! L-> elem) return (OVERFLOW );
L-> length = 0;
L-> listsize = List_init_size;
Return OK;
}

Status Creatlist_sq (Sqlist * l, int n) // The initialization sequence table is an ordered table containing n elements.
{
Int I;
Printf ("Input % d numbers: \ n", n );
For (I = 0; I <n; I ++)
Scanf ("% d", & (l-> elem [I]);
L-> length = n;
Return OK;
}

Void Outputlist_sq (Sqlist * l) // function (output sequence table element)
{
Int I;
For (I = 0; I <l-> length; I ++)
{
Printf ("% 5d", l-> elem [I]);
If (I + 1) % 5 = 0)
Printf ("\ n ");
}
Printf ("\ n ");

}
Void Destroylist_sq (Sqlist * l) // destroy the sequence table l
{
Free (l-> elem );

}

Status Insertlist_sq (Sqlist * l, int I, Elemtype e) // insert element e before position I of the sequence table
{
Int j;
Elemtype * p;
If (I> l-> length)
{
Printf ("the input data is invalid! ");
Return (OVERFLOW );
}

If (l-> length> = l-> listsize)
{
Elemtype * p;
P = (Elemtype *) realloc (l-> elem, (List_init_size + Listincrement) * sizeof (Elemtype ));
If (p = 0)
{... Remaining full text>

Related Article

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.