C Language Data Structure-basic knowledge about algorithms and static tables

Source: Internet
Author: User

I. Basic knowledge about Algorithms

1. Write BASIC program rules

① Use as little memory as possible. ② Solve the problem with as few code as possible.

2. Data features

① It must be input to a computer. ② It must be processed by the program.

3. Several Concepts of data

① Data element: the basic unit of data. ② Data items: an element consists of several data items. ③ Data Object: a set of data elements of the same nature.

For example:

4. Composition of the Data Structure

(1) Logical Structure: Set structure, linear structure, tree structure, and Graphic Structure

(2) physical structure:

① Sequential Storage Structure: stores data in continuous storage units. (For example, array)
② Chained Storage Structure: stores data in any storage unit and finds the associated data by saving the address.

5. The data structure statically describes the relationship between data elements. Efficient programs need to design reasonable algorithms based on the data structure.

Algorithm concept: describes the steps for solving a specific problem. An ordered sequence of commands in a computer.

6. algorithm features: (1) input (2) output (3) Poor (4) deterministic (5) Feasibility

7. program = Data Structure + algorithm.

8. The efficiency Representation Method of the algorithm adopts the large O notation.

Concept: Large O notation (O (), that is, view the notation of the maximum item. The time complexity of the analyzed algorithm refers to the worst time complexity of the algorithm.
The space complexity of the algorithm:
S (n) = O (f (n ))

The large O representation is as follows:

 

 

Ii. Basic concepts of linear tables

1. Definition:

(1) A linear table is a set of 0 or more data elements.
(2) Data Elements in a linear table are ordered.
(3) The number of data elements in a linear table must be limited.
(4) the data element types in a linear table must be the same.

A linear table is a finite sequence of n data elements of the same type.

2. features:

(1) The first element has only one successor
(2) The last element of a linear table has only one precursor.
(3) All elements except A0 and an should have both a precursor and a successor.
(4) linear table access and sequential access

3. Linear table related operations:

(1) Create
(2) Destruction
(3) Get the length
(4) Delete an element from a linear table
(5) add an element from a linear table
(6) add elements to specific positions of a linear table

3. Linear table operations and implementation

1. Sequential storage definition of linear tables: Data Elements of linear tables are stored in sequence using memory units with consecutive addresses.

2. Specific operations on Linear tables:

(1). h file

# Ifndef _ define h _ # DEFINE _ define h _ // here is a type encapsulation, and all external interfaces are void *. Typedef void seqlist; typedef void seqlistnode; // defines the maximum capacity seqlist * seqlist_create (INT capacity) of a linear table; void Merge (seqlist * List); void seqlist_clear (seqlist * List ); int seqlist_length (seqlist * List); // returns the maximum capacity of the sequence table int seqlist_capacity (seqlist * List); int seqlist_insert (seqlist * List, seqlistnode * node, int POS ); seqlistnode * seqlist_get (seqlist * List, int POS); seqlistnode * seqlist_delete (seqlist * List, int POS); # endif

(2) Data Structure. c file

# Include <stdio. h> # include <malloc. h> # include "1.h" typedef unsigned int tseqlistnode; // The data address is stored, so the unsigned int typedef struct student {int length; // The length of the sequence table int capacity; // capacity of the sequence table (defined by yourself, which is also a defect) tseqlistnode * node; // used for data entry and exit in the sequence table} tseqlist; seqlist * seqlist_create (INT capacity) {tseqlist * ret = NULL; If (capacity> = 0) {ret = (tseqlist *) malloc (sizeof (tseqlist) + sizeof (tseqlistnode) * capacity );} if (Ret! = NULL) {ret-> length = 0; ret-> capacity = capacity; ret-> node = (tseqlistnode *) (Ret + 1);} return ret ;} void seqlist_destroy (seqlist * List) {free (list);} void seqlist_clear (seqlist * List) {tseqlist * slist = (tseqlist *) list; If (slist! = NULL) {slist-> length = 0 ;}} int seqlist_length (seqlist * List) {tseqlist * slist = (tseqlist *) (list); int ret =-1; if (slist! = 0) {ret = slist-> length;} return ret;} int seqlist_capacity (seqlist * List) {tseqlist * slist = (tseqlist *) list; int ret =-1; if (slist! = NULL) {ret = slist-> capacity;} return ret;} int seqlist_insert (seqlist * List, seqlistnode * node, int POS) {tseqlist * slist = (tseqlist *) list; int ret = (slist! = NULL); int I = 0; ret = RET & (slist-> Length + 1 <= slist-> capacity ); ret = RET & (Pos> = 0); If (RET) {If (Pos> = slist-> length) // if the position of the inserted data is longer than the table length, by default, the end of the table is reached. {Pos = slist-> length;} for (I = slist-> length; I> Pos; I --) {slist-> node [I] = slist-> node [I-1];} slist-> node [I] = (tseqlistnode) node; // forcibly convert the type of the address to be inserted to slist-> length ++;} return ret; // 1 success, 0 failure .} Seqlistnode * seqlist_get (seqlist * List, int POS) {tseqlist * slist = (tseqlist *) list; seqlistnode * ret = NULL; If (slist! = NULL & Pos> = 0 & Pos <slist-> length) {ret = (seqlistnode *) (slist-> node [POS]);} return ret ;} /* do not comment too much. I personally think there are more things to pay attention to when deleting a function. */Seqlistnode * seqlist_delete (seqlist * List, int POS) {tseqlist * slist = (tseqlist *) list; seqlistnode * ret = seqlist_get (list, POS); int I = 0; if (Ret! = NULL) {// ret = slist-> node [POS]; for (I = POS + 1; I <slist-> length; I ++) {slist-> node [I-1] = slist-> node [I];} slist-> length --;} return ret ;}

(3) test part of the code. c file

#include <stdio.h>#include "1.h"#include <stdlib.h>int main(){SeqList *list = SeqList_Create(9);int i = 0;int j = 1;int k = 2;int x = 3;int y = 4;int z = 5;int index = 0;SeqList_Insert(list, &i,0);SeqList_Insert(list, &j,0);SeqList_Insert(list, &k,0);SeqList_Insert(list, &x,0);SeqList_Insert(list, &y,0);SeqList_Insert(list, &z,0);for (index = 0; index < SeqList_Length(list); index++){int *p = (int*)SeqList_Get(list, index);printf ("%d\n",*p);}printf("\n");        while( SeqList_Length(list) > 0 )    {        int* p = (int*)SeqList_Delete(list, 0);        printf("%d\n", *p);    }    SeqList_Destroy(list);    return 0;}

The following is a classic summary of my colleagues.

The code in this section is a sequence table that can be used for various types. The reason why it can be used for various types is that it stores the element address (actually a pointer array) in the sequence table ).

