Stack learning notes, wifi protocol stack Learning

Source: Internet
Author: User

Stack learning notes, wifi protocol stack Learning
Stack

  • Stack is a special linked list, which can only be pushed and deleted at the end of the table.Back-in-first-out(LIFO)
  • Linked lists are classified into dynamic linked lists and declarative linked lists. A Dynamic Linked List allocates storage space to stack elements as needed, while a static linked list provides a fixed storage space.
  • C ++ STL (Standard Template Library) defines the basic operations of the stack and must contain the <stack> header file. The sample code is 1-1.
  • # Include <iostream> # include <stack> using namespace std; int main () {stack <int> s; // declare that the elements of stack s are similar to int s. empty () // stack is empty. true is returned; otherwise, false s is returned. push (1); // 1 into Stack s. top (); // get the top element s of the stack. pop (); // return 0 for the elements at the top of the stack ;}

    Figure 1-1 example of using the standard template library stack

  • When the scale is determined, an array can be used to store stacks. Sample Code 1-2 is shown.
  • # Include <iostream> # include <stack> using namespace std; int main () {int stk [1000], top = 0; top = 0; // stack empty stk [++ top] = 1; // 1 stack stk [-- top]; // top element --; // return 0 ;}

    Figure 1-2 simple stack with Arrays

  • Stack-related programming exercises, TOJ 1196 Web Navigation and TOJ 1036 Rails. The former refers to the FORWARD and backward directions of the visit web page, and the latter refers to the outbound order after the train enters the "dead end. The former can use two stacks, one to save the forward Web page URL and the other to save the backward URL. The train carriage enters the "dead end", and the rear carriage goes out first. These two questions are old but classic stack-related questions. The corresponding codes for these two questions are 1-3 and 1-4, respectively.
  • #include <iostream>#include <stack>#include <string>using namespace std;int main(){    string cmd, visit = "http://www.acm.org/";    stack<string>Forward, Back;    while(cin>>cmd && cmd != "QUIT")    {        if(cmd == "VISIT")        {            Back.push(visit);            while(!Forward.empty())                Forward.pop();            cin>>visit;        }        else if(cmd == "FORWARD")        {            if(Forward.empty())            {                cout<<"Ignored"<<endl;                continue;            }            else            {                Back.push(visit);                visit = Forward.top();                Forward.pop();            }        }        else if(cmd == "BACK")        {            if(Back.empty())            {                cout<<"Ignored"<<endl;                continue;            }            else            {                Forward.push(visit);                visit = Back.top();                Back.pop();            }        }        else            printf("Invalid input!\n");        cout<<visit<<endl;    }    return 0;}

    Figure 1-3 reference code for TOJ1196 Web Navigation

  • # Include <stdio. h> # include <stdlib. h ># include <stack> using namespace std; int main () {stack <int> s; int n, I, j; while (scanf ("% d ", & n) {// init block int a [n]; while (scanf ("% d", & a [0]) & a [0]) {for (I = 1; I <n; I ++) {scanf ("% d", & a [I]) ;}// judge j = 1, I = 0; while (! S. empty () s. pop (); while (I <n) {// push j when j <= a [I] while (j <= a [I]) {s. push (j); j ++;} // pop s. top if s. top () = a [I] while (! S. empty () & s. top () = a [I]) {s. pop (); I ++;} // according to LIFO, s. top () should always be <= a [I] if (! S. empty () & s. top ()> a [I]) break;} if (s. empty () printf ("Yes \ n"); else printf ("No \ n");} printf ("\ n");} return 0 ;}

    Figure 1-4 reference code for TOJ 1036 Rails

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.