Summary of various basic algorithms (II)-Stack

Source: Internet
Author: User

Summary of various basic algorithms (II)-Stack

(ALL tests passed)

========================================================== ======================================

Stack-Array Implementation

Test environment: Win-TC

# Include <stdio. h> <br/> char stack [512]; <br/> int Top = 0; <br/> void push (char C) <br/>{< br/> stack [Top] = C; <br/> top ++; <br/>}< br/> char POP () <br/>{< br/> top --; <br/> return stack [Top]; <br/>}< br/> int is_empty () <br/>{< br/> return 0 = top; <br/>}< br/> void main () <br/>{< br/> push ('1'); <br/> push ('2'); <br/> push ('3 '); <br/> push ('4'); <br/> push ('5'); <br/> while (! Is_empty () <br/> putchar (POP (); <br/> putchar ('/N'); <br/> getch (); <br/>}

Running result:

========================================================== ================

 

Stack-array implementation 2

Test environment: Win-TC

# Include <stdio. h> <br/> # include <malloc. h> <br/>/* typedef int datatype; */<br/> # define datatype int <br/> # define Max 1024 <br/> typedef struct <br/>{< br/> datatype data [Max]; <br/> int top; <br/>} stack, * pstack; <br/> pstack * init_stack () <br/>{< br/> pstack pS; <br/> PS = (pstack) malloc (sizeof (stack); <br/> If (! PS) <br/>{< br/> printf ("error. fail malloc... /n "); <br/> return NULL; <br/>}< br/> PS-> Top =-1; <br/> return pS; <br/>}< br/> int empty_stack (pstack PS) <br/>{< br/> If (-1 = ps-> top) <br/> return 1; <br/> else <br/> return 0; <br/>}< br/> int push (pstack ps, datatype data) <br/>{< br/> If (PS-> Top = MAX-1) <br/>{< br/> printf ("Stack is full... /n "); <br/> return 0; <br/>}< br/> PS-> top ++; <br/> PS-> data [PS-> top] = data; <br/> return 1; <br/>}< br/> int POP (pstack ps, datatype * Data) <br/>{< br/> If (empty_stack (PS) <br/>{< br/> printf ("Stack is empty... /n "); <br/> return 0; <br/>}< br/> * Data = ps-> data [PS-> top]; <br/> PS-> top --; <br/> return 1; <br/>}< br/> datatype top_stack (pstack PS) <br/>{< br/> If (empty_stack (PS) <br/>{< br/> printf ("Stack is empty... /n "); <br/> return 0; <br/>}< br/> return PS-> data [PS-> top]; <br/>}< br/> void display (pstack PS) <br/>{< br/> int I; <br/> If (empty_stack (PS )) <br/>{< br/> printf ("Stack is empty... /n "); <br/> return; <br/>}< br/> printf (" printf the items of stack... /n "); <br/> for (I = ps-> top; I>-1; I --) <br/> printf (" % 4D ", PS-> data [I]); <br/> printf ("/n"); <br/>}< br/> void main () <br/>{< br/> int I, num, Data, * pdata; <br/> pstack pS; <br/> PS = init_stack (); <br/> printf ("Enter stack num:"); <br/> scanf ("% d", & num); <br/> for (I = 0; I <num; I ++) <br/>{< br/> scanf ("% d", & data); <br/> push (Ps, data ); <br/>}< br/> display (PS); <br/> printf ("Top is % d/n", top_stack (PS )); <br/> for (I = 0; I <num; I ++) <br/>{< br/> POP (Ps, pdata ); <br/> printf ("% 3d", * pdata); <br/>}< br/> printf ("/n "); <br/> display (PS); <br/> getch (); <br/>}< br/>

Running result:

 

========================================================== ================

 

Stack-Linked List Implementation

Test environment: Win-TC

 

# Include <stdio. h> <br/> # include <malloc. h> <br/> typedef char datatype; <br/> struct _ node <br/>{< br/> datatype data; <br/> struct _ node * next; <br/>}; <br/> typedef struct _ node, * pstack; <br/> pstack init_stack () <br/>{< br/> pstack pS; <br/> PS = (pstack) malloc (sizeof (node); <br/> If (null = ps) <br/>{< br/> printf ("error. malloc is fail... /n "); <br/> return NULL; <br/>}< br/> PS-> DATA =-1 ;/ * Base of stack: Data =-1 and next = NULL */<br/> PS-> next = NULL; <br/> return pS; <br/>}< br/> pstack push (pstack ps, datatype data) <br/>{< br/> pstack pTop; <br/> pTop = (pstack) malloc (sizeof (node); <br/> If (null = pTop) <br/> {<br/> printf ("error. malloc is fail... /n "); <br/> return NULL; <br/>}< br/> pTop-> DATA = data; <br/> pTop-> next = Ps; /* Insert new item */<br/> PS = pTop;/* move top */<br/> Return pS; <br/>}< br/> pstack POP (pstack ps, datatype * Data) <br/>{< br/> If (PS-> next = NULL) <br/>{< br/> printf ("Stack is empty... /n "); <br/> return NULL; <br/>}< br/> * Data = ps-> data; <br/> PS = ps-> next; <br/> return pS; <br/>}< br/> datatype top_stack (pstack PS) <br/>{< br/> If (PS-> next = NULL) /* If empty */<br/> {<br/> printf ("Stack is empty... /n "); <br/> return-1; <br/>}< br/> return PS-> data; <br/>}< br/> int len_stack (pstack PS) <br/>{< br/> int Len = 0; <br/> pstack pTop = Ps; <br/> while (pTop-> next) <br/> {<br/> Len ++; <br/> pTop = pTop-> next; <br/>}< br/> return Len; <br/>}< br/> void display (pstack PS) <br/>{< br/> pstack pTop; <br/> pTop = Ps; <br/> while (pTop-> next! = NULL) <br/>{< br/> printf ("% 4C", pTop-> data); <br/> pTop = pTop-> next; <br/>}< br/> printf ("/n"); <br/>}< br/> void main () <br/>{< br/> pstack pS; <br/> datatype * Data = (datatype *) malloc (sizeof (datatype )); <br/> PS = init_stack (); <br/> PS = Push (Ps, 'A'); <br/> PS = Push (Ps, 'B '); <br/> PS = Push (Ps, 'C'); <br/> PS = Push (Ps, 'D'); <br/> PS = Push (Ps, 'E'); <br/> display (PS); <br/> printf ("Len of stack is: % d/n", len_stack (PS )); <br/> printf ("top of stack is: % C/n", top_stack (PS); <br/> PS = POP (Ps, data ); <br/> printf ("Pop % C/N", * data); <br/> display (PS); <br/> PS = POP (Ps, data ); <br/> printf ("Pop % C/N", * data); <br/> display (PS); <br/> getch (); <br/>}

