Data Structure Tutorial The second lesson the representation and realization of the abstract datum type

Source: Internet
Author: User
Tags abstract definition expression printf

The topic of this lesson: representation and implementation of abstract data types

Teaching Purpose: To understand the definition, representation and implementation methods of abstract data types

Teaching Focus: abstract data type representation, Class C language syntax

Teaching Difficulty: abstract data type representation

Teaching Content:

Abstract data type definition (ADT)

Role: Abstract data types make it easier to describe the real world. Example: a linear table is used to describe a student's score sheet, and a tree or graph is used to describe the genetic relationship.

Definition: A mathematical model and a set of operations defined on the model.

Key: The person who uses it can only care about its logical features and does not need to know how it is stored. The person who defines it also does not have to care about how it is stored.

Example: The mathematical model of an abstract data type such as a linear table is a collection of data elements that have a relationship with each element except the first and the last, each of which has a unique forward and unique successor. There are some things you can do: Insert an element, delete an element, and so on.

Abstract data Type classification
Atomic type Value is not decomposed, such as int
Fixed aggregation type A value is composed of a determined number of components, such as a plural
Variable aggregation type The number of components of a value is uncertain, as in the case of students

Abstract data type notation:

One

Ternary group representation: (D,S,P)

where d is the data object, S is the set of relationships on D, and P is the basic set of operations for D.

The definition format of the book:

ADT abstract data type name {

Data Object:< data Object definition >

Definition of data Relationship:< data relationship >

Basic operations:< definition of basic operations >

}adt abstract Data type name

Example: Representation of a linear table

Name Linear table
Data Objects d={ai| ai (-elemset,i=1,2,..., n,n>=0} A collection of arbitrary data elements
Data relationships r1={<ai-1,ai>| Ai-1,ai (-d,i=2,..., n} With the exception of the first and last, each element has a single direct forward and only direct successor
Basic operations Listinsert (&l,i,e) L is the linear table, I is the position, E is the data element.
Listdelete (&l,i,e)
...

Second, Class C language grammar

Examples of Class C language syntax
1. Predefined Constants and types #define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE-1
#define OVERFLOW-2
typedef in STATUS; The status is the type of the function whose value is the function result status code.
2. Storage structure of data structure typedef elemtype-A;
3, the basic operation of the algorithm

function type function name (function parameter table) {
Algorithm description
Statement sequence
}//Name of the letter

4. Assignment statement Simple assignment: Variable name = expression;
To assign values in series: Variable Name 1 = variable name 2=...= variable name k= expression;
Assign values in groups: (variable name 1, ..., variable name k) = (expression 1, ..., expression k);
Structure name = structure name;
Structure name = (value 1, ..., value k);
Variable name []= expression;
Variable name [start subscript ... Terminate subscript]= variable name [start subscript ...] terminate the subscript];
Exchange Assignment: Variable name <--> variable name;
Conditional assignment: Variable name = conditional expression? An expression? Expression T: expression F
5, select the statement

1, if (expression) statement;
2, if (expression) statement;
else statement;
3, switch (expression) {
Case value 1: statement sequence 1;break;

...
Case value N: statement sequence n;break;
Default: Statement sequence n+1;break;
}
4, switch{
Case Condition 1: sentence sequence 1;break;

...
Case condition N: sentence sequence n;break;
Default: Statement sequence n+1;break;
}

6. Circular statements for (assignment initial expression; condition; modify expression sequence) statement;
while (condition) statement;
do{Statement sequence}while (condition);
7. Concluding sentences

return [expression];
Return Function concluding sentence
Break Case concluding sentence
Exit (exception code); Abnormal closing sentence

8. Input and OUTPUT statements scanf ([format string], variable 1, ..., variable n);
9, Notes Text sequence
10. Basic function Max (expression 1, ..., expression N)
Min,abs,floor,ceil,eof,eoln
11. Logical operation && and operation; or operation

Example: The realization of a linear table:
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)
Listinsert (&l,i,e)
Listdelete (&l,i,&e)

}adt List

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;

}

The following is an example of the C language compilation pass:

#define ERROR 0
#define OK 1
struct STU
{char name[20];
Char stuno[10];
int age; int score;
}stu[50];
struct LIST
{struct STU stu[50];
int length;
}l;

int printlist (struct LIST L)
{int i;
printf ("Name Stuno age score\n");
for (i=0;i<l.length;i++)
printf ("%s%s\t%d\t%d\n", L.stu[i].name, L.stu[i].stuno, L.stu[i].age, L.stu[i). Score);
printf ("\ n");
}

int listinsert (struct LIST *l,int i,struct STU e)
{struct STU *p,*q;
If (i<1| | i>l->length+1)
return ERROR;
q=& (l->stu[i-1]);
for (p=&l->stu[l->length-1];p >=q;--p)
* (p+1) =*p; *q=e
return OK;
}/*listinsert before I */

Main ()
{struct STU e;
L.length=0;
strcpy (e.name, "Zmofun");
strcpy (E.stuno, "100001");
E.age=80;
e.score=1000;
Listinsert (&l,1,e);
Printlist (L);
printf ("List length now%d.\n\n", l.length);

strcpy (e.name, "Bobjin");
strcpy (E.stuno, "100002");
e.age=80;
e.score=1000;
Listinsert (&l,1,e);
Printlist (L);
printf ("List length now%d.\n\n", l.length);
}

E:\zm\zmdoc\datastru\class02>listdemo

Name Stuno Age Score

Zmofun 100001 80 1000

The List length is 1.

Name Stuno Age Score

Bobjin 100002 80 1000

Zmofun 100001 80 1000

The List length is 2.

Third, summary

abstract data type definition;

Implementation of abstract data types: One, Class C language implementation of two, C language implementation

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.