2. introduction to the elements in the struct in the description sequence table in the Code: length indicates the number of elements in the sequence table, capacity indicates the capacity of the sequence table, node indicates the header address of the sequence table (also the header address of the pointer array), and Pos indicates the number of elements in the sequence table, POS is a parameter used for deletion and insertion. It represents the subscript inserted to the position of the sequence table (the subscript of the array starts from 0 ). The subscript of the length element in the sequence table ranges from 0 to length-1. Note that the POs permitted ranges of different function functions of the operation sequence table are different. 3. The code in this section attaches great importance to determining the validity of function parameters. This specification is worth learning. 4. in this section, the operation functions of the sequence table, which are input and output to the outside, are of the void * type, this ensures that only these operation functions can change the values in the struct that describes the sequence table. All functions in other files receive the void * type, the value in this struct cannot be changed directly. Such encapsulation ensures code security. 5. for the code in this section, the common sequence table is typedef a type, and then defines the number of elements of this a type array and length sequence table in the sequence table, this sequence table contains multiple sequence sets of the type. The space occupied is sizeof (a) * capacity. In this section, the sequence table contains multiple unsigned int * address-type sequence sets. The table only contains addresses. The first section saves the space of the sequence table, and the second section saves data of different types in disguise, third, it achieves the separation of the sequence table (that is, the data structure) and the data (that is, the element) we intend to use. For example, the Linux kernel linked list (a two-way circular linked list) is a separate linked list system. This linked list is used in many mechanisms. It stores many types of data in disguise, and separated the linked list and data.

Supplement to the Old Tang Dynasty:

Let's change the test code first:

#include <stdio.h>#include "1.h"#include <stdlib.h>int main(){SeqList *list = SeqList_Create(9);int i = 0;int j = 1;int k = 2;int x = 3;int y = 4;int z = 5;int index = 0;SeqList_Insert(list, &i,0);SeqList_Insert(list, &j,0);SeqList_Insert(list, &k,0);SeqList_Insert(list, &x,0);SeqList_Insert(list, &y,0);SeqList_Insert(list, &z,0);for (index = 0; index < SeqList_Length(list); index++){int *p = (int*)SeqList_Get(list, index);printf ("%d\n",*p);}printf("\n");        int* p = (int*)SeqList_Delete(list, 5);    printf("%x\n", SeqList_Get(list, 5));SeqList_Destroy(list);    return 0;}

Functions implemented by this Code: We first insert six elements in the sequence table: 0, 1, 2, 3, 4, 5. then we execute the print operation and delete the elements at the first position. In this way, we delete the elements with the value 5, that is, the elements at the end of the sequence table, then we can get the address of the deleted element and use the seqlist_get function. At this time, we print 0, while the original program of laotang prints an address, however, if the printed Address is the address, but the data element has been deleted, it can also print the address. This is incorrect, so we should pay attention to the condition detection in the get function. The security detection in the get function is as follows:

SeqListNode* SeqList_Get(SeqList* list, int pos){TSeqList * slist = (TSeqList*)list;SeqListNode * ret = NULL;if (slist != NULL && pos >= 0 && pos < slist -> length){ret = (SeqListNode*)(slist->node[pos]);}return ret;}

Pay attention to the POs <slist-> length, instead of the POs <= slist-> length, because our length starts from 1, our position is calculated from 0.

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.