Running result:

 

========================================================== ======================

 

Heap-Linked List Implementation

Test environment: Win-TC

# Include <stdio. h> <br/> # include <malloc. h> <br/> # include <stdlib. h> <br/> struct _ node <br/>{< br/> int data; <br/> struct _ node * Next; <br/> }; <br/> typedef struct _ node, * pnode; <br/> struct _ linkqueue <br/>{< br/> pnode front; <br/> pnode rear; <br/>}; <br/> typedef struct _ linkqueue, * plinkqueue; <br/> linkqueue init_queue () <br/>{< br/> linkqueue SCSI; <br/> SCSI. front = SCSI. rear = (pnode) malloc (sizeof (node); <br/> If (null = SCSI. front) <br/> {<br/> printf ("error. malloc is fail... /n "); <br/> exit (1); <br/>}< br/> SCSI. rear-> DATA = SCSI. front-> DATA =-1; <br/> SCSI. rear-> next = SCSI. front-> next = NULL; <br/> return SCSI; <br/>}< br/> int empty_queue (linkqueue SCSI) <br/>{< br/> If (SCSI. front = SCSI. rear) <br/> return 1; <br/> else <br/> return 0; <br/>}< br/> linkqueue insert_item (linkqueue SCSI, int data) <br/>{< br/> pnode PQ; <br/> PQ = (pnode) malloc (sizeof (node); <br/> If (PQ = NULL) <br/>{< br/> printf ("error. malloc is fail... /n "); <br/> exit (1); <br/>}< br/> PQ-> DATA = data; <br/> PQ-> next = SCSI. rear-> next; <br/> SCSI. rear-> next = PQ; <br/> SCSI. rear = SCSI. rear-> next; <br/> return SCSI; <br/>}< br/> linkqueue delete_item (linkqueue SCSI, int * Data) <br/>{< br/> If (empty_queue (SCSI) <br/>{< br/> printf ("queue is empty... /n "); <br/> exit (1); <br/>}< br/> * Data = SCSI. front-> data; <br/> SCSI. front = SCSI. front-> next; <br/> return SCSI; <br/>}< br/> int len_queue (linkqueue SCSI) <br/>{< br/> int Len = 0; <br/> while (SCSI. front) <br/>{< br/> Len ++; <br/> SCSI. front = SCSI. front-> next; <br/>}< br/> return Len; <br/>}< br/> void display (linkqueue SCSI) <br/> {<br/> linkqueue P; <br/> P = SCSI; <br/> If (empty_queue (SCSI )) <br/>{< br/> printf ("queue is empty... /n "); <br/> return; <br/>}< br/> while (P. front-> next) <br/>{< br/> printf ("% 4D", p. front-> data); <br/> P. front = P. front-> next; <br/>}< br/> printf ("% 4D/n", p. front-> data); <br/>}< br/> void main () <br/>{< br/> int * Data = (int *) malloc (sizeof (INT); <br/> linkqueue SCSI; <br/> SCSI = init_queue (); <br/> SCSI = insert_item (SCSI, 1 ); <br/> SCSI = insert_item (SCSI, 2); <br/> SCSI = insert_item (SCSI, 3); <br/> SCSI = insert_item (SCSI, 4 ); <br/> SCSI = insert_item (SCSI, 5); <br/> display (SCSI); <br/> printf ("Len of queue is: % d/n ", len_queue (SCSI); <br/> SCSI = delete_item (SCSI, data ); <br/> printf ("Delete % d/N", * data); <br/> display (SCSI); <br/> SCSI = delete_item (SCSI, data ); <br/> printf ("Delete % d/N", * data); <br/> display (SCSI); <br/> getch (); <br/>}

Running result:


Reference recommendations:

The path to Learning Algorithms

Summary of various basic algorithms (1) -- chain table

Summary of various basic algorithms (II)-Stack

Summary of various basic algorithms (III)-tree and binary tree

Summary of various basic algorithms (4)-graph and traversal

Summary of various basic algorithms (V)-sorting algorithms

Summary of various basic algorithms (6)-search algorithms

Summary of various basic algorithms (7)-common algorithms

12 interesting questions in C Language

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.