Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language sequence table

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to use the C language sequence table". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The programming environment is ubuntu 18.04.

A sequential table requires a continuous piece of storage space to store any type of element. here we take storing int type data as an example.

I. the structural definition of the sequence table

Size is the capacity, and length is the number of currently known data table elements

Typedef struct Vector {int * data; / / the first address of the contiguous space of the sequence table is int size, length;} Vec; II, the structure operation of the sequence table 1. Initialize Vec * init (int n) {/ / the sequential table has n storage units Vec * v = (Vec *) malloc (sizeof (Vec)); / / opens a space malloc on the memory stack, and can also access v-> data = (int *) malloc (sizeof (int) * n) outside the function; v-> size = n; v-> length = 0; return v;} 2. Insert operation int insert (Vec * v, int ind, int val) {/ / ind is the position of the inserted element, and val is the value of the inserted element if (v = = NULL) return 0; if (ind)

< 0 || ind >

V-> length) return 0; / / determine whether the location to be inserted is legal if (v-> length = = v-> size) {if (! expand (v)) {/ / expansion failed printf (RED ("fail to expand!\ n"));} printf (GREEN ("success to expand!! The size =% d\ n "), v-> size);} for (int I = v-> length; I > ind; I -) {v-> data [I] = v-> data [I-1];} v-> data [ind] = val; v-> length + = 1; return 1;}

Why do you need to judge whether the insertion position is legal? This is because the sequence table is a continuous piece of storage space, so the memory is continuous.

The following figure takes length = 5 and 9 as an example. We can only insert data in numbers between 0 and 4 in the subscript.

Insert an element footprint

3. Delete operation int erase (Vec * v, int ind) {/ / delete the subscript ind element if (v = = NULL) return 0; if (ind

< 0 || ind >

= v-> length) return 0; for (int I = ind + 1; I

< v->

Length; iTunes +) {v-> data [I-1] = v-> data [I];} v-> length-= 1; return 1;}

Determine whether the subscript of an element that needs to be deleted is legal, similar to inserting an element

Delete an element diagram

4. Expansion operation int expand (Vec * v) {/ / sequence table expansion / / malloc dynamic application space, space is not necessarily clean calloc dynamic application space, and clear realloc reapplication space int extr_size = v-> size; int * p; while (extr_size) {p = (int *) realloc (v-> data, sizeof (int) * (v-> size + extr_size)) If (p! = NULL) break; / / p is not empty, indicating that the capacity expansion is successful. At this time, you can directly jump out of the loop extr_size > > = 1; / / otherwise, divide the extra capacity space by 2 and reduce the requirement} if (p = = NULL) return 0. / / determine whether the expansion is successful or failed to jump out of the loop. If the expansion fails, the address p is empty and the qualified memory region v-> size + = extr_size; v-> data = p; return 1;} cannot be found.

Note that capacity expansion is ingenious here. First of all, int extr_size = v-> size; means to set the size of capacity expansion to the original size, and then determine whether such a large space can be found. P = (int *) realloc (v-> data, sizeof (int) * (v-> size + extr_size)); if you can find such a large capacity in the system, then return the first address of the found memory address, and then you can end the pop-out loop; if you can't find it, you can only reduce the requirement and divide extr_size by 2 to see if you can know that if you really can't find it, you will jump out of the loop if extr_size is 0. Then you can judge whether p is a null pointer to determine whether the program is able to expand the space to exit or can not find exit.

If you are not familiar with malloc, calloc and realloc, you can see my blog post: C language explores the use of dynamic memory allocation in depth.

5. Release operation void clear (Vec * v) {/ / release space if (v = = NULL) return; free (v-> data); free (v); return;}

First release the data, then release the entire sequence table.

