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

Example Analysis of sequence Table in C language Linear Table

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

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian to share with you the C language linear table in the sequence table example analysis, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

I. the key points of this chapter

1. The concept of linear table and sequential table

two。 Implementation of dynamic and static sequence table interface

3. Online 0j training

Second, linear table

Linear tables meet the following conditions:

A linear table (linear list) is a finite sequence of n data elements with the same characteristics.

Linear tables are linear in logic, but not necessarily continuous in physical structure. The physical structure here generally refers to the physical address space.

III. Sequence table

Sequence tables are those that meet the following conditions:

Is a linear table.

Physical structure is continuous.

Sequence tables can generally be divided into:

Static sequence table: use fixed-length array storage.

Dynamic sequence table: use dynamically opened array storage.

4. Implementation of static sequence table interface 4.1 sequence table initialization void SeqListInint (SeqList* s) {assert (s); memset (s-> a, 0, sizeof (SeqListDataType) * MAXSIZE); s-> size = 0;}

There is also a simple way to initialize:

Directly assign a value of 0 when creating the sequence table s, that is, SeqList s = {0}

4.2 sequence table printing void SeqListPrint (SeqList* s) {int I = 0; for (I = 0; I

< s->

Size; iTunes +) {printf ("% d", s-> a [I]);} printf ("\ n");}

Pass the address of the sequence table and use the for loop statement to print the array elements step by step.

4.3 insert void SeqListPushBack (SeqList* s, int x) {assert (s); if (s-> size = = MAXSIZE) {printf ("current space is full, cannot continue to add\ n"); exit (1);} s-> a [s-> size] = x; s-> size++;}

First check whether s is empty, report an error if it is empty, and then check if it is full. if it is full, prompt it to be full and end the program.

4.4 delete void SeqListPopBack (SeqList* s) {assert (s); if (s-> size = = 0) {printf ("the current sequence table is empty and cannot be deleted\ n"); exit (1);} s-> size--;}

Directly s-> size-- is fine without setting the last element to 0. 0.

4.5 insert void SeqListPushFront (SeqList* s, int x) {if (s-> size = = MAXSIZE) {printf ("space is full, cannot continue to add\ n"); exit (1);} if (s-> size = = 0) {s-> a [s-> size] = x; s-> size++ Return;} else {int j = 0; for (j = s-> size-1; j > = 0; jmurm -) {s-> a [j + 1] = s-> a [j];} s-> a [0] = x S-> size++;}}

Move the element back first, and then put in the element you want to insert.

4.6 sequence header deletion void SeqListPopFront (SeqList* s) {if (s-> size = = 0) {printf ("the current sequence table is empty and cannot be deleted\ n"); exit (1);} int j = 0; for (j = 1; j size; jacks +) {s-> a [j-1] = s-> a [j] } s-> size--;}

Use the way of moving elements to overwrite the previous content to achieve the purpose of deletion.

4.7 insert void SeqListInsert (SeqList* s, int pos, int x) {if (s-> size = = MAXSIZE) {printf ("current space is full, cannot continue to add\ n"); exit (1);} if (pos)

< 0||pos>

S-> size) {printf ("incorrect insertion position, cannot be inserted\ n"); exit (1);} if (pos = = s-> size) {s-> a [s-> size] = x; s-> size++; return;} for (int j = s-> size-1) J > = pos; Jmuri -) {s-> a [j + 1] = s-> a [j];} s-> a [pos] = x; s-> size++;}

Find the location of the element, move the element, and then put in the element you want to insert.

Delete void SeqListErase (SeqList* s, int pos) {assert (s); if (s-> size = = 0) {printf ("sequence table is empty, deletion failed\ n"); exit (1);} if (pos > = s-> size | | pos

< 0) { printf("删除位置不存在\n"); exit(1); } int j = 0; for (j = pos; j < s->

Size-1; jacks +) {s-> a [j] = s-> a [j + 1];} s-> size--;}

