Introduction to the algorithm 10: Simplified stack list, array implementation of the queue 2016.1.10

Source: Internet
Author: User

The first 10 days of the new year to do a more meaningful thing is to insist on writing a daily blog, tomorrow will begin the final exam, so wait for the holidays to continue to study.

After simplifying the s.bottom mentioned yesterday, we changed the list of the stacks. The code is as follows (collapsed):

#include <stdio.h>#include<stdlib.h>typedefstruct_node{intnum; struct_node *Next;} Node;node*s;voidBuild (Node *&S) {s= (node *)malloc(sizeof(node)); S->next=NULL;}intSempty (Node *&R) {    if(S->next==null)return 1; Else return 0;}intPop (node *&R) {    if(Sempty (S)) {printf ("stack is empty! \ n"); }    Else{node*p; P=s; S=s->Next; intK=p->num;  Free(P); returnK; }}voidInit (node *&R) {         while(!sempty (S))    {pop (S); }}voidPush (Node *&s,intN) {Node*p; P= (node *)malloc(sizeof(node)); P->num=N; P->next=s; S=p;}voidShowstack (Node *S) {     while(s->next!=NULL) {        intK=s->num; S=s->Next; printf ("|%d|\n", K); } printf ("| |\n"); printf ("-\ n");}intMain () {build (s); intN;  while(1) {printf ("1: Initialize the stack, 2: Into the Stack, 3: Out of the stack; 4: Exit. \ n"); intK; scanf ("%d",&k); Switch(k) { Case 1: Init (s); Break;  Case 2: scanf ("%d", &n); Push (S,N); Break;  Case 3:p op (s); Break;  Case 4:return 0;    } showstack (s); }        return 0;}
View Code


Here is the array implementation of the queue, the difference between the queue and the stack, the queue is FIFO, the stack is advanced. As the rules are similar, the array implementation of the stack is modified slightly. But the queue also needs to have a mold optimization, had to wait until the holiday to write again, the following is the general queue code:

#include <stdio.h>#defineMaxtop 10struct_queue {intHead,tail; intnum[maxtop+1];} s;voidInitstruct_queue &S) {S.head=0; S.tail=0;}intSempty (struct_queue &R) {    if(S.head==s.tail)return 1; Else return 0;}voidEnqueuestruct_queue &s,intN) {    if(s.tail==maxtop) {printf ("Queue Overflow! \ n"); }    Else{S.num[s.tail]=N; S.tail++; }}intDequeuestruct_queue &S) {    if(s.head==s.tail) {printf ("Empty Queue! \ n"); }    Else {        returns.num[s.head++]; }}voidShowqueue (struct_queue S) {     while(!sempty (S)) {        intk=dequeue (S); printf ("|%d|\n", K); } printf ("| |\n");}intMain () {intN;  while(1) {printf ("1: Initialize the queue; 2: Into the queue; 3: Out of queue; 4: Exit. \ n"); intK; scanf ("%d",&k); Switch(k) { Case 1: Init (s); Break;  Case 2: scanf ("%d", &n); Enqueue (S,n); Break;  Case 3:d Equeue (s); Break;  Case 4:return 0;    } showqueue (s); }    return 0;}

Introduction to the algorithm 10: Simplified stack list, array implementation of the queue 2016.1.10

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.