6. Output void output (Vec * v) {if (v = NULL) return; printf ("["); for (int I = 0; I

< v->

Length; iTunes +) {I & & printf (","); printf ("% d", v-> data [I]);} printf ("]\ n"); return Example # include#include # include//#include#define COLOR (a, b) "\ 033 [" # b "m" a "\ 033 [0m" # define GREEN (a) COLOR (a, 32) # define RED (a) COLOR (a, 31) typedef struct Vector {int * data; / / the first address of the sequential space int size, length;} Vec Vec * init (int n) {/ / the sequential table has n storage units Vec * v = (Vec *) malloc (sizeof (Vec)); / / A space malloc in the memory stack is opened up, and v-> data = (int *) malloc (sizeof (int) * n) can also be accessed outside the function; v-> size = n; v-> length = 0; return v } int expand (Vec * v) {/ / sequence table expansion / / malloc dynamic application space, space is not necessarily clean calloc dynamic application space, and clear realloc reapplication space int extr_size = v-> size; int * p; while (extr_size) {p = (int *) realloc (v-> data, sizeof (int) * (v-> size + extr_size)) If (p! = NULL) break; / / p is not empty, indicating that the capacity expansion is successful. At this time, you can directly jump out of the loop extr_size > > = 1; / / otherwise, divide the extra capacity space by 2 and reduce the requirement} if (p = = NULL) return 0. / / determine whether the expansion is successful or failed to jump out of the loop. If the expansion fails, the address is empty and the qualified memory region v-> size + = extr_size; v-> data = p; return 1;} int insert (Vec * v, int ind, int val) {/ / ind is the position of the inserted element, and val is the value of the inserted element if (v = NULL) return 0; if (ind)

< 0 || ind >

V-> length) return 0; if (v-> length = = v-> size) {if (! expand (v)) {printf (RED ("fail to expand!\ n"));} printf (GREEN ("success to expand!! The size =% d\ n "), v-> size);} for (int I = v-> length; I > ind; I -) {v-> data [I] = v-> data [I-1];} v-> data [ind] = val; v-> length + = 1; return 1;} int erase (Vec * v, int ind) {/ / remove if (v = NULL) return 0 from the subscript if (v = NULL)

< 0 || ind >

= v-> length) return 0; for (int I = ind + 1; I

< v->

Length; iTunes +) {v-> data [I-1] = v-> data [I];} v-> length-= 1; return 1;} void output (Vec * v) {if (v = NULL) return; printf ("["); for (int I = 0; I)

< v->

Length; iTunes +) {I & & printf (","); printf ("% d", v-> data [I]);} printf ("]\ n"); return;} void clear (Vec * v) {/ / Free Space if (v = = NULL) return; free (v-> data); free (v); return;} int main () {# define MAX_N 20 Vec * v = init (1) Srand (time (0)); / / set seed for (int I = 0; I

< MAX_N; i++){ int op = rand() % 4; int ind = rand() % (v->

Length + 3)-1; / value range [- 1, v-> length + 1] int val = rand ()% 100; / / val is the number between 1 and 99 switch (op) {case 0: case 1: case 2: {printf ("insert% d at% d to the Vector =% d\ n", val, ind, insert (v, ind, val)) } break; case 3: {printf ("erase an item at% d =% d\ n", ind,erase (v, ind));} break;} output (v); printf ("\ n");} # undef MAX_N clear (v); return 0;}

The output is as follows:

Insert 82 at 0 to the Vector = 1

[82]

Insert 38 at 2 to the Vector = 0

[82]

Success to expand! The size = 2

Insert 7 at 1 to the Vector = 1

[82, 7]

Success to expand! The size = 4

Insert 86 at 2 to the Vector = 1

[82, 7, 86]

Erase an item at 4 = 0

[82, 7, 86]

Erase an item at 4 = 0

[82, 7, 86]

Insert 48 at 0 to the Vector = 1

[48, 82, 7, 86]

Insert 65 at 5 to the Vector = 0

[48, 82, 7, 86]

Success to expand! The size = 8

Insert 92 at 4 to the Vector = 1

[48, 82, 7, 86, 92]

Erase an item at 2 = 1

[48, 82, 86, 92]

Insert 81 at 2 to the Vector = 1

[48, 82, 81, 86, 92]

Insert 9 at 0 to the Vector = 1

[9, 48, 82, 81, 86, 92]

Insert 99 at 1 to the Vector = 1

[9, 99, 48, 82, 81, 86, 92]

Insert 29 at 7 to the Vector = 1

[9, 99, 48, 82, 81, 86, 92, 29]

Success to expand! The size = 16

Insert 38 at 0 to the Vector = 1

[38, 9, 99, 48, 82, 81, 86, 92, 29]

Erase an item at 0 = 1

[9, 99, 48, 82, 81, 86, 92, 29]

Erase an item at 8 = 0

[9, 99, 48, 82, 81, 86, 92, 29]

Erase an item at 6 = 1

[9, 99, 48, 82, 81, 86, 29]

Insert 57 at-1 to the Vector = 0

[9, 99, 48, 82, 81, 86, 29]

Insert 32 at 4 to the Vector = 1

[9, 99, 48, 82, 32, 81, 86, 29]

This is the end of the content of "how to use the C language sequence Table". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report