Basic operations of sequential stack and chain Stack

Source: Internet
Author: User
// Stack. CPP: defines the entry point for the console application. // # include "stdafx. H "# include" stdio. H "# include" stdlib. H "// * # define stack_max_size 7 int stackdata [stack_max_size] = {'A', 'B', 'C', 'D', 'E', 'F ', 'G'}; // * # define stack_max_size 14 int stackdata [stack_max_size] = {'A', 'B', 'C', 'D ', 'E', 'F', 'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n '}; * // * sequence stack type definition */typedef struct {int data [stack_max_size]; int P;} sqstack;/* chain stack type definition */struct linkstack {int data; struct linkstack * link;}; typedef struct linkstack lkstack; /* basic operation function definition of ordered stack */void sqinitialize (sqstack * stack) {stack-> Top = 0;} int sqisempty (sqstack * stack) {If (Stack-> Top = 0) Return (1); else return (0);} int sqisfull (sqstack * stack) {If (Stack-> top> = stack_max_size) Return (1); else return (0);} int sqpush (sqstack * stack, int data) {If (sqisfull (Stack) {Printf ("the stack has been merged! /N "); Return (0) ;}else {stack-> data [stack-> top ++] = data; Return (1 );}} int sqpop (sqstack * stack, int * P) {If (sqisempty (stack) {printf ("Stack is empty! /N "); Return (0);} else {* P = stack-> data [-- Stack-> top]; Return (1 );}} int sqgettop (sqstack * stack, int * P) {If (sqisempty (stack) {printf ("Stack is empty! /N "); Return (0);} else {* P = stack-> data [stack-> top]; Return (1 );}} void sqwritevalue (sqstack * stack, int array [], int N) {int I; for (I = 0; I <n; I ++) if (sqpush (stack, array [I]) printf ("stack [% d] = % C/T", stack-> top-1, array [I]);} void sqprintvalue (sqstack * stack) {int stackdata, I; I = 0; while (sqpop (stack, & stackdata )) printf ("stack [% d] = % C/T", stack-> top, stackdata); printf ("/N ");} /* Basic Function Definition of the stack */void lkini Tialize (lkstack * Top) {Top = NULL;} int lkisempty (lkstack * Top) {If (Top = NULL) Return (1); else return (0 );} int lkpush (lkstack * Top, int data) {lkstack * q; q = (lkstack *) malloc (sizeof (lkstack); If (q = NULL) return (0); else {q-> DATA = data; q-> link = NULL; top-> link = Q; Top = Q; Return (1 );} int lkpop (struct linkstack * stack, int * P) {struct linkstack * q; If (lkisempty (stack) {printf ("the stack is empty! /N "); Return (0);} else {q = stack; * P = stack-> data; stack = stack-> next; // free (Q ); return (1) ;}} int lkgettop (struct linkstack * stack, int * P) {If (lkisempty (stack) {printf ("Stack is empty! /N "); Return (0);} else {* P = stack-> data; Return (1) ;}} void lkwritevalue (struct linkstack * stack, int N) {int I; for (I = 0; I <n; I ++) if (lkpush (stack, I * 10 )) printf ("stack [% d] = % d/T", I, I * 10);} void lkprintvalue (struct linkstack * stack, int N) {int stackdata, I; I = 0; for (I = 0; I <n; I ++) {lkpop (stack, & stackdata ); printf ("stack [% d] = % d/T", I ++, stackdata);} printf ("/N");} int main (INT argc, char * argv []) {sqstack * stack1; struct linkstack * stack2; // sqinitialize (stack1) of sequential stack processing ); printf ("/n sequential stack [inbound Stack] data ......... /n "); sqwritevalue (stack1, stackdata, stack_max_size); printf ("/n sequential stack [out Stack] data ......... /n "); sqprintvalue (stack1); // * // link stack processing part of lkinitialize (stack2 ); printf ("/n chain stack data into the stack ......... /n "); lkwritevalue (stack2, 10); printf ("/n chain stack out stack data ......... /n "); lkprintvalue (stack2, 10); */return 0 ;}

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.