Locate the location you want to delete and overwrite the element you want to delete by moving.

5. Initialize void SeqListInint (SeqList* s) {assert (s); s-> a = (DataType*) malloc (10 * sizeof (DataType)); s-> size = 0; s-> capacity = 10;}

Set the number of elements size to 0

Open up the space of a

The initial capacity is set to 10

5.2 print void SeqListPrint (SeqList* s) {assert (s); int I = 0; for (I = 0; I)

< s->

Size; iTunes +) {printf ("% d", s-> a [I]);} printf ("\ n");} 5.3 insert void SeqListPushBack (SeqList* s, DataType x) {assert (s); SeqListCheckCapacity (s); s-> a [s-> size] = x; s-> size++ Delete void SeqListPopBack (SeqList* s) {assert (s); if (s-> size = = 0) {printf ("current sequence table is empty, deletion failed\ n"); exit (1);} s-> size--;} 5.5 sequence header insert void SeqListPushFront (SeqList* s, DataType x) {assert (s) SeqListCheckCapacity (s); if (s-> size = = 0) {s-> a [0] = x; s-> size++;} else {int end = s-> size-1 While (end > = 0) {s-> a [end + 1] = s-> a [end]; end--;} s-> a [0] = x; s-> size++ Delete void SeqListPopFront (SeqList* s) {assert (s); if (s-> size = = 0) {printf ("current sequence table is empty and cannot be deleted\ n"); exit (1);} if (s-> size = = 1) {s-> size-- Return;} else {int I = 0; for (I = 0; I size-2; iTunes +) {s-> a [I] = s-> a [I + 1];} s-> size-- Insert void SeqListInsert (SeqList* s, int pos, DataType x) {assert (s); SeqListCheckCapacity (s); if (poss- > size) {printf ("insert position does not exist\ n"); exit (1) } else if (pos==s- > size) {s-> a [s-> size] = x; s-> size++;} else {int I = 0; for (I = s-> size-1; I > = pos Void SeqListErase -) {s-> a [I + 1] = s-> a [I];} s-> a [pos] = x; s-> size++;}} 5.8 delete void SeqListErase (SeqList* s, int pos) {assert (s) anywhere in the sequence table If (s-> size = = 0) {printf ("current sequence table is empty, deletion failed\ n"); exit (1);} if (poss- > size-1) {printf ("location to be deleted does not exist\ n"); exit (1) } else {int I = 0; for (I = pos; I size- 2; iTunes +) {s-> a [I] = s-> a [I + 1];} s-> size--;}} VI, online 0j exercise 1, remove elements (force buckle)

To give you an array nums and a value val, you need to remove all elements whose values are equal to val in place and return the new length of the removed array.

Do not use extra array space, you must only use O (1) extra space and modify the input array in place.

The order of elements can be changed. You don't need to consider the elements in the array that exceed the new length.

Example 1:

Input: nums = [0meme 1meme2], val = 2 output: 5, nums = [0meme1d4d0recorder 3] explain: the function should return a new length 5, and the first five elements in the nums are 0memery 1,3memery 4. Note that these five elements can be in any order. You don't need to consider the elements in the array that exceed the new length.

Idea: use two pointers, one to traverse the array and the other to point to where you want to store the data.

If we can apply for extra space, generally speaking, we can do this: apply for a new array space to store data with non-Val values. In fact, this new space we can directly use the original space of the nums array as a new space, we only need to traverse the nums array.

Int removeElement (int* nums, int numsSize, int val) {int iTuno; int j = 0; for (iTun0 / end2 > = 0) {if (nums1 [end1] > = nums2 [end2]) {nums1 [k] = nums1 [end1]; Kmurf; end1--;} else {nums1 [k] = nums2 [end2] While (end2 > = 0) {nums1 [k] = nums2 [end2]; nums1 [k]; end2--;}} above are all the contents of the article "sample Analysis of sequence tables in C language Linear tables". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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