In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use linear tables in C language data structures". The editor shows you the operation process through practical cases. The operation method is simple, fast and practical. I hope this article "how to use linear tables in C language data structures" can help you solve the problem.
1. The concept of sub-document writing
In the Visual Stdio compiler, we can run the program by creating .h header files and .cpp source files to make the code more beautiful and readable, as shown in the figure:
The SqList.h header file and the Sq.List.cpp source file store the declaration of global variables, structures and functions and the complete implementation code of the corresponding functions respectively. We need to note that the name of the header file and the source file should be the same, and the source file should refer to the header file (# include "SqList.h"). The reason for using "" instead of using "" is that the header file is written by ourselves and can only be referenced by "".
2. Code display
The SqList.cpp content is as follows:
Tips: # pragma once code is to avoid repeated introduction of header files, we can just memorize them a little bit
# pragma once#include # include# include#define LIST_INIT_SIZE 10 # define LISTINCREMENT 10 # define OK 1 # define ERROR 0 typedef struct SqList {int* elem; int len; int size;} SqList;int InitList_Sq (struct SqList* L); / / initialize the sequence table int ListInsert_Sq (struct SqList* L, int I, int e) / insert data int ListDelete_Sq (struct SqList* L, int I, int* e) into the sequence table; / delete data void ListShow_Sq (struct SqList* L, const char* s) in the sequence table; / / output data void DestroyList (SqList* L) in the sequence table; / / destroy the table
The SqList.cpp section is as follows:
# include "SqList.h" int InitList_Sq (struct SqList* L) {L-> elem = (int*) malloc (LIST_INIT_SIZE * sizeof (int)); if (! L-> elem) exit (0); L-> len = 0; L-> size = LIST_INIT_SIZE; return OK;} II, dynamic distributed memory malloc1, first acquaintance of malloc
In C language, malloc is a dynamic memory allocation function.
Function prototype: void * malloc (unsigned int num_bytes)
Parameter: num_bytes is an unsigned integer that represents the number of bytes allocated.
Return value: returns a pointer to the allocated memory if the allocation is successful (the initial value in this store is uncertain), otherwise a null pointer NULL is returned. Void* represents an undetermined type of pointer, and void* can point to any type of data. More specifically, it means that when you apply for memory space, you do not know what type of data the user is using this space to store (such as char or int, etc.)
2. Use method typedef struct SqList {int* elem; int len; int size;} SqList;int InitList_Sq (struct SqList* L) {L-> elem = (int*) malloc (LIST_INIT_SIZE * sizeof (int)); if (! L-> elem) exit (0); L-> len = 0; L-> size = LIST_INIT_SIZE; return OK;}
We can see this line of code "L-> elem = (int*) malloc (LIST_INIT_SIZE * sizeof (int));" here L-> elem is the formal parameter volume variable L calls the int* elem property, so malloc needs to return a pointer of type (int*), while the right parenthesis of malloc is the memory space, the size is the number of bytes of macro definition multiplied by the number of bytes occupied by integer (int), which is 10 bytes to put it bluntly here. The template can look like this: (allocation type *) malloc (number of allocation elements * sizeof) if successful, the first address of the space is returned, and the space is not initialized. If it fails, 0 is returned.
Create a linked list and add or delete it. 1. Initialize the linked list
Int InitList_Sq (struct SqList* L)
{
L-> elem = (int*) malloc (LIST_INIT_SIZE * sizeof (int))
If (! l-> elem) exit (0)
L-> len = 0
L-> size = LIST_INIT_SIZE
Return OK
}
First of all, allocate memory space for int*elem. If the failure returns zero, the first address of memory space is returned. The length of the linked list is set to zero, and the maximum length of the linked list is set to LIST_INIT_SIZE (size 10).
2. Add data int ListInsert_Sq (struct SqList* L, int I, int e) {if (iL- > len) return ERROR; if (L-> len > = L-> size) {int* newbase = (int*) realloc (L-> elem, (LIST_INIT_SIZE + LISTINCREMENT) * sizeof (int)); if (! newbase) exit (0); L-> size + = LISTINCREMENT } int* Q = & (L-> ELEM [I]); * Q = e; L-> len++; return OK;}
The formal parameter I corresponds to L-> len, that is, the initial length, and e corresponds to the inserted value. If we only look at the first if condition, we will feel that the condition is true forever. In fact, we will add one operation after inserting the data below, so the inserted data can only be inserted one by one. The second if is not difficult to understand. If the length of the linked list reaches the maximum length, the space can be expanded so that more data can be inserted. The latter is actually tail interpolation, where * Q points to the last position of the linked list, puts the data in it, then adds one to the length, and inserts the data at the end.
3. Delete the specified location data int ListDelete_Sq (struct SqList* L, int I, int* e) {if (iL- > len) return ERROR; int* p = & (L-> elem [I-1]); * e = * p; int* Q = L-> elem + L-> len- 1; for (+ + p; p len--; return OK;}
Here I represents the position in the linked list, and * e is the data at that location, so that we can know the value of the deleted element, and then I define * Q as the address of the last element in the linked list, and then repeatedly move the element forward after the deleted position of the linked list. finally, the total length of the linked list is reduced by one, and the deletion ends. Modifying the linked list can be done using a combination of insert and delete operations. There is no separate method defined here, and the details will be reflected in the general code below.
4. Code display and running effect 1, code display / / 1, SqList.h:#pragma once#include # include# include#define LIST_INIT_SIZE 10 # define LISTINCREMENT 10 # define OK 1 # define ERROR 0 typedef struct SqList {int* elem; int len; int size;} SqList;int InitList_Sq (struct SqList* L); / / initialize sequence table int ListInsert_Sq (struct SqList* L, int I, int e) / insert int ListDelete_Sq (struct SqList* L, int I, int* e) into the sequence table; / delete the data void ListShow_Sq (struct SqList* L, const char* s) in the sequence table; / / output the data void DestroyList (SqList* L) in the sequence table; / / destroy the table / / 2, SqList.cpp#include "SqList.h" int InitList_Sq (struct SqList* L) {L-> elem = (int*) malloc (LIST_INIT_SIZE * sizeof (int)) If (! l-> elem) exit (0); L-> len = 0; L-> size = LIST_INIT_SIZE; return OK;} int ListInsert_Sq (struct SqList* L, int I, int e) {if (iL- > len) return ERROR; if (L-> len > = L-> size) {int* newbase = (int*) realloc (L-> elem, (LIST_INIT_SIZE + LISTINCREMENT) * sizeof (int)) If (! newbase) exit (0); L-> size + = LISTINCREMENT;} int* Q = & (L-> Elm [I]); * Q = e; L-> len++; return OK;} int ListDelete_Sq (struct SqList* L, int I, int* e) {if (iL- > len) return ERROR; int* p = & (L-> elem [I-1]); * e = * p Int* Q = L-> elem + L-> len- 1; for (+ + p; p len--; return OK;} void ListShow_Sq (struct SqList* L, const char* s) {printf ("% s", s); int i; for (I = 0; I
< L->Len; iTunes +) {printf ("% d", L-> Elim [I]);} putchar ('\ n');} void DestroyList (SqList* L) {free (L-> elem); L-> elem = NULL; L-> len = 0; L-> size = 0;} / 3, linked list operation .CPP # include "SqList.h" void mainview_user () / / interface function {struct SqList L; InitList_Sq (& L); int c Printf ("- -\ n"); printf ("| * linear table * |\ n"); printf ("| * 1 input data * |\ n") Printf ("| * 2 View data * |\ n"); printf ("| * 3 delete data * |\ n"); printf ("| * 4 change data * |\ n") Printf ("| * 5 insert data * |\ n"); printf ("| * 0 exit system * |\ n"); printf ("- -\ n") Printf ("\ n"); while (1) {printf ("Please select:"); scanf_s ("% d", & c); switch (c) {case 1: {int n = 0; printf ("enter the number of data to insert:"); scanf_s ("% d", & n) For (int I = 0; I < n; iTunes +) {int t; scanf_s ("% d", & t); ListInsert_Sq (& L, L.len, t);}} break; case 2: {ListShow_Sq (& L, "current data is:") System ("pause"); break;} case 3: {int s, v; printf ("Please enter the location of data deletion s:"); scanf_s ("% d", & s); if (& L, s, & v) printf ("deleted successfully. Deleted data is:% d\ n ", v); else printf (" deletion failed. Incorrect location. "); break;} case 4: {printf (" Please enter the location you want to modify: "); int s, v; scanf_s ("% d ", & s); if (sL.len) printf (" illegal data ") Else {ListDelete_Sq (& L, s, & v); printf ("Please enter modified data:"); scanf_s ("% d", & v); ListInsert_Sq (& L, Smae 1, v); ListShow_Sq (& L, "modified to:") } break; case 5: {int I, b; printf ("input insertion location:"); scanf_s ("% d", & I); if (iL.len) printf ("illegal data") Else {printf ("inserted element:"); scanf_s ("% d", & b); ListInsert_Sq (& L, I, b); ListShow_Sq (& L, "inserted data is:"); break }} case 0: {DestroyList (& L); return;} default:printf ("input error, please re-enter!\ n"); system ("pause"); printf ("Please reselect:"); scanf_s ("% d", & c);} system ("PAUSE") } int main () {mainview_user ();} 2. Running effect
This is the end of the introduction to "how to use linear tables in C language data structures". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.