In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to write the C language dynamic sequence table example code, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Sequence table concept:
A sequential table is a linear structure in which data elements are stored successively in a storage unit with a continuous physical address. It is generally stored in an array. Complete the addition, deletion, query and modification of the data on the array.
Code parsing:
one。 Preparatory work
1. First, reference some header files and create a structure that contains an array. Size indicates how many elements the array currently has, and capacity indicates how many elements can be stored in the array. For example:
# include#include#include#include typedef int DataType;typedef struct SeqList {DataType* a; int size; int capacity;} SL
two。 Create a sequence table
SL sl; II, the basic operation of sequence table 1. Initialization function of sequential table
Note that the parameter here has to be a pointer, and the parameter is a temporary copy of the argument, so you have to pass the address to the function to change the value.
Void SeqListInit (SL* sl) {sl- > a = NULL; sl- > size = 0; sl- > capacity = 0;} 2. Tail interpolation function (insert data at the tail)
Before we write the trailing interpolation function, we have to determine whether there is enough array space. For example, the following example.
So we have to check whether the space is full first, and expand the capacity when it is full.
Name this judgment space function void CheckSpace (SL* sl) (this is the most important part of the difference between a dynamic sequence table and a static sequence table)
Void CheckSpace (SL* sl) {if (sl- > size = = sl- > capacity) {/ / because capacity is equal to 0 at first, give capacity a value int newcapacity = sl- > capacity = = 0.4: sl- > capacity * 2; DataType* tmp = (DataType*) realloc (sl- > a, sizeof (DataType) * newcapacity) If (tmp = = NULL) {printf ("Open up failed\ n"); exit (- 1);} else {sl- > capacity = newcapacity; sl- > a = tmp }}}
Tail interpolation function:
Void SeqListPushBack (SL* sl, DataType x) {CheckSpace (sl); sl- > a [sl-> size] = x; sl- > size++;} 3. Header function (insert data in the head of the array)
Void SeqListPushFront (SL* sl, DataType x) {/ / because you need to check whether the space is full when inserting, and expand CheckSpace (sl) when it is full; for (int end = sl- > size-1; end > = 0; end--) {sl- > a [end + 1] = sl- > a [end];} sl- > a [0] = x; sl- > size++;}
Moving the data corresponds to the code of the for loop, and finally assigns a value to the first position of the array. Don't forget to size+1.
4. Tail deletion function
If size equals 0, there is no data in the sequence table, so
Void SeqListPopBack (SL* sl) {assert (sl- > size > 0); sl- > size--;} 5. Header deletion function
Just move forward as a whole from the second data. (to move from the front):
Void SeqListPopFront (SL* sl) {for (int I = 1; I)
< sl->Size; iTunes +) {sl- > a [I-1] = sl- > a [I];} sl- > size--;} 6. Insert data at pos location
First of all, the pos cannot be less than the number of existing data.
Second, the space for judgment will be expanded when it is full.
Third: start with the data of the first pos position (here the subscript of the data of the first pos position in the array is pos-1) and move the following data back one bit as a whole.
Fourth: assign a value to this position, don't forget to size++
Void SeqListInsert (SL* sl, int pos, DataType x) {/ / View space assert (pos size); CheckSpace (sl); for (int end = sl- > size-1; end > = pos-1; end--) / / sl- > size-- {sl- > a [end+1] = sl- > a [end];} sl- > a [pos-1] = x Sl- > size++;} 7. Delete the data at the pos location
First of all, pos cannot be less than the number of existing data.
Second: move the data from the first pos position to the last one
Third: to size--
Void SeqListErase (SL* sl, int pos) {assert (pos size); for (int I = pos; I
< sl->Size; iTunes +) {sl- > a [I-1] = sl- > a [I];} sl- > size--;} 8. Modify the data of the pos location
One: pos cannot be less than the number of existing data
Second: assignment. The data of the pos position is pos-1 in the array. (because the following table of the array starts at 0)
Void SeqListModify (SL* sl, int pos, DataType x) {assert (pos size); sl- > a [pos-1] = x;} 9. Find the function.
Go through it to see if there is this data, and if so, which element is the returned data? (instead of returning the subscript of the data in the array, I am returning the subscript + 1)
Here I don't consider the situation if there are two identical data.
Int SeqListFind (SL* sl, DataType x) {for (int I = 0; I)
< sl->Size;i++) {if (sl- > a [I] = = x) {iSuppli; printf ("at% d position\ n", I); return I;}} printf ("No such data\ n") Return-1;} 10. Destroy function
Empty sl- > an after releasing it, because after the pointer free, the free function simply frees the memory space that the pointer points to, that is, the value stored in memory, but does not assign the value of the pointer to NULL, and the pointer still points to this piece of memory.
Void SeqListDestory (SL* sl) {free (sl- > a); sl- > a = NULL; sl- > size = 0; sl- > capacity = 0;} 11. Print function void SeqListPrint (SL* sl) {for (int I = 0; I)
< sl->Size;i++) {printf ("% d", sl- > a [I]);} printf ("\ n");} III. General code:
The menu is relatively simple.
# include#include#include#include typedef int DataType;typedef struct SeqList {DataType* a; int size; int capacity;} SL; void SeqListInit (SL* sl) {sl- > a = NULL; sl- > size = 0; sl- > capacity = 0;} void SeqListPrint (SL* sl) {for (int I = 0; I
< sl->Size; iTunes +) {printf ("% d", sl- > a [I]);} printf ("\ n");} void CheckSpace (SL* sl) {if (sl- > size = = sl- > capacity) {/ / because capacity initially equals 0, give capacity a value int newcapacity = sl- > capacity = 0? 4: sl- > capacity * 2 DataType* tmp = (DataType*) realloc (sl- > a, sizeof (DataType) * newcapacity); if (tmp = = NULL) {printf ("pioneering failure\ n"); exit (- 1) } else {sl- > capacity = newcapacity; sl- > a = tmp } void SeqListPushBack (SL* sl, DataType x) {/ / see if the space is full, expand / / if (sl- > size = = sl- > capacity) / / {/ because capacity is equal to 0 at first, so give capacity a value / / int newcapacity = sl- > capacity = 0? 4: sl- > capacity * 2 / / DataType* tmp = (DataType*) realloc (sl- > a, sizeof (DataType) * newcapacity); / / if (tmp = = NULL) / / {/ / printf ("pioneering failure\ n"); / / exit (- 1) / /} / / else / / {/ / sl- > a = tmp; / /} /} / / encapsulated as a function CheckSpace (sl); sl- > a [sl-> size] = x; sl- > size++ } void SeqListPushFront (SL* sl, DataType x) {/ / because you need to check whether the space is full when inserting, and expand CheckSpace (sl) when it is full; for (int end = sl- > size-1; end > = 0; end--) {sl- > a [end + 1] = sl- > a [end];} sl- > a [0] = x; sl- > size++ } void SeqListPopBack (SL* sl) {assert (sl- > size > 0); sl- > size--;} void SeqListPopFront (SL* sl) {for (int I = 1; I)
< sl->Size; iTunes +) {sl- > a [I-1] = sl- > a [I];} sl- > size--;} void SeqListInsert (SL* sl, int pos, DataType x) {/ / View space assert (pos size); CheckSpace (sl); for (int end = sl- > size- 1; end > = pos-1) End--) / / sl- > size-- {sl- > a [end + 1] = sl- > a [end];} sl- > a [pos-1] = x; sl- > size++;} void SeqListErase (SL* sl, int pos) {assert (pos size); for (int I = pos; I)
< sl->Size; iTunes +) {sl- > a [I-1] = sl- > a [I];} sl- > size--;} void SeqListModify (SL* sl, int pos, DataType x) {assert (pos size); sl- > a [pos-1] = x;} int SeqListFind (SL* sl, DataType x) {for (int I = 0; I)
< sl->Size; iTunes +) {if (sl- > a [I] = = x) {iTunes; printf ("at% d position\ n", I); return I;}} printf ("No such data\ n") Return-1;} void SeqListDestory (SL* sl) {free (sl- > a); sl- > a = NULL; sl- > size = 0; sl- > capacity = 0;} void menu () {printf ("* *\ n"); printf ("* 1. Insert data 2. Insert data *\ n "); printf (" * 3. Insert data in pos position *\ n "); printf (" * 4. Delete data 5. Delete data *\ n "); printf (" * 6. Delete data at pos location *\ n "); printf (" * * 7. Modify the data of pos position *\ n "); printf (" * 8. Find data 9. Print data * *\ n "); printf (" *-1. Exit *\ n "); printf (" * *\ n ");} int main () {SL sl; SeqListInit (& sl); int option = 0; int x = 0; int pos = 1 While (option! =-1) {menu (); scanf ("% d", & option); switch (option) {case 1: printf ("Please enter the data to insert, ending with-1:\ n") Do {scanf ("% d", & x); if (x! =-1) {SeqListPushBack (& sl, x) }} while (x! =-1); break; case 2: printf ("Please enter the data to be inserted, ending with-1:\ n") Do {scanf ("% d", & x); if (x! =-1) {SeqListPushFront (& sl, x) }} while (x! =-1); break; case 3: printf ("Please enter the data to insert, from which to insert, and end with a non-positive number\ n") Do {scanf ("% d", & x); scanf ("% d", & pos) If (pos > = 0) {SeqListInsert (& sl, pos, x);}} while (pos > = 0); break Case 4: SeqListPopBack (& sl); break; case 5: SeqListPopFront (& sl); break Case 6: printf ("Please enter the position where you want to delete the data, ending with a non-positive number\ n"); do {scanf ("% d", & pos) If (pos > 0) {SeqListErase (& sl, pos);}} while (pos > 0); break Case 7: printf ("Please enter the data to be modified, which data to modify, ending with a non-positive integer\ n"); do {scanf ("% d", & x) Scanf ("% d", & pos); if (pos > 0) {SeqListInsert (& sl, pos, x);}} while (pos > 0) Break; case 8: printf ("Please enter the data to be found\ n"); scanf ("% d", & x); SeqListFind (& sl, x); break Case 9: SeqListPrint (& sl); break; case-1: printf ("exit\ n"); break; default: printf ("input error, please re-enter\ n") }} SeqListDestory (& sl); return 0;} about how to write the C language dynamic sequence table example code is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.