Data Structure stack Study Notes

Source: Internet
Author: User

Stack is an advanced data structure.

The code is displayed below. The following uses data to simulate the stack structure.

//// Main. CPP // studyc ++ // created by du jia on 14-9-12. // copyright (c) June 2014 dujia. all rights reserved. // # include <iostream> // STD: cout # include <stdio. h> using namespace STD; # define maxsize 10int stack [maxsize]; int Top =-1; void push (INT value) {int I; If (top> = maxsize) STD:: cout <"\ nthe stack is full" <Endl; else {STD: cout <"\ nthe stack content before (top-> bottom ): "<Endl; for (I = top; I> = 0; I --) cout <stack [I] <Endl; top ++; stack [Top] = value; // put data into the array cout <"the stack content after push (top-> bottom):" <Endl; for (I = top; I> = 0; I --) cout <stack [I] <Endl ;}} int POP () {int temp; int I; If (top <0) {cout <"the stack is empty! "<Endl; Return-1 ;}cout <" the stack content before (top-> bottom): "<Endl; for (I = top; i> = 0; I --) cout <stack [I] <Endl; temp = stack [Top]; top --; printf ("\ nthe pop value is [% d]", temp); printf ("\ nthe stack content after pop (top-> bottom ):"); for (I = top; I> = 0; I --) printf ("[% d]", stack [I]); printf ("\ n "); return temp;} int main (INT argc, const char * argv []) {// test push (3); push (5); push (9 ); int num = POP (); printf ("num = % d \ n", num); num = POP (); printf ("num = % d \ n ", num); push (7); Return 0 ;}


Imitating stacks with linked lists

//// Main. CPP // studyc ++ // created by du jia on 14-9-12. // copyright (c) June 2014 dujia. all rights reserved. // # include <iostream> // STD: cout # include <stdio. h> using namespace STD; struct s_node {int data; struct s_node * Next;}; typedef struct s_node s_list; typedef s_list * link; Link stack = nullptr; void print_stack () {link temp = nullptr; temp = stack; If (temp = nullptr) printf ("the stack is empty! \ N "); else {While (temp! = Nullptr) {printf ("[% d]", temp-> data); temp = temp-> next;} printf ("\ n ");}} void push (INT value) {link newnode; printf ("\ nthe stack content before (top-> bottom):"); print_stack (); // initialize newnode = (Link) malloc (sizeof (s_list); // value newnode-> DATA = value; // stack is the first to put the previous node behind newnode-> next = stack; // put the new one in front of stack = newnode;} int POP () {link top; int temp; printf ("\ nthe stack content before (top-> Bott Om): "); print_stack (); If (Stack! = Nullptr) {Top = stack; stack = stack-> next; temp = Top-> data; free (top); Return temp;} else {return-1 ;}} int main (INT argc, const char * argv []) {push (9); push (10); POP (); push (11); print_stack (); return 0 ;}










Address: http://blog.csdn.net/qqmcy/article/details/39393941

Data Structure stack Study Notes

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.