One-step write algorithm (General Data Structure)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

The previous blog introduced general algorithms. With this foundation, we can continue to analyze general data structures. We know that in c ++, there are both data and functions, so a class can do a lot of things. For a simple example, we can compile a class computing class for data.

 

 

View plaincopy to clipboardprint? Class calculate {

Int m;

Int n;

Public:

Calculate (): m (0), n (0 ){}

Calculate (int a, int B): m (a), n (B ){}

~ Calculate (){}

 

Int add () {return m + n ;}

Int sub () {return m-n ;}

Int mul () {return m * n ;}

Int div () {return (n! = 0 )? M/n:-1 ;}

};

Class calculate {

Int m;

Int n;

Public:

Calculate (): m (0), n (0 ){}

Calculate (int a, int B): m (a), n (B ){}

~ Calculate (){}

 

Int add () {return m + n ;}

Int sub () {return m-n ;}

Int mul () {return m * n ;}

Int div () {return (n! = 0 )? M/n:-1 ;}

}; Can we copy this idea and add some function pointers to common data structures? As to why we need these function pointers, the main reason is that the data structure we designed is a common data type. Therefore, some compare functions must be involved in specific data types. Now, we define a circular queue,

 

 

View plaincopy to clipboardprint? Typedef struct _ QUEUE

{

Int start;

Int end;

Int length;

Int count;

Void ** head;

 

Int (* compare) (void *, void *);

Void (* print) (void *);

Void * (* find) (void *, void *);

} QUEUE;

Typedef struct _ QUEUE

{

Int start;

Int end;

Int length;

Int count;

Void ** head;

 

Int (* compare) (void *, void *);

Void (* print) (void *);

Void * (* find) (void *, void *);

} QUEUE; so what are the differences between the QUEUE creation function and the print function?

View plaincopy to clipboardprint? QUEUE * create_new_queue (int length)

{

QUEUE * pQueue;

If (0 = length)

Return NULL;

 

PQueue = (QUEUE *) malloc (sizeof (QUEUE ));

Assert (NULL! = PQueue );

 

PQueue-> head = (void **) malloc (sizeof (void *) * length );

Assert (NULL! = PQueue-> head );

 

PQueue-> start = 0;

PQueue-> end = 0;

PQueue-> count = 0;

PQueue-> length = length;

PQueue-> compare = compare;

PQueue-> find = find;

PQueue-> print = print;

Return pQueue;

}

QUEUE * create_new_queue (int length)

{

QUEUE * pQueue;

If (0 = length)

Return NULL;

 

PQueue = (QUEUE *) malloc (sizeof (QUEUE ));

Assert (NULL! = PQueue );

 

PQueue-> head = (void **) malloc (sizeof (void *) * length );

Assert (NULL! = PQueue-> head );

 

PQueue-> start = 0;

PQueue-> end = 0;

PQueue-> count = 0;

PQueue-> length = length;

PQueue-> compare = compare;

PQueue-> find = find;

PQueue-> print = print;

Return pQueue;

}

With function pointers, the entire data structure is a bit complex. However, there is no way. This is a price that must be taken to design a general data structure. With this data structure, how can we print the data of the entire queue? You can write it on your own and see if I write it correctly.

 

 

View plaincopy to clipboardprint? Void print_value_in_queue (QUEUE * pQueue)

{

Int index;

Int end;

If (NULL = pQueue | 0 = pQueue-> count)

Return;

 

End = pQueue-> start;

If (end <pQueue-> end)

End = pQueue-> end + pQueue-> length;

 

For (index = pQueue-> start; index <end; index ++ ){

PQueue-> print (pQueue-> head [index % pQueue-> length]);

}

 

Return;

}

Void print_value_in_queue (QUEUE * pQueue)

{

Int index;

Int end;

If (NULL = pQueue | 0 = pQueue-> count)

Return;

 

End = pQueue-> start;

If (end <pQueue-> end)

End = pQueue-> end + pQueue-> length;

 

For (index = pQueue-> start; index <end; index ++ ){

PQueue-> print (pQueue-> head [index % pQueue-> length]);

}

 

Return;

}

 

 

 

Summary:

 

(1) There are still two subfunctions: compare and find. You can think about how to use them?

 

(2) There are many benefits to a general data structure. The more familiar you write, the better you use.

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.