Learning notes: Chapter 3 stack and queue

Keywords: C C++ Algorithm data structure

Chapter 3 stack and queue

Part I stack

1. Definition of stack

A stack is a linear table that is restricted to insert and delete operations only at the end of the table.

We call the end that allows insertion and deletion as the top of the stack, the other end as the bottom of the stack, and the stack without any data elements as an empty stack.

Stack is also called last in first out linear table, which is called LIFO structure for short.

The insertion operation of stack is called stack entry, also known as stack pressing. The deletion of stack is called out of stack, and some are also called bounce stack.

2. Abstract data type of stack

ADT Stack(stack)
Data 
    Same linear table. Elements have the same type, and adjacent elements have precursor and successor relationships.
Operation
    InitStack(&S): Initialize the operation and create an empty stack s. 
    DestroyStack(&S): If stack exists, destroy it.
    ClearStack(&S): Empty the stack.
    StatckEmpty(S): If the stack exists and is not empty, return true´╝îOtherwise return false. 
    GetTop(S,&e): If the stack exists and is not empty, use e return S The top element of the stack.
    Push(&S,e): If stack S Existing, insert new element e To stack S And become its stack top object.
    Pop(&S,&e): Delete stack S The top element of the stack in, and e Returns its value.
    StackLength(S): Return stack S Number of elements.
    StackTraverse(S,visit()): Stack S Already exists and is not empty. From the bottom of the stack to the top of the stack S Called for each data element of the visit(). 
endADT

3. Sequential storage structure and implementation of stack

The sequential stack, that is, the sequential storage structure of the stack, uses a group of storage units with continuous addresses to store the data elements from the bottom of the stack to the top of the stack, and the pointer top is attached to indicate the position of the top elements in the sequential stack. (top=0 means empty stack).

First, allocate a basic capacity for the stack, and then expand it paragraph by paragraph when the stack space is not enough. To do this, set two constants: STACK_INIT_SIZE (initial storage space allocation) and stockincrement (storage space allocation increment).

(1) Stack definition method:

typedef struct{
	SElemtype *base;  //Stack bottom pointer
	SElemtype *top;   //Stack top pointer
    int stacksize;    //Maximum capacity currently available
}SqStack;

(1) Initialization of stack:

Status InitStack(SqStack &S){
	S.base=(SElemtype *)malloc(STACK_INIT_SIZE*sizeof(SElemtype));
	if(!S.base)     //If the value of base is empty, the structure does not exist
		return(OVERFLOW);
	S.top=S.base;   //(top=base can be used as a tag with empty stack)
	S.stacksize=STACK_INIT_SIZE;   //The first storage allocation is made according to the initial allocation amount
	return OK;
}

(2) Stack destruction:

Status DestoryStack(SqStack &S)
{
	free(S.base);
	S.base = NULL;
	S.top = NULL;
	S.stacksize = 0;
	return OK;
}

(3) Stack emptying:

Status ClearStack(SqStack &S)
{
	S.top = S.base;
	return OK;
}

(4) Determine whether the stack is empty:

Status StackEmpty(SqStack S)
{
	if(S.top == S.base)
		return TRUE;
	else
		return FALSE;
}

(5) Return stack top element:

Status GetTop(SqStack S,SElemType &e)
{
	if(S.top == S.base) 
        return ERROR;
	e = *(S.top -1);
	return OK;
}

(6) Stack pressing:

Status Push(SqStack &S, SElemType e)
{
	if(S.top - S.base >= S.stacksize)  //Stack full, additional storage space
	{
		S.base = (SElemType *)realloc(S.base,(S.stacksize + STACKINCREMENT)*sizeof(SElemType));
		if(!S.base) 
            exit(OVERFLOW);
		S.top = S.base + S.stacksize;
		S.stacksize += STACKINCREMENT; 
	}
	*S.top++ = e;
	return OK;
}

(6) Out of stack:

Status Pop(SqStack &S, SElemType &e)
{
	if(S.top == S.base) return ERROR;
	e = * --S.top;
	return OK;
}

(7) Number of stack elements:

int StackLength(SqStack S)
{
	return S.top - S.base;
}

(8) Stack elements are sequentially output from the bottom of the stack:

Status StackTraverse(SqStack S, Status(* visit)(SElemType))
{
	while(S.top > S.base)
		visit(*S.base++);
	printf("\n");
	return OK;
}

Status visit(SElemType e)
{
	printf("%d ", e);
	return OK;
}

(2) Another way to define the stack:

typedef struct
{
        SElemType data[MAXSIZE];
        int top; // Pointer for stack top 
}SqStack;

(1) Initialization of stack:

Status InitStack(SqStack &S)
{ 
        /* S.data=(SElemType *)malloc(MAXSIZE*sizeof(SElemType)); */
        S.top=-1;
        return OK;
}

(2) Stack emptying:

Status ClearStack(SqStack &S)
{ 
        S.top=-1;
        return OK;
}

(3) Determine whether the stack is empty:

Status StackEmpty(SqStack S)
{ 
        if (S.top==-1)
                return TRUE;
        else
                return FALSE;
}

(4) Return stack top element:

Status GetTop(SqStack S,SElemType &e)
{
        if (S.top==-1)
                return ERROR;
        else
                e=S.data[S.top];
        return OK;
}

(5) Stack pressing:

Status Push(SqStack &S,SElemType e)
{
        if(S.top == MAXSIZE -1) 
        {
                return ERROR;
        }
        S.top++;				//The stack top pointer is increased by one 
        S.data[S.top]=e;  //Assign the newly inserted element to the stack top space
        return OK;
}

(6) Out of stack:

Status Pop(SqStack &S,SElemType &e)
{ 
        if(S.top==-1)
                return ERROR;
        e=S.data[S.top];	
        S.top--;			
        return OK;
}

(7) Number of stack elements:

int StackLength(SqStack S)
{ 
        return S.top+1;
}

(8) Stack elements are sequentially output from the bottom of the stack:

Status visit(SElemType c)
{
        printf("%d ",c);
        return OK;
}

Status StackTraverse(SqStack S)
{
        int i;
        i=0;
        while(i<=S.top)
        {
                visit(S.data[i++]);
        }
        printf("\n");
        return OK;
}

4. Shared space of two stacks

The bottom of a stack is the beginning of the array, that is, the subscript is 0. The other stack is the end of the array, that is, the subscript is at the length n-1 of the array.
If two stacks add elements, the two ends extend to the middle.

typedef struct 
{
        SElemType data[MAXSIZE];
        int top1;	//Stack 1 stack top pointer
        int top2;	//Stack 2 stack top pointer
}SqDoubleStack;

(1) Initialization of stack:

Status InitStack(SqDoubleStack &S)
{ 
        S.top1=-1;
        S.top2=MAXSIZE;
        return OK;
}

(2) Stack emptying:

Status ClearStack(SqDoubleStack &S)
{ 
        S.top1=-1;
        S.top2=MAXSIZE;
        return OK;
}

(3) Determine whether the stack is empty:

Status StackEmpty(SqDoubleStack S)
{ 
        if (S.top1==-1 && S.top2==MAXSIZE)
                return TRUE;
        else
                return FALSE;
}

(4) Stack pressing:

//Insert element e as the new stack top element
Status Push(SqDoubleStack &S,SElemType e,int stackNumber)
{
        if (S.top1+1==S.top2)	// Stack full
                return ERROR;	
        if (stackNumber==1)			//There are elements in stack 1
                S.data[++S.top1]=e; 
        else if (stackNumber==2)	//There are elements in stack 2
                S.data[--S.top2]=e; 
        return OK;
}

(5) Out of stack:

Status Pop(SqDoubleStack &S,SElemType &e,int stackNumber)
{ 
        if (stackNumber==1) 
        {
                if (S.top1==-1) 
                        return ERROR;
                e=S.data[S.top1--]; 
        }
        else if (stackNumber==2)
        { 
                if (S.top2==MAXSIZE) 
                        return ERROR; 
                e=S.data[S.top2++];
        }
        return OK;
}

(6) Number of stack elements:

// Returns the number of elements of S, that is, the length of the stack
int StackLength(SqDoubleStack S)
{ 
        return (S.top1+1)+(MAXSIZE-S.top2);
}

(7) Stack elements are sequentially output from the bottom of the stack:

Status visit(SElemType c)
{
        printf("%d ",c);
        return OK;
}

Status StackTraverse(SqDoubleStack S)
{
        int i;
        i=0;
        while(i<=S.top1)
        {
                visit(S.data[i++]);
        }
        i=S.top2;
        while(i<MAXSIZE)
        {
                visit(S.data[i++]);
        }
        printf("\n");
        return OK;
}

5. Chain storage structure and implementation of stack

typedef struct StackNode
{
        SElemType data;
        struct StackNode *next;
}StackNode,*LinkStackPtr;


typedef struct
{
        LinkStackPtr top;
        int count;
}LinkStack;

(1) Initialization of stack:

Status InitStack(LinkStack &S)
{ 
        S.top = (LinkStackPtr)malloc(sizeof(StackNode));
        if(!S.top)
                return ERROR;
        S.top=NULL;
        S.count=0;
        return OK;
}

(2) Stack emptying:

//Set S to empty stack
Status ClearStack(LinkStack &S)
{ 
        LinkStackPtr p,q;
        p=S.top;
        while(p)
        {  
                q=p;
                p=p->next;
                free(q);
        } 
        S.count=0;
        return OK;
}

(3) Determine whether the stack is empty:

//If stack S is an empty stack, it returns TRUE; otherwise, it returns FALSE
Status StackEmpty(LinkStack S)
{ 
        if (S.count==0)
                return TRUE;
        else
                return FALSE;
}

(4) Return stack top element:

Status GetTop(LinkStack S,SElemType &e)
{
        if (S.top==NULL)
                return ERROR;
        else
                e=S.top->data;
        return OK;
}

(5) Stack pressing:

Status Push(LinkStack &S,SElemType e)
{
        LinkStackPtr s=(LinkStackPtr)malloc(sizeof(StackNode)); 
        s->data=e; 
        s->next=S.top;	// Assign the current stack top element to the direct successor of the new node.
        S.top=s;        // Assign the new node s to the stack top pointer
        S.count++;
        return OK;
}

(6) Out of stack:

Status Pop(LinkStack &S,SElemType &e)
{ 
        LinkStackPtr p;
        if(StackEmpty(S))
                return ERROR;
        e=S.top->data;
        p=S.top;					//Assign the stack top node to p.
        S.top=S.top->next;    		//Make the stack top pointer move down one bit to point to the next node.
        free(p);                        
        S.count--;
        return OK;
}

(7) Number of stack elements:

int StackLength(LinkStack S)
{ 
        return S.count;
}

(8) Stack elements are output in sequence:

Status visit(SElemType c)
{
        printf("%d ",c);
        return OK;
}

Status StackTraverse(LinkStack S)
{
        LinkStackPtr p;
        p=S.top;
        while(p)
        {
                 visit(p->data);
                 p=p->next;
        }
        printf("\n");
        return OK;
}


But if you do good, don't ask about your future.
Never forget why you started, and your mission can be accomplished.
Be yourself, warm and positive!!!

Posted by westexasman on Thu, 28 Oct 2021 13:33:09 -0700