Detailed explanation of the sequence table in the linear table

content

1. The focus of this chapter

2. Linear table

3. Sequence table

Fourth, the static sequence table interface implementation

4.1 Sequence table initialization

4.2 Sequence table printing

4.3 Sequential table footer insertion

4.4 Sequential table footer deletion

4.5 Sequence header plug

4.6 Sequence header deletion

4.7 Insert at any position in the sequence table

4.8 Delete anywhere in the sequence table

Five, dynamic sequence table interface implementation

5.1 Initialization of the sequence table

5.2 Sequence table printing

5.3 Sequential table footer insertion

5.4 Sequential table footer deletion

5.5 Sequence header plug

5.6 Sequence header deletion

5.7 Insert at any position in the sequence table

5.8 Delete anywhere in the sequence table

6. Online 0j practice


1. The focus of this chapter

1. The concept of linear table and sequential table

2. Dynamic and static sequence table interface implementation

3. Online 0j training


2. Linear table

A linear table is a table that satisfies the following conditions:

  1. A linear list is a finite sequence of n data elements with the same characteristics .
  2. Linear tables are logically linear in structure, but not necessarily continuous in physical structure. (The physical structure here generally refers to the physical address space).

3. Sequence table

A sequence table that satisfies the following conditions:

  1. is a linear table
  2. physically continuous

The sequence table can generally be divided into:

  1. Static sequence table: use fixed-length array storage.
  2. Dynamic sequence table: use dynamically developed array storage. 


Fourth, the static sequence table interface implementation

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:

Assign 0 directly 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; i++)
	{
		printf("%d ", s->a[i]);
	}
	printf("\n");
}

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


4.3 Sequential table footer insertion

void SeqListPushBack(SeqList* s, int x)
{
	assert(s);
	if (s->size == MAXSIZE)
	{
		printf("当前空间已满,无法继续添加\n");
		exit(1);
	}
	s->a[s->size] = x;
	s->size++;
}

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


4.4 Sequential table footer deletion

void SeqListPopBack(SeqList* s)
{
	assert(s);
	if (s->size == 0)
	{
		printf("当前顺序表为空,无法删除\n");
		exit(1);
	}
	s->size--;
}

Just s->size--, you don't need to set the last element to 0.


4.5 Sequence header plug

void SeqListPushFront(SeqList* s, int x)
{
	if (s->size == MAXSIZE)
	{
		printf("空间已满,无法继续添加\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; j--)
		{
			s->a[j + 1] = s->a[j];
		}
		s->a[0] = x;
		s->size++;
	}
}

Move the element back first, and then insert the element to be inserted after the move is complete.


4.6 Sequence header deletion

void SeqListPopFront(SeqList* s)
{
	if (s->size == 0)
	{
		printf("当前顺序表为空,无法删除\n");
		exit(1);
	}
	int j = 0;
	for (j = 1; j <s->size; j++)
	{
		s->a[j - 1] = s->a[j];
	}
	s->size--;
}

Use the method of moving the element to cover the previous content to achieve the purpose of deletion.


4.7 Insert at any position in the sequence table

void SeqListInsert(SeqList* s, int pos, int x)
{
	if (s->size == MAXSIZE)
	{
		printf("当前空间已满,无法继续添加\n");
		exit(1);
	}
	if (pos < 0||pos>s->size)
	{
		printf("插入位置有误,无法插入\n");
		exit(1);
	}
	if (pos == s->size)
	{
		s->a[s->size] = x;
		s->size++;
		return;
	}
	for (int j = s->size - 1; j >= pos; j--)
	{
		s->a[j + 1] = s->a[j];
	}
	s->a[pos] = x;
	s->size++;
}

Find the element position, move the element, and put the element to be inserted.


4.8 Delete anywhere in the sequence table

void SeqListErase(SeqList* s, int pos)
{
	assert(s);
	if (s->size == 0)
	{
		printf("顺序表为空,删除失败\n");
		exit(1);
	}
	if (pos >= s->size || pos < 0)
	{
		printf("删除位置不存在\n");
		exit(1);
	}
	int j = 0;
	for (j = pos; j < s->size-1; j++)
	{
		s->a[j] = s->a[j + 1];
	}
	s->size--;
}

Find the position you want to delete, cover the element you want to delete by moving it. 


Five, dynamic sequence table interface implementation

5.1 Initialization of the sequence table

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 a space

The initial capacity is set to 10

5.2 Sequence table printing

void SeqListPrint(SeqList* s)
{
	assert(s);
	int i = 0;
	for (i = 0; i < s->size; i++)
	{
		printf("%d ", s->a[i]);
	}
	printf("\n");
}

5.3 Sequential table footer insertion

void SeqListPushBack(SeqList* s, DataType x)
{
	assert(s);
	SeqListCheckCapacity(s);
	s->a[s->size] = x;
	s->size++;
}

5.4 Sequential table footer deletion

void SeqListPopBack(SeqList* s)
{
	assert(s);
	if (s->size == 0)
	{
		printf("当前顺序表为空,删除失败\n");
		exit(1);
	}
	s->size--;
}

5.5 Sequence header plug

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++;
	}
}

