C algorithm series --- 1. queue and stack, algorithm --- 1 Queue

Source: Internet
Author: User

C algorithm series --- 1. queue and stack, algorithm --- 1 Queue

Preface: I have been playing at home for a long time. I really don't know what to do. I suddenly want to find something to do. Now it's time to do something. These things are not much advanced and may be very simple, but they are very basic and cannot be ignored. At the same time, it is also a way to learn.

This is the beginning. Let's write some algorithms for the queue and stack of C.

The first is the implementation of some basic functions of the stack, first paste the Code:

# Include <stdlib. h> # include <stdio. h> typedef int SElemType; // declare that the stack element type is inttypedef int Status; // function return value type: int # define MAXSIZE 20 // stack capacity typedef struct {SElemType data [MAXSIZE]; int top ;}sqstock; Status InitStock (SqStock *); // stack initialization function declaration Status push (SqStock *, SElemType); // stack function declaration Status pop (SqStock *, SElemType *); // void Conversion (); // This Is A Conversion Function tested at the time, the/*************** function area ******** // 0. initialize Status InitStock (S QStock * s) {int StockDataArray [MAXSIZE]; for (int I = 0; I <MAXSIZE; I ++) {s-> data [I] = '\ 0';} s-> top =-1; return 1;} // push (SqStock * s, SElemType e) {if (s-> top = MAXSIZE-1) {// has reached the maximum capacity return 0 ;}; s-> top ++; s-> data [s-> top] = e; return 1 ;}// output stack Status pop (SqStock * s, SElemType * e) {if (s-> top =-1) {// return 0 to the bottom of the stack;} * e = s-> data [s-> top --]; return 1;} // convert to octal void Conversion () {int N; SqStock * s; s = (SqStock *) Malloc (sizeof (SqStock); // apply for memory space InitStock (s); printf ("enter a decimal INTEGER:"); scanf ("% d ", & N); while (N) {push (s, N % 8); N = N/8;} int e; while (s-> top! =-1) {pop (s, & e); printf ("% d", e );}} /************ function area *******/

The code above: the stack function is implemented using arrays. In normal times, an array and an int-type top are directly used, and its value is the subscript of the top element of the current stack. As for how to handle stack overflow, either set the capacity to a large value or dynamically apply for some memory space when it is about to overflow. Of course, there must be other methods. You must have the spirit of exploration.

The following is an algorithm for some basic functions of the queue:

1 # include <stdio. h> 2 # include <tchar. h> 3 # include <stdlib. h> 4 # include <math. h> 5 # include <time. h> 6 7 8 # define MAXSIZE 20 9 10 typedef int QElemType; 11 typedef int Status; 12 // use the cyclic queue 13 typedef struct {14 15 QElemType Data [MAXSIZE]; 16 int front; 17 int rear; 18} XHQueue; 19 20 int getRand (int, int); // random number tested at that time, the two variables are the upper and lower limits of 21 void InitQueue (XHQueue *); // initialize the queue 22 Status EnQueue (XHQueue *, qelemts Ype); // 23 Status DeQueue (XHQueue *, QElemType *); // 24 Status IsEmpty (XHQueue *); // judge whether it is empty 25 26/***** function area ***/27 // generate random function 28 int getRand (int MinSize, int MMaxSize) {29 time_t; 30 int r; 31 srand (unsigned) time (& t); 32 r = rand () % (MMaxSize-MinSize + 1) + MinSize; 33 return r; 34} 35 36 37 // queue initialization 38 void InitQueue (XHQueue * Q) {39 Q-> front = 0; 40 Q-> rear = 0; 41 42 43} 44 45 // team position 46 47 Status EnQu Eue (XHQueue * Q, QElemType E) {48 49 if (Q-> rear + 1) % MAXSIZE = Q-> front) // The queue is full. 50 {51 return 0; 52} 53 Q-> Data [Q-> rear] = E; 54 Q-> rear = (Q-> rear + 1) % MAXSIZE; 55 return 1; 56} 57 58 // exit 59 Status DeQueue (XHQueue * Q, QElemType * E) {// Why is * E, let's take a look at the data transfer and address of c. When using it, we should write it as DeQueue (Q, & variable ), 60 if (Q-> front = Q-> rear) {// The queue is empty 61 return 0; 62} 63 * E = Q-> Data [Q-> front]; 64 Q-> front = (Q-> front + 1) % MAXSIZE; 65 66 retu Rn 1; 67 68} 69 70 // determine whether it is null 71 Status IsEmpty (XHQueue * Q) {72 if (Q-> front = Q-> rear) {73 return 1; 74} 75 return 0; 76 77} 78/***** function area ***/79 80 int _ tmain (int argc, _ TCHAR * argv []) 81 {82/* int r, r1; 83 r = getRand (20, 35); 84 r1 = getRand (20, 40 ); 85 printf ("% d \ n", r, r1); */86 87 int Re; 88 XHQueue * Q; 89 Q = (XHQueue *) malloc (sizeof (XHQueue); 90 InitQueue (Q); 91 EnQueue (Q, 2); 92 EnQueue (Q, 0); 93 EnQueue (Q, 1); 94 EnQueue (Q, 5); 95 while (! IsEmpty (Q) {96 DeQueue (Q, & Re); 97 printf ("% d", Re); 98} 99 100 system ("pause "); 101 return 0; 102}

Code Description: Forgive me for being lazy .. A lot of them are still attached =! In terms of thinking, in real life, the queue is one, and everyone is moving forward, but this is also written in the program, it is not impossible, but when the queue is relatively long, it takes a lot of time to move these elements, which is not cost-effective. Therefore, to avoid such large-scale movement, we use the idea of circular queue, that is to say, when the team's head leaves, we move the team's head pointer back one. When the team's tail comes, we can see that we can no longer stand, so let's see it in front, you may be able to stand before.

So much has been posted for the moment ~ I will try to paste some other content later ~

 

Related Article

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.