Use two stacks to implement the source code of the queue

Source: Internet
Author: User

 

/*

* Use two stacks to implement queues

*/

# Include <stdio. h>

# Include <stdlib. h>

Typedef struct stack

{

// Used to save Elements

Int *;

// Current subscript

Int loc;

// The maximum number of elements in the stack

Int max;

} St;

/*

* Initialize a stack of the specified size

*/

St * initstack (int num)

{

St * s = (st *) malloc (sizeof (st ));

If (s = NULL)

{

Printf ("create fail! \ N ");

Exit (1 );

}

S-> loc = 0;

S-> max = num;

S-> a = (int *) malloc (sizeof (int) * num );

If (s-> a = NULL)

{

Printf ("create fail! \ N ");

Exit (1 );

}

Return s;

}

/*

* Judge whether the stack is empty (null: return 1 non-null: Return 0)

*/

Int isstackempty (st * s)

{

// Stack is empty

If (s-> loc = 0) return 1;

Else return 0;

}

/*

* Judge whether the stack is full (full: return 1 not full: Return 0)

*/

Int isstackfull (st * s)

{

// The stack is full.

If (s-> loc = s-> max) return 1;

Else return 0;

}

/*

* Number of elements in the stack

*/

Int getstacknum (st * s)

{

If (isstackempty (s) = 1) return 0;

Else return s-> loc;

}

/*

* Stack entry

*/

Void pushstack (st * s, int value)

{

// The stack is full and cannot be written into the stack

If (isstackfull (s) = 1)

{

Printf ("the stack is full and cannot be written into the stack! \ N ");

Exit (1 );

}

S-> a [s-> loc] = value;

S-> loc ++;

}

/*

* Output Stack

*/

Int popstack (st * s)

{

If (isstackempty (s) = 1)

{

Printf ("Stack is empty, unable to exit stack! \ N ");

Exit (1 );

}

Int temp = s-> a [s-> loc-1];

S-> loc --;

Return temp;

}

/*

* View top stack Elements

*/

Int peekstack (st * s)

{

If (isstackempty (s) = 1)

{

Printf ("the stack is empty. You cannot view the top elements of the stack! \ N ");

Exit (1 );

}

Int temp = s-> a [s-> loc-1];

Return temp;

}

/*

* Copy the elements in one stack to another.

*/

Void copy (st * src, st * dest)

{

// Overflow occurs during the copy process.

If (src-> loc + dest-> loc)> dest-> max)

{

Printf ("an overflow occurred during the copy process! \ N ");

Exit (1 );

}

While (isstackempty (src )! = 1)

{

Int value = popstack (src );

Pushstack (dest, value );

}

}

/*

* Print each element in the stack

*/

Void printstack (st * s)

{

If (isstackempty (s) = 1)

{

Printf ("the stack is empty. You cannot print each element in the stack! \ N ");

Exit (1 );

}

While (isstackempty (s )! = 1)

{

Int value = popstack (s );

Printf ("% d --->", value );

}

}

/*

* Inbound Queue (the elements of the inbound queue are stored in src)

*/

Void pushqueue (st * src, st * dest, int value)

{

If (src-> loc = src-> max)

{

Printf ("the queue is full and cannot join! \ N ");

Exit (1 );

}

Src-> a [src-> loc] = value;

Src-> loc ++;

}

/*

* Output queue

* (If dest is not empty, an element is directly displayed from dest)

* (If dest is empty and src is not empty, first copy the elements in src to dest and an element pops up from dest)

*/

Int popqueue (st * src, st * dest)

{

If (isstackempty (src) = 1 & isstackempty (dest) = 1)

{

Printf ("the queue is empty and cannot leave! \ N ");

Exit (1 );

}

If (isstackempty (dest )! = 1)

{

Int value = popstack (dest );

Return value;

}

Else if (isstackempty (dest) = 1 & isstackempty (src )! = 1)

{

Copy (src, dest );

Int value = popstack (dest );

Return value;

}

}

Int main ()

{

Int num = 3;

Int value = 0;

St * src = initstack (num );

St * dest = initstack (num );

Pushstack (src, 1 );

Pushstack (src, 2 );

Value = popqueue (src, dest );

Printf ("pop-up elements = % d \ n", value );

Pushstack (src, 3 );

Pushstack (src, 4 );

Pushstack (src, 5 );

Value = popqueue (src, dest );

Printf ("pop-up elements = % d \ n", value );

Value = popqueue (src, dest );

Printf ("pop-up elements = % d \ n", value );

Value = popqueue (src, dest );

Printf ("pop-up elements = % d \ n", value );

Value = popqueue (src, dest );

Printf ("pop-up elements = % d \ n", value );

Return 0;

}

 

From: johnny710vip Column

Related Article

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.