5.6 Sequence header deletion

void SeqListPopFront(SeqList* s)
{
	assert(s);
	if (s->size == 0)
	{
		printf("当前顺序表为空,无法删除\n");
		exit(1);
	}
	if (s->size == 1)
	{
		s->size--;
		return;
	}
	else
	{
		int i = 0;
		for (i = 0; i <=s->size-2 ; i++)
		{
			s->a[i] = s->a[i + 1];
		}
		s->size--;
	}
}

5.7 Insert at any position in the sequence table

void SeqListInsert(SeqList* s, int pos, DataType x)
{
	assert(s);
	SeqListCheckCapacity(s);
	if (pos<0 || pos>s->size)
	{
		printf("插入位置不存在\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; i--)
		{
			s->a[i + 1] = s->a[i];
		}
		s->a[pos] = x;
		s->size++;
	}
}

5.8 Delete anywhere in the sequence table

void SeqListErase(SeqList* s, int pos)
{
	assert(s);
	if (s->size == 0)
	{
		printf("当前顺序表为空,删除失败\n");
		exit(1);
	}
	if (pos<0||pos>s->size-1)
	{
		printf("要删除的位置不存在\n");
		exit(1);
	}
	else
	{
		int i = 0;
		for (i = pos; i <= s->size - 2; i++)
		{
			s->a[i] = s->a[i + 1];
		}
		s->size--;
	}
}

6. Online 0j practice

One: remove elements (force buckle)

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

Instead of using extra array space, you have to use only O(1) extra space and modify the input array in place.

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

Example 1:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3]
Explanation: The function should return the new length 5, and the first five elements in nums are 0, 1, 3, 0, 4. Note that these five elements can be in any order. You don't need to consider elements in the array beyond 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 you can apply for additional space, in general, we can do this: apply for a new array space to store non-val value data. In fact, in 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 once.

int removeElement(int* nums, int numsSize, int val)
{
    int i = 0;
    int j = 0;
    for(i=0;i<numsSize;i++)
    {
        if(nums[i]!=val)
        {
            nums[j]=nums[i];
            j++;
        }
    }
    return j;
}

2. Merge two ordered arrays (force buckle)

You are given two integer arrays nums1 and nums2 in non-decreasing order, and two more integers m and n, representing the number of elements in nums1 and nums2, respectively.

Please merge nums2 into nums1 so that the merged arrays are also arranged in non-decreasing order.

Note: Ultimately, the merged array should not be returned by the function, but stored in the array nums1. To cope with this situation, the initial length of nums1 is m + n, where the first m elements represent the elements that should be merged, and the last n elements are 0 and should be ignored. nums2 has length n 

Example 1

Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: Need to combine [1,2,3] and [2,5,6].
The combined result is [1,2,2,3,5,6], where the elements in nums1 are marked in bold italics.

Idea: put it from the back to the front, the larger number in nums1 and nums2. (Reference 1)

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
{
    int end1 = m-1;
    int end2 = n-1;
    int k = m + n -1;
    while(end1>=0 && end2>=0)
    {
        if(nums1[end1] >= nums2[end2])
        {
            nums1[k]=nums1[end1];
            k--;
            end1--;
        }
        else
        {
            nums1[k]=nums2[end2];
            k--;
            end2--;
        }
    }
    while(end2>=0)
    {
        nums1[k]=nums2[end2];
        k--;
        end2--;
    }
}

Guess you like

Origin blog.csdn.net/m0_62171658/article/details/123266138