操作实现部分 procedure initialise (var S:stack); var i integer; begin 一旦我们小心翼翼地完成了定义和实现, 我们就可以发布这个“数据结构”为一 个“数据类型”供别的程序员直接使用, 他们不用关心这个类型的所有实现细节, 就像我们自己使用int类型一样! end; function top(var S:stack):elemtype; begin return the top element of the stack x) top t=S.elements[S.last] end;
操作实现部分 一旦我们小心翼翼地完成了定义和实现, 我们就可以发布这个“数据结构”为一 个“数据类型”供别的程序员直接使用, 他们不用关心这个类型的所有实现细节, 就像我们自己使用int类型一样!
Program reverse(input,output) var s:stack;c:char begin initialize(s); read (c); while c<>”#”do begin push(s,c);read(c); end while not empty(s)do begin write(top(s)); pop(s); end end
Program reverse(input, output) var s: stack; c: char begin initialize(s); read (c); while c<>”#” do begin push(s,c);read(c); end while not empty(s) do begin write(top(s)); pop(s); end end
实际上,我们还可以采用不同的实现方式来实 现某个数据管理方式! 一} Figure 3.2 Pointer representation of a stack S. ■难道我们就定义了两个不同的stack了吗?
实际上,我们还可以采用不同的实现方式来实 现某个数据管理方式! ◼ 难道我们就定义了两个不同的stack了吗?
如果我们在构造自己的数据组织方式时,写出 了一个关于这个方式的“约定”,而暂时没有 涉及实现细节,甚至不再关心实现细节而交由 其他人员实现时,我们写出来的“约定”就有 了新名词: 抽象数据类型:ADT
如果我们在构造自己的数据组织方式时,写出 了一个关于这个方式的“约定”,而暂时没有 涉及实现细节,甚至不再关心实现细节而交由 其他人员实现时,我们写出来的“约定”就有 了新名词: 抽象数据类型:ADT
ADT stack:set of elements of type elemtype (elemtype is used to refer to the type of the individual elements in a stack. Elemtype can potentially be any defined type * Operations: procedure initialise(var S:stack); This procedure assigns an empty stack to S. 完整的 procedure push(var S:stack;a:elemtype); Stack S should not be full.This procedure adds the element a at stack的 the top of the stack. procedure pop(var S:stack); Stack S should not be empty.The top element of the stack is ADT removed. function top(S stack):elemtype; Stack S should be non-empty and this function returns the top element of the stack S.The stack S is left unchanged.(If elem- type is a structured type,this function should be rewritten as a procedure,see note 1 of section 3.1.) function empty(S:stack):boolean; This function returns true if S is empty and false otherwise. function full(S stack):boolean; If S is full this function returns a true value. Otherwise it returns a false value
完整的 stack的 ADT