Data Structure-Stack

Source: Internet
Author: User

Definition


Only linear tables that are inserted and deleted at the end of the table are allowed. (First in and then out)


Sequential Structure


C Definition
[Cpp]
# Define MAXSIZE 20
 
# Pragma mark sequential Stack
 
Typedef int SElemType;
Typedef struct {
SElemType data [MAXSIZE];
Int top;
} SqStack;

# Define MAXSIZE 20

# Pragma mark sequential Stack

Typedef int SElemType;
Typedef struct {
SElemType data [MAXSIZE];
Int top;
} SqStack;

Stack operation
[Cpp]
Int Push (SqStack * S, SElemType e)
{
If (S-> top = MAXSIZE-1)
Return 0;
S-> data [++ S-> top] = e;
Return 1;
}

Int Push (SqStack * S, SElemType e)
{
If (S-> top = MAXSIZE-1)
Return 0;
S-> data [++ S-> top] = e;
Return 1;
}

Stack Creation
[Cpp]
Int Pop (SqStack * S, SElemType * e)
{
If (S-> top =-1)
Return 0;
* E = S-> data [S-> top --];
Return 1;
}

Int Pop (SqStack * S, SElemType * e)
{
If (S-> top =-1)
Return 0;
* E = S-> data [S-> top --];
Return 1;
}


Shared Stack


The shared stack can be seen as the data structure at the end of two sequential stacks that fall down. It is usually used when there is an inverse relationship between the two stack space requirements, for example, to buy and sell.


C Definition
[Cpp]
Typedef struct {
SElemType data [MAXSIZE];
Int top1;
Int top2;
} SqDoubleStack;

Typedef struct {
SElemType data [MAXSIZE];
Int top1;
Int top2;
} SqDoubleStack;

Stack pressure
[Cpp]
Int PushDouble (SqDoubleStack * S, SElemType e, int stackNumber)
{
// Stack full
If (S-> top1 + 1 = S-> top2)
Return 0;
If (stackNumber = 1)
S-> data [++ S-> top1] = e;
Else if (stackNumber = 2)
S-> data [-- S-> top2] = e;
Return 1;
}

Int PushDouble (SqDoubleStack * S, SElemType e, int stackNumber)
{
// Stack full
If (S-> top1 + 1 = S-> top2)
Return 0;
If (stackNumber = 1)
S-> data [++ S-> top1] = e;
Else if (stackNumber = 2)
S-> data [-- S-> top2] = e;
Return 1;
}

Stack
[Cpp]
Int PopDouble (SqDoubleStack * S, SElemType * e, int stackNumber)
{
If (stackNumber = 1)
If (S-> top1 =-1)
Return 0;
* E = S-> data [S-> top1 --];
If (stackNumber = 2)
If (S-> top2 = MAXSIZE)
Return 0;
* E = S-> data [S-> top2 ++];
Return 1;
}

Int PopDouble (SqDoubleStack * S, SElemType * e, int stackNumber)
{
If (stackNumber = 1)
If (S-> top1 =-1)
Return 0;
* E = S-> data [S-> top1 --];
If (stackNumber = 2)
If (S-> top2 = MAXSIZE)
Return 0;
* E = S-> data [S-> top2 ++];
Return 1;
}


Chain Stack


The stack chain storage structure is similar to the chain storage structure in a linear table.


C Definition
[Cpp]
Typedef struct StackNode {
SElemType data;
Struct StackNode * next;
} StackNode, * LinkStackPtr;
 
Typedef struct LinkStack {
LinkStackPtr top;
Int count;
} LinkStack;

Typedef struct StackNode {
SElemType data;
Struct StackNode * next;
} StackNode, * LinkStackPtr;

Typedef struct LinkStack {
LinkStackPtr top;
Int count;
} LinkStack;

Stack pressure
[Cpp]
Int PushLink (LinkStack * S, SElemType e)
{
LinkStackPtr p = (LinkStackPtr) malloc (sizeof (StackNode ));
P-> data = e;
P-> next = S-> top;
S-> top = p;
S-> count ++;
Return 1;
}

Int PushLink (LinkStack * S, SElemType e)
{
LinkStackPtr p = (LinkStackPtr) malloc (sizeof (StackNode ));
P-> data = e;
P-> next = S-> top;
S-> top = p;
S-> count ++;
Return 1;
}

Stack
[Cpp]
Int PopLink (LinkStack * S, SElemType * e)
{
LinkStackPtr p;
* E = S-> top-> data;
P = S-> top;
S-> top = S-> top-> next;
Free (p );
S-> count --;
Return 1;
}

Int PopLink (LinkStack * S, SElemType * e)
{
LinkStackPtr p;
* E = S-> top-> data;
P = S-> top;
S-> top = S-> top-> next;
Free (p );
S-> count --;
Return 1;
}

Compare sequential stack and chain Stack


The two data structure operations are not complex, and the same time complexity is O (1 ).
The sequential stack needs a fixed length, but it is easy to locate during access.
The chain stack requires pointer fields, so some memory overhead needs to be added, but different from the sequential stack limit length.


Stack application-recursion
For example, the famous Fibonacci series:
[Cpp]
Int Fbi (int I)
{
If (I <2)
Return I = 0? 0: 1;
Return Fbi (I-1) + Fbi (I-2 );
}

Int Fbi (int I)
{
If (I <2)
Return I = 0? 0: 1;
Return Fbi (I-1) + Fbi (I-2 );
}

 

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.