Data Structure and algorithm analysis 3.23-use an array to implement three (multiple) stacks

Source: Internet
Author: User
I. Question

Use an array to implement three (or more) stacks

Ii. Answers

Using an array to implement three or more stacks does not work if you want to use an array to construct two stacks;

Consider using a static linked list. There are two fields in the array node. The keyword field and the cursor indicating the stack's precursor can make the three stacks be represented in one array;

Key ADT terms:

Capacity: The capacity of the array;

Size: number of data elements stored in the array;

Top_fst: Top Of The first stack;

Iii. Code
struct Node;typedef struct Node ElementType;typedef struct StackRecord * Stack;struct Node{    Type Key;    int Pre;};struct StackRecord{    int Capacity;    int Size;    int Top_Fst;    int Top_Snd;    int Top_Trd;    ElementType *Array;};Stack CreateStack( int MaxElements );int IsEmpty_Fst( Stack S );int IsFull( Stack S );void Push_Fst( Type Key, Stack S );Type Pop_Fst( Stack S );void MakeEmpty( Stack S );void DisposeStack( Stack S );Stack CreateStack( int MaxElements ){    Stack S;    S = (Stack)malloc( sizeof(struct StackRecord) );    if ( S == NULL )    {        printf( "Out of space" );        return NULL;    }    S->Array = (ElementType *)malloc( sizeof(ElementType) * MaxElements );    if ( S->Array == NULL )    {        printf( "Out of space" );        return NULL;    }    S->Capacity = MaxElements;    MakeEmpty(S);    return S;}int IsEmpty_Fst( Stack S ){    return S->Top_Fst == -1;}int IsFull( Stack S ){    return S->Size == S->Capacity;}void MakeEmpty( Stack S ){    if ( S != NULL )    {        S->Size =0;        S->Top_Fst = -1;        S->Top_Snd = -1;        S->Top_Trd = -1;    }}void Push_Fst( Type Key, Stack S ){    int TmpCell;    if ( IsFull(S) )        printf( "Stack is full" );    else    {        TmpCell = S->Top_Fst;        S->Top_Fst = S->Size;        S->Array[S->Top_Fst].Key = Key;        S->Array[S->Top_Fst].Pre = TmpCell;        S->Size++;    }}Type Pop_Fst( Stack S ){    int TmpCell;    if ( IsEmpty_Fst(S) )        printf( "Stack is empty" );    else    {        TmpCell = S->Top_Fst;        S->Top_Fst = S->Array[TmpCell].Pre;        S->Size--;    }    return S->Array[TmpCell].Key;}void DisposeStack( Stack S ){    if ( S != NULL )    {        free( S->Array );        free( S );    }}

 

Data Structure and algorithm analysis 3.23-use an array to implement three (multiple) 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.