Data structure tutorial class Five linear table type definition

Source: Internet
Author: User
Tags definition data structures empty

Teaching Purpose: grasping the concept and type definition of linear table

Teaching emphases: the type definition of linear table

Teaching Difficulty: The type definition of linear table

Teaching Content:

Review: Types of data structures

The characteristics of linear structure:

In a non-empty finite set of data elements,

(1) The existence of a unique data element called "the first";

(2) The existence of the only one known as the "last" data element;

(3) In addition to the first, each data element in the set has only one precursor;

(4) Except for the last one, each data element in the set has only one successor.

Definition of a linear table

The linear table is the most common and simplest kind of data structure.

A linear table is a finite sequence of n data elements.

A data element can be a number, a symbol, or it can be a picture, a page, or more complex information.

Linear Table Example:

1,

1 2 3 4 5 6 7

2,

School Number Name Chinese Mathematical C language
6201001 Tom 85 54 92
6201002 John doe 92 84 64
6201003 Harry 87 74 73
6201004
...

A data element can also consist of several data items (as in example 3 above). This often calls the data element a record. A linear table containing a large number of records is also called a file.

There are many types of data elements in the linear table, but the elements in the same linear table must have the same characteristics, that is, the same data object and the sequential couple relationship between adjacent data elements.

A1 ... Ai-1 Ai Ai+1 ... An

AI is the direct precursor element of ai+1, and Ai+1 is the direct successor of AI.

The number N of elements in a linear table is defined as the length of a linear table, and 0 o'clock is called an empty table. Each data element in a Non-empty table has a determined location. AI is the first element, which refers to I as the data element AI in the linear sequence.

Type definition of linear table

1. The definition of the abstract data type linear table is as follows:

ADT list{

Data objects: d={ai| ai (-elemset,i=1,2,..., n,n>=0}

Data relationship: r1={<ai-1,ai>| Ai-1,ai (-d,i=2,..., n}

Basic operations:

Initlist (&L)
Destroylist (&L)
Clearlist (&L)
Listempty (L)
Listlength (L)
Getelem (L,i,&e)
Locateelem (L,e,compare ())
Priorelem (L,cur_e,&pre_e)
Nextelem (l,cur_e,&next_e)
Listinsert (&l,i,e)
Listdelete (&l,i,&e)
Listtraverse (L,visit ())
Union (List &la,list &lb)

}adt List

2, part of the operation of the Class C implementation:

Initlist (&L)

{l.elem= (Elemtype *) malloc (list_init_size*sizeof (elemtype));

if (! L.elem) exit (OVERFLOW);

l.length=0;

L.listsize=list_init_size;

return OK;

}//initlist

Getelem (L,i,&e)

{*e=l.lem[i]

}//getelem

Listinsert (List &l,int i,elemtype e)

{if (i<1| | i>l.length+) return ERROR;

q=& (L.elem[i-1]);

For (p=& (l.elem[l.length-1));p >=q;--p) * (p+1) =*p;

*q=e;

++l.length;

return OK;

}//listinsert

void Union (List &la,list &lb)

{la_len=listlength (La); Lb_len=listlength (LB);

for (i=1;i<=lb_len;i++) {

Getelem (lb,i,e);

if (! Locateelem (la,e,equal))

Listinsert (la,++la_len,e);

}//union

void Mergelist (List la,list lb,list &LC)

{initlist (Lc);

i=j=1;k=0;

La_len=listlength (La); Lb_len=listlength (LB);

while ((I<=la_len) && (J<lb_len)) {

Getelem (La,i,ai); Getelem (LB,J,BJ);

if (AI<=BJ) {Listinsert (lc,++k,ai); ++i;}

Else{listinsert (LC,++K,BJ); ++j;}

}

while (K<=la_len) {

Getelem (La,i++,ai); Listinsert (Lc,++k,ai);

}

while (J<=lb_len) {

Getelem (LB,J++,BJ); Listinsert (LC,++K,BJ);

}

}//mergelist

3, part of the operation of the C language implementation, the following is the results of the program run:

-------------------List Demo is running ...----------------
The IS Insertlist function.
Name Stuno Age Score
STU1 100001 80 1000
STU2 100002 80 1000
List A length now is 2.
Name Stuno Age Score
STU1 100001 80 1000
STU2 100002 80 1000
STU3 100003 80 1000
List A length now is 3.
Name Stuno Age Score
Zmofun 100001 80 1000
Bobjin 100002 80 1000
STU1 100001 80 1000
List B length is 3.

Second is unionlist function.
Now Union list A and List B .....
Name Stuno Age Score
STU1 100001 80 1000
STU2 100002 80 1000
STU3 100003 80 1000
Zmofun 100001 80 1000
Bobjin 100002 80 1000
List A length now is 5.

Welcome to visit Http://zmofun.heha.net!

Third, summary

Definition of a linear table

Type definition 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.