The representation and realization of the tenth stack in the course of data structure

Source: Internet
Author: User
Tags exit empty

The topic of this lesson: Stack representation and implementation

Teaching Purpose: data type definition of stack, expression and realization of sequential storage of stack

Teaching emphases: The sequential storage representation and realization method of stack

Teaching Difficulty: the definition of stack

Teaching Content:

First, the definition of the stack

Stacks are linear tables that qualify inserts or deletes only at the end of a table.

The footer of the stack is called the top of the stack, the table header is called the bottom of the stack, and the empty table without elements is called empty stack.

The abstract data type definition of the stack:

ADT stack{

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:

Initstack (&s) constructs an empty stack S

Destroystack (&s) stack s exists then stack S is destroyed

Clearstack (&s) stack S exist is clear as empty stack

Stackempty (s) stack S exists to return TRUE or false

Stacklength (s) stack s exists to return the number of elements of S, that is, the length of the stack

GetTop (s,&e) stack S is present and NOT NULL returns the top element of s

Push (&s,e) stack S exists to insert element e as the new stack top element

Pop (&s,&e) stack s exists and non-null deletes the top element of S and returns its value with E

Stacktraverse (S,visit ()) stack s exists and is not NULL to invoke function visit () once the visit () fails, the operation fails from the bottom of the stack to the top of the stack in turn

}adt Stack

Second, the representation and implementation of the stack

How the stack is stored:

1, sequential stack: the use of a group of address consecutive storage units from the bottom of the stack to the top of the stack of data elements, while the top of the pointer to indicate the stack of elements in the order stack position

2, Chain stack: the use of linked list to achieve

Class C language definition for sequential stacks:

typedef struct{

Selemtype *base;

Selemtype *top; The purpose of setting the stack bottom two pointers is to make it easier to determine whether the stack is empty

int stacksize; The maximum capacity currently available for the stack.

}sqstack;

The module description of the sequential stack:

struct STACK {

Selemtype *base;

Selemtype *top;

int stacksize;

};

typedef struct STACK Sqstack;

Status Initstack (Sqstack &s);

Status Destroystack (Sqstack &s);

Status Clearstack (Sqstack &s);

Status stackempty (Sqstack S);

int Stacklength (Sqstack S);

Status GetTop (Sqstack s,selemtype &e);

Status Push (Sqstack &s,selemtype e);

Status POPs (Sqstack &s,selemtype &e);

Status Stacktraverse (Sqstack s,status (*visit));

Status Initstack (Sqstack &s) {

S.base= (Selemtype *) malloc (stack_init_size *sizeof (elemtype));

if (! s.base) exit (OVERFLOW);

S.top=s.base;

S.stacksize=stack_ini_size;

return OK;

}//inistack

Status Destroystack (Sqstack &s); {

}//destroystack

Status Clearstack (Sqstack &s); {

S.top=s.base;

}//clearstack

Status stackempty (Sqstack S); {

if (s.top==s.base) return TRUE;

else return FALSE;

}//stackempty

int Stacklength (Sqstack S); {

int i; Selemtype *p;

i=0;

P=s.top;

while (p!=s.base) {p++; i++}

}//stacklength

Status GetTop (Sqstack s,selemtype &e); {

if (s.top==s.base) return ERROR;

e=* (s.top-1);

return OK;

}//gettop

Status Push (Sqstack &s,selemtype e); {

if (s.top-s.base>=s.stacksize) {

S.base= (Elemtype *) realloc (s.base,

(S.stacksize + stackincrement) * sizeof (elemtype));

if (! s.base) exit (OVERFLOW);

S.top=s.base+s.stacksize;

S.stacksize+=stackincrement;

}

*s.top++=e;

return OK;

}//push

Status POPs (Sqstack &s,selemtype &e); {

if (s.top==s.base)

return ERROR;

E=*--s.top;

return OK;

}//pop

Status Stacktraverse (Sqstack s,status (*visit)); {

}//stacktraverse

The above pseudocode C language source code

Third, summary

The definition of the stack

Sequential storage implementation of stacks

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.