Step-by-step write algorithm (general data structure)

Source: Internet
Author: User

Original: Step-by-step write algorithm (general data structure)

"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing "

The previous blog introduced the general algorithm, so with this foundation we can continue to analyze the general data structure. We know that in C + + There are both data and functions, so a class can do a lot of things. To give a simple example, we can write a class of data.

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;}};
So can we imitate this idea and add some function pointers in the usual data structures? As to why these function pointers are needed, mainly because the data structures we design are general-purpose types, it is inevitable that some functions, such as compare, require the participation of specific data types. Now, we define a loop 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's the difference between a queue's creation function and a print function?
queue* create_new_queue (int length) {queue* pqueue;if (0 = = length) return Null;pqueue = (queue*) malloc (sizeof (queue)); ASSERT (null! = pqueue);p Queue->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 the function pointers, the entire data structure is a bit more complex. But there is no way, this is the cost of designing a common data structure that must be spent. With this data structure, how can you achieve data printing for the entire queue? Friends can write their own, and then see if I write the correct.

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 < E nd Index + +) {pqueue->print (pqueue->head[index% pqueue->length]);} return;}


(1) There are compare, find two sub-functions, friends can think how to use?

(2) The general data structure has many advantages, the more familiar the writing, the better.

Step-by-step write algorithm (general data structure)

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: 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.