Simulate the store purchase system using stacks

Source: Internet
Author: User

# Define stacksize 10
# Include "stdio. h"
# Include <malloc. h>
Typedef struct
{Int num [stacksize];
Int top;
} Seqstack;/* stack definition */

Typedef struct node
{Unsigned num;
Struct node * next;
} Linkqnode;

Typedef struct
{Linkqnode * front, * rear;} linkqueue;/* chain queue definition */

Void initstack (seqstack * s)/* initialize stack */
{S-> top =-1 ;}

Int push (seqstack * s, int e)/* inbound stack */
{If (s-> top = stacksize-1) {printf ("Stack is full! N "); return 0 ;}
S-> top ++;
S-> num [s-> top] = e; return 1;
}


Int pop (seqstack * s, int * e)/* output stack */
{If (s-> top =-1) return 0;
* E = s-> num [s-> top];
S-> top --;
Return 1;
}

Void printstack (seqstack * s)/* elements in the output stack, bottom of stack to top of stack */
{Int I;
For (I = 0; I <= s-> top; I ++)
Printf ("% 5d", s-> num [I]);
Printf ("n ");
}

Int initqueue (linkqueue * q)/* initialize queue */
{
Q-> front = (linkqnode *) malloc (sizeof (linkqnode ));
If (q-> front! = NULL)
{Q-> front-> next = NULL;
Q-> rear = q-> front; return 1;
 }
Else return 0;
}

Int enterqueue (linkqueue * q, int e)/* queue */
{Linkqnode * s;
S = (linkqnode *) malloc (sizeof (linkqnode ));
If (s! = NULL)
{S-> num = e; q-> rear-> next = s; s-> next = NULL; q-> rear = s; return 1 ;}
Else return 0;
 }

Int delqueue (linkqueue * q, int * e)/* team out */
{Linkqnode * p;
If (q-> front = q-> rear) return 0;
P = q-> front-> next;
Q-> front-> next = p-> next;
If (q-> rear = p) q-> rear = q-> front;
* E = p-> num;
Free (p); return 1;
 }

Main ()/* main function */
{Seqstack s;
Linkqueue q;
Int goodsnum;
 
Initstack (& s );
Initqueue (& q );
Printf ("Enter the incoming product number,-1 end :");
Scanf ("% d", & goodsnum );

While (goodsnum! =-1)
{Push (& s, goodsnum); scanf ("% d", & goodsnum );}

Printf ("n original shelf: n ");
Printstack (& s );

While (s. top! =-1)
If (pop (& s, & goodsnum) enterqueue (& q, goodsnum );

While (q. front)-> next! = NULL)
{Delqueue (& q,

<
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.