Basic data structure (c)

Source: Internet
Author: User

STACK:

# Include "stdio. H "# include" stdlib. H "# define max_size 100 typedef struct stack {int * base; int top;} stack; int initstack (stack * stack) {stack-> base = (int *) malloc (max_size * sizeof (INT); If (! Stack-> base) Return-1; stack-> Top = 0; return 1;} int isempty (stack * stack) {If (0 = stack-> top) return 1; return 0;} int isfull (stack * stack) {If (MAX_SIZE-1 = stack-> top) return 1; return 0;} int push (stack * stack, int e) {If (! Isfull (stack) {stack-> base [stack-> top] = E; stack-> top + = 1; return 1;} return-1 ;} int POP (stack * stack) {If (! Isempty (stack) {stack-> top-= 1; return stack-> base [stack-> top];} return-1;} void main () {int I = 1; stack * stack = (stack *) malloc (sizeof (stack); If (initstack (stack) {While (I <= 10) {push (stack, I); I ++ ;}} printf ("the elements in the stack go out of the stack (first and then out): \ n"); While (! Isempty (stack) printf ("% d \ t", pop (stack ));}

Queue:

# Include "stdio. H "# include" stdlib. H "# define max_size 10 // although the capacity is 10, note that this cyclic queue can only store nine integers: typedef struct queue {int * base; int rear; int front;} sqqueue; // initialize the cyclic queue int initqueue (sqqueue * sqqueue) {sqqueue-> base = (int *) malloc (max_size * sizeof (INT); If (! Sqqueue-> base) Return-1; sqqueue-> rear = 0; sqqueue-> front = 0; return 1;} // queue int enqueue (sqqueue * sqqueue, int e) {If (sqqueue-> rear + 1) % max_size = sqqueue-> front) Return-1; sqqueue-> base [sqqueue-> rear] = E; sqqueue-> rear = (sqqueue-> rear + 1) % max_size; return 1;} // get the first element int frontqueue (sqqueue * sqqueue) {If (empty (sqqueue )) return-1; return sqqueue-> base [sqqueue-> front];} // departure int dequeue (sqqueue * sqqueue) {int I = 0; If (sqqueue-> rear = sqqueue-> front) Return-1; I = frontqueue (sqqueue); sqqueue-> front = (sqqueue-> front + 1) % max_size; return I;} // determines whether the queue is empty. Note that the null and full conditions int empty (sqqueue * sqqueue) {If (sqqueue-> rear = sqqueue-> front) return 1; else return 0;} void main () {int I = 1; sqqueue * sqqueue = (sqqueue *) malloc (sizeof (sqqueue); If (initqueue (sqqueue) {While (I <= 10) // The last failed to join the team. Because a space is required to store the tail pointer {enqueue (sqqueue, I); I ++ ;}} printf ("print queue element \ n"); While (! Empty (sqqueue) printf ("% d \ n", dequeue (sqqueue ));}

Loser tree:

# Include "stdio. H "# define K 8 # define Minkey-1 // a value smaller than all keywords # define maxkey 1000 // a value greater than all keywords int B [k]; void adjust (INT ls [K], int s) // The loser tree is stored in LS [1] ~ In ls [K-1], S is the record's merge segment number {int T, temp; t = (S + k)/2; // T is the subscript In the loser tree where the parent node of B [s] is located. k is the number of merged segments while (T> 0) // if it does not reach the root of the tree, continue {If (B [s]> B [ls [T]) {// compare it with the data indicated by the parent node temp = s; S = ls [T]; // indicates the winner. The winner will go to the last level to compare ls [T] = temp; // ls [T] records the segment number of the loser} t = T/2; // returns a layer to the root} ls [0] = s; // ls [0] specifies the short number of the minimum keyword of this trip} int get_next (int I) {} void k_merger (INT ls [k])/* ls [0] ~ Ls [K-1] is the internal node of the loser tree. B [0] ~ B [K-1] stores the current records of K initial merge segments respectively * // * function get_next (I) reads from the I merge segment and returns the current record, if the merging segment is null, maxkey */{int B [k + 1], I, Q; for (I = 0; I <K; I ++) is returned) B [I] = get_next (I); // read the first keyword B [k] = Minkey; for (I = 0; I <K; ++ I) ls [I] = K; // create the loser tree and set the initial values of the losers in LS. For (I = K-1; I> = 0; -- I) // adjust the loser tree from B [K-1], B [K-1]... B [0] sequentially. Adjust (LS, I); While (B [ls [0]! = Maxkey) // ls [0] records the field number {q = ls [0] where the smallest keyword is located. // Q is the merging segment where the current smallest keyword is located. Printf ("% d", B [Q]); B [Q] = get_next (Q); adjust (LS, q );}}